Skip to content

Commit 9a806c0

Browse files
committed
Bug: links are relative to linkdir, not rootdir
1 parent 718a938 commit 9a806c0

File tree

3 files changed

+95
-33
lines changed

3 files changed

+95
-33
lines changed

abspath.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ func (abs absPath) Canonical() (absPath, error) {
4848
return absPath(result), nil
4949
}
5050

51-
// Join appends more path elements to abs, like filepath.Join.
51+
// Join appends more path elements to abs, like filepath.Join. This will clean
52+
// the final path (e.g. resolve ".." elements).
5253
func (abs absPath) Join(elems ...string) absPath {
5354
all := make([]string, 0, 1+len(elems))
5455
all = append(all, abs.String())

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ func (git *repoSync) publishSymlink(ctx context.Context, worktree worktree) erro
12771277
return fmt.Errorf("error making symlink dir: %w", err)
12781278
}
12791279

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

15791580
// SyncRepo syncs the repository to the desired ref, publishes it via the link,

test_e2e.sh

Lines changed: 90 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -411,36 +411,6 @@ function e2e::init_root_fails_sanity() {
411411
assert_file_eq "$ROOT/link/file" "$FUNCNAME"
412412
}
413413

414-
##############################################
415-
# Test init with an absolute-path link
416-
##############################################
417-
function e2e::sync_absolute_link() {
418-
GIT_SYNC \
419-
--one-time \
420-
--repo="file://$REPO" \
421-
--root="$ROOT/root" \
422-
--link="$ROOT/other/dir/link"
423-
assert_file_absent "$ROOT/root/link"
424-
assert_link_exists "$ROOT/other/dir/link"
425-
assert_file_exists "$ROOT/other/dir/link/file"
426-
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME"
427-
}
428-
429-
##############################################
430-
# Test init with a subdir-path link
431-
##############################################
432-
function e2e::sync_subdir_link() {
433-
GIT_SYNC \
434-
--one-time \
435-
--repo="file://$REPO" \
436-
--root="$ROOT" \
437-
--link="other/dir/link"
438-
assert_file_absent "$ROOT/link"
439-
assert_link_exists "$ROOT/other/dir/link"
440-
assert_file_exists "$ROOT/other/dir/link/file"
441-
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME"
442-
}
443-
444414
##############################################
445415
# Test non-zero exit with a bad ref
446416
##############################################
@@ -539,6 +509,96 @@ function e2e::sync_head() {
539509
assert_metric_eq "${METRIC_FETCH_COUNT}" 3
540510
}
541511

512+
##############################################
513+
# Test sync with an absolute-path link
514+
##############################################
515+
function e2e::sync_head_absolute_link() {
516+
# First sync
517+
echo "$FUNCNAME 1" > "$REPO/file"
518+
git -C "$REPO" commit -qam "$FUNCNAME 1"
519+
520+
GIT_SYNC \
521+
--period=100ms \
522+
--repo="file://$REPO" \
523+
--ref=HEAD \
524+
--root="$ROOT/root" \
525+
--link="$ROOT/other/dir/link" \
526+
&
527+
wait_for_sync "${MAXWAIT}"
528+
assert_file_absent "$ROOT/root/link"
529+
assert_link_exists "$ROOT/other/dir/link"
530+
assert_file_exists "$ROOT/other/dir/link/file"
531+
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME 1"
532+
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 1
533+
assert_metric_eq "${METRIC_FETCH_COUNT}" 1
534+
535+
# Move HEAD forward
536+
echo "$FUNCNAME 2" > "$REPO/file"
537+
git -C "$REPO" commit -qam "$FUNCNAME 2"
538+
wait_for_sync "${MAXWAIT}"
539+
assert_file_absent "$ROOT/root/link"
540+
assert_link_exists "$ROOT/other/dir/link"
541+
assert_file_exists "$ROOT/other/dir/link/file"
542+
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME 2"
543+
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 2
544+
assert_metric_eq "${METRIC_FETCH_COUNT}" 2
545+
546+
# Move HEAD backward
547+
git -C "$REPO" reset -q --hard HEAD^
548+
wait_for_sync "${MAXWAIT}"
549+
assert_file_absent "$ROOT/root/link"
550+
assert_link_exists "$ROOT/other/dir/link"
551+
assert_file_exists "$ROOT/other/dir/link/file"
552+
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME 1"
553+
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 3
554+
assert_metric_eq "${METRIC_FETCH_COUNT}" 3
555+
}
556+
557+
##############################################
558+
# Test sync with a subdir-path link
559+
##############################################
560+
function e2e::sync_head_subdir_link() {
561+
# First sync
562+
echo "$FUNCNAME 1" > "$REPO/file"
563+
git -C "$REPO" commit -qam "$FUNCNAME 1"
564+
565+
GIT_SYNC \
566+
--period=100ms \
567+
--repo="file://$REPO" \
568+
--ref=HEAD \
569+
--root="$ROOT" \
570+
--link="other/dir/link" \
571+
&
572+
wait_for_sync "${MAXWAIT}"
573+
assert_file_absent "$ROOT/link"
574+
assert_link_exists "$ROOT/other/dir/link"
575+
assert_file_exists "$ROOT/other/dir/link/file"
576+
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME 1"
577+
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 1
578+
assert_metric_eq "${METRIC_FETCH_COUNT}" 1
579+
580+
# Move HEAD forward
581+
echo "$FUNCNAME 2" > "$REPO/file"
582+
git -C "$REPO" commit -qam "$FUNCNAME 2"
583+
wait_for_sync "${MAXWAIT}"
584+
assert_file_absent "$ROOT/link"
585+
assert_link_exists "$ROOT/other/dir/link"
586+
assert_file_exists "$ROOT/other/dir/link/file"
587+
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME 2"
588+
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 2
589+
assert_metric_eq "${METRIC_FETCH_COUNT}" 2
590+
591+
# Move HEAD backward
592+
git -C "$REPO" reset -q --hard HEAD^
593+
wait_for_sync "${MAXWAIT}"
594+
assert_file_absent "$ROOT/link"
595+
assert_link_exists "$ROOT/other/dir/link"
596+
assert_file_exists "$ROOT/other/dir/link/file"
597+
assert_file_eq "$ROOT/other/dir/link/file" "$FUNCNAME 1"
598+
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 3
599+
assert_metric_eq "${METRIC_FETCH_COUNT}" 3
600+
}
601+
542602
##############################################
543603
# Test worktree-cleanup
544604
##############################################

0 commit comments

Comments
 (0)