From ea3bb5c0bb2be821ef6718ce57899ad5cb7a2d6b Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Tue, 21 Mar 2017 18:12:02 +0000 Subject: git_repository_set_head: use tag name in reflog When `git_repository_set_head` is provided a tag reference, update the reflog with the tag name, like we do with a branch. This helps consumers match the semantics of `git checkout tag`. --- src/repository.c | 75 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/repository.c b/src/repository.c index 0db481638..5d54acd94 100644 --- a/src/repository.c +++ b/src/repository.c @@ -2525,7 +2525,7 @@ static int checkout_message(git_buf *out, git_reference *old, const char *new) git_buf_puts(out, " to "); - if (git_reference__is_branch(new)) + if (git_reference__is_branch(new) || git_reference__is_tag(new)) git_buf_puts(out, git_reference__shorthand(new)); else git_buf_puts(out, new); @@ -2536,6 +2536,41 @@ static int checkout_message(git_buf *out, git_reference *old, const char *new) return 0; } +static int detach(git_repository *repo, const git_oid *id, const char *new) +{ + int error; + git_buf log_message = GIT_BUF_INIT; + git_object *object = NULL, *peeled = NULL; + git_reference *new_head = NULL, *current = NULL; + + assert(repo && id); + + if ((error = git_reference_lookup(¤t, repo, GIT_HEAD_FILE)) < 0) + return error; + + if ((error = git_object_lookup(&object, repo, id, GIT_OBJ_ANY)) < 0) + goto cleanup; + + if ((error = git_object_peel(&peeled, object, GIT_OBJ_COMMIT)) < 0) + goto cleanup; + + if (new == NULL) + new = git_oid_tostr_s(git_object_id(peeled)); + + if ((error = checkout_message(&log_message, current, new)) < 0) + goto cleanup; + + error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_object_id(peeled), true, git_buf_cstr(&log_message)); + +cleanup: + git_buf_free(&log_message); + git_object_free(object); + git_object_free(peeled); + git_reference_free(current); + git_reference_free(new_head); + return error; +} + int git_repository_set_head( git_repository* repo, const char* refname) @@ -2567,7 +2602,8 @@ int git_repository_set_head( error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, git_reference_name(ref), true, git_buf_cstr(&log_message)); } else { - error = git_repository_set_head_detached(repo, git_reference_target(ref)); + error = detach(repo, git_reference_target(ref), + git_reference_is_tag(ref) ? refname : NULL); } } else if (git_reference__is_branch(refname)) { error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, refname, @@ -2582,41 +2618,6 @@ cleanup: return error; } -static int detach(git_repository *repo, const git_oid *id, const char *from) -{ - int error; - git_buf log_message = GIT_BUF_INIT; - git_object *object = NULL, *peeled = NULL; - git_reference *new_head = NULL, *current = NULL; - - assert(repo && id); - - if ((error = git_reference_lookup(¤t, repo, GIT_HEAD_FILE)) < 0) - return error; - - if ((error = git_object_lookup(&object, repo, id, GIT_OBJ_ANY)) < 0) - goto cleanup; - - if ((error = git_object_peel(&peeled, object, GIT_OBJ_COMMIT)) < 0) - goto cleanup; - - if (from == NULL) - from = git_oid_tostr_s(git_object_id(peeled)); - - if ((error = checkout_message(&log_message, current, from)) < 0) - goto cleanup; - - error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_object_id(peeled), true, git_buf_cstr(&log_message)); - -cleanup: - git_buf_free(&log_message); - git_object_free(object); - git_object_free(peeled); - git_reference_free(current); - git_reference_free(new_head); - return error; -} - int git_repository_set_head_detached( git_repository* repo, const git_oid* commitish) -- cgit v1.2.1 From e86d02f92de2c446b370e4711dcfe2ea5ce75166 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Mon, 3 Apr 2017 00:10:47 +0100 Subject: 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`. --- src/repository.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/repository.c b/src/repository.c index 5d54acd94..c26aa3465 100644 --- a/src/repository.c +++ b/src/repository.c @@ -2525,7 +2525,9 @@ static int checkout_message(git_buf *out, git_reference *old, const char *new) git_buf_puts(out, " to "); - if (git_reference__is_branch(new) || git_reference__is_tag(new)) + if (git_reference__is_branch(new) || + git_reference__is_tag(new) || + git_reference__is_remote(new)) git_buf_puts(out, git_reference__shorthand(new)); else git_buf_puts(out, new); @@ -2603,7 +2605,7 @@ int git_repository_set_head( git_reference_name(ref), true, git_buf_cstr(&log_message)); } else { error = detach(repo, git_reference_target(ref), - git_reference_is_tag(ref) ? refname : NULL); + git_reference_is_tag(ref) || git_reference_is_remote(ref) ? refname : NULL); } } else if (git_reference__is_branch(refname)) { error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, refname, -- cgit v1.2.1