diff --git a/cmd/git-sync/main.go b/cmd/git-sync/main.go index 39e2b3b..434bf76 100644 --- a/cmd/git-sync/main.go +++ b/cmd/git-sync/main.go @@ -704,7 +704,7 @@ func (git *repoSync) AddWorktreeAndSwap(ctx context.Context, hash string) error } // Make a worktree for this exact git hash. - worktreePath := filepath.Join(git.root, "rev-"+hash) + worktreePath := filepath.Join(git.root, hash) _, err := runCommand(ctx, git.root, git.cmd, "worktree", "add", worktreePath, "origin/"+git.branch) log.V(0).Info("adding worktree", "path", worktreePath, "branch", fmt.Sprintf("origin/%s", git.branch)) if err != nil { @@ -1167,7 +1167,9 @@ OPTIONS --link , $GIT_SYNC_LINK The name of the final symlink (under --root) which will point to the current git worktree. This must be a filename, not a path, and may - not start with a period. (default: the leaf dir of --repo) + not start with a period. The destination of this link (i.e. + readlink()) is the currently checked out SHA. (default: the leaf + dir of --repo) --git , $GIT_SYNC_GIT The git command to run (subject to PATH search, mostly for testing). diff --git a/test_e2e.sh b/test_e2e.sh index e61619a..4ce211c 100755 --- a/test_e2e.sh +++ b/test_e2e.sh @@ -33,6 +33,13 @@ function assert_link_exists() { fi } +function assert_link_eq() { + if [[ $(readlink "$1") == "$2" ]]; then + return + fi + fail "link $1 does not point to '$2': $(readlink $1)" +} + function assert_file_exists() { if ! [[ -f "$1" ]]; then fail "$1 does not exist" @@ -316,6 +323,38 @@ assert_file_eq "$ROOT"/link/file "$TESTCASE 1" # Wrap up pass +############################################## +# Test readlink +############################################## +testcase "readlink" +# First sync +echo "$TESTCASE 1" > "$REPO"/file +git -C "$REPO" commit -qam "$TESTCASE 1" +GIT_SYNC \ + --period=100ms \ + --repo="file://$REPO" \ + --branch=e2e-branch \ + --rev=HEAD \ + --root="$ROOT" \ + --link="link" \ + > "$DIR"/log."$TESTCASE" 2>&1 & +sleep 3 +assert_link_exists "$ROOT"/link +assert_link_eq "$ROOT"/link $(git -C "$REPO" rev-parse HEAD) +# Move HEAD forward +echo "$TESTCASE 2" > "$REPO"/file +git -C "$REPO" commit -qam "$TESTCASE 2" +sleep 3 +assert_link_exists "$ROOT"/link +assert_link_eq "$ROOT"/link $(git -C "$REPO" rev-parse HEAD) +# Move HEAD backward +git -C "$REPO" reset -q --hard HEAD^ +sleep 3 +assert_link_exists "$ROOT"/link +assert_link_eq "$ROOT"/link $(git -C "$REPO" rev-parse HEAD) +# Wrap up +pass + ############################################## # Test branch syncing ##############################################