checkout: fix bug with --to and relative HEAD

Given "git checkout --to <path> HEAD~1", the new worktree's HEAD should begin life at the current branch's HEAD~1, however, it actually ends up at HEAD~2. This happens because: 1. git-checkout resolves HEAD~1 2. to satisfy is_git_directory(), prepare_linked_worktree() creates a HEAD for the new worktree with the value of the resolved HEAD~1 3. git-checkout re-invokes itself with the same arguments within the new worktree to populate the worktree 4. the sub git-checkout resolves HEAD~1 relative to its own HEAD, which is the resolved HEAD~1 from the original invocation, resulting unexpectedly and incorrectly in HEAD~2 (relative to the original) Fix this by unconditionally assigning the current worktree's HEAD as the value of the new worktree's HEAD. As a side-effect, this change also eliminates a dependence within prepare_linked_checkout() upon 'struct branch_info'. The plan is to eventually relocate "git checkout --to" functionality to "git worktree add", and worktree.c won't have knowledge of 'struct branch_info', so removal of this dependency is a step toward that goal. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jul 6, 2015 at 13:30 UTC c990a4c11dd7bb671da1b30e14568ad986621488
2 files changed +22 -4
builtin/checkout.c
+12 -4
@@ -863,6 +863,7 @@ static int prepare_linked_checkout(const struct checkout_opts *opts,
863 struct stat st;
864 struct child_process cp;
865 int counter = 0, len, ret;
866 + unsigned char rev[20];
867
868 if (!new->commit)
869 die(_("no branch specified"));
@@ -920,13 +921,20 @@ static int prepare_linked_checkout(const struct checkout_opts *opts,
921 real_path(get_git_common_dir()), name);
922 /*
923 * This is to keep resolve_ref() happy. We need a valid HEAD
923 - * or is_git_directory() will reject the directory. Any valid
924 - * value would do because this value will be ignored and
925 - * replaced at the next (real) checkout.
924 + * or is_git_directory() will reject the directory. Moreover, HEAD
925 + * in the new worktree must resolve to the same value as HEAD in
926 + * the current tree since the command invoked to populate the new
927 + * worktree will be handed the branch/ref specified by the user.
928 + * For instance, if the user asks for the new worktree to be based
929 + * at HEAD~5, then the resolved HEAD~5 in the new worktree must
930 + * match the resolved HEAD~5 in the current tree in order to match
931 + * the user's expectation.
932 */
933 + if (!resolve_ref_unsafe("HEAD", 0, rev, NULL))
934 + die(_("unable to resolve HEAD"));
935 strbuf_reset(&sb);
936 strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
929 - write_file(sb.buf, 1, "%s\n", sha1_to_hex(new->commit->object.sha1));
937 + write_file(sb.buf, 1, "%s\n", sha1_to_hex(rev));
938 strbuf_reset(&sb);
939 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
940 write_file(sb.buf, 1, "../..\n");
t/t2025-checkout-to.sh
+10
@@ -134,4 +134,14 @@ test_expect_success 'checkout with grafts' '
134 test_cmp expected actual
135 '
136
137 +test_expect_success 'checkout --to from relative HEAD' '
138 + test_commit a &&
139 + test_commit b &&
140 + test_commit c &&
141 + git rev-parse HEAD~1 >expected &&
142 + git checkout --to relhead HEAD~1 &&
143 + git -C relhead rev-parse HEAD >actual &&
144 + test_cmp expected actual
145 +'
146 +
147 test_done