ref_transaction_update(): remove "have_old" parameter

Instead, verify the reference's old value if and only if old_sha1 is non-NULL. ref_transaction_delete() will get the same treatment in a moment. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Reviewed-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Feb 17, 2015 at 18:00 UTC 1d147bdff0b8132d3aa53a46df8dbab7b673b796
12 files changed +31 -29
branch.c
+3 -2
@@ -284,8 +284,9 @@ void create_branch(const char *head,
284
285 transaction = ref_transaction_begin(&err);
286 if (!transaction ||
287 - ref_transaction_update(transaction, ref.buf, sha1,
288 - null_sha1, 0, !forcing, msg, &err) ||
287 + ref_transaction_update(transaction, ref.buf,
288 + sha1, forcing ? NULL : null_sha1,
289 + 0, msg, &err) ||
290 ref_transaction_commit(transaction, &err))
291 die("%s", err.buf);
292 ref_transaction_free(transaction);
builtin/commit.c
+1 -1
@@ -1767,7 +1767,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1767 ref_transaction_update(transaction, "HEAD", sha1,
1768 current_head
1769 ? current_head->object.sha1 : NULL,
1770 - 0, !!current_head, sb.buf, &err) ||
1770 + 0, sb.buf, &err) ||
1771 ref_transaction_commit(transaction, &err)) {
1772 rollback_index_files();
1773 die("%s", err.buf);
builtin/fetch.c
+4 -2
@@ -416,8 +416,10 @@ static int s_update_ref(const char *action,
416
417 transaction = ref_transaction_begin(&err);
418 if (!transaction ||
419 - ref_transaction_update(transaction, ref->name, ref->new_sha1,
420 - ref->old_sha1, 0, check_old, msg, &err))
419 + ref_transaction_update(transaction, ref->name,
420 + ref->new_sha1,
421 + check_old ? ref->old_sha1 : NULL,
422 + 0, msg, &err))
423 goto fail;
424
425 ret = ref_transaction_commit(transaction, &err);
builtin/receive-pack.c
+1 -1
@@ -971,7 +971,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
971 if (ref_transaction_update(transaction,
972 namespaced_name,
973 new_sha1, old_sha1,
974 - 0, 1, "push",
974 + 0, "push",
975 &err)) {
976 rp_error("%s", err.buf);
977 strbuf_release(&err);
builtin/replace.c
+1 -1
@@ -172,7 +172,7 @@ static int replace_object_sha1(const char *object_ref,
172 transaction = ref_transaction_begin(&err);
173 if (!transaction ||
174 ref_transaction_update(transaction, ref, repl, prev,
175 - 0, 1, NULL, &err) ||
175 + 0, NULL, &err) ||
176 ref_transaction_commit(transaction, &err))
177 die("%s", err.buf);
178
builtin/tag.c
+1 -1
@@ -733,7 +733,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
733 transaction = ref_transaction_begin(&err);
734 if (!transaction ||
735 ref_transaction_update(transaction, ref.buf, object, prev,
736 - 0, 1, NULL, &err) ||
736 + 0, NULL, &err) ||
737 ref_transaction_commit(transaction, &err))
738 die("%s", err.buf);
739 ref_transaction_free(transaction);
builtin/update-ref.c
+4 -3
@@ -198,8 +198,9 @@ static const char *parse_cmd_update(struct ref_transaction *transaction,
198 if (*next != line_termination)
199 die("update %s: extra input: %s", refname, next);
200
201 - if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,
202 - update_flags, have_old, msg, &err))
201 + if (ref_transaction_update(transaction, refname,
202 + new_sha1, have_old ? old_sha1 : NULL,
203 + update_flags, msg, &err))
204 die("%s", err.buf);
205
206 update_flags = 0;
@@ -297,7 +298,7 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
298 die("verify %s: extra input: %s", refname, next);
299
300 if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,
300 - update_flags, 1, msg, &err))
301 + update_flags, msg, &err))
302 die("%s", err.buf);
303
304 update_flags = 0;
fast-import.c
+3 -3
@@ -1716,7 +1716,7 @@ static int update_branch(struct branch *b)
1716 transaction = ref_transaction_begin(&err);
1717 if (!transaction ||
1718 ref_transaction_update(transaction, b->name, b->sha1, old_sha1,
1719 - 0, 1, msg, &err) ||
1719 + 0, msg, &err) ||
1720 ref_transaction_commit(transaction, &err)) {
1721 ref_transaction_free(transaction);
1722 error("%s", err.buf);
@@ -1756,8 +1756,8 @@ static void dump_tags(void)
1756 strbuf_reset(&ref_name);
1757 strbuf_addf(&ref_name, "refs/tags/%s", t->name);
1758
1759 - if (ref_transaction_update(transaction, ref_name.buf, t->sha1,
1760 - NULL, 0, 0, msg, &err)) {
1759 + if (ref_transaction_update(transaction, ref_name.buf,
1760 + t->sha1, NULL, 0, msg, &err)) {
1761 failure |= error("%s", err.buf);
1762 goto cleanup;
1763 }
refs.c
+8 -10
@@ -3654,7 +3654,7 @@ int ref_transaction_update(struct ref_transaction *transaction,
3654 const char *refname,
3655 const unsigned char *new_sha1,
3656 const unsigned char *old_sha1,
3657 - unsigned int flags, int have_old, const char *msg,
3657 + unsigned int flags, const char *msg,
3658 struct strbuf *err)
3659 {
3660 struct ref_update *update;
@@ -3664,9 +3664,6 @@ int ref_transaction_update(struct ref_transaction *transaction,
3664 if (transaction->state != REF_TRANSACTION_OPEN)
3665 die("BUG: update called for transaction that is not open");
3666
3667 - if (have_old && !old_sha1)
3668 - die("BUG: have_old is true but old_sha1 is NULL");
3669 -
3667 if (!is_null_sha1(new_sha1) &&
3668 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
3669 strbuf_addf(err, "refusing to update ref with bad name %s",
@@ -3676,7 +3673,7 @@ int ref_transaction_update(struct ref_transaction *transaction,
3673
3674 update = add_update(transaction, refname);
3675 hashcpy(update->new_sha1, new_sha1);
3679 - if (have_old) {
3676 + if (old_sha1) {
3677 hashcpy(update->old_sha1, old_sha1);
3678 flags |= REF_HAVE_OLD;
3679 }
@@ -3693,7 +3690,7 @@ int ref_transaction_create(struct ref_transaction *transaction,
3690 struct strbuf *err)
3691 {
3692 return ref_transaction_update(transaction, refname, new_sha1,
3696 - null_sha1, flags, 1, msg, err);
3693 + null_sha1, flags, msg, err);
3694 }
3695
3696 int ref_transaction_delete(struct ref_transaction *transaction,
@@ -3702,8 +3699,9 @@ int ref_transaction_delete(struct ref_transaction *transaction,
3699 unsigned int flags, int have_old, const char *msg,
3700 struct strbuf *err)
3701 {
3705 - return ref_transaction_update(transaction, refname, null_sha1,
3706 - old_sha1, flags, have_old, msg, err);
3702 + return ref_transaction_update(transaction, refname,
3703 + null_sha1, have_old ? old_sha1 : NULL,
3704 + flags, msg, err);
3705 }
3706
3707 int update_ref(const char *action, const char *refname,
@@ -3715,8 +3713,8 @@ int update_ref(const char *action, const char *refname,
3713
3714 t = ref_transaction_begin(&err);
3715 if (!t ||
3718 - ref_transaction_update(t, refname, sha1, oldval, flags,
3719 - !!oldval, action, &err) ||
3716 + ref_transaction_update(t, refname, sha1, oldval,
3717 + flags, action, &err) ||
3718 ref_transaction_commit(t, &err)) {
3719 const char *str = "update_ref failed for ref '%s': %s";
3720
refs.h
+3 -3
@@ -265,8 +265,8 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
265 /*
266 * Add a reference update to transaction. new_sha1 is the value that
267 * the reference should have after the update, or null_sha1 if it should
268 - * be deleted. If have_old is true, then old_sha1 holds the value
269 - * that the reference should have had before the update, or zeros if
268 + * be deleted. If old_sha1 is non-NULL, then it is the value
269 + * that the reference should have had before the update, or null_sha1 if
270 * it must not have existed beforehand.
271 * Function returns 0 on success and non-zero on failure. A failure to update
272 * means that the transaction as a whole has failed and will need to be
@@ -276,7 +276,7 @@ int ref_transaction_update(struct ref_transaction *transaction,
276 const char *refname,
277 const unsigned char *new_sha1,
278 const unsigned char *old_sha1,
279 - unsigned int flags, int have_old, const char *msg,
279 + unsigned int flags, const char *msg,
280 struct strbuf *err);
281
282 /*
sequencer.c
+1 -1
@@ -252,7 +252,7 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from,
252 if (!transaction ||
253 ref_transaction_update(transaction, "HEAD",
254 to, unborn ? null_sha1 : from,
255 - 0, 1, sb.buf, &err) ||
255 + 0, sb.buf, &err) ||
256 ref_transaction_commit(transaction, &err)) {
257 ref_transaction_free(transaction);
258 error("%s", err.buf);
walker.c
+1 -1
@@ -299,7 +299,7 @@ int walker_fetch(struct walker *walker, int targets, char **target,
299 strbuf_reset(&refname);
300 strbuf_addf(&refname, "refs/%s", write_ref[i]);
301 if (ref_transaction_update(transaction, refname.buf,
302 - &sha1[20 * i], NULL, 0, 0,
302 + &sha1[20 * i], NULL, 0,
303 msg ? msg : "fetch (unknown)",
304 &err)) {
305 error("%s", err.buf);