delete_ref: accept a reflog message argument

When the current branch is renamed with 'git branch -m/-M' or deleted with 'git update-ref -m<msg> -d', the event is recorded in HEAD's log with an empty message. In preparation for adding a more meaningful message to HEAD's log in these cases, update delete_ref() to take a message argument and pass it along to ref_transaction_delete(). Modify all callers to pass NULL for the new message argument; no change in behavior is intended. Note that this is relevant for HEAD's log but not for the deleted ref's log, which is currently deleted along with the ref. Even if it were not, an entry for the deletion wouldn't be present in the deleted ref's log. files_transaction_commit() writes to the log if REF_NEEDS_COMMIT or REF_LOG_ONLY are set, but lock_ref_for_update() doesn't set REF_NEEDS_COMMIT for the deleted ref because REF_DELETING is set. In contrast, the update for HEAD has REF_LOG_ONLY set by split_head_update(), resulting in the deletion being logged. Signed-off-by: Kyle Meyer <kyle@kyleam.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kyle Meyer committed Feb 20, 2017 at 20:10 UTC 755b49ae965c9d17d91fbe7902050428f366bf69
14 files changed +22 -22
builtin/am.c
+2 -2
@@ -1049,7 +1049,7 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
1049 } else {
1050 write_state_text(state, "abort-safety", "");
1051 if (!state->rebasing)
1052 - delete_ref("ORIG_HEAD", NULL, 0);
1052 + delete_ref(NULL, "ORIG_HEAD", NULL, 0);
1053 }
1054
1055 /*
@@ -2172,7 +2172,7 @@ static void am_abort(struct am_state *state)
2172 has_curr_head ? &curr_head : NULL, 0,
2173 UPDATE_REFS_DIE_ON_ERR);
2174 else if (curr_branch)
2175 - delete_ref(curr_branch, NULL, REF_NODEREF);
2175 + delete_ref(NULL, curr_branch, NULL, REF_NODEREF);
2176
2177 free(curr_branch);
2178 am_destroy(state);
builtin/branch.c
+1 -1
@@ -251,7 +251,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
251 goto next;
252 }
253
254 - if (delete_ref(name, is_null_sha1(sha1) ? NULL : sha1,
254 + if (delete_ref(NULL, name, is_null_sha1(sha1) ? NULL : sha1,
255 REF_NODEREF)) {
256 error(remote_branch
257 ? _("Error deleting remote-tracking branch '%s'")
builtin/notes.c
+2 -2
@@ -681,9 +681,9 @@ static int merge_abort(struct notes_merge_options *o)
681 * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE.
682 */
683
684 - if (delete_ref("NOTES_MERGE_PARTIAL", NULL, 0))
684 + if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0))
685 ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL"));
686 - if (delete_ref("NOTES_MERGE_REF", NULL, REF_NODEREF))
686 + if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NODEREF))
687 ret += error(_("failed to delete ref NOTES_MERGE_REF"));
688 if (notes_merge_abort(o))
689 ret += error(_("failed to remove 'git notes merge' worktree"));
builtin/remote.c
+2 -2
@@ -691,7 +691,7 @@ static int mv(int argc, const char **argv)
691 read_ref_full(item->string, RESOLVE_REF_READING, oid.hash, &flag);
692 if (!(flag & REF_ISSYMREF))
693 continue;
694 - if (delete_ref(item->string, NULL, REF_NODEREF))
694 + if (delete_ref(NULL, item->string, NULL, REF_NODEREF))
695 die(_("deleting '%s' failed"), item->string);
696 }
697 for (i = 0; i < remote_branches.nr; i++) {
@@ -1248,7 +1248,7 @@ static int set_head(int argc, const char **argv)
1248 head_name = xstrdup(states.heads.items[0].string);
1249 free_remote_ref_states(&states);
1250 } else if (opt_d && !opt_a && argc == 1) {
1251 - if (delete_ref(buf.buf, NULL, REF_NODEREF))
1251 + if (delete_ref(NULL, buf.buf, NULL, REF_NODEREF))
1252 result |= error(_("Could not delete %s"), buf.buf);
1253 } else
1254 usage_with_options(builtin_remote_sethead_usage, options);
builtin/replace.c
+1 -1
@@ -121,7 +121,7 @@ static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
121 static int delete_replace_ref(const char *name, const char *ref,
122 const unsigned char *sha1)
123 {
124 - if (delete_ref(ref, sha1, 0))
124 + if (delete_ref(NULL, ref, sha1, 0))
125 return 1;
126 printf("Deleted replace ref '%s'\n", name);
127 return 0;
builtin/reset.c
+1 -1
@@ -256,7 +256,7 @@ static int reset_refs(const char *rev, const struct object_id *oid)
256 update_ref_oid(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
257 UPDATE_REFS_MSG_ON_ERR);
258 } else if (old_orig)
259 - delete_ref("ORIG_HEAD", old_orig->hash, 0);
259 + delete_ref(NULL, "ORIG_HEAD", old_orig->hash, 0);
260 set_reflog_message(&msg, "updating HEAD", rev);
261 update_ref_status = update_ref_oid(msg.buf, "HEAD", oid, orig, 0,
262 UPDATE_REFS_MSG_ON_ERR);
builtin/symbolic-ref.c
+1 -1
@@ -58,7 +58,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
58 die("Cannot delete %s, not a symbolic ref", argv[0]);
59 if (!strcmp(argv[0], "HEAD"))
60 die("deleting '%s' is not allowed", argv[0]);
61 - return delete_ref(argv[0], NULL, REF_NODEREF);
61 + return delete_ref(NULL, argv[0], NULL, REF_NODEREF);
62 }
63
64 switch (argc) {
builtin/tag.c
+1 -1
@@ -97,7 +97,7 @@ static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
97 static int delete_tag(const char *name, const char *ref,
98 const unsigned char *sha1, const void *cb_data)
99 {
100 - if (delete_ref(ref, sha1, 0))
100 + if (delete_ref(NULL, ref, sha1, 0))
101 return 1;
102 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
103 return 0;
builtin/update-ref.c
+1 -1
@@ -433,7 +433,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
433 * For purposes of backwards compatibility, we treat
434 * NULL_SHA1 as "don't care" here:
435 */
436 - return delete_ref(refname,
436 + return delete_ref(NULL, refname,
437 (oldval && !is_null_sha1(oldsha1)) ? oldsha1 : NULL,
438 flags);
439 else
fast-import.c
+1 -1
@@ -1752,7 +1752,7 @@ static int update_branch(struct branch *b)
1752
1753 if (is_null_sha1(b->sha1)) {
1754 if (b->delete)
1755 - delete_ref(b->name, NULL, 0);
1755 + delete_ref(NULL, b->name, NULL, 0);
1756 return 0;
1757 }
1758 if (read_ref(b->name, old_sha1))
refs.c
+3 -3
@@ -591,8 +591,8 @@ static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1
591 return 0;
592 }
593
594 -int delete_ref(const char *refname, const unsigned char *old_sha1,
595 - unsigned int flags)
594 +int delete_ref(const char *msg, const char *refname,
595 + const unsigned char *old_sha1, unsigned int flags)
596 {
597 struct ref_transaction *transaction;
598 struct strbuf err = STRBUF_INIT;
@@ -603,7 +603,7 @@ int delete_ref(const char *refname, const unsigned char *old_sha1,
603 transaction = ref_transaction_begin(&err);
604 if (!transaction ||
605 ref_transaction_delete(transaction, refname, old_sha1,
606 - flags, NULL, &err) ||
606 + flags, msg, &err) ||
607 ref_transaction_commit(transaction, &err)) {
608 error("%s", err.buf);
609 ref_transaction_free(transaction);
refs.h
+2 -2
@@ -276,8 +276,8 @@ int reflog_exists(const char *refname);
276 * exists, regardless of its old value. It is an error for old_sha1 to
277 * be NULL_SHA1. flags is passed through to ref_transaction_delete().
278 */
279 -int delete_ref(const char *refname, const unsigned char *old_sha1,
280 - unsigned int flags);
279 +int delete_ref(const char *msg, const char *refname,
280 + const unsigned char *old_sha1, unsigned int flags);
281
282 /*
283 * Delete the specified references. If there are any problems, emit
refs/files-backend.c
+3 -3
@@ -2489,7 +2489,7 @@ static int files_delete_refs(struct ref_store *ref_store,
2489 for (i = 0; i < refnames->nr; i++) {
2490 const char *refname = refnames->items[i].string;
2491
2492 - if (delete_ref(refname, NULL, flags))
2492 + if (delete_ref(NULL, refname, NULL, flags))
2493 result |= error(_("could not remove reference %s"), refname);
2494 }
2495
@@ -2616,7 +2616,7 @@ static int files_rename_ref(struct ref_store *ref_store,
2616 return error("unable to move logfile logs/%s to "TMP_RENAMED_LOG": %s",
2617 oldrefname, strerror(errno));
2618
2619 - if (delete_ref(oldrefname, orig_sha1, REF_NODEREF)) {
2619 + if (delete_ref(NULL, oldrefname, orig_sha1, REF_NODEREF)) {
2620 error("unable to delete old %s", oldrefname);
2621 goto rollback;
2622 }
@@ -2630,7 +2630,7 @@ static int files_rename_ref(struct ref_store *ref_store,
2630 */
2631 if (!read_ref_full(newrefname, RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
2632 sha1, NULL) &&
2633 - delete_ref(newrefname, NULL, REF_NODEREF)) {
2633 + delete_ref(NULL, newrefname, NULL, REF_NODEREF)) {
2634 if (errno==EISDIR) {
2635 struct strbuf path = STRBUF_INIT;
2636 int result;
transport.c
+1 -1
@@ -299,7 +299,7 @@ void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int v
299 if (verbose)
300 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
301 if (ref->deletion) {
302 - delete_ref(rs.dst, NULL, 0);
302 + delete_ref(NULL, rs.dst, NULL, 0);
303 } else
304 update_ref("update by push", rs.dst,
305 ref->new_oid.hash, NULL, 0, 0);