Skip to content

Commit 89f3bbd

Browse files
bk2204gitster
authored andcommitted
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>
1 parent 6ee1821 commit 89f3bbd

File tree

15 files changed

+69
-71
lines changed

15 files changed

+69
-71
lines changed

branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ void create_branch(const char *name, const char *start_name,
305305
transaction = ref_transaction_begin(&err);
306306
if (!transaction ||
307307
ref_transaction_update(transaction, ref.buf,
308-
oid.hash, forcing ? NULL : null_sha1,
308+
&oid, forcing ? NULL : &null_oid,
309309
0, msg, &err) ||
310310
ref_transaction_commit(transaction, &err))
311311
die("%s", err.buf);

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ static void write_remote_refs(const struct ref *local_refs)
588588
for (r = local_refs; r; r = r->next) {
589589
if (!r->peer_ref)
590590
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,
592592
0, NULL, &err))
593593
die("%s", err.buf);
594594
}

builtin/commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,9 +1788,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
17881788

17891789
transaction = ref_transaction_begin(&err);
17901790
if (!transaction ||
1791-
ref_transaction_update(transaction, "HEAD", oid.hash,
1791+
ref_transaction_update(transaction, "HEAD", &oid,
17921792
current_head
1793-
? current_head->object.oid.hash : null_sha1,
1793+
? &current_head->object.oid : &null_oid,
17941794
0, sb.buf, &err) ||
17951795
ref_transaction_commit(transaction, &err)) {
17961796
rollback_index_files();

builtin/fetch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@ static int s_update_ref(const char *action,
457457
transaction = ref_transaction_begin(&err);
458458
if (!transaction ||
459459
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,
462462
0, msg, &err))
463463
goto fail;
464464

builtin/receive-pack.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
11391139
}
11401140
if (ref_transaction_delete(transaction,
11411141
namespaced_name,
1142-
old_oid ? old_oid->hash : NULL,
1142+
old_oid,
11431143
0, "push", &err)) {
11441144
rp_error("%s", err.buf);
11451145
strbuf_release(&err);
@@ -1156,7 +1156,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
11561156

11571157
if (ref_transaction_update(transaction,
11581158
namespaced_name,
1159-
new_oid->hash, old_oid->hash,
1159+
new_oid, old_oid,
11601160
0, "push",
11611161
&err)) {
11621162
rp_error("%s", err.buf);

builtin/replace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static int replace_object_oid(const char *object_ref,
175175

176176
transaction = ref_transaction_begin(&err);
177177
if (!transaction ||
178-
ref_transaction_update(transaction, ref.buf, repl->hash, prev.hash,
178+
ref_transaction_update(transaction, ref.buf, repl, &prev,
179179
0, NULL, &err) ||
180180
ref_transaction_commit(transaction, &err))
181181
die("%s", err.buf);

builtin/tag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
544544

545545
transaction = ref_transaction_begin(&err);
546546
if (!transaction ||
547-
ref_transaction_update(transaction, ref.buf, object.hash, prev.hash,
547+
ref_transaction_update(transaction, ref.buf, &object, &prev,
548548
create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
549549
reflog_msg.buf, &err) ||
550550
ref_transaction_commit(transaction, &err))

builtin/update-ref.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static const char *parse_cmd_update(struct ref_transaction *transaction,
200200
die("update %s: extra input: %s", refname, next);
201201

202202
if (ref_transaction_update(transaction, refname,
203-
new_oid.hash, have_old ? old_oid.hash : NULL,
203+
&new_oid, have_old ? &old_oid : NULL,
204204
update_flags | create_reflog_flag,
205205
msg, &err))
206206
die("%s", err.buf);
@@ -232,7 +232,7 @@ static const char *parse_cmd_create(struct ref_transaction *transaction,
232232
if (*next != line_termination)
233233
die("create %s: extra input: %s", refname, next);
234234

235-
if (ref_transaction_create(transaction, refname, new_oid.hash,
235+
if (ref_transaction_create(transaction, refname, &new_oid,
236236
update_flags | create_reflog_flag,
237237
msg, &err))
238238
die("%s", err.buf);
@@ -269,7 +269,7 @@ static const char *parse_cmd_delete(struct ref_transaction *transaction,
269269
die("delete %s: extra input: %s", refname, next);
270270

271271
if (ref_transaction_delete(transaction, refname,
272-
have_old ? old_oid.hash : NULL,
272+
have_old ? &old_oid : NULL,
273273
update_flags, msg, &err))
274274
die("%s", err.buf);
275275

@@ -298,7 +298,7 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
298298
if (*next != line_termination)
299299
die("verify %s: extra input: %s", refname, next);
300300

301-
if (ref_transaction_verify(transaction, refname, old_oid.hash,
301+
if (ref_transaction_verify(transaction, refname, &old_oid,
302302
update_flags, &err))
303303
die("%s", err.buf);
304304

fast-import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,7 @@ static int update_branch(struct branch *b)
17781778
}
17791779
transaction = ref_transaction_begin(&err);
17801780
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,
17821782
0, msg, &err) ||
17831783
ref_transaction_commit(transaction, &err)) {
17841784
ref_transaction_free(transaction);
@@ -1820,7 +1820,7 @@ static void dump_tags(void)
18201820
strbuf_addf(&ref_name, "refs/tags/%s", t->name);
18211821

18221822
if (ref_transaction_update(transaction, ref_name.buf,
1823-
t->oid.hash, NULL, 0, msg, &err)) {
1823+
&t->oid, NULL, 0, msg, &err)) {
18241824
failure |= error("%s", err.buf);
18251825
goto cleanup;
18261826
}

refs.c

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,7 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,
671671

672672
transaction = ref_store_transaction_begin(refs, &err);
673673
if (!transaction ||
674-
ref_transaction_delete(transaction, refname,
675-
old_oid ? old_oid->hash : NULL,
674+
ref_transaction_delete(transaction, refname, old_oid,
676675
flags, msg, &err) ||
677676
ref_transaction_commit(transaction, &err)) {
678677
error("%s", err.buf);
@@ -898,8 +897,8 @@ void ref_transaction_free(struct ref_transaction *transaction)
898897
struct ref_update *ref_transaction_add_update(
899898
struct ref_transaction *transaction,
900899
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,
903902
const char *msg)
904903
{
905904
struct ref_update *update;
@@ -917,23 +916,23 @@ struct ref_update *ref_transaction_add_update(
917916
update->flags = flags;
918917

919918
if (flags & REF_HAVE_NEW)
920-
hashcpy(update->new_oid.hash, new_sha1);
919+
oidcpy(&update->new_oid, new_oid);
921920
if (flags & REF_HAVE_OLD)
922-
hashcpy(update->old_oid.hash, old_sha1);
921+
oidcpy(&update->old_oid, old_oid);
923922
update->msg = xstrdup_or_null(msg);
924923
return update;
925924
}
926925

927926
int ref_transaction_update(struct ref_transaction *transaction,
928927
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,
931930
unsigned int flags, const char *msg,
932931
struct strbuf *err)
933932
{
934933
assert(err);
935934

936-
if ((new_sha1 && !is_null_sha1(new_sha1)) ?
935+
if ((new_oid && !is_null_oid(new_oid)) ?
937936
check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
938937
!refname_is_safe(refname)) {
939938
strbuf_addf(err, "refusing to update ref with bad name '%s'",
@@ -943,48 +942,48 @@ int ref_transaction_update(struct ref_transaction *transaction,
943942

944943
flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS;
945944

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);
947946

948947
ref_transaction_add_update(transaction, refname, flags,
949-
new_sha1, old_sha1, msg);
948+
new_oid, old_oid, msg);
950949
return 0;
951950
}
952951

953952
int ref_transaction_create(struct ref_transaction *transaction,
954953
const char *refname,
955-
const unsigned char *new_sha1,
954+
const struct object_id *new_oid,
956955
unsigned int flags, const char *msg,
957956
struct strbuf *err)
958957
{
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);
963962
}
964963

965964
int ref_transaction_delete(struct ref_transaction *transaction,
966965
const char *refname,
967-
const unsigned char *old_sha1,
966+
const struct object_id *old_oid,
968967
unsigned int flags, const char *msg,
969968
struct strbuf *err)
970969
{
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");
973972
return ref_transaction_update(transaction, refname,
974-
null_sha1, old_sha1,
973+
&null_oid, old_oid,
975974
flags, msg, err);
976975
}
977976

978977
int ref_transaction_verify(struct ref_transaction *transaction,
979978
const char *refname,
980-
const unsigned char *old_sha1,
979+
const struct object_id *old_oid,
981980
unsigned int flags,
982981
struct strbuf *err)
983982
{
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");
986985
return ref_transaction_update(transaction, refname,
987-
NULL, old_sha1,
986+
NULL, old_oid,
988987
flags, NULL, err);
989988
}
990989

@@ -1003,8 +1002,7 @@ int refs_update_ref(struct ref_store *refs, const char *msg,
10031002
} else {
10041003
t = ref_store_transaction_begin(refs, &err);
10051004
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,
10081006
flags, msg, &err) ||
10091007
ref_transaction_commit(t, &err)) {
10101008
ret = 1;

0 commit comments

Comments
 (0)