Merge pull request #285 from unknowingknow/add-sync-hook-command
add an option to run the command when the repository is updated
This commit is contained in:
commit
410c5950b8
|
|
@ -79,7 +79,7 @@ docker run -d \
|
|||
## Parameters
|
||||
|
||||
| Environment Variable | Flag | Description | Default |
|
||||
|---------------------------------|----------------------------|----------------------------------------------------------------------------------------------------------------------------------------|-------------------------------|
|
||||
|---------------------------------|----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------|
|
||||
| GIT_SYNC_REPO | `--repo` | the git repository to clone | "" |
|
||||
| GIT_SYNC_BRANCH | `--branch` | the git branch to check out | "master" |
|
||||
| GIT_SYNC_REV | `--rev` | the git revision (tag or hash) to check out | "HEAD" |
|
||||
|
|
@ -92,6 +92,7 @@ docker run -d \
|
|||
| GIT_SYNC_ONE_TIME | `--one-time` | exit after the first sync | false |
|
||||
| GIT_SYNC_MAX_SYNC_FAILURES | `--max-sync-failures` | the number of consecutive failures allowed before aborting (the first sync must succeed, -1 will retry forever after the initial sync) | 0 |
|
||||
| GIT_SYNC_PERMISSIONS | `--change-permissions` | the file permissions to apply to the checked-out files (0 will not change permissions at all) | 0 |
|
||||
| GIT_SYNC_HOOK_COMMAND | `--sync-hook-command` | the command executed with the syncing repository as its working directory after syncing a new hash of the remote repository. it is subject to the sync time out and will extend period between syncs. (doesn't support the command arguments) | "" |
|
||||
| GIT_SYNC_WEBHOOK_URL | `--webhook-url` | the URL for a webook notification when syncs complete | "" |
|
||||
| GIT_SYNC_WEBHOOK_METHOD | `--webhook-method` | the HTTP method for the webhook | "POST" |
|
||||
| GIT_SYNC_WEBHOOK_SUCCESS_STATUS | `--webhook-success-status` | the HTTP status code indicating a successful webhook (-1 disables success checks to make webhooks fire-and-forget) | 200 |
|
||||
|
|
|
|||
|
|
@ -70,6 +70,9 @@ var flMaxSyncFailures = flag.Int("max-sync-failures", envInt("GIT_SYNC_MAX_SYNC_
|
|||
"the number of consecutive failures allowed before aborting (the first sync must succeed, -1 will retry forever after the initial sync)")
|
||||
var flChmod = flag.Int("change-permissions", envInt("GIT_SYNC_PERMISSIONS", 0),
|
||||
"the file permissions to apply to the checked-out files (0 will not change permissions at all)")
|
||||
var flSyncHookCommand = flag.String("sync-hook-command", envString("GIT_SYNC_HOOK_COMMAND", ""),
|
||||
"the command executed with the syncing repository as its working directory after syncing a new hash of the remote repository. "+
|
||||
"it is subject to the sync time out and will extend period between syncs. (doesn't support the command arguments)")
|
||||
|
||||
var flWebhookURL = flag.String("webhook-url", envString("GIT_SYNC_WEBHOOK_URL", ""),
|
||||
"the URL for a webook notification when syncs complete (default is no webook)")
|
||||
|
|
@ -653,6 +656,15 @@ func addWorktreeAndSwap(ctx context.Context, gitRoot, dest, branch, rev string,
|
|||
}
|
||||
}
|
||||
|
||||
// Execute the command, if requested.
|
||||
if *flSyncHookCommand != "" {
|
||||
log.V(0).Info("executing command for git sync hooks", "command", *flSyncHookCommand)
|
||||
_, err = runCommand(ctx, worktreePath, *flSyncHookCommand)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Flip the symlink.
|
||||
oldWorktree, err := updateSymlink(ctx, gitRoot, dest, worktreePath)
|
||||
if err != nil {
|
||||
|
|
|
|||
35
test_e2e.sh
35
test_e2e.sh
|
|
@ -113,6 +113,7 @@ trap finish INT EXIT
|
|||
|
||||
SLOW_GIT=/slow_git.sh
|
||||
ASKPASS_GIT=/askpass_git.sh
|
||||
SYNC_HOOK_COMMAND=/test_sync_hook_command.sh
|
||||
|
||||
function GIT_SYNC() {
|
||||
#./bin/linux_amd64/git-sync "$@"
|
||||
|
|
@ -125,6 +126,7 @@ function GIT_SYNC() {
|
|||
-v "$DIR":"$DIR":rw \
|
||||
-v "$(pwd)/slow_git.sh":"$SLOW_GIT":ro \
|
||||
-v "$(pwd)/askpass_git.sh":"$ASKPASS_GIT":ro \
|
||||
-v "$(pwd)/test_sync_hook_command.sh":"$SYNC_HOOK_COMMAND":ro \
|
||||
-v "$DOT_SSH/id_test":"/etc/git-secret/ssh":ro \
|
||||
--env XDG_CONFIG_HOME=$DIR \
|
||||
e2e/git-sync:$(make -s version)__$(go env GOOS)_$(go env GOARCH) \
|
||||
|
|
@ -640,6 +642,39 @@ assert_file_eq "$ROOT"/link/file "$TESTCASE 1"
|
|||
# Wrap up
|
||||
pass
|
||||
|
||||
##############################################
|
||||
# Test sync_hook_command
|
||||
##############################################
|
||||
testcase "sync_hook_command"
|
||||
# First sync
|
||||
echo "$TESTCASE 1" > "$REPO"/file
|
||||
git -C "$REPO" commit -qam "$TESTCASE 1"
|
||||
GIT_SYNC \
|
||||
--v=5 \
|
||||
--wait=0.1 \
|
||||
--repo="file://$REPO" \
|
||||
--root="$ROOT" \
|
||||
--dest="link" \
|
||||
--sync-hook-command="$SYNC_HOOK_COMMAND" \
|
||||
> "$DIR"/log."$TESTCASE" 2>&1 &
|
||||
sleep 3
|
||||
assert_link_exists "$ROOT"/link
|
||||
assert_file_exists "$ROOT"/link/file
|
||||
assert_file_exists "$ROOT"/link/sync-hook
|
||||
assert_file_eq "$ROOT"/link/file "$TESTCASE 1"
|
||||
assert_file_eq "$ROOT"/link/sync-hook "$TESTCASE 1"
|
||||
# Move forward
|
||||
echo "$TESTCASE 2" > "$REPO"/file
|
||||
git -C "$REPO" commit -qam "$TESTCASE 2"
|
||||
sleep 3
|
||||
assert_link_exists "$ROOT"/link
|
||||
assert_file_exists "$ROOT"/link/file
|
||||
assert_file_exists "$ROOT"/link/sync-hook
|
||||
assert_file_eq "$ROOT"/link/file "$TESTCASE 2"
|
||||
assert_file_eq "$ROOT"/link/sync-hook "$TESTCASE 2"
|
||||
# Wrap up
|
||||
pass
|
||||
|
||||
##############################################
|
||||
# Test webhook success
|
||||
##############################################
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
#!/bin/sh
|
||||
# Use for e2e test of --sync-hook-command.
|
||||
# This option takes no command arguments, so requires a wrapper script.
|
||||
|
||||
yes | cp -i file sync-hook
|
||||
Loading…
Reference in New Issue