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>
This commit is contained in:
Michael Haggerty 2016-04-25 11:39:54 +02:00
parent 3a8af7be8f
commit 71564516de
2 changed files with 40 additions and 22 deletions

48
refs.c
View File

@ -766,13 +766,33 @@ void ref_transaction_free(struct ref_transaction *transaction)
free(transaction); free(transaction);
} }
static struct ref_update *add_update(struct ref_transaction *transaction, struct ref_update *ref_transaction_add_update(
const char *refname) struct ref_transaction *transaction,
const char *refname, unsigned int flags,
const unsigned char *new_sha1,
const unsigned char *old_sha1,
const char *msg)
{ {
struct ref_update *update; struct ref_update *update;
if (transaction->state != REF_TRANSACTION_OPEN)
die("BUG: update called for transaction that is not open");
if ((flags & REF_ISPRUNING) && !(flags & REF_NODEREF))
die("BUG: REF_ISPRUNING set without REF_NODEREF");
FLEX_ALLOC_STR(update, refname, refname); FLEX_ALLOC_STR(update, refname, refname);
ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc); ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
transaction->updates[transaction->nr++] = update; transaction->updates[transaction->nr++] = update;
update->flags = flags;
if (flags & REF_HAVE_NEW)
hashcpy(update->new_sha1, new_sha1);
if (flags & REF_HAVE_OLD)
hashcpy(update->old_sha1, old_sha1);
if (msg)
update->msg = xstrdup(msg);
return update; return update;
} }
@ -783,16 +803,8 @@ int ref_transaction_update(struct ref_transaction *transaction,
unsigned int flags, const char *msg, unsigned int flags, const char *msg,
struct strbuf *err) struct strbuf *err)
{ {
struct ref_update *update;
assert(err); assert(err);
if (transaction->state != REF_TRANSACTION_OPEN)
die("BUG: update called for transaction that is not open");
if ((flags & REF_ISPRUNING) && !(flags & REF_NODEREF))
die("BUG: REF_ISPRUNING set without REF_NODEREF");
if (new_sha1 && !is_null_sha1(new_sha1) && if (new_sha1 && !is_null_sha1(new_sha1) &&
check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
strbuf_addf(err, "refusing to update ref with bad name '%s'", strbuf_addf(err, "refusing to update ref with bad name '%s'",
@ -800,18 +812,10 @@ int ref_transaction_update(struct ref_transaction *transaction,
return -1; return -1;
} }
update = add_update(transaction, refname); flags |= (new_sha1 ? REF_HAVE_NEW : 0) | (old_sha1 ? REF_HAVE_OLD : 0);
if (new_sha1) {
hashcpy(update->new_sha1, new_sha1); ref_transaction_add_update(transaction, refname, flags,
flags |= REF_HAVE_NEW; new_sha1, old_sha1, msg);
}
if (old_sha1) {
hashcpy(update->old_sha1, old_sha1);
flags |= REF_HAVE_OLD;
}
update->flags = flags;
if (msg)
update->msg = xstrdup(msg);
return 0; return 0;
} }

View File

@ -157,6 +157,20 @@ struct ref_update {
const char refname[FLEX_ARRAY]; const char refname[FLEX_ARRAY];
}; };
/*
* Add a ref_update with the specified properties to transaction, and
* return a pointer to the new object. This function does not verify
* that refname is well-formed. new_sha1 and old_sha1 are only
* dereferenced if the REF_HAVE_NEW and REF_HAVE_OLD bits,
* respectively, are set in flags.
*/
struct ref_update *ref_transaction_add_update(
struct ref_transaction *transaction,
const char *refname, unsigned int flags,
const unsigned char *new_sha1,
const unsigned char *old_sha1,
const char *msg);
/* /*
* Transaction states. * Transaction states.
* OPEN: The transaction is in a valid state and can accept new updates. * OPEN: The transaction is in a valid state and can accept new updates.