@@ -240,6 +240,9 @@ func main() {
240
240
flHooksAsync := pflag .Bool ("hooks-async" ,
241
241
envBool (true , "GITSYNC_HOOKS_ASYNC" , "GIT_SYNC_HOOKS_ASYNC" ),
242
242
"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)" )
243
246
244
247
flUsername := pflag .String ("username" ,
245
248
envString ("" , "GITSYNC_USERNAME" , "GIT_SYNC_USERNAME" ),
@@ -879,6 +882,25 @@ func main() {
879
882
go exechookRunner .Run (context .Background ())
880
883
}
881
884
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
+
882
904
// Setup signal notify channel
883
905
sigChan := make (chan os.Signal , 1 )
884
906
if syncSig != 0 {
@@ -919,11 +941,12 @@ func main() {
919
941
920
942
failCount := 0
921
943
syncCount := uint64 (0 )
944
+
922
945
for {
923
946
start := time .Now ()
924
947
ctx , cancel := context .WithTimeout (context .Background (), * flSyncTimeout )
925
948
926
- if changed , hash , err := git .SyncRepo (ctx , refreshCreds ); err != nil {
949
+ if changed , hash , err := git .SyncRepo (ctx , refreshCreds , runHooks , * flHooksBeforeSymlink ); err != nil {
927
950
failCount ++
928
951
updateSyncMetrics (metricKeyError , start )
929
952
if * flMaxFailures >= 0 && failCount >= * flMaxFailures {
@@ -944,11 +967,10 @@ func main() {
944
967
log .V (3 ).Info ("touched touch-file" , "path" , absTouchFile )
945
968
}
946
969
}
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 )
952
974
}
953
975
updateSyncMetrics (metricKeySuccess , start )
954
976
} else {
@@ -968,14 +990,17 @@ func main() {
968
990
// Assumes that if hook channels are not nil, they will have at
969
991
// least one value before getting closed
970
992
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
+ }
974
999
}
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
+ }
979
1004
}
980
1005
}
981
1006
log .DeleteErrorFile ()
@@ -1723,7 +1748,7 @@ func (git *repoSync) currentWorktree() (worktree, error) {
1723
1748
// SyncRepo syncs the repository to the desired ref, publishes it via the link,
1724
1749
// and tries to clean up any detritus. This function returns whether the
1725
1750
// 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 ) {
1727
1752
git .log .V (3 ).Info ("syncing" , "repo" , redactURL (git .repo ))
1728
1753
1729
1754
if err := refreshCreds (ctx ); err != nil {
@@ -1778,6 +1803,11 @@ func (git *repoSync) SyncRepo(ctx context.Context, refreshCreds func(context.Con
1778
1803
// path was different.
1779
1804
changed := (currentHash != remoteHash ) || (currentWorktree != git .worktreeFor (currentHash ))
1780
1805
1806
+ // Fire hooks if needed.
1807
+ if flHooksBeforeSymlink {
1808
+ runHooks (remoteHash )
1809
+ }
1810
+
1781
1811
// We have to do at least one fetch, to ensure that parameters like depth
1782
1812
// are set properly. This is cheap when we already have the target hash.
1783
1813
if changed || git .syncCount == 0 {
0 commit comments