diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2017-03-21 18:12:02 +0000 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2017-03-21 18:12:02 +0000 |
commit | ea3bb5c0bb2be821ef6718ce57899ad5cb7a2d6b (patch) | |
tree | e19047f2ae2d0338f93501ba255907cd5fc3ebab /src/repository.c | |
parent | 33ea4aae378f390aa4e2163389cd2cc183420ebe (diff) | |
download | libgit2-ea3bb5c0bb2be821ef6718ce57899ad5cb7a2d6b.tar.gz |
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`.
Diffstat (limited to 'src/repository.c')
-rw-r--r-- | src/repository.c | 75 |
1 files changed, 38 insertions, 37 deletions
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) |