Skip to content

Commit 7d51865

Browse files
committed
add function to call hooks and implement --hooks-before-symlink
1 parent 1e02613 commit 7d51865

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed

_test_tools/exechook_command_with_sleep.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@
1919

2020
sleep 3
2121
cat file > exechook
22+
cat exechook
23+
if [[ "$(pwd)" != "$(pwd -P)" ]]; then echo "true" > delaycheck; fi
2224
echo "ENVKEY=$ENVKEY" > exechook-env

main.go

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ func main() {
240240
flHooksAsync := pflag.Bool("hooks-async",
241241
envBool(true, "GITSYNC_HOOKS_ASYNC", "GIT_SYNC_HOOKS_ASYNC"),
242242
"run hooks asynchronously")
243+
flHooksBeforeSymlink := pflag.Bool("hooks-before-symlink",
244+
envBool(false, "GITSYNC_HOOKS_BEFORE_SYMLINK", "GIT_SYNC_HOOKS_BEFORE_SYMLINK"),
245+
"run hooks before creating the symlink (defaults to false)")
243246

244247
flUsername := pflag.String("username",
245248
envString("", "GITSYNC_USERNAME", "GIT_SYNC_USERNAME"),
@@ -879,6 +882,25 @@ func main() {
879882
go exechookRunner.Run(context.Background())
880883
}
881884

885+
runHooks := func(hash string) error {
886+
var err error
887+
if exechookRunner != nil {
888+
log.V(3).Info("sending exechook")
889+
err = exechookRunner.Send(hash)
890+
if err != nil {
891+
return err
892+
}
893+
}
894+
if webhookRunner != nil {
895+
log.V(3).Info("sending webhook")
896+
err = webhookRunner.Send(hash)
897+
}
898+
if err != nil {
899+
return err
900+
}
901+
return nil
902+
}
903+
882904
// Setup signal notify channel
883905
sigChan := make(chan os.Signal, 1)
884906
if syncSig != 0 {
@@ -919,11 +941,12 @@ func main() {
919941

920942
failCount := 0
921943
syncCount := uint64(0)
944+
922945
for {
923946
start := time.Now()
924947
ctx, cancel := context.WithTimeout(context.Background(), *flSyncTimeout)
925948

926-
if changed, hash, err := git.SyncRepo(ctx, refreshCreds); err != nil {
949+
if changed, hash, err := git.SyncRepo(ctx, refreshCreds, runHooks, *flHooksBeforeSymlink); err != nil {
927950
failCount++
928951
updateSyncMetrics(metricKeyError, start)
929952
if *flMaxFailures >= 0 && failCount >= *flMaxFailures {
@@ -944,11 +967,10 @@ func main() {
944967
log.V(3).Info("touched touch-file", "path", absTouchFile)
945968
}
946969
}
947-
if webhookRunner != nil {
948-
webhookRunner.Send(hash)
949-
}
950-
if exechookRunner != nil {
951-
exechookRunner.Send(hash)
970+
// if --hooks-before-symlink is set, these will have already been sent and completed.
971+
// otherwise, we send them now.
972+
if !*flHooksBeforeSymlink {
973+
runHooks(hash)
952974
}
953975
updateSyncMetrics(metricKeySuccess, start)
954976
} else {
@@ -968,14 +990,17 @@ func main() {
968990
// Assumes that if hook channels are not nil, they will have at
969991
// least one value before getting closed
970992
exitCode := 0 // is 0 if all hooks succeed, else is 1
971-
if exechookRunner != nil {
972-
if err := exechookRunner.WaitForCompletion(); err != nil {
973-
exitCode = 1
993+
// This will not be needed if async == false, because the Send func for the hookRunners will wait
994+
if *flHooksAsync {
995+
if exechookRunner != nil {
996+
if err := exechookRunner.WaitForCompletion(); err != nil {
997+
exitCode = 1
998+
}
974999
}
975-
}
976-
if webhookRunner != nil {
977-
if err := webhookRunner.WaitForCompletion(); err != nil {
978-
exitCode = 1
1000+
if webhookRunner != nil {
1001+
if err := webhookRunner.WaitForCompletion(); err != nil {
1002+
exitCode = 1
1003+
}
9791004
}
9801005
}
9811006
log.DeleteErrorFile()
@@ -1723,7 +1748,7 @@ func (git *repoSync) currentWorktree() (worktree, error) {
17231748
// SyncRepo syncs the repository to the desired ref, publishes it via the link,
17241749
// and tries to clean up any detritus. This function returns whether the
17251750
// current hash has changed and what the new hash is.
1726-
func (git *repoSync) SyncRepo(ctx context.Context, refreshCreds func(context.Context) error) (bool, string, error) {
1751+
func (git *repoSync) SyncRepo(ctx context.Context, refreshCreds func(context.Context) error, runHooks func(hash string) error, flHooksBeforeSymlink bool) (bool, string, error) {
17271752
git.log.V(3).Info("syncing", "repo", redactURL(git.repo))
17281753

17291754
if err := refreshCreds(ctx); err != nil {
@@ -1778,6 +1803,11 @@ func (git *repoSync) SyncRepo(ctx context.Context, refreshCreds func(context.Con
17781803
// path was different.
17791804
changed := (currentHash != remoteHash) || (currentWorktree != git.worktreeFor(currentHash))
17801805

1806+
// Fire hooks if needed.
1807+
if flHooksBeforeSymlink {
1808+
runHooks(remoteHash)
1809+
}
1810+
17811811
// We have to do at least one fetch, to ensure that parameters like depth
17821812
// are set properly. This is cheap when we already have the target hash.
17831813
if changed || git.syncCount == 0 {

test_e2e.sh

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function assert_file_eq() {
9696
if [[ $(cat "$1") == "$2" ]]; then
9797
return
9898
fi
99-
fail "$1 does not contain '$2': $(cat "$1")"
99+
fail "$1 does not equal '$2': $(cat "$1")"
100100
}
101101

102102
function assert_file_contains() {
@@ -2509,6 +2509,32 @@ function e2e::exechook_success_hooks_non_async() {
25092509
assert_file_eq "$ROOT/link/exechook-env" "$EXECHOOK_ENVKEY=$EXECHOOK_ENVVAL"
25102510
}
25112511

2512+
##############################################
2513+
# Test exechook-success with --hooks-async=false and --hooks-before-symlink
2514+
##############################################
2515+
function e2e::exechook_success_hooks_before_symlink_non_async() {
2516+
cat /dev/null > "$RUNLOG"
2517+
2518+
GIT_SYNC \
2519+
--hooks-async=false \
2520+
--hooks-before-symlink \
2521+
--period=100ms \
2522+
--repo="file://$REPO" \
2523+
--root="$ROOT" \
2524+
--link="link" \
2525+
--exechook-command="/$EXECHOOK_COMMAND_SLEEPY" \
2526+
--exechook-backoff=1s \
2527+
&
2528+
sleep 5 # give it time to run
2529+
assert_link_exists "$ROOT/link"
2530+
assert_file_exists "$ROOT/link/file"
2531+
assert_file_exists "$ROOT/link/exechook"
2532+
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]}"
2533+
assert_file_eq "$ROOT/link/exechook" "${FUNCNAME[0]}"
2534+
assert_file_eq "$ROOT/link/exechook-env" "$EXECHOOK_ENVKEY=$EXECHOOK_ENVVAL"
2535+
assert_file_absent "$ROOT/link/delaycheck"
2536+
}
2537+
25122538
##############################################
25132539
# Test webhook success
25142540
##############################################

0 commit comments

Comments
 (0)