refs: update ref transactions to use struct object_id

Update the ref transaction code to use struct object_id.  Remove one
NULL pointer check which was previously inserted around a dereference;
since we now pass a pointer to struct object_id directly through, the
code we're calling handles this for us.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
brian m. carlson 2017-10-15 22:06:53 +00:00 committed by Junio C Hamano
parent 6ee18216d8
commit 89f3bbdd3b
15 changed files with 69 additions and 71 deletions

View File

@ -305,7 +305,7 @@ void create_branch(const char *name, const char *start_name,
transaction = ref_transaction_begin(&err); transaction = ref_transaction_begin(&err);
if (!transaction || if (!transaction ||
ref_transaction_update(transaction, ref.buf, ref_transaction_update(transaction, ref.buf,
oid.hash, forcing ? NULL : null_sha1, &oid, forcing ? NULL : &null_oid,
0, msg, &err) || 0, msg, &err) ||
ref_transaction_commit(transaction, &err)) ref_transaction_commit(transaction, &err))
die("%s", err.buf); die("%s", err.buf);

View File

@ -588,7 +588,7 @@ static void write_remote_refs(const struct ref *local_refs)
for (r = local_refs; r; r = r->next) { for (r = local_refs; r; r = r->next) {
if (!r->peer_ref) if (!r->peer_ref)
continue; continue;
if (ref_transaction_create(t, r->peer_ref->name, r->old_oid.hash, if (ref_transaction_create(t, r->peer_ref->name, &r->old_oid,
0, NULL, &err)) 0, NULL, &err))
die("%s", err.buf); die("%s", err.buf);
} }

View File

@ -1788,9 +1788,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
transaction = ref_transaction_begin(&err); transaction = ref_transaction_begin(&err);
if (!transaction || if (!transaction ||
ref_transaction_update(transaction, "HEAD", oid.hash, ref_transaction_update(transaction, "HEAD", &oid,
current_head current_head
? current_head->object.oid.hash : null_sha1, ? &current_head->object.oid : &null_oid,
0, sb.buf, &err) || 0, sb.buf, &err) ||
ref_transaction_commit(transaction, &err)) { ref_transaction_commit(transaction, &err)) {
rollback_index_files(); rollback_index_files();

View File

@ -457,8 +457,8 @@ static int s_update_ref(const char *action,
transaction = ref_transaction_begin(&err); transaction = ref_transaction_begin(&err);
if (!transaction || if (!transaction ||
ref_transaction_update(transaction, ref->name, ref_transaction_update(transaction, ref->name,
ref->new_oid.hash, &ref->new_oid,
check_old ? ref->old_oid.hash : NULL, check_old ? &ref->old_oid : NULL,
0, msg, &err)) 0, msg, &err))
goto fail; goto fail;

View File

@ -1139,7 +1139,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
} }
if (ref_transaction_delete(transaction, if (ref_transaction_delete(transaction,
namespaced_name, namespaced_name,
old_oid ? old_oid->hash : NULL, old_oid,
0, "push", &err)) { 0, "push", &err)) {
rp_error("%s", err.buf); rp_error("%s", err.buf);
strbuf_release(&err); strbuf_release(&err);
@ -1156,7 +1156,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
if (ref_transaction_update(transaction, if (ref_transaction_update(transaction,
namespaced_name, namespaced_name,
new_oid->hash, old_oid->hash, new_oid, old_oid,
0, "push", 0, "push",
&err)) { &err)) {
rp_error("%s", err.buf); rp_error("%s", err.buf);

View File

@ -175,7 +175,7 @@ static int replace_object_oid(const char *object_ref,
transaction = ref_transaction_begin(&err); transaction = ref_transaction_begin(&err);
if (!transaction || if (!transaction ||
ref_transaction_update(transaction, ref.buf, repl->hash, prev.hash, ref_transaction_update(transaction, ref.buf, repl, &prev,
0, NULL, &err) || 0, NULL, &err) ||
ref_transaction_commit(transaction, &err)) ref_transaction_commit(transaction, &err))
die("%s", err.buf); die("%s", err.buf);

View File

@ -544,7 +544,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
transaction = ref_transaction_begin(&err); transaction = ref_transaction_begin(&err);
if (!transaction || if (!transaction ||
ref_transaction_update(transaction, ref.buf, object.hash, prev.hash, ref_transaction_update(transaction, ref.buf, &object, &prev,
create_reflog ? REF_FORCE_CREATE_REFLOG : 0, create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
reflog_msg.buf, &err) || reflog_msg.buf, &err) ||
ref_transaction_commit(transaction, &err)) ref_transaction_commit(transaction, &err))

View File

@ -200,7 +200,7 @@ static const char *parse_cmd_update(struct ref_transaction *transaction,
die("update %s: extra input: %s", refname, next); die("update %s: extra input: %s", refname, next);
if (ref_transaction_update(transaction, refname, if (ref_transaction_update(transaction, refname,
new_oid.hash, have_old ? old_oid.hash : NULL, &new_oid, have_old ? &old_oid : NULL,
update_flags | create_reflog_flag, update_flags | create_reflog_flag,
msg, &err)) msg, &err))
die("%s", err.buf); die("%s", err.buf);
@ -232,7 +232,7 @@ static const char *parse_cmd_create(struct ref_transaction *transaction,
if (*next != line_termination) if (*next != line_termination)
die("create %s: extra input: %s", refname, next); die("create %s: extra input: %s", refname, next);
if (ref_transaction_create(transaction, refname, new_oid.hash, if (ref_transaction_create(transaction, refname, &new_oid,
update_flags | create_reflog_flag, update_flags | create_reflog_flag,
msg, &err)) msg, &err))
die("%s", err.buf); die("%s", err.buf);
@ -269,7 +269,7 @@ static const char *parse_cmd_delete(struct ref_transaction *transaction,
die("delete %s: extra input: %s", refname, next); die("delete %s: extra input: %s", refname, next);
if (ref_transaction_delete(transaction, refname, if (ref_transaction_delete(transaction, refname,
have_old ? old_oid.hash : NULL, have_old ? &old_oid : NULL,
update_flags, msg, &err)) update_flags, msg, &err))
die("%s", err.buf); die("%s", err.buf);
@ -298,7 +298,7 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
if (*next != line_termination) if (*next != line_termination)
die("verify %s: extra input: %s", refname, next); die("verify %s: extra input: %s", refname, next);
if (ref_transaction_verify(transaction, refname, old_oid.hash, if (ref_transaction_verify(transaction, refname, &old_oid,
update_flags, &err)) update_flags, &err))
die("%s", err.buf); die("%s", err.buf);

View File

@ -1778,7 +1778,7 @@ static int update_branch(struct branch *b)
} }
transaction = ref_transaction_begin(&err); transaction = ref_transaction_begin(&err);
if (!transaction || if (!transaction ||
ref_transaction_update(transaction, b->name, b->oid.hash, old_oid.hash, ref_transaction_update(transaction, b->name, &b->oid, &old_oid,
0, msg, &err) || 0, msg, &err) ||
ref_transaction_commit(transaction, &err)) { ref_transaction_commit(transaction, &err)) {
ref_transaction_free(transaction); ref_transaction_free(transaction);
@ -1820,7 +1820,7 @@ static void dump_tags(void)
strbuf_addf(&ref_name, "refs/tags/%s", t->name); strbuf_addf(&ref_name, "refs/tags/%s", t->name);
if (ref_transaction_update(transaction, ref_name.buf, if (ref_transaction_update(transaction, ref_name.buf,
t->oid.hash, NULL, 0, msg, &err)) { &t->oid, NULL, 0, msg, &err)) {
failure |= error("%s", err.buf); failure |= error("%s", err.buf);
goto cleanup; goto cleanup;
} }

50
refs.c
View File

@ -671,8 +671,7 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,
transaction = ref_store_transaction_begin(refs, &err); transaction = ref_store_transaction_begin(refs, &err);
if (!transaction || if (!transaction ||
ref_transaction_delete(transaction, refname, ref_transaction_delete(transaction, refname, old_oid,
old_oid ? old_oid->hash : NULL,
flags, msg, &err) || flags, msg, &err) ||
ref_transaction_commit(transaction, &err)) { ref_transaction_commit(transaction, &err)) {
error("%s", err.buf); error("%s", err.buf);
@ -898,8 +897,8 @@ void ref_transaction_free(struct ref_transaction *transaction)
struct ref_update *ref_transaction_add_update( struct ref_update *ref_transaction_add_update(
struct ref_transaction *transaction, struct ref_transaction *transaction,
const char *refname, unsigned int flags, const char *refname, unsigned int flags,
const unsigned char *new_sha1, const struct object_id *new_oid,
const unsigned char *old_sha1, const struct object_id *old_oid,
const char *msg) const char *msg)
{ {
struct ref_update *update; struct ref_update *update;
@ -917,23 +916,23 @@ struct ref_update *ref_transaction_add_update(
update->flags = flags; update->flags = flags;
if (flags & REF_HAVE_NEW) if (flags & REF_HAVE_NEW)
hashcpy(update->new_oid.hash, new_sha1); oidcpy(&update->new_oid, new_oid);
if (flags & REF_HAVE_OLD) if (flags & REF_HAVE_OLD)
hashcpy(update->old_oid.hash, old_sha1); oidcpy(&update->old_oid, old_oid);
update->msg = xstrdup_or_null(msg); update->msg = xstrdup_or_null(msg);
return update; return update;
} }
int ref_transaction_update(struct ref_transaction *transaction, int ref_transaction_update(struct ref_transaction *transaction,
const char *refname, const char *refname,
const unsigned char *new_sha1, const struct object_id *new_oid,
const unsigned char *old_sha1, const struct object_id *old_oid,
unsigned int flags, const char *msg, unsigned int flags, const char *msg,
struct strbuf *err) struct strbuf *err)
{ {
assert(err); assert(err);
if ((new_sha1 && !is_null_sha1(new_sha1)) ? if ((new_oid && !is_null_oid(new_oid)) ?
check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) : check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
!refname_is_safe(refname)) { !refname_is_safe(refname)) {
strbuf_addf(err, "refusing to update ref with bad name '%s'", strbuf_addf(err, "refusing to update ref with bad name '%s'",
@ -943,48 +942,48 @@ int ref_transaction_update(struct ref_transaction *transaction,
flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS; flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS;
flags |= (new_sha1 ? REF_HAVE_NEW : 0) | (old_sha1 ? REF_HAVE_OLD : 0); flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0);
ref_transaction_add_update(transaction, refname, flags, ref_transaction_add_update(transaction, refname, flags,
new_sha1, old_sha1, msg); new_oid, old_oid, msg);
return 0; return 0;
} }
int ref_transaction_create(struct ref_transaction *transaction, int ref_transaction_create(struct ref_transaction *transaction,
const char *refname, const char *refname,
const unsigned char *new_sha1, const struct object_id *new_oid,
unsigned int flags, const char *msg, unsigned int flags, const char *msg,
struct strbuf *err) struct strbuf *err)
{ {
if (!new_sha1 || is_null_sha1(new_sha1)) if (!new_oid || is_null_oid(new_oid))
die("BUG: create called without valid new_sha1"); die("BUG: create called without valid new_oid");
return ref_transaction_update(transaction, refname, new_sha1, return ref_transaction_update(transaction, refname, new_oid,
null_sha1, flags, msg, err); &null_oid, flags, msg, err);
} }
int ref_transaction_delete(struct ref_transaction *transaction, int ref_transaction_delete(struct ref_transaction *transaction,
const char *refname, const char *refname,
const unsigned char *old_sha1, const struct object_id *old_oid,
unsigned int flags, const char *msg, unsigned int flags, const char *msg,
struct strbuf *err) struct strbuf *err)
{ {
if (old_sha1 && is_null_sha1(old_sha1)) if (old_oid && is_null_oid(old_oid))
die("BUG: delete called with old_sha1 set to zeros"); die("BUG: delete called with old_oid set to zeros");
return ref_transaction_update(transaction, refname, return ref_transaction_update(transaction, refname,
null_sha1, old_sha1, &null_oid, old_oid,
flags, msg, err); flags, msg, err);
} }
int ref_transaction_verify(struct ref_transaction *transaction, int ref_transaction_verify(struct ref_transaction *transaction,
const char *refname, const char *refname,
const unsigned char *old_sha1, const struct object_id *old_oid,
unsigned int flags, unsigned int flags,
struct strbuf *err) struct strbuf *err)
{ {
if (!old_sha1) if (!old_oid)
die("BUG: verify called with old_sha1 set to NULL"); die("BUG: verify called with old_oid set to NULL");
return ref_transaction_update(transaction, refname, return ref_transaction_update(transaction, refname,
NULL, old_sha1, NULL, old_oid,
flags, NULL, err); flags, NULL, err);
} }
@ -1003,8 +1002,7 @@ int refs_update_ref(struct ref_store *refs, const char *msg,
} else { } else {
t = ref_store_transaction_begin(refs, &err); t = ref_store_transaction_begin(refs, &err);
if (!t || if (!t ||
ref_transaction_update(t, refname, new_oid ? new_oid->hash : NULL, ref_transaction_update(t, refname, new_oid, old_oid,
old_oid ? old_oid->hash : NULL,
flags, msg, &err) || flags, msg, &err) ||
ref_transaction_commit(t, &err)) { ref_transaction_commit(t, &err)) {
ret = 1; ret = 1;

38
refs.h
View File

@ -511,14 +511,14 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
*/ */
/* /*
* Add a reference update to transaction. new_sha1 is the value that * Add a reference update to transaction. new_oid is the value that
* the reference should have after the update, or null_sha1 if it * the reference should have after the update, or null_oid if it
* should be deleted. If new_sha1 is NULL, then the reference is not * should be deleted. If new_oid is NULL, then the reference is not
* changed at all. old_sha1 is the value that the reference must have * changed at all. old_oid is the value that the reference must have
* before the update, or null_sha1 if it must not have existed * before the update, or null_oid if it must not have existed
* beforehand. The old value is checked after the lock is taken to * beforehand. The old value is checked after the lock is taken to
* prevent races. If the old value doesn't agree with old_sha1, the * prevent races. If the old value doesn't agree with old_oid, the
* whole transaction fails. If old_sha1 is NULL, then the previous * whole transaction fails. If old_oid is NULL, then the previous
* value is not checked. * value is not checked.
* *
* See the above comment "Reference transaction updates" for more * See the above comment "Reference transaction updates" for more
@ -526,15 +526,15 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
*/ */
int ref_transaction_update(struct ref_transaction *transaction, int ref_transaction_update(struct ref_transaction *transaction,
const char *refname, const char *refname,
const unsigned char *new_sha1, const struct object_id *new_oid,
const unsigned char *old_sha1, const struct object_id *old_oid,
unsigned int flags, const char *msg, unsigned int flags, const char *msg,
struct strbuf *err); struct strbuf *err);
/* /*
* Add a reference creation to transaction. new_sha1 is the value that * Add a reference creation to transaction. new_oid is the value that
* the reference should have after the update; it must not be * the reference should have after the update; it must not be
* null_sha1. It is verified that the reference does not exist * null_oid. It is verified that the reference does not exist
* already. * already.
* *
* See the above comment "Reference transaction updates" for more * See the above comment "Reference transaction updates" for more
@ -542,35 +542,35 @@ int ref_transaction_update(struct ref_transaction *transaction,
*/ */
int ref_transaction_create(struct ref_transaction *transaction, int ref_transaction_create(struct ref_transaction *transaction,
const char *refname, const char *refname,
const unsigned char *new_sha1, const struct object_id *new_oid,
unsigned int flags, const char *msg, unsigned int flags, const char *msg,
struct strbuf *err); struct strbuf *err);
/* /*
* Add a reference deletion to transaction. If old_sha1 is non-NULL, * Add a reference deletion to transaction. If old_oid is non-NULL,
* then it holds the value that the reference should have had before * then it holds the value that the reference should have had before
* the update (which must not be null_sha1). * the update (which must not be null_oid).
* *
* See the above comment "Reference transaction updates" for more * See the above comment "Reference transaction updates" for more
* information. * information.
*/ */
int ref_transaction_delete(struct ref_transaction *transaction, int ref_transaction_delete(struct ref_transaction *transaction,
const char *refname, const char *refname,
const unsigned char *old_sha1, const struct object_id *old_oid,
unsigned int flags, const char *msg, unsigned int flags, const char *msg,
struct strbuf *err); struct strbuf *err);
/* /*
* Verify, within a transaction, that refname has the value old_sha1, * Verify, within a transaction, that refname has the value old_oid,
* or, if old_sha1 is null_sha1, then verify that the reference * or, if old_oid is null_oid, then verify that the reference
* doesn't exist. old_sha1 must be non-NULL. * doesn't exist. old_oid must be non-NULL.
* *
* See the above comment "Reference transaction updates" for more * See the above comment "Reference transaction updates" for more
* information. * information.
*/ */
int ref_transaction_verify(struct ref_transaction *transaction, int ref_transaction_verify(struct ref_transaction *transaction,
const char *refname, const char *refname,
const unsigned char *old_sha1, const struct object_id *old_oid,
unsigned int flags, unsigned int flags,
struct strbuf *err); struct strbuf *err);

View File

@ -994,7 +994,7 @@ static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
transaction = ref_store_transaction_begin(&refs->base, &err); transaction = ref_store_transaction_begin(&refs->base, &err);
if (!transaction || if (!transaction ||
ref_transaction_delete(transaction, r->name, r->oid.hash, ref_transaction_delete(transaction, r->name, &r->oid,
REF_ISPRUNING | REF_NODEREF, NULL, &err) || REF_ISPRUNING | REF_NODEREF, NULL, &err) ||
ref_transaction_commit(transaction, &err)) { ref_transaction_commit(transaction, &err)) {
ref_transaction_free(transaction); ref_transaction_free(transaction);
@ -1079,7 +1079,7 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
* packed-refs transaction: * packed-refs transaction:
*/ */
if (ref_transaction_update(transaction, iter->refname, if (ref_transaction_update(transaction, iter->refname,
iter->oid->hash, NULL, iter->oid, NULL,
REF_NODEREF, NULL, &err)) REF_NODEREF, NULL, &err))
die("failure preparing to create packed reference %s: %s", die("failure preparing to create packed reference %s: %s",
iter->refname, err.buf); iter->refname, err.buf);
@ -2148,7 +2148,7 @@ static int split_head_update(struct ref_update *update,
new_update = ref_transaction_add_update( new_update = ref_transaction_add_update(
transaction, "HEAD", transaction, "HEAD",
update->flags | REF_LOG_ONLY | REF_NODEREF, update->flags | REF_LOG_ONLY | REF_NODEREF,
update->new_oid.hash, update->old_oid.hash, &update->new_oid, &update->old_oid,
update->msg); update->msg);
/* /*
@ -2212,7 +2212,7 @@ static int split_symref_update(struct files_ref_store *refs,
new_update = ref_transaction_add_update( new_update = ref_transaction_add_update(
transaction, referent, new_flags, transaction, referent, new_flags,
update->new_oid.hash, update->old_oid.hash, &update->new_oid, &update->old_oid,
update->msg); update->msg);
new_update->parent_update = update; new_update->parent_update = update;
@ -2594,7 +2594,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
ref_transaction_add_update( ref_transaction_add_update(
packed_transaction, update->refname, packed_transaction, update->refname,
update->flags & ~REF_HAVE_OLD, update->flags & ~REF_HAVE_OLD,
update->new_oid.hash, update->old_oid.hash, &update->new_oid, &update->old_oid,
NULL); NULL);
} }
} }
@ -2847,7 +2847,7 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
*/ */
ref_transaction_add_update(packed_transaction, update->refname, ref_transaction_add_update(packed_transaction, update->refname,
update->flags & ~REF_HAVE_OLD, update->flags & ~REF_HAVE_OLD,
update->new_oid.hash, update->old_oid.hash, &update->new_oid, &update->old_oid,
NULL); NULL);
} }

View File

@ -202,8 +202,8 @@ int ref_update_reject_duplicates(struct string_list *refnames,
struct ref_update *ref_transaction_add_update( struct ref_update *ref_transaction_add_update(
struct ref_transaction *transaction, struct ref_transaction *transaction,
const char *refname, unsigned int flags, const char *refname, unsigned int flags,
const unsigned char *new_sha1, const struct object_id *new_oid,
const unsigned char *old_sha1, const struct object_id *old_oid,
const char *msg); const char *msg);
/* /*

View File

@ -393,7 +393,7 @@ static int fast_forward_to(const struct object_id *to, const struct object_id *f
transaction = ref_transaction_begin(&err); transaction = ref_transaction_begin(&err);
if (!transaction || if (!transaction ||
ref_transaction_update(transaction, "HEAD", ref_transaction_update(transaction, "HEAD",
to->hash, unborn ? null_sha1 : from->hash, to, unborn ? &null_oid : from,
0, sb.buf, &err) || 0, sb.buf, &err) ||
ref_transaction_commit(transaction, &err)) { ref_transaction_commit(transaction, &err)) {
ref_transaction_free(transaction); ref_transaction_free(transaction);

View File

@ -304,7 +304,7 @@ int walker_fetch(struct walker *walker, int targets, char **target,
strbuf_reset(&refname); strbuf_reset(&refname);
strbuf_addf(&refname, "refs/%s", write_ref[i]); strbuf_addf(&refname, "refs/%s", write_ref[i]);
if (ref_transaction_update(transaction, refname.buf, if (ref_transaction_update(transaction, refname.buf,
oids[i].hash, NULL, 0, oids + i, NULL, 0,
msg ? msg : "fetch (unknown)", msg ? msg : "fetch (unknown)",
&err)) { &err)) {
error("%s", err.buf); error("%s", err.buf);