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>
brian m. carlson committed
Oct 15, 2017 at 22:06 UTC
89f3bbdd3b1f46a5747aa5618b7742f7b3f2adef
15 files changed
+69
-71
branch.c
+1
-1
@@ -305,7 +305,7 @@ void create_branch(const char *name, const char *start_name,
305
transaction = ref_transaction_begin(&err);
306
if (!transaction ||
307
ref_transaction_update(transaction, ref.buf,
308
- oid.hash, forcing ? NULL : null_sha1,
308
+ &oid, forcing ? NULL : &null_oid,
309
0, msg, &err) ||
310
ref_transaction_commit(transaction, &err))
311
die("%s", err.buf);
builtin/clone.c
+1
-1
@@ -588,7 +588,7 @@ static void write_remote_refs(const struct ref *local_refs)
588
for (r = local_refs; r; r = r->next) {
589
if (!r->peer_ref)
590
continue;
591
- if (ref_transaction_create(t, r->peer_ref->name, r->old_oid.hash,
591
+ if (ref_transaction_create(t, r->peer_ref->name, &r->old_oid,
592
0, NULL, &err))
593
die("%s", err.buf);
594
}
builtin/commit.c
+2
-2
@@ -1788,9 +1788,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1788
1789
transaction = ref_transaction_begin(&err);
1790
if (!transaction ||
1791
- ref_transaction_update(transaction, "HEAD", oid.hash,
1791
+ ref_transaction_update(transaction, "HEAD", &oid,
1792
current_head
1793
- ? current_head->object.oid.hash : null_sha1,
1793
+ ? ¤t_head->object.oid : &null_oid,
1794
0, sb.buf, &err) ||
1795
ref_transaction_commit(transaction, &err)) {
1796
rollback_index_files();
builtin/fetch.c
+2
-2
@@ -457,8 +457,8 @@ static int s_update_ref(const char *action,
457
transaction = ref_transaction_begin(&err);
458
if (!transaction ||
459
ref_transaction_update(transaction, ref->name,
460
- ref->new_oid.hash,
461
- check_old ? ref->old_oid.hash : NULL,
460
+ &ref->new_oid,
461
+ check_old ? &ref->old_oid : NULL,
462
0, msg, &err))
463
goto fail;
464
builtin/receive-pack.c
+2
-2
@@ -1139,7 +1139,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1139
}
1140
if (ref_transaction_delete(transaction,
1141
namespaced_name,
1142
- old_oid ? old_oid->hash : NULL,
1142
+ old_oid,
1143
0, "push", &err)) {
1144
rp_error("%s", err.buf);
1145
strbuf_release(&err);
@@ -1156,7 +1156,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1156
1157
if (ref_transaction_update(transaction,
1158
namespaced_name,
1159
- new_oid->hash, old_oid->hash,
1159
+ new_oid, old_oid,
1160
0, "push",
1161
&err)) {
1162
rp_error("%s", err.buf);
builtin/replace.c
+1
-1
@@ -175,7 +175,7 @@ static int replace_object_oid(const char *object_ref,
175
176
transaction = ref_transaction_begin(&err);
177
if (!transaction ||
178
- ref_transaction_update(transaction, ref.buf, repl->hash, prev.hash,
178
+ ref_transaction_update(transaction, ref.buf, repl, &prev,
179
0, NULL, &err) ||
180
ref_transaction_commit(transaction, &err))
181
die("%s", err.buf);
builtin/tag.c
+1
-1
@@ -544,7 +544,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
544
545
transaction = ref_transaction_begin(&err);
546
if (!transaction ||
547
- ref_transaction_update(transaction, ref.buf, object.hash, prev.hash,
547
+ ref_transaction_update(transaction, ref.buf, &object, &prev,
548
create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
549
reflog_msg.buf, &err) ||
550
ref_transaction_commit(transaction, &err))
builtin/update-ref.c
+4
-4
@@ -200,7 +200,7 @@ static const char *parse_cmd_update(struct ref_transaction *transaction,
200
die("update %s: extra input: %s", refname, next);
201
202
if (ref_transaction_update(transaction, refname,
203
- new_oid.hash, have_old ? old_oid.hash : NULL,
203
+ &new_oid, have_old ? &old_oid : NULL,
204
update_flags | create_reflog_flag,
205
msg, &err))
206
die("%s", err.buf);
@@ -232,7 +232,7 @@ static const char *parse_cmd_create(struct ref_transaction *transaction,
232
if (*next != line_termination)
233
die("create %s: extra input: %s", refname, next);
234
235
- if (ref_transaction_create(transaction, refname, new_oid.hash,
235
+ if (ref_transaction_create(transaction, refname, &new_oid,
236
update_flags | create_reflog_flag,
237
msg, &err))
238
die("%s", err.buf);
@@ -269,7 +269,7 @@ static const char *parse_cmd_delete(struct ref_transaction *transaction,
269
die("delete %s: extra input: %s", refname, next);
270
271
if (ref_transaction_delete(transaction, refname,
272
- have_old ? old_oid.hash : NULL,
272
+ have_old ? &old_oid : NULL,
273
update_flags, msg, &err))
274
die("%s", err.buf);
275
@@ -298,7 +298,7 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
298
if (*next != line_termination)
299
die("verify %s: extra input: %s", refname, next);
300
301
- if (ref_transaction_verify(transaction, refname, old_oid.hash,
301
+ if (ref_transaction_verify(transaction, refname, &old_oid,
302
update_flags, &err))
303
die("%s", err.buf);
304
fast-import.c
+2
-2
@@ -1778,7 +1778,7 @@ static int update_branch(struct branch *b)
1778
}
1779
transaction = ref_transaction_begin(&err);
1780
if (!transaction ||
1781
- ref_transaction_update(transaction, b->name, b->oid.hash, old_oid.hash,
1781
+ ref_transaction_update(transaction, b->name, &b->oid, &old_oid,
1782
0, msg, &err) ||
1783
ref_transaction_commit(transaction, &err)) {
1784
ref_transaction_free(transaction);
@@ -1820,7 +1820,7 @@ static void dump_tags(void)
1820
strbuf_addf(&ref_name, "refs/tags/%s", t->name);
1821
1822
if (ref_transaction_update(transaction, ref_name.buf,
1823
- t->oid.hash, NULL, 0, msg, &err)) {
1823
+ &t->oid, NULL, 0, msg, &err)) {
1824
failure |= error("%s", err.buf);
1825
goto cleanup;
1826
}
refs.c
+24
-26
@@ -671,8 +671,7 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,
671
672
transaction = ref_store_transaction_begin(refs, &err);
673
if (!transaction ||
674
- ref_transaction_delete(transaction, refname,
675
- old_oid ? old_oid->hash : NULL,
674
+ ref_transaction_delete(transaction, refname, old_oid,
675
flags, msg, &err) ||
676
ref_transaction_commit(transaction, &err)) {
677
error("%s", err.buf);
@@ -898,8 +897,8 @@ void ref_transaction_free(struct ref_transaction *transaction)
897
struct ref_update *ref_transaction_add_update(
898
struct ref_transaction *transaction,
899
const char *refname, unsigned int flags,
901
- const unsigned char *new_sha1,
902
- const unsigned char *old_sha1,
900
+ const struct object_id *new_oid,
901
+ const struct object_id *old_oid,
902
const char *msg)
903
{
904
struct ref_update *update;
@@ -917,23 +916,23 @@ struct ref_update *ref_transaction_add_update(
916
update->flags = flags;
917
918
if (flags & REF_HAVE_NEW)
920
- hashcpy(update->new_oid.hash, new_sha1);
919
+ oidcpy(&update->new_oid, new_oid);
920
if (flags & REF_HAVE_OLD)
922
- hashcpy(update->old_oid.hash, old_sha1);
921
+ oidcpy(&update->old_oid, old_oid);
922
update->msg = xstrdup_or_null(msg);
923
return update;
924
}
925
926
int ref_transaction_update(struct ref_transaction *transaction,
927
const char *refname,
929
- const unsigned char *new_sha1,
930
- const unsigned char *old_sha1,
928
+ const struct object_id *new_oid,
929
+ const struct object_id *old_oid,
930
unsigned int flags, const char *msg,
931
struct strbuf *err)
932
{
933
assert(err);
934
936
- if ((new_sha1 && !is_null_sha1(new_sha1)) ?
935
+ if ((new_oid && !is_null_oid(new_oid)) ?
936
check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
937
!refname_is_safe(refname)) {
938
strbuf_addf(err, "refusing to update ref with bad name '%s'",
@@ -943,48 +942,48 @@ int ref_transaction_update(struct ref_transaction *transaction,
942
943
flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS;
944
946
- flags |= (new_sha1 ? REF_HAVE_NEW : 0) | (old_sha1 ? REF_HAVE_OLD : 0);
945
+ flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0);
946
947
ref_transaction_add_update(transaction, refname, flags,
949
- new_sha1, old_sha1, msg);
948
+ new_oid, old_oid, msg);
949
return 0;
950
}
951
952
int ref_transaction_create(struct ref_transaction *transaction,
953
const char *refname,
955
- const unsigned char *new_sha1,
954
+ const struct object_id *new_oid,
955
unsigned int flags, const char *msg,
956
struct strbuf *err)
957
{
959
- if (!new_sha1 || is_null_sha1(new_sha1))
960
- die("BUG: create called without valid new_sha1");
961
- return ref_transaction_update(transaction, refname, new_sha1,
962
- null_sha1, flags, msg, err);
958
+ if (!new_oid || is_null_oid(new_oid))
959
+ die("BUG: create called without valid new_oid");
960
+ return ref_transaction_update(transaction, refname, new_oid,
961
+ &null_oid, flags, msg, err);
962
}
963
964
int ref_transaction_delete(struct ref_transaction *transaction,
965
const char *refname,
967
- const unsigned char *old_sha1,
966
+ const struct object_id *old_oid,
967
unsigned int flags, const char *msg,
968
struct strbuf *err)
969
{
971
- if (old_sha1 && is_null_sha1(old_sha1))
972
- die("BUG: delete called with old_sha1 set to zeros");
970
+ if (old_oid && is_null_oid(old_oid))
971
+ die("BUG: delete called with old_oid set to zeros");
972
return ref_transaction_update(transaction, refname,
974
- null_sha1, old_sha1,
973
+ &null_oid, old_oid,
974
flags, msg, err);
975
}
976
977
int ref_transaction_verify(struct ref_transaction *transaction,
978
const char *refname,
980
- const unsigned char *old_sha1,
979
+ const struct object_id *old_oid,
980
unsigned int flags,
981
struct strbuf *err)
982
{
984
- if (!old_sha1)
985
- die("BUG: verify called with old_sha1 set to NULL");
983
+ if (!old_oid)
984
+ die("BUG: verify called with old_oid set to NULL");
985
return ref_transaction_update(transaction, refname,
987
- NULL, old_sha1,
986
+ NULL, old_oid,
987
flags, NULL, err);
988
}
989
@@ -1003,8 +1002,7 @@ int refs_update_ref(struct ref_store *refs, const char *msg,
1002
} else {
1003
t = ref_store_transaction_begin(refs, &err);
1004
if (!t ||
1006
- ref_transaction_update(t, refname, new_oid ? new_oid->hash : NULL,
1007
- old_oid ? old_oid->hash : NULL,
1005
+ ref_transaction_update(t, refname, new_oid, old_oid,
1006
flags, msg, &err) ||
1007
ref_transaction_commit(t, &err)) {
1008
ret = 1;
refs.h
+19
-19
@@ -511,14 +511,14 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
511
*/
512
513
/*
514
- * Add a reference update to transaction. new_sha1 is the value that
515
- * the reference should have after the update, or null_sha1 if it
516
- * should be deleted. If new_sha1 is NULL, then the reference is not
517
- * changed at all. old_sha1 is the value that the reference must have
518
- * before the update, or null_sha1 if it must not have existed
514
+ * Add a reference update to transaction. new_oid is the value that
515
+ * the reference should have after the update, or null_oid if it
516
+ * should be deleted. If new_oid is NULL, then the reference is not
517
+ * changed at all. old_oid is the value that the reference must have
518
+ * before the update, or null_oid if it must not have existed
519
* beforehand. The old value is checked after the lock is taken to
520
- * prevent races. If the old value doesn't agree with old_sha1, the
521
- * whole transaction fails. If old_sha1 is NULL, then the previous
520
+ * prevent races. If the old value doesn't agree with old_oid, the
521
+ * whole transaction fails. If old_oid is NULL, then the previous
522
* value is not checked.
523
*
524
* See the above comment "Reference transaction updates" for more
@@ -526,15 +526,15 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
526
*/
527
int ref_transaction_update(struct ref_transaction *transaction,
528
const char *refname,
529
- const unsigned char *new_sha1,
530
- const unsigned char *old_sha1,
529
+ const struct object_id *new_oid,
530
+ const struct object_id *old_oid,
531
unsigned int flags, const char *msg,
532
struct strbuf *err);
533
534
/*
535
- * Add a reference creation to transaction. new_sha1 is the value that
535
+ * Add a reference creation to transaction. new_oid is the value that
536
* the reference should have after the update; it must not be
537
- * null_sha1. It is verified that the reference does not exist
537
+ * null_oid. It is verified that the reference does not exist
538
* already.
539
*
540
* See the above comment "Reference transaction updates" for more
@@ -542,35 +542,35 @@ int ref_transaction_update(struct ref_transaction *transaction,
542
*/
543
int ref_transaction_create(struct ref_transaction *transaction,
544
const char *refname,
545
- const unsigned char *new_sha1,
545
+ const struct object_id *new_oid,
546
unsigned int flags, const char *msg,
547
struct strbuf *err);
548
549
/*
550
- * Add a reference deletion to transaction. If old_sha1 is non-NULL,
550
+ * Add a reference deletion to transaction. If old_oid is non-NULL,
551
* then it holds the value that the reference should have had before
552
- * the update (which must not be null_sha1).
552
+ * the update (which must not be null_oid).
553
*
554
* See the above comment "Reference transaction updates" for more
555
* information.
556
*/
557
int ref_transaction_delete(struct ref_transaction *transaction,
558
const char *refname,
559
- const unsigned char *old_sha1,
559
+ const struct object_id *old_oid,
560
unsigned int flags, const char *msg,
561
struct strbuf *err);
562
563
/*
564
- * Verify, within a transaction, that refname has the value old_sha1,
565
- * or, if old_sha1 is null_sha1, then verify that the reference
566
- * doesn't exist. old_sha1 must be non-NULL.
564
+ * Verify, within a transaction, that refname has the value old_oid,
565
+ * or, if old_oid is null_oid, then verify that the reference
566
+ * doesn't exist. old_oid must be non-NULL.
567
*
568
* See the above comment "Reference transaction updates" for more
569
* information.
570
*/
571
int ref_transaction_verify(struct ref_transaction *transaction,
572
const char *refname,
573
- const unsigned char *old_sha1,
573
+ const struct object_id *old_oid,
574
unsigned int flags,
575
struct strbuf *err);
576
refs/files-backend.c
+6
-6
@@ -994,7 +994,7 @@ static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
994
995
transaction = ref_store_transaction_begin(&refs->base, &err);
996
if (!transaction ||
997
- ref_transaction_delete(transaction, r->name, r->oid.hash,
997
+ ref_transaction_delete(transaction, r->name, &r->oid,
998
REF_ISPRUNING | REF_NODEREF, NULL, &err) ||
999
ref_transaction_commit(transaction, &err)) {
1000
ref_transaction_free(transaction);
@@ -1079,7 +1079,7 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
1079
* packed-refs transaction:
1080
*/
1081
if (ref_transaction_update(transaction, iter->refname,
1082
- iter->oid->hash, NULL,
1082
+ iter->oid, NULL,
1083
REF_NODEREF, NULL, &err))
1084
die("failure preparing to create packed reference %s: %s",
1085
iter->refname, err.buf);
@@ -2148,7 +2148,7 @@ static int split_head_update(struct ref_update *update,
2148
new_update = ref_transaction_add_update(
2149
transaction, "HEAD",
2150
update->flags | REF_LOG_ONLY | REF_NODEREF,
2151
- update->new_oid.hash, update->old_oid.hash,
2151
+ &update->new_oid, &update->old_oid,
2152
update->msg);
2153
2154
/*
@@ -2212,7 +2212,7 @@ static int split_symref_update(struct files_ref_store *refs,
2212
2213
new_update = ref_transaction_add_update(
2214
transaction, referent, new_flags,
2215
- update->new_oid.hash, update->old_oid.hash,
2215
+ &update->new_oid, &update->old_oid,
2216
update->msg);
2217
2218
new_update->parent_update = update;
@@ -2594,7 +2594,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
2594
ref_transaction_add_update(
2595
packed_transaction, update->refname,
2596
update->flags & ~REF_HAVE_OLD,
2597
- update->new_oid.hash, update->old_oid.hash,
2597
+ &update->new_oid, &update->old_oid,
2598
NULL);
2599
}
2600
}
@@ -2847,7 +2847,7 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
2847
*/
2848
ref_transaction_add_update(packed_transaction, update->refname,
2849
update->flags & ~REF_HAVE_OLD,
2850
- update->new_oid.hash, update->old_oid.hash,
2850
+ &update->new_oid, &update->old_oid,
2851
NULL);
2852
}
2853
refs/refs-internal.h
+2
-2
@@ -202,8 +202,8 @@ int ref_update_reject_duplicates(struct string_list *refnames,
202
struct ref_update *ref_transaction_add_update(
203
struct ref_transaction *transaction,
204
const char *refname, unsigned int flags,
205
- const unsigned char *new_sha1,
206
- const unsigned char *old_sha1,
205
+ const struct object_id *new_oid,
206
+ const struct object_id *old_oid,
207
const char *msg);
208
209
/*
sequencer.c
+1
-1
@@ -393,7 +393,7 @@ static int fast_forward_to(const struct object_id *to, const struct object_id *f
393
transaction = ref_transaction_begin(&err);
394
if (!transaction ||
395
ref_transaction_update(transaction, "HEAD",
396
- to->hash, unborn ? null_sha1 : from->hash,
396
+ to, unborn ? &null_oid : from,
397
0, sb.buf, &err) ||
398
ref_transaction_commit(transaction, &err)) {
399
ref_transaction_free(transaction);
walker.c
+1
-1
@@ -304,7 +304,7 @@ int walker_fetch(struct walker *walker, int targets, char **target,
304
strbuf_reset(&refname);
305
strbuf_addf(&refname, "refs/%s", write_ref[i]);
306
if (ref_transaction_update(transaction, refname.buf,
307
- oids[i].hash, NULL, 0,
307
+ oids + i, NULL, 0,
308
msg ? msg : "fetch (unknown)",
309
&err)) {
310
error("%s", err.buf);