Skip to content

Commit

Permalink
Bug: links are relative to linkdir, not rootdir
Browse files Browse the repository at this point in the history
  • Loading branch information
thockin committed Dec 9, 2023
1 parent 718a938 commit 9a806c0
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 33 deletions.
3 changes: 2 additions & 1 deletion abspath.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func (abs absPath) Canonical() (absPath, error) {
return absPath(result), nil
}

// Join appends more path elements to abs, like filepath.Join.
// Join appends more path elements to abs, like filepath.Join. This will clean
// the final path (e.g. resolve ".." elements).
func (abs absPath) Join(elems ...string) absPath {
all := make([]string, 0, 1+len(elems))
all = append(all, abs.String())
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ func (git *repoSync) publishSymlink(ctx context.Context, worktree worktree) erro
return fmt.Errorf("error making symlink dir: %w", err)
}

// newDir is absolute, so we need to change it to a relative path. This is
// linkDir is absolute, so we need to change it to a relative path. This is
// so it can be volume-mounted at another path and the symlink still works.
targetRelative, err := filepath.Rel(linkDir, targetPath.String())
if err != nil {
Expand Down Expand Up @@ -1573,7 +1573,8 @@ func (git *repoSync) currentWorktree() (worktree, error) {
if filepath.IsAbs(target) {
return worktree(target), nil
}
return worktree(git.root.Join(target)), nil
linkDir, _ := git.link.Split()
return worktree(absPath(linkDir).Join(target)), nil
}

// SyncRepo syncs the repository to the desired ref, publishes it via the link,
Expand Down
120 changes: 90 additions & 30 deletions test_e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -411,36 +411,6 @@ function e2e::init_root_fails_sanity() {
assert_file_eq "$ROOT/link/file" "$FUNCNAME"
}

##############################################
# Test init with an absolute-path link
##############################################
function e2e::sync_absolute_link() {
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--root="$ROOT/root" \
--link="$ROOT/other/dir/link"
assert_file_absent "$ROOT/root/link"
assert_link_exists "$ROOT/other/dir/link"
assert_file_exists "$ROOT/other/dir/link/file"
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME"
}

##############################################
# Test init with a subdir-path link
##############################################
function e2e::sync_subdir_link() {
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--root="$ROOT" \
--link="other/dir/link"
assert_file_absent "$ROOT/link"
assert_link_exists "$ROOT/other/dir/link"
assert_file_exists "$ROOT/other/dir/link/file"
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME"
}

##############################################
# Test non-zero exit with a bad ref
##############################################
Expand Down Expand Up @@ -539,6 +509,96 @@ function e2e::sync_head() {
assert_metric_eq "${METRIC_FETCH_COUNT}" 3
}

##############################################
# Test sync with an absolute-path link
##############################################
function e2e::sync_head_absolute_link() {
# First sync
echo "$FUNCNAME 1" > "$REPO/file"
git -C "$REPO" commit -qam "$FUNCNAME 1"

GIT_SYNC \
--period=100ms \
--repo="file://$REPO" \
--ref=HEAD \
--root="$ROOT/root" \
--link="$ROOT/other/dir/link" \
&
wait_for_sync "${MAXWAIT}"
assert_file_absent "$ROOT/root/link"
assert_link_exists "$ROOT/other/dir/link"
assert_file_exists "$ROOT/other/dir/link/file"
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME 1"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 1
assert_metric_eq "${METRIC_FETCH_COUNT}" 1

# Move HEAD forward
echo "$FUNCNAME 2" > "$REPO/file"
git -C "$REPO" commit -qam "$FUNCNAME 2"
wait_for_sync "${MAXWAIT}"
assert_file_absent "$ROOT/root/link"
assert_link_exists "$ROOT/other/dir/link"
assert_file_exists "$ROOT/other/dir/link/file"
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME 2"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 2
assert_metric_eq "${METRIC_FETCH_COUNT}" 2

# Move HEAD backward
git -C "$REPO" reset -q --hard HEAD^
wait_for_sync "${MAXWAIT}"
assert_file_absent "$ROOT/root/link"
assert_link_exists "$ROOT/other/dir/link"
assert_file_exists "$ROOT/other/dir/link/file"
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME 1"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 3
assert_metric_eq "${METRIC_FETCH_COUNT}" 3
}

##############################################
# Test sync with a subdir-path link
##############################################
function e2e::sync_head_subdir_link() {
# First sync
echo "$FUNCNAME 1" > "$REPO/file"
git -C "$REPO" commit -qam "$FUNCNAME 1"

GIT_SYNC \
--period=100ms \
--repo="file://$REPO" \
--ref=HEAD \
--root="$ROOT" \
--link="other/dir/link" \
&
wait_for_sync "${MAXWAIT}"
assert_file_absent "$ROOT/link"
assert_link_exists "$ROOT/other/dir/link"
assert_file_exists "$ROOT/other/dir/link/file"
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME 1"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 1
assert_metric_eq "${METRIC_FETCH_COUNT}" 1

# Move HEAD forward
echo "$FUNCNAME 2" > "$REPO/file"
git -C "$REPO" commit -qam "$FUNCNAME 2"
wait_for_sync "${MAXWAIT}"
assert_file_absent "$ROOT/link"
assert_link_exists "$ROOT/other/dir/link"
assert_file_exists "$ROOT/other/dir/link/file"
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME 2"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 2
assert_metric_eq "${METRIC_FETCH_COUNT}" 2

# Move HEAD backward
git -C "$REPO" reset -q --hard HEAD^
wait_for_sync "${MAXWAIT}"
assert_file_absent "$ROOT/link"
assert_link_exists "$ROOT/other/dir/link"
assert_file_exists "$ROOT/other/dir/link/file"
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME 1"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 3
assert_metric_eq "${METRIC_FETCH_COUNT}" 3
}

##############################################
# Test worktree-cleanup
##############################################
Expand Down

0 comments on commit 9a806c0

Please sign in to comment.