Skip to content

Commit e86d02f

Browse files
committed
git_repository_set_head: use remote name in reflog
When `git_repository_set_head` is provided a remote reference, update the reflog with the tag name, like we do with a branch. This helps consumers match the semantics of `git checkout remote`.
1 parent ea3bb5c commit e86d02f

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/repository.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,7 +2525,9 @@ static int checkout_message(git_buf *out, git_reference *old, const char *new)
25252525

25262526
git_buf_puts(out, " to ");
25272527

2528-
if (git_reference__is_branch(new) || git_reference__is_tag(new))
2528+
if (git_reference__is_branch(new) ||
2529+
git_reference__is_tag(new) ||
2530+
git_reference__is_remote(new))
25292531
git_buf_puts(out, git_reference__shorthand(new));
25302532
else
25312533
git_buf_puts(out, new);
@@ -2603,7 +2605,7 @@ int git_repository_set_head(
26032605
git_reference_name(ref), true, git_buf_cstr(&log_message));
26042606
} else {
26052607
error = detach(repo, git_reference_target(ref),
2606-
git_reference_is_tag(ref) ? refname : NULL);
2608+
git_reference_is_tag(ref) || git_reference_is_remote(ref) ? refname : NULL);
26072609
}
26082610
} else if (git_reference__is_branch(refname)) {
26092611
error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, refname,

tests/repo/head.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,18 @@ void test_repo_head__setting_head_updates_reflog(void)
262262
cl_git_pass(git_repository_set_head_detached(repo, git_object_id(tag)));
263263
cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked"));
264264
cl_git_pass(git_repository_set_head(repo, "refs/tags/test"));
265+
cl_git_pass(git_repository_set_head(repo, "refs/remotes/test/master"));
265266

266-
test_reflog(repo, 3, NULL, "refs/heads/haacked", "foo@example.com", "checkout: moving from master to haacked");
267-
test_reflog(repo, 2, NULL, "tags/test^{commit}", "foo@example.com", "checkout: moving from unborn to e90810b8df3e80c413d903f631643c716887138d");
268-
test_reflog(repo, 1, "tags/test^{commit}", "refs/heads/haacked", "foo@example.com", "checkout: moving from e90810b8df3e80c413d903f631643c716887138d to haacked");
269-
test_reflog(repo, 0, "refs/heads/haacked", "tags/test^{commit}", "foo@example.com", "checkout: moving from haacked to test");
267+
test_reflog(repo, 4, NULL, "refs/heads/haacked", "foo@example.com", "checkout: moving from master to haacked");
268+
test_reflog(repo, 3, NULL, "tags/test^{commit}", "foo@example.com", "checkout: moving from unborn to e90810b8df3e80c413d903f631643c716887138d");
269+
test_reflog(repo, 2, "tags/test^{commit}", "refs/heads/haacked", "foo@example.com", "checkout: moving from e90810b8df3e80c413d903f631643c716887138d to haacked");
270+
test_reflog(repo, 1, "refs/heads/haacked", "tags/test^{commit}", "foo@example.com", "checkout: moving from haacked to test");
271+
test_reflog(repo, 0, "tags/test^{commit}", "refs/remotes/test/master", "foo@example.com", "checkout: moving from e90810b8df3e80c413d903f631643c716887138d to test/master");
270272

271273
cl_git_pass(git_annotated_commit_from_revspec(&annotated, repo, "haacked~0"));
272274
cl_git_pass(git_repository_set_head_detached_from_annotated(repo, annotated));
273275

274-
test_reflog(repo, 0, NULL, "refs/heads/haacked", "foo@example.com", "checkout: moving from e90810b8df3e80c413d903f631643c716887138d to haacked~0");
276+
test_reflog(repo, 0, NULL, "refs/heads/haacked", "foo@example.com", "checkout: moving from be3563ae3f795b2b4353bcce3a527ad0a4f7f644 to haacked~0");
275277

276278
git_annotated_commit_free(annotated);
277279
git_object_free(tag);

0 commit comments

Comments
 (0)