Move sync-hook to after symlink flip

This commit is contained in:
Tim Hockin 2021-04-05 17:02:40 -07:00
parent 32828834a7
commit d508f04bbe
4 changed files with 35 additions and 19 deletions

1
.gitallowed Normal file
View File

@ -0,0 +1 @@
xxxyyyzzz

View File

@ -663,32 +663,42 @@ 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. // Flip the symlink.
oldWorktree, err := updateSymlink(ctx, gitRoot, dest, worktreePath) oldWorktree, err := updateSymlink(ctx, gitRoot, dest, worktreePath)
if err != nil { if err != nil {
return err return err
} }
setRepoReady() setRepoReady()
if oldWorktree != "" {
// Clean up previous worktree // From here on we have to save errors until the end.
log.V(1).Info("removing old worktree", "path", oldWorktree)
if err := os.RemoveAll(oldWorktree); err != nil { // Execute the hook command, if requested.
return fmt.Errorf("error removing directory: %v", err) var execErr error
} if *flSyncHookCommand != "" {
if _, err := runCommand(ctx, gitRoot, *flGitCmd, "worktree", "prune"); err != nil { log.V(1).Info("executing command for git sync hooks", "command", *flSyncHookCommand)
return err if _, err := runCommand(ctx, worktreePath, *flSyncHookCommand); err != nil {
// Save it until after cleanup runs.
execErr = err
} }
} }
// Clean up previous worktree(s).
var cleanupErr error
if oldWorktree != "" {
log.V(1).Info("removing old worktree", "path", oldWorktree)
if err := os.RemoveAll(oldWorktree); err != nil {
cleanupErr = fmt.Errorf("error removing directory: %v", err)
} else if _, err := runCommand(ctx, gitRoot, *flGitCmd, "worktree", "prune"); err != nil {
cleanupErr = err
}
}
if cleanupErr != nil {
return cleanupErr
}
if execErr != nil {
return execErr
}
return nil return nil
} }
@ -946,7 +956,7 @@ func setupGitCookieFile(ctx context.Context) error {
// The expected ASKPASS callback output are below, // The expected ASKPASS callback output are below,
// see https://git-scm.com/docs/gitcredentials for more examples: // see https://git-scm.com/docs/gitcredentials for more examples:
// username=xxx@example.com // username=xxx@example.com
// password=ya29.xxxyyyzzz // password=xxxyyyzzz
func callGitAskPassURL(ctx context.Context, url string) error { func callGitAskPassURL(ctx context.Context, url string) error {
log.V(1).Info("calling GIT_ASKPASS URL to get credentials") log.V(1).Info("calling GIT_ASKPASS URL to get credentials")

View File

@ -739,8 +739,10 @@ sleep 3
assert_link_exists "$ROOT"/link assert_link_exists "$ROOT"/link
assert_file_exists "$ROOT"/link/file assert_file_exists "$ROOT"/link/file
assert_file_exists "$ROOT"/link/sync-hook assert_file_exists "$ROOT"/link/sync-hook
assert_file_exists "$ROOT"/link/link-sync-hook
assert_file_eq "$ROOT"/link/file "$TESTCASE 1" assert_file_eq "$ROOT"/link/file "$TESTCASE 1"
assert_file_eq "$ROOT"/link/sync-hook "$TESTCASE 1" assert_file_eq "$ROOT"/link/sync-hook "$TESTCASE 1"
assert_file_eq "$ROOT"/link/link-sync-hook "$TESTCASE 1"
# Move forward # Move forward
echo "$TESTCASE 2" > "$REPO"/file echo "$TESTCASE 2" > "$REPO"/file
git -C "$REPO" commit -qam "$TESTCASE 2" git -C "$REPO" commit -qam "$TESTCASE 2"
@ -748,8 +750,10 @@ sleep 3
assert_link_exists "$ROOT"/link assert_link_exists "$ROOT"/link
assert_file_exists "$ROOT"/link/file assert_file_exists "$ROOT"/link/file
assert_file_exists "$ROOT"/link/sync-hook assert_file_exists "$ROOT"/link/sync-hook
assert_file_exists "$ROOT"/link/link-sync-hook
assert_file_eq "$ROOT"/link/file "$TESTCASE 2" assert_file_eq "$ROOT"/link/file "$TESTCASE 2"
assert_file_eq "$ROOT"/link/sync-hook "$TESTCASE 2" assert_file_eq "$ROOT"/link/sync-hook "$TESTCASE 2"
assert_file_eq "$ROOT"/link/link-sync-hook "$TESTCASE 2"
# Wrap up # Wrap up
pass pass

View File

@ -2,4 +2,5 @@
# Use for e2e test of --sync-hook-command. # Use for e2e test of --sync-hook-command.
# This option takes no command arguments, so requires a wrapper script. # This option takes no command arguments, so requires a wrapper script.
yes | cp -i file sync-hook cat file > sync-hook
cat ../link/file > link-sync-hook