add_update(): initialize the whole ref_update

Change add_update() to initialize all of the fields in the new ref_update object. Rename the function to ref_transaction_add_update(), and increase its visibility to all of the refs-related code. All of this makes the function more useful for other future callers. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>

Michael Haggerty committed Apr 25, 2016 at 11:39 UTC 71564516deccafba0a58129bd7d3851e28fdb4bb
2 files changed +40 -22
refs.c
+26 -22
@@ -766,13 +766,33 @@ void ref_transaction_free(struct ref_transaction *transaction)
766 free(transaction);
767 }
768
769 -static struct ref_update *add_update(struct ref_transaction *transaction,
770 - const char *refname)
769 +struct ref_update *ref_transaction_add_update(
770 + struct ref_transaction *transaction,
771 + const char *refname, unsigned int flags,
772 + const unsigned char *new_sha1,
773 + const unsigned char *old_sha1,
774 + const char *msg)
775 {
776 struct ref_update *update;
777 +
778 + if (transaction->state != REF_TRANSACTION_OPEN)
779 + die("BUG: update called for transaction that is not open");
780 +
781 + if ((flags & REF_ISPRUNING) && !(flags & REF_NODEREF))
782 + die("BUG: REF_ISPRUNING set without REF_NODEREF");
783 +
784 FLEX_ALLOC_STR(update, refname, refname);
785 ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
786 transaction->updates[transaction->nr++] = update;
787 +
788 + update->flags = flags;
789 +
790 + if (flags & REF_HAVE_NEW)
791 + hashcpy(update->new_sha1, new_sha1);
792 + if (flags & REF_HAVE_OLD)
793 + hashcpy(update->old_sha1, old_sha1);
794 + if (msg)
795 + update->msg = xstrdup(msg);
796 return update;
797 }
798
@@ -783,16 +803,8 @@ int ref_transaction_update(struct ref_transaction *transaction,
803 unsigned int flags, const char *msg,
804 struct strbuf *err)
805 {
786 - struct ref_update *update;
787 -
806 assert(err);
807
790 - if (transaction->state != REF_TRANSACTION_OPEN)
791 - die("BUG: update called for transaction that is not open");
792 -
793 - if ((flags & REF_ISPRUNING) && !(flags & REF_NODEREF))
794 - die("BUG: REF_ISPRUNING set without REF_NODEREF");
795 -
808 if (new_sha1 && !is_null_sha1(new_sha1) &&
809 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
810 strbuf_addf(err, "refusing to update ref with bad name '%s'",
@@ -800,18 +812,10 @@ int ref_transaction_update(struct ref_transaction *transaction,
812 return -1;
813 }
814
803 - update = add_update(transaction, refname);
804 - if (new_sha1) {
805 - hashcpy(update->new_sha1, new_sha1);
806 - flags |= REF_HAVE_NEW;
807 - }
808 - if (old_sha1) {
809 - hashcpy(update->old_sha1, old_sha1);
810 - flags |= REF_HAVE_OLD;
811 - }
812 - update->flags = flags;
813 - if (msg)
814 - update->msg = xstrdup(msg);
815 + flags |= (new_sha1 ? REF_HAVE_NEW : 0) | (old_sha1 ? REF_HAVE_OLD : 0);
816 +
817 + ref_transaction_add_update(transaction, refname, flags,
818 + new_sha1, old_sha1, msg);
819 return 0;
820 }
821
refs/refs-internal.h
+14
@@ -157,6 +157,20 @@ struct ref_update {
157 const char refname[FLEX_ARRAY];
158 };
159
160 +/*
161 + * Add a ref_update with the specified properties to transaction, and
162 + * return a pointer to the new object. This function does not verify
163 + * that refname is well-formed. new_sha1 and old_sha1 are only
164 + * dereferenced if the REF_HAVE_NEW and REF_HAVE_OLD bits,
165 + * respectively, are set in flags.
166 + */
167 +struct ref_update *ref_transaction_add_update(
168 + struct ref_transaction *transaction,
169 + const char *refname, unsigned int flags,
170 + const unsigned char *new_sha1,
171 + const unsigned char *old_sha1,
172 + const char *msg);
173 +
174 /*
175 * Transaction states.
176 * OPEN: The transaction is in a valid state and can accept new updates.