diff options
author | Carlos Martín Nieto <cmn@dwim.me> | 2015-01-15 16:50:31 +0100 |
---|---|---|
committer | Carlos Martín Nieto <cmn@dwim.me> | 2015-03-03 14:40:50 +0100 |
commit | 4e498646b6e4f3e6303cd3a27682dcd10d97eaeb (patch) | |
tree | cab3a87c8845f549d9355a505df665a57ed4f4b3 /src | |
parent | 412a3808889de65d8f94f22502aba10b9afbf755 (diff) | |
download | libgit2-4e498646b6e4f3e6303cd3a27682dcd10d97eaeb.tar.gz |
repository: remove log message override for switching the active branch
We want to use the "checkout: moving from ..." message in order to let
git know when a change of branch has happened. Make the convenience
functions for this goal write this message.
Diffstat (limited to 'src')
-rw-r--r-- | src/clone.c | 5 | ||||
-rw-r--r-- | src/refs.c | 11 | ||||
-rw-r--r-- | src/refs.h | 1 | ||||
-rw-r--r-- | src/repository.c | 86 | ||||
-rw-r--r-- | src/submodule.c | 4 |
5 files changed, 72 insertions, 35 deletions
diff --git a/src/clone.c b/src/clone.c index ac6a059dd..7e5d3302e 100644 --- a/src/clone.c +++ b/src/clone.c @@ -127,8 +127,7 @@ static int update_head_to_new_branch( if (!error) error = git_repository_set_head( - repo, git_reference_name(tracking_branch), - reflog_message); + repo, git_reference_name(tracking_branch)); git_reference_free(tracking_branch); @@ -169,7 +168,7 @@ static int update_head_to_remote( error = git_remote_default_branch(&branch, remote); if (error == GIT_ENOTFOUND) { error = git_repository_set_head_detached( - repo, remote_head_id, reflog_message); + repo, remote_head_id); goto cleanup; } diff --git a/src/refs.c b/src/refs.c index 415918600..3f6c33dcd 100644 --- a/src/refs.c +++ b/src/refs.c @@ -589,7 +589,7 @@ static int reference__rename(git_reference **out, git_reference *ref, const char /* Update HEAD it was pointing to the reference being renamed */ if (should_head_be_updated && - (error = git_repository_set_head(ref->db->repo, normalized, message)) < 0) { + (error = git_repository_set_head(ref->db->repo, normalized)) < 0) { giterr_set(GITERR_REFERENCE, "Failed to update HEAD after renaming reference"); return error; } @@ -1284,10 +1284,8 @@ int git_reference_is_valid_name(const char *refname) return git_reference__is_valid_name(refname, GIT_REF_FORMAT_ALLOW_ONELEVEL); } -const char *git_reference_shorthand(const git_reference *ref) +const char *git_reference__shorthand(const char *name) { - const char *name = ref->name; - if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR)) return name + strlen(GIT_REFS_HEADS_DIR); else if (!git__prefixcmp(name, GIT_REFS_TAGS_DIR)) @@ -1300,3 +1298,8 @@ const char *git_reference_shorthand(const git_reference *ref) /* No shorthands are avaiable, so just return the name */ return name; } + +const char *git_reference_shorthand(const git_reference *ref) +{ + return git_reference__shorthand(ref->name); +} diff --git a/src/refs.h b/src/refs.h index ace93e33b..f78ea06b0 100644 --- a/src/refs.h +++ b/src/refs.h @@ -74,6 +74,7 @@ int git_reference__is_valid_name(const char *refname, unsigned int flags); int git_reference__is_branch(const char *ref_name); int git_reference__is_remote(const char *ref_name); int git_reference__is_tag(const char *ref_name); +const char *git_reference__shorthand(const char *name); /** * Lookup a reference by name and try to resolve to an OID. diff --git a/src/repository.c b/src/repository.c index f8a4d3e56..778cdefd7 100644 --- a/src/repository.c +++ b/src/repository.c @@ -1889,39 +1889,62 @@ cleanup: return error; } -static bool looks_like_a_branch(const char *refname) +static int checkout_message(git_buf *out, git_reference *old, const char *new) { - return git__prefixcmp(refname, GIT_REFS_HEADS_DIR) == 0; + git_buf_puts(out, "checkout: moving from "); + + if (git_reference_type(old) == GIT_REF_SYMBOLIC) + git_buf_puts(out, git_reference__shorthand(git_reference_symbolic_target(old))); + else + git_buf_puts(out, git_oid_tostr_s(git_reference_target(old))); + + git_buf_puts(out, " to "); + + if (git_reference__is_branch(new)) + git_buf_puts(out, git_reference__shorthand(new)); + else + git_buf_puts(out, new); + + if (git_buf_oom(out)) + return -1; + + return 0; } int git_repository_set_head( git_repository* repo, - const char* refname, - const char *log_message) + const char* refname) { - git_reference *ref, - *new_head = NULL; + git_reference *ref = NULL, *current = NULL, *new_head = NULL; + git_buf log_message = GIT_BUF_INIT; int error; assert(repo && refname); + if ((error = git_reference_lookup(¤t, repo, GIT_HEAD_FILE)) < 0) + return error; + + if ((error = checkout_message(&log_message, current, refname)) < 0) + goto cleanup; + error = git_reference_lookup(&ref, repo, refname); if (error < 0 && error != GIT_ENOTFOUND) - return error; + goto cleanup; if (!error) { if (git_reference_is_branch(ref)) { error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, - git_reference_name(ref), true, log_message); + git_reference_name(ref), true, git_buf_cstr(&log_message)); } else { - error = git_repository_set_head_detached(repo, git_reference_target(ref), - log_message); + error = git_repository_set_head_detached(repo, git_reference_target(ref)); } - } else if (looks_like_a_branch(refname)) { + } else if (git_reference__is_branch(refname)) { error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, refname, - true, log_message); + true, git_buf_cstr(&log_message)); } +cleanup: + git_reference_free(current); git_reference_free(ref); git_reference_free(new_head); return error; @@ -1929,55 +1952,66 @@ int git_repository_set_head( int git_repository_set_head_detached( git_repository* repo, - const git_oid* commitish, - const char *log_message) + const git_oid* commitish) { int error; - git_object *object, - *peeled = NULL; - git_reference *new_head = NULL; + git_buf log_message = GIT_BUF_INIT; + git_object *object = NULL, *peeled = NULL; + git_reference *new_head = NULL, *current = NULL; assert(repo && commitish); - if ((error = git_object_lookup(&object, repo, commitish, GIT_OBJ_ANY)) < 0) + if ((error = git_reference_lookup(¤t, repo, GIT_HEAD_FILE)) < 0) return error; + if ((error = git_object_lookup(&object, repo, commitish, GIT_OBJ_ANY)) < 0) + goto cleanup; + if ((error = git_object_peel(&peeled, object, GIT_OBJ_COMMIT)) < 0) goto cleanup; - error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_object_id(peeled), true, log_message); + if ((error = checkout_message(&log_message, current, git_oid_tostr_s(git_object_id(peeled)))) < 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_object_free(object); git_object_free(peeled); + git_reference_free(current); git_reference_free(new_head); return error; } -int git_repository_detach_head( - git_repository* repo, - const char *reflog_message) +int git_repository_detach_head(git_repository* repo) { - git_reference *old_head = NULL, - *new_head = NULL; + git_reference *old_head = NULL, *new_head = NULL, *current = NULL; git_object *object = NULL; + git_buf log_message = GIT_BUF_INIT; int error; assert(repo); - if ((error = git_repository_head(&old_head, repo)) < 0) + if ((error = git_reference_lookup(¤t, repo, GIT_HEAD_FILE)) < 0) return error; + if ((error = git_repository_head(&old_head, repo)) < 0) + goto cleanup; + if ((error = git_object_lookup(&object, repo, git_reference_target(old_head), GIT_OBJ_COMMIT)) < 0) goto cleanup; + if ((error = checkout_message(&log_message, current, git_oid_tostr_s(git_object_id(object)))) < 0) + goto cleanup; + error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_reference_target(old_head), - 1, reflog_message); + 1, git_buf_cstr(&log_message)); cleanup: git_object_free(object); git_reference_free(old_head); git_reference_free(new_head); + git_reference_free(current); return error; } diff --git a/src/submodule.c b/src/submodule.c index cba6c0f77..567ab748e 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -984,7 +984,7 @@ int git_submodule_update(git_submodule *sm, int init, git_submodule_update_optio update_options.checkout_opts.checkout_strategy = update_options.clone_checkout_strategy; if ((error = git_clone(&sub_repo, submodule_url, sm->path, &clone_options)) < 0 || - (error = git_repository_set_head_detached(sub_repo, git_submodule_index_id(sm), NULL)) < 0 || + (error = git_repository_set_head_detached(sub_repo, git_submodule_index_id(sm))) < 0 || (error = git_checkout_head(sub_repo, &update_options.checkout_opts)) != 0) goto done; } else { @@ -996,7 +996,7 @@ int git_submodule_update(git_submodule *sm, int init, git_submodule_update_optio if ((error = git_submodule_open(&sub_repo, sm)) < 0 || (error = git_object_lookup(&target_commit, sub_repo, git_submodule_index_id(sm), GIT_OBJ_COMMIT)) < 0 || (error = git_checkout_tree(sub_repo, target_commit, &update_options.checkout_opts)) != 0 || - (error = git_repository_set_head_detached(sub_repo, git_submodule_index_id(sm), NULL)) < 0) + (error = git_repository_set_head_detached(sub_repo, git_submodule_index_id(sm))) < 0) goto done; /* Invalidate the wd flags as the workdir has been updated. */ |