fix 'got log -r' loading refs from the wrong repo if invoked in a work tree ok tracey
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
diff --git a/got/got.c b/got/got.c
index 377d2b0..cce99fd 100644
--- a/got/got.c
+++ b/got/got.c
@@ -3389,10 +3389,12 @@ cmd_log(int argc, char *argv[])
goto done;
}
- error = got_worktree_open(&worktree, cwd);
- if (error && error->code != GOT_ERR_NOT_WORKTREE)
- goto done;
- error = NULL;
+ if (repo_path == NULL) {
+ error = got_worktree_open(&worktree, cwd);
+ if (error && error->code != GOT_ERR_NOT_WORKTREE)
+ goto done;
+ error = NULL;
+ }
if (argc == 0) {
path = strdup("");
diff --git a/regress/cmdline/log.sh b/regress/cmdline/log.sh
index a92186b..ea958f5 100755
--- a/regress/cmdline/log.sh
+++ b/regress/cmdline/log.sh
@@ -547,6 +547,71 @@ function test_log_reverse_display {
test_done "$testroot" "$ret"
}
+function test_log_in_worktree_different_repo {
+ local testroot=`test_init log_in_worktree_different_repo 1`
+
+ make_test_tree $testroot/repo
+ mkdir -p $testroot/repo/epsilon/d
+ echo foo > $testroot/repo/epsilon/d/foo
+ (cd $testroot/repo && git add .)
+ git_commit $testroot/repo -m "adding the test tree"
+ local head_commit=`git_show_head $testroot/repo`
+
+ got init $testroot/other-repo
+ mkdir -p $testroot/tree
+ make_test_tree $testroot/tree
+ got import -mm -b foo -r $testroot/other-repo $testroot/tree >/dev/null
+ got checkout -b foo $testroot/other-repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "commit $head_commit (master)" > $testroot/stdout.expected
+
+ # 'got log' used to fail with "reference refs/heads/foo not found"
+ # even though that reference belongs to an unrelated repository
+ # found via a worktree via the current working directory
+ for p in "" alpha epsilon; do
+ (cd $testroot/wt && got log -r $testroot/repo $p | \
+ grep ^commit > $testroot/stdout)
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+ done
+
+ for p in "" epsilon/zeta; do
+ (cd $testroot/wt/epsilon && got log -r $testroot/repo $p | \
+ grep ^commit > $testroot/stdout)
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+ done
+
+ for p in "" foo; do
+ (cd $testroot/wt/epsilon && got log -r $testroot/repo epsilon/d/$p | \
+ grep ^commit > $testroot/stdout)
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+ done
+
+ test_done "$testroot" "0"
+}
+
run_test test_log_in_repo
run_test test_log_in_bare_repo
run_test test_log_in_worktree
@@ -556,3 +621,4 @@ run_test test_log_limit
run_test test_log_nonexistent_path
run_test test_log_end_at_commit
run_test test_log_reverse_display
+run_test test_log_in_worktree_different_repo