diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2015-03-03 10:07:36 -0500 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2015-03-03 10:07:36 -0500 |
| commit | 72ca65d538d1e3ab7a9052613412bf121470dfd6 (patch) | |
| tree | 1a7db7342d02450a871b4a73469a22b2e4ef07a1 /src | |
| parent | fe477951be315e6fe28d6df9baf1557cef6fe539 (diff) | |
| parent | 41513659de1958fd5907c511f7ddd2ee46e6cd69 (diff) | |
| download | libgit2-72ca65d538d1e3ab7a9052613412bf121470dfd6.tar.gz | |
Merge pull request #2808 from libgit2/cmn/repo-ident
Remove the signature from ref-modifying functions
Diffstat (limited to 'src')
| -rw-r--r-- | src/branch.c | 33 | ||||
| -rw-r--r-- | src/clone.c | 48 | ||||
| -rw-r--r-- | src/commit.c | 4 | ||||
| -rw-r--r-- | src/push.c | 9 | ||||
| -rw-r--r-- | src/push.h | 7 | ||||
| -rw-r--r-- | src/rebase.c | 23 | ||||
| -rw-r--r-- | src/refs.c | 162 | ||||
| -rw-r--r-- | src/refs.h | 4 | ||||
| -rw-r--r-- | src/remote.c | 28 | ||||
| -rw-r--r-- | src/repository.c | 122 | ||||
| -rw-r--r-- | src/repository.h | 3 | ||||
| -rw-r--r-- | src/reset.c | 16 | ||||
| -rw-r--r-- | src/stash.c | 5 | ||||
| -rw-r--r-- | src/submodule.c | 5 | ||||
| -rw-r--r-- | src/tag.c | 4 | ||||
| -rw-r--r-- | src/transports/local.c | 2 |
16 files changed, 267 insertions, 208 deletions
diff --git a/src/branch.c b/src/branch.c index b39d74749..4e9460f36 100644 --- a/src/branch.c +++ b/src/branch.c @@ -54,14 +54,12 @@ int git_branch_create( git_repository *repository, const char *branch_name, const git_commit *commit, - int force, - const git_signature *signature, - const char *log_message) + int force) { int is_head = 0; git_reference *branch = NULL; git_buf canonical_branch_name = GIT_BUF_INIT, - log_message_buf = GIT_BUF_INIT; + log_message = GIT_BUF_INIT; int error = -1; assert(branch_name && commit && ref_out); @@ -88,19 +86,19 @@ int git_branch_create( if (git_buf_joinpath(&canonical_branch_name, GIT_REFS_HEADS_DIR, branch_name) < 0) goto cleanup; - if (git_buf_sets(&log_message_buf, log_message ? log_message : "Branch: created") < 0) + if (git_buf_printf(&log_message, "Branch: created from %s", git_oid_tostr_s(git_commit_id(commit))) < 0) goto cleanup; error = git_reference_create(&branch, repository, - git_buf_cstr(&canonical_branch_name), git_commit_id(commit), force, signature, - git_buf_cstr(&log_message_buf)); + git_buf_cstr(&canonical_branch_name), git_commit_id(commit), force, + git_buf_cstr(&log_message)); if (!error) *ref_out = branch; cleanup: git_buf_free(&canonical_branch_name); - git_buf_free(&log_message_buf); + git_buf_free(&log_message); return error; } @@ -222,14 +220,12 @@ int git_branch_move( git_reference **out, git_reference *branch, const char *new_branch_name, - int force, - const git_signature *signature, - const char *log_message) + int force) { git_buf new_reference_name = GIT_BUF_INIT, old_config_section = GIT_BUF_INIT, new_config_section = GIT_BUF_INIT, - log_message_buf = GIT_BUF_INIT; + log_message = GIT_BUF_INIT; int error; assert(branch && new_branch_name); @@ -240,20 +236,15 @@ int git_branch_move( if ((error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name)) < 0) goto done; - if (log_message) { - if ((error = git_buf_sets(&log_message_buf, log_message)) < 0) + if ((error = git_buf_printf(&log_message, "Branch: renamed %s to %s", + git_reference_name(branch), git_buf_cstr(&new_reference_name))) < 0) goto done; - } else { - if ((error = git_buf_printf(&log_message_buf, "Branch: renamed %s to %s", - git_reference_name(branch), git_buf_cstr(&new_reference_name))) < 0) - goto done; - } /* first update ref then config so failure won't trash config */ error = git_reference_rename( out, branch, git_buf_cstr(&new_reference_name), force, - signature, git_buf_cstr(&log_message_buf)); + git_buf_cstr(&log_message)); if (error < 0) goto done; @@ -270,7 +261,7 @@ done: git_buf_free(&new_reference_name); git_buf_free(&old_config_section); git_buf_free(&new_config_section); - git_buf_free(&log_message_buf); + git_buf_free(&log_message); return error; } diff --git a/src/clone.c b/src/clone.c index f18f07611..7e5d3302e 100644 --- a/src/clone.c +++ b/src/clone.c @@ -24,18 +24,18 @@ #include "repository.h" #include "odb.h" -static int clone_local_into(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, int link, const git_signature *signature); +static int clone_local_into(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, int link); static int create_branch( git_reference **branch, git_repository *repo, const git_oid *target, const char *name, - const git_signature *signature, const char *log_message) { git_commit *head_obj = NULL; git_reference *branch_ref = NULL; + git_buf refname = GIT_BUF_INIT; int error; /* Find the target commit */ @@ -43,8 +43,11 @@ static int create_branch( return error; /* Create the new branch */ - error = git_branch_create(&branch_ref, repo, name, head_obj, 0, signature, log_message); + if ((error = git_buf_printf(&refname, GIT_REFS_HEADS_DIR "%s", name)) < 0) + return error; + error = git_reference_create(&branch_ref, repo, git_buf_cstr(&refname), target, 0, log_message); + git_buf_free(&refname); git_commit_free(head_obj); if (!error) @@ -93,12 +96,11 @@ static int create_tracking_branch( git_repository *repo, const git_oid *target, const char *branch_name, - const git_signature *signature, const char *log_message) { int error; - if ((error = create_branch(branch, repo, target, branch_name, signature, log_message)) < 0) + if ((error = create_branch(branch, repo, target, branch_name, log_message)) < 0) return error; return setup_tracking_config( @@ -112,7 +114,6 @@ static int update_head_to_new_branch( git_repository *repo, const git_oid *target, const char *name, - const git_signature *signature, const char *reflog_message) { git_reference *tracking_branch = NULL; @@ -122,12 +123,11 @@ static int update_head_to_new_branch( name += strlen(GIT_REFS_HEADS_DIR); error = create_tracking_branch(&tracking_branch, repo, target, name, - signature, reflog_message); + reflog_message); if (!error) error = git_repository_set_head( - repo, git_reference_name(tracking_branch), - signature, reflog_message); + repo, git_reference_name(tracking_branch)); git_reference_free(tracking_branch); @@ -141,7 +141,6 @@ static int update_head_to_new_branch( static int update_head_to_remote( git_repository *repo, git_remote *remote, - const git_signature *signature, const char *reflog_message) { int error = 0; @@ -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, signature, reflog_message); + repo, remote_head_id); goto cleanup; } @@ -192,7 +191,7 @@ static int update_head_to_remote( repo, remote_head_id, git_buf_cstr(&branch), - signature, reflog_message); + reflog_message); cleanup: git_buf_free(&remote_master_name); @@ -205,7 +204,6 @@ static int update_head_to_branch( git_repository *repo, const char *remote_name, const char *branch, - const git_signature *signature, const char *reflog_message) { int retcode; @@ -222,7 +220,7 @@ static int update_head_to_branch( goto cleanup; retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), branch, - signature, reflog_message); + reflog_message); cleanup: git_reference_free(remote_ref); @@ -313,16 +311,16 @@ static bool should_checkout( return !git_repository_head_unborn(repo); } -static int checkout_branch(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, const git_signature *signature, const char *reflog_message) +static int checkout_branch(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, const char *reflog_message) { int error; if (branch) error = update_head_to_branch(repo, git_remote_name(remote), branch, - signature, reflog_message); + reflog_message); /* Point HEAD to the same ref as the remote's head */ else - error = update_head_to_remote(repo, remote, signature, reflog_message); + error = update_head_to_remote(repo, remote, reflog_message); if (!error && should_checkout(repo, git_repository_is_bare(repo), co_opts)) error = git_checkout_head(repo, co_opts); @@ -330,7 +328,7 @@ static int checkout_branch(git_repository *repo, git_remote *remote, const git_c return error; } -static int clone_into(git_repository *repo, git_remote *_remote, const git_checkout_options *co_opts, const char *branch, const git_signature *signature) +static int clone_into(git_repository *repo, git_remote *_remote, const git_checkout_options *co_opts, const char *branch) { int error; git_buf reflog_message = GIT_BUF_INIT; @@ -358,10 +356,10 @@ static int clone_into(git_repository *repo, git_remote *_remote, const git_check git_remote_set_update_fetchhead(remote, 0); git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote)); - if ((error = git_remote_fetch(remote, NULL, signature, git_buf_cstr(&reflog_message))) != 0) + if ((error = git_remote_fetch(remote, NULL, git_buf_cstr(&reflog_message))) != 0) goto cleanup; - error = checkout_branch(repo, remote, co_opts, branch, signature, git_buf_cstr(&reflog_message)); + error = checkout_branch(repo, remote, co_opts, branch, git_buf_cstr(&reflog_message)); cleanup: git_remote_free(remote); @@ -442,11 +440,11 @@ int git_clone( if (clone_local == 1) error = clone_local_into( repo, origin, &options.checkout_opts, - options.checkout_branch, link, options.signature); + options.checkout_branch, link); else if (clone_local == 0) error = clone_into( repo, origin, &options.checkout_opts, - options.checkout_branch, options.signature); + options.checkout_branch); else error = -1; @@ -508,7 +506,7 @@ static bool can_link(const char *src, const char *dst, int link) #endif } -static int clone_local_into(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, int link, const git_signature *signature) +static int clone_local_into(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, int link) { int error, flags; git_repository *src; @@ -553,10 +551,10 @@ static int clone_local_into(git_repository *repo, git_remote *remote, const git_ git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote)); - if ((error = git_remote_fetch(remote, NULL, signature, git_buf_cstr(&reflog_message))) != 0) + if ((error = git_remote_fetch(remote, NULL, git_buf_cstr(&reflog_message))) != 0) goto cleanup; - error = checkout_branch(repo, remote, co_opts, branch, signature, git_buf_cstr(&reflog_message)); + error = checkout_branch(repo, remote, co_opts, branch, git_buf_cstr(&reflog_message)); cleanup: git_buf_free(&reflog_message); diff --git a/src/commit.c b/src/commit.c index beb2c64c3..84f24c6cf 100644 --- a/src/commit.c +++ b/src/commit.c @@ -104,7 +104,7 @@ int git_commit_create_from_callback( if (update_ref != NULL) { error = git_reference__update_for_commit( - repo, ref, update_ref, id, committer, "commit"); + repo, ref, update_ref, id, "commit"); git_reference_free(ref); return error; } @@ -295,7 +295,7 @@ int git_commit_amend( if (!error && update_ref) { error = git_reference__update_for_commit( - repo, ref, NULL, id, committer, "commit"); + repo, ref, NULL, id, "commit"); git_reference_free(ref); } diff --git a/src/push.c b/src/push.c index fb51383cb..c6a93ba2f 100644 --- a/src/push.c +++ b/src/push.c @@ -167,10 +167,7 @@ int git_push_add_refspec(git_push *push, const char *refspec) return 0; } -int git_push_update_tips( - git_push *push, - const git_signature *signature, - const char *reflog_message) +int git_push_update_tips(git_push *push) { git_buf remote_ref_name = GIT_BUF_INIT; size_t i, j; @@ -213,8 +210,8 @@ int git_push_update_tips( } } else { error = git_reference_create(NULL, push->remote->repo, - git_buf_cstr(&remote_ref_name), &push_spec->loid, 1, signature, - reflog_message ? reflog_message : "update by push"); + git_buf_cstr(&remote_ref_name), &push_spec->loid, 1, + "update by push"); } } diff --git a/src/push.h b/src/push.h index 264ecad8c..b19d40e03 100644 --- a/src/push.h +++ b/src/push.h @@ -109,15 +109,10 @@ int git_push_add_refspec(git_push *push, const char *refspec); * * @param push The push object * @param signature The identity to use when updating reflogs - * @param reflog_message The message to insert into the reflogs. If NULL, the - * default is "update by push". * * @return 0 or an error code */ -int git_push_update_tips( - git_push *push, - const git_signature *signature, - const char *reflog_message); +int git_push_update_tips(git_push *push); /** * Perform the push diff --git a/src/rebase.c b/src/rebase.c index cf3558d16..43945df15 100644 --- a/src/rebase.c +++ b/src/rebase.c @@ -655,7 +655,6 @@ int git_rebase_init( const git_annotated_commit *branch, const git_annotated_commit *upstream, const git_annotated_commit *onto, - const git_signature *signature, const git_rebase_options *given_opts) { git_rebase *rebase = NULL; @@ -663,6 +662,7 @@ int git_rebase_init( git_buf reflog = GIT_BUF_INIT; git_commit *onto_commit = NULL; git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; + git_reference *head_ref = NULL; int error; assert(repo && (upstream || onto)); @@ -694,13 +694,14 @@ int git_rebase_init( "rebase: checkout %s", rebase_onto_name(onto))) < 0 || (error = git_checkout_tree( repo, (git_object *)onto_commit, &checkout_opts)) < 0 || - (error = git_repository_set_head_detached( - repo, git_annotated_commit_id(onto), signature, reflog.ptr)) < 0) + (error = git_reference_create(&head_ref, repo, GIT_HEAD_FILE, + git_annotated_commit_id(onto), 1, reflog.ptr)) < 0) goto done; *out = rebase; done: + git_reference_free(head_ref); if (error < 0) { rebase_cleanup(rebase); git_rebase_free(rebase); @@ -902,7 +903,7 @@ static int rebase_commit_merge( (const git_commit **)&head_commit)) < 0 || (error = git_commit_lookup(&commit, rebase->repo, commit_id)) < 0 || (error = git_reference__update_for_commit( - rebase->repo, NULL, "HEAD", commit_id, committer, "rebase")) < 0) + rebase->repo, NULL, "HEAD", commit_id, "rebase")) < 0) goto done; git_oid_fmt(old_idstr, git_commit_id(current_commit)); @@ -949,20 +950,20 @@ int git_rebase_commit( return error; } -int git_rebase_abort(git_rebase *rebase, const git_signature *signature) +int git_rebase_abort(git_rebase *rebase) { git_reference *orig_head_ref = NULL; git_commit *orig_head_commit = NULL; int error; - assert(rebase && signature); + assert(rebase); error = rebase->head_detached ? git_reference_create(&orig_head_ref, rebase->repo, GIT_HEAD_FILE, - &rebase->orig_head_id, 1, signature, "rebase: aborting") : + &rebase->orig_head_id, 1, "rebase: aborting") : git_reference_symbolic_create( &orig_head_ref, rebase->repo, GIT_HEAD_FILE, rebase->orig_head_name, 1, - signature, "rebase: aborting"); + "rebase: aborting"); if (error < 0) goto done; @@ -970,7 +971,7 @@ int git_rebase_abort(git_rebase *rebase, const git_signature *signature) if ((error = git_commit_lookup( &orig_head_commit, rebase->repo, &rebase->orig_head_id)) < 0 || (error = git_reset(rebase->repo, (git_object *)orig_head_commit, - GIT_RESET_HARD, NULL, signature, NULL)) < 0) + GIT_RESET_HARD, NULL)) < 0) goto done; error = rebase_cleanup(rebase); @@ -1115,10 +1116,10 @@ int git_rebase_finish( terminal_ref, GIT_OBJ_COMMIT)) < 0 || (error = git_reference_create_matching(&branch_ref, rebase->repo, rebase->orig_head_name, git_commit_id(terminal_commit), 1, - &rebase->orig_head_id, signature, branch_msg.ptr)) < 0 || + &rebase->orig_head_id, branch_msg.ptr)) < 0 || (error = git_reference_symbolic_create(&head_ref, rebase->repo, GIT_HEAD_FILE, rebase->orig_head_name, 1, - signature, head_msg.ptr)) < 0 || + head_msg.ptr)) < 0 || (error = rebase_copy_notes(rebase, signature, &opts)) < 0) goto done; diff --git a/src/refs.c b/src/refs.c index e3157529b..3f6c33dcd 100644 --- a/src/refs.c +++ b/src/refs.c @@ -416,12 +416,22 @@ static int reference__create( return 0; } +int configured_ident(git_signature **out, const git_repository *repo) +{ + if (repo->ident_name && repo->ident_email) + return git_signature_now(out, repo->ident_name, repo->ident_email); + + /* if not configured let us fall-through to the next method */ + return -1; +} + int git_reference__log_signature(git_signature **out, git_repository *repo) { int error; git_signature *who; - if(((error = git_signature_default(&who, repo)) < 0) && + if(((error = configured_ident(&who, repo)) < 0) && + ((error = git_signature_default(&who, repo)) < 0) && ((error = git_signature_now(&who, "unknown", "unknown")) < 0)) return error; @@ -436,7 +446,6 @@ int git_reference_create_matching( const git_oid *id, int force, const git_oid *old_id, - const git_signature *signature, const char *log_message) { @@ -445,15 +454,11 @@ int git_reference_create_matching( assert(id); - if (!signature) { - if ((error = git_reference__log_signature(&who, repo)) < 0) - return error; - else - signature = who; - } + if ((error = git_reference__log_signature(&who, repo)) < 0) + return error; error = reference__create( - ref_out, repo, name, id, NULL, force, signature, log_message, old_id, NULL); + ref_out, repo, name, id, NULL, force, who, log_message, old_id, NULL); git_signature_free(who); return error; @@ -465,10 +470,9 @@ int git_reference_create( const char *name, const git_oid *id, int force, - const git_signature *signature, const char *log_message) { - return git_reference_create_matching(ref_out, repo, name, id, force, NULL, signature, log_message); + return git_reference_create_matching(ref_out, repo, name, id, force, NULL, log_message); } int git_reference_symbolic_create_matching( @@ -478,7 +482,6 @@ int git_reference_symbolic_create_matching( const char *target, int force, const char *old_target, - const git_signature *signature, const char *log_message) { int error; @@ -486,15 +489,11 @@ int git_reference_symbolic_create_matching( assert(target); - if (!signature) { - if ((error = git_reference__log_signature(&who, repo)) < 0) - return error; - else - signature = who; - } + if ((error = git_reference__log_signature(&who, repo)) < 0) + return error; error = reference__create( - ref_out, repo, name, NULL, target, force, signature, log_message, NULL, old_target); + ref_out, repo, name, NULL, target, force, who, log_message, NULL, old_target); git_signature_free(who); return error; @@ -506,10 +505,9 @@ int git_reference_symbolic_create( const char *name, const char *target, int force, - const git_signature *signature, const char *log_message) { - return git_reference_symbolic_create_matching(ref_out, repo, name, target, force, NULL, signature, log_message); + return git_reference_symbolic_create_matching(ref_out, repo, name, target, force, NULL, log_message); } static int ensure_is_an_updatable_direct_reference(git_reference *ref) @@ -525,7 +523,6 @@ int git_reference_set_target( git_reference **out, git_reference *ref, const git_oid *id, - const git_signature *signature, const char *log_message) { int error; @@ -538,7 +535,7 @@ int git_reference_set_target( if ((error = ensure_is_an_updatable_direct_reference(ref)) < 0) return error; - return git_reference_create_matching(out, repo, ref->name, id, 1, &ref->target.oid, signature, log_message); + return git_reference_create_matching(out, repo, ref->name, id, 1, &ref->target.oid, log_message); } static int ensure_is_an_updatable_symbolic_reference(git_reference *ref) @@ -554,7 +551,6 @@ int git_reference_symbolic_set_target( git_reference **out, git_reference *ref, const char *target, - const git_signature *signature, const char *log_message) { int error; @@ -565,7 +561,7 @@ int git_reference_symbolic_set_target( return error; return git_reference_symbolic_create_matching( - out, ref->db->repo, ref->name, target, 1, ref->target.symbolic, signature, log_message); + out, ref->db->repo, ref->name, target, 1, ref->target.symbolic, log_message); } static int reference__rename(git_reference **out, git_reference *ref, const char *new_name, int force, @@ -593,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, signature, 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; } @@ -607,23 +603,16 @@ int git_reference_rename( git_reference *ref, const char *new_name, int force, - const git_signature *signature, const char *log_message) { - git_signature *who = (git_signature*)signature; + git_signature *who; int error; - /* Should we return an error if there is no default? */ - if (!who && - ((error = git_signature_default(&who, ref->db->repo)) < 0) && - ((error = git_signature_now(&who, "unknown", "unknown")) < 0)) { + if ((error = git_reference__log_signature(&who, ref->db->repo)) < 0) return error; - } error = reference__rename(out, ref, new_name, force, who, log_message); - - if (!signature) - git_signature_free(who); + git_signature_free(who); return error; } @@ -1038,13 +1027,11 @@ int git_reference_cmp( return git_oid__cmp(&ref1->target.oid, &ref2->target.oid); } -static int reference__update_terminal( - git_repository *repo, - const char *ref_name, - const git_oid *oid, - int nesting, - const git_signature *signature, - const char *log_message) +/** + * Get the end of a chain of references. If the final one is not + * found, we return the reference just before that. + */ +static int get_terminal(git_reference **out, git_repository *repo, const char *ref_name, int nesting) { git_reference *ref; int error = 0; @@ -1054,27 +1041,23 @@ static int reference__update_terminal( return GIT_ENOTFOUND; } - error = git_reference_lookup(&ref, repo, ref_name); - - /* If we haven't found the reference at all, create a new reference. */ - if (error == GIT_ENOTFOUND) { - giterr_clear(); - return git_reference_create(NULL, repo, ref_name, oid, 0, signature, log_message); - } - - if (error < 0) + /* set to NULL to let the caller know that they're at the end of the chain */ + if ((error = git_reference_lookup(&ref, repo, ref_name)) < 0) { + *out = NULL; return error; + } - /* If the ref is a symbolic reference, follow its target. */ - if (git_reference_type(ref) == GIT_REF_SYMBOLIC) { - error = reference__update_terminal(repo, git_reference_symbolic_target(ref), oid, - nesting+1, signature, log_message); - git_reference_free(ref); + if (git_reference_type(ref) == GIT_REF_OID) { + *out = ref; + error = 0; } else { - /* If we're not moving the target, don't recreate the ref */ - if (0 != git_oid_cmp(git_reference_target(ref), oid)) - error = git_reference_create(NULL, repo, ref_name, oid, 1, signature, log_message); - git_reference_free(ref); + error = get_terminal(out, repo, git_reference_symbolic_target(ref), nesting + 1); + if (error == GIT_ENOTFOUND) { + if (!*out) /* set by the error case in lookup above */ + *out = ref; + } else { + git_reference_free(ref); + } } return error; @@ -1089,10 +1072,38 @@ int git_reference__update_terminal( git_repository *repo, const char *ref_name, const git_oid *oid, - const git_signature *signature, + const git_signature *sig, const char *log_message) { - return reference__update_terminal(repo, ref_name, oid, 0, signature, log_message); + git_reference *ref = NULL; + git_signature *who = NULL; + const git_signature *to_use; + int error = 0; + + if (!sig && (error = git_reference__log_signature(&who, repo)) < 0) + return error; + + to_use = sig ? sig : who; + error = get_terminal(&ref, repo, ref_name, 0); + + /* found a dangling symref */ + if (error == GIT_ENOTFOUND && ref) { + assert(git_reference_type(ref) == GIT_REF_SYMBOLIC); + giterr_clear(); + error = reference__create(&ref, repo, ref->target.symbolic, oid, NULL, 0, to_use, + log_message, NULL, NULL); + } else if (error == GIT_ENOTFOUND) { + giterr_clear(); + error = reference__create(&ref, repo, ref_name, oid, NULL, 0, to_use, + log_message, NULL, NULL); + } else if (error == 0) { + assert(git_reference_type(ref) == GIT_REF_OID); + error = reference__create(&ref, repo, ref->name, oid, NULL, 1, to_use, + log_message, &ref->target.oid, NULL); + } + + git_signature_free(who); + return error; } int git_reference__update_for_commit( @@ -1100,12 +1111,12 @@ int git_reference__update_for_commit( git_reference *ref, const char *ref_name, const git_oid *id, - const git_signature *committer, const char *operation) { git_reference *ref_new = NULL; git_commit *commit = NULL; git_buf reflog_msg = GIT_BUF_INIT; + const git_signature *who; int error; if ((error = git_commit_lookup(&commit, repo, id)) < 0 || @@ -1115,12 +1126,18 @@ int git_reference__update_for_commit( git_commit_summary(commit))) < 0) goto done; - if (ref) - error = git_reference_set_target( - &ref_new, ref, id, committer, git_buf_cstr(&reflog_msg)); + who = git_commit_committer(commit); + + if (ref) { + if ((error = ensure_is_an_updatable_direct_reference(ref)) < 0) + return error; + + error = reference__create(&ref_new, repo, ref->name, id, NULL, 1, who, + git_buf_cstr(&reflog_msg), &ref->target.oid, NULL); + } else error = git_reference__update_terminal( - repo, ref_name, id, committer, git_buf_cstr(&reflog_msg)); + repo, ref_name, id, who, git_buf_cstr(&reflog_msg)); done: git_reference_free(ref_new); @@ -1267,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)) @@ -1283,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 5f48efc41..f78ea06b0 100644 --- a/src/refs.h +++ b/src/refs.h @@ -69,11 +69,12 @@ struct git_reference { git_reference *git_reference__set_name(git_reference *ref, const char *name); int git_reference__normalize_name(git_buf *buf, const char *name, unsigned int flags); -int git_reference__update_terminal(git_repository *repo, const char *ref_name, const git_oid *oid, const git_signature *signature, const char *log_message); +int git_reference__update_terminal(git_repository *repo, const char *ref_name, const git_oid *oid, const git_signature *sig, const char *log_message); 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. @@ -106,7 +107,6 @@ int git_reference__update_for_commit( git_reference *ref, const char *ref_name, const git_oid *id, - const git_signature *committer, const char *operation); #endif diff --git a/src/remote.c b/src/remote.c index d96274f1d..bc6d8a2c4 100644 --- a/src/remote.c +++ b/src/remote.c @@ -924,7 +924,6 @@ on_error: int git_remote_fetch( git_remote *remote, const git_strarray *refspecs, - const git_signature *signature, const char *reflog_message) { int error; @@ -952,7 +951,7 @@ int git_remote_fetch( } /* Create "remote/foo" branches for all remote branches */ - error = git_remote_update_tips(remote, signature, git_buf_cstr(&reflog_msg_buf)); + error = git_remote_update_tips(remote, git_buf_cstr(&reflog_msg_buf)); git_buf_free(&reflog_msg_buf); if (error < 0) return error; @@ -1257,7 +1256,6 @@ static int update_tips_for_spec( git_remote *remote, git_refspec *spec, git_vector *refs, - const git_signature *signature, const char *log_message) { int error = 0, autotag; @@ -1332,7 +1330,7 @@ static int update_tips_for_spec( /* In autotag mode, don't overwrite any locally-existing tags */ error = git_reference_create(&ref, remote->repo, refname.ptr, &head->oid, !autotag, - signature, log_message); + log_message); if (error < 0 && error != GIT_EEXISTS) goto on_error; @@ -1418,7 +1416,7 @@ static int next_head(const git_remote *remote, git_vector *refs, return GIT_ITEROVER; } -static int opportunistic_updates(const git_remote *remote, git_vector *refs, const git_signature *sig, const char *msg) +static int opportunistic_updates(const git_remote *remote, git_vector *refs, const char *msg) { size_t i, j, k; git_refspec *spec; @@ -1441,7 +1439,7 @@ static int opportunistic_updates(const git_remote *remote, git_vector *refs, con if ((error = git_refspec_transform(&refname, spec, head->name)) < 0) return error; - error = git_reference_create(&ref, remote->repo, refname.ptr, &head->oid, true, sig, msg); + error = git_reference_create(&ref, remote->repo, refname.ptr, &head->oid, true, msg); git_buf_free(&refname); git_reference_free(ref); @@ -1454,7 +1452,6 @@ static int opportunistic_updates(const git_remote *remote, git_vector *refs, con int git_remote_update_tips( git_remote *remote, - const git_signature *signature, const char *reflog_message) { git_refspec *spec, tagspec; @@ -1464,7 +1461,7 @@ int git_remote_update_tips( /* push has its own logic hidden away in the push object */ if (remote->push) { - return git_push_update_tips(remote->push, signature, reflog_message); + return git_push_update_tips(remote->push); } if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0) @@ -1475,7 +1472,7 @@ int git_remote_update_tips( goto out; if (remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL) { - if ((error = update_tips_for_spec(remote, &tagspec, &refs, signature, reflog_message)) < 0) + if ((error = update_tips_for_spec(remote, &tagspec, &refs, reflog_message)) < 0) goto out; } @@ -1483,13 +1480,13 @@ int git_remote_update_tips( if (spec->push) continue; - if ((error = update_tips_for_spec(remote, spec, &refs, signature, reflog_message)) < 0) + if ((error = update_tips_for_spec(remote, spec, &refs, reflog_message)) < 0) goto out; } /* only try to do opportunisitic updates if the refpec lists differ */ if (remote->passed_refspecs) - error = opportunistic_updates(remote, &refs, signature, reflog_message); + error = opportunistic_updates(remote, &refs, reflog_message); out: git_vector_free(&refs); @@ -1755,7 +1752,7 @@ static int rename_one_remote_reference( goto cleanup; if ((error = git_reference_rename(&ref, reference_in, git_buf_cstr(&new_name), 1, - NULL, git_buf_cstr(&log_message))) < 0) + git_buf_cstr(&log_message))) < 0) goto cleanup; if (git_reference_type(ref) != GIT_REF_SYMBOLIC) @@ -1775,7 +1772,7 @@ static int rename_one_remote_reference( goto cleanup; error = git_reference_symbolic_set_target(&dummy, ref, git_buf_cstr(&new_name), - NULL, git_buf_cstr(&log_message)); + git_buf_cstr(&log_message)); git_reference_free(dummy); @@ -2373,8 +2370,7 @@ cleanup: return error; } -int git_remote_push(git_remote *remote, const git_strarray *refspecs, const git_push_options *opts, - const git_signature *signature, const char *reflog_message) +int git_remote_push(git_remote *remote, const git_strarray *refspecs, const git_push_options *opts) { int error; @@ -2386,7 +2382,7 @@ int git_remote_push(git_remote *remote, const git_strarray *refspecs, const git_ if ((error = git_remote_upload(remote, refspecs, opts)) < 0) return error; - error = git_remote_update_tips(remote, signature, reflog_message); + error = git_remote_update_tips(remote, NULL); git_remote_disconnect(remote); return error; diff --git a/src/repository.c b/src/repository.c index 23c99b0f0..778cdefd7 100644 --- a/src/repository.c +++ b/src/repository.c @@ -124,6 +124,8 @@ void git_repository_free(git_repository *repo) git__free(repo->workdir); git__free(repo->namespace); git__free(repo->name_8dot3); + git__free(repo->ident_name); + git__free(repo->ident_email); git__memzero(repo, sizeof(*repo)); git__free(repo); @@ -1887,40 +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 git_signature *signature, - 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, signature, log_message); + git_reference_name(ref), true, git_buf_cstr(&log_message)); } else { - error = git_repository_set_head_detached(repo, git_reference_target(ref), - signature, 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, signature, log_message); + true, git_buf_cstr(&log_message)); } +cleanup: + git_reference_free(current); git_reference_free(ref); git_reference_free(new_head); return error; @@ -1928,57 +1952,66 @@ int git_repository_set_head( int git_repository_set_head_detached( git_repository* repo, - const git_oid* commitish, - const git_signature *signature, - 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, signature, 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 git_signature *signature, - 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, signature, 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; } @@ -2096,3 +2129,34 @@ int git_repository_init_init_options( GIT_REPOSITORY_INIT_OPTIONS_INIT); return 0; } + +int git_repository_ident(const char **name, const char **email, const git_repository *repo) +{ + *name = repo->ident_name; + *email = repo->ident_email; + + return 0; +} + +int git_repository_set_ident(git_repository *repo, const char *name, const char *email) +{ + char *tmp_name = NULL, *tmp_email = NULL; + + if (name) { + tmp_name = git__strdup(name); + GITERR_CHECK_ALLOC(tmp_name); + } + + if (email) { + tmp_email = git__strdup(email); + GITERR_CHECK_ALLOC(tmp_email); + } + + tmp_name = git__swap(repo->ident_name, tmp_name); + tmp_email = git__swap(repo->ident_email, tmp_email); + + git__free(tmp_name); + git__free(tmp_email); + + return 0; +} diff --git a/src/repository.h b/src/repository.h index dffa9a8ae..56d443d3c 100644 --- a/src/repository.h +++ b/src/repository.h @@ -128,6 +128,9 @@ struct git_repository { char *namespace; char *name_8dot3; + char *ident_name; + char *ident_email; + unsigned is_bare:1, has_8dot3:1, has_8dot3_default:1; diff --git a/src/reset.c b/src/reset.c index dc3aa4a66..f42a0b3ae 100644 --- a/src/reset.c +++ b/src/reset.c @@ -100,16 +100,14 @@ int git_reset( git_repository *repo, git_object *target, git_reset_t reset_type, - git_checkout_options *checkout_opts, - const git_signature *signature, - const char *log_message) + git_checkout_options *checkout_opts) { git_object *commit = NULL; git_index *index = NULL; git_tree *tree = NULL; int error = 0; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; - git_buf log_message_buf = GIT_BUF_INIT; + git_buf log_message = GIT_BUF_INIT; assert(repo && target); @@ -141,14 +139,12 @@ int git_reset( goto cleanup; } - if (log_message) - git_buf_sets(&log_message_buf, log_message); - else - git_buf_sets(&log_message_buf, "reset: moving"); + if ((error = git_buf_printf(&log_message, "reset: moving to %s", git_oid_tostr_s(git_object_id(commit)))) < 0) + return error; /* move HEAD to the new target */ if ((error = git_reference__update_terminal(repo, GIT_HEAD_FILE, - git_object_id(commit), signature, git_buf_cstr(&log_message_buf))) < 0) + git_object_id(commit), NULL, git_buf_cstr(&log_message))) < 0) goto cleanup; if (reset_type == GIT_RESET_HARD) { @@ -176,7 +172,7 @@ cleanup: git_object_free(commit); git_index_free(index); git_tree_free(tree); - git_buf_free(&log_message_buf); + git_buf_free(&log_message); return error; } diff --git a/src/stash.c b/src/stash.c index cad87d120..1cd64a6fe 100644 --- a/src/stash.c +++ b/src/stash.c @@ -411,7 +411,6 @@ cleanup: static int update_reflog( git_oid *w_commit_oid, git_repository *repo, - const git_signature *stasher, const char *message) { git_reference *stash; @@ -420,7 +419,7 @@ static int update_reflog( if ((error = git_reference_ensure_log(repo, GIT_REFS_STASH_FILE)) < 0) return error; - error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, w_commit_oid, 1, stasher, message); + error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, w_commit_oid, 1, message); git_reference_free(stash); @@ -534,7 +533,7 @@ int git_stash_save( git_buf_rtrim(&msg); - if ((error = update_reflog(out, repo, stasher, git_buf_cstr(&msg))) < 0) + if ((error = update_reflog(out, repo, git_buf_cstr(&msg))) < 0) goto cleanup; if ((error = reset_index_and_workdir( diff --git a/src/submodule.c b/src/submodule.c index 80f1b3789..567ab748e 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -927,7 +927,6 @@ int git_submodule_update(git_submodule *sm, int init, git_submodule_update_optio /* Copy over the remote callbacks */ clone_options.remote_callbacks = update_options.remote_callbacks; - clone_options.signature = update_options.signature; /* Get the status of the submodule to determine if it is already initialized */ if ((error = git_submodule_status(&submodule_status, sm)) < 0) @@ -985,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), update_options.signature, 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 { @@ -997,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), update_options.signature, 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. */ @@ -273,7 +273,7 @@ static int git_tag_create__internal( } else git_oid_cpy(oid, git_object_id(target)); - error = git_reference_create(&new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite, NULL, NULL); + error = git_reference_create(&new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite, NULL); cleanup: git_reference_free(new_ref); @@ -380,7 +380,7 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu } error = git_reference_create( - &new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite, NULL, NULL); + &new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite, NULL); git_reference_free(new_ref); git_buf_free(&ref_name); diff --git a/src/transports/local.c b/src/transports/local.c index 89f2651cd..bedd2390b 100644 --- a/src/transports/local.c +++ b/src/transports/local.c @@ -348,7 +348,7 @@ static int local_push_update_remote_ref( if (lref[0] != '\0') { /* Create or update a ref */ error = git_reference_create(NULL, remote_repo, rref, loid, - !git_oid_iszero(roid), NULL, NULL); + !git_oid_iszero(roid), NULL); } else { /* Delete a ref */ if ((error = git_reference_lookup(&remote_ref, remote_repo, rref)) < 0) { |
