Skip to content

Commit fd9de86

Browse files
committed
Merge branch 'mh/refs-have-new'
Simplify the ref transaction API around how "the ref should be pointing at this object" is specified. * mh/refs-have-new: refs.h: remove duplication in function docstrings update_ref(): improve documentation ref_transaction_verify(): new function to check a reference's value ref_transaction_delete(): check that old_sha1 is not null_sha1 ref_transaction_create(): check that new_sha1 is valid commit: avoid race when creating orphan commits commit: add tests of commit races ref_transaction_delete(): remove "have_old" parameter ref_transaction_update(): remove "have_old" parameter struct ref_update: move "have_old" into "flags" refs.c: change some "flags" to "unsigned int" refs: remove the gap in the REF_* constant values refs: move REF_DELETING to refs.c
2 parents 97c12a8 + d1dd721 commit fd9de86

File tree

14 files changed

+233
-106
lines changed

14 files changed

+233
-106
lines changed

branch.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,9 @@ void create_branch(const char *head,
284284

285285
transaction = ref_transaction_begin(&err);
286286
if (!transaction ||
287-
ref_transaction_update(transaction, ref.buf, sha1,
288-
null_sha1, 0, !forcing, msg, &err) ||
287+
ref_transaction_update(transaction, ref.buf,
288+
sha1, forcing ? NULL : null_sha1,
289+
0, msg, &err) ||
289290
ref_transaction_commit(transaction, &err))
290291
die("%s", err.buf);
291292
ref_transaction_free(transaction);

builtin/commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,8 +1766,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
17661766
if (!transaction ||
17671767
ref_transaction_update(transaction, "HEAD", sha1,
17681768
current_head
1769-
? current_head->object.sha1 : NULL,
1770-
0, !!current_head, sb.buf, &err) ||
1769+
? current_head->object.sha1 : null_sha1,
1770+
0, sb.buf, &err) ||
17711771
ref_transaction_commit(transaction, &err)) {
17721772
rollback_index_files();
17731773
die("%s", err.buf);

builtin/fetch.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,10 @@ static int s_update_ref(const char *action,
415415

416416
transaction = ref_transaction_begin(&err);
417417
if (!transaction ||
418-
ref_transaction_update(transaction, ref->name, ref->new_sha1,
419-
ref->old_sha1, 0, check_old, msg, &err))
418+
ref_transaction_update(transaction, ref->name,
419+
ref->new_sha1,
420+
check_old ? ref->old_sha1 : NULL,
421+
0, msg, &err))
420422
goto fail;
421423

422424
ret = ref_transaction_commit(transaction, &err);

builtin/receive-pack.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -953,8 +953,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
953953
if (ref_transaction_delete(transaction,
954954
namespaced_name,
955955
old_sha1,
956-
0, old_sha1 != NULL,
957-
"push", &err)) {
956+
0, "push", &err)) {
958957
rp_error("%s", err.buf);
959958
strbuf_release(&err);
960959
return "failed to delete";
@@ -971,7 +970,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
971970
if (ref_transaction_update(transaction,
972971
namespaced_name,
973972
new_sha1, old_sha1,
974-
0, 1, "push",
973+
0, "push",
975974
&err)) {
976975
rp_error("%s", err.buf);
977976
strbuf_release(&err);

builtin/replace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ static int replace_object_sha1(const char *object_ref,
172172
transaction = ref_transaction_begin(&err);
173173
if (!transaction ||
174174
ref_transaction_update(transaction, ref, repl, prev,
175-
0, 1, NULL, &err) ||
175+
0, NULL, &err) ||
176176
ref_transaction_commit(transaction, &err))
177177
die("%s", err.buf);
178178

builtin/tag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
733733
transaction = ref_transaction_begin(&err);
734734
if (!transaction ||
735735
ref_transaction_update(transaction, ref.buf, object, prev,
736-
0, 1, NULL, &err) ||
736+
0, NULL, &err) ||
737737
ref_transaction_commit(transaction, &err))
738738
die("%s", err.buf);
739739
ref_transaction_free(transaction);

builtin/update-ref.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ static const char *parse_cmd_update(struct ref_transaction *transaction,
198198
if (*next != line_termination)
199199
die("update %s: extra input: %s", refname, next);
200200

201-
if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,
202-
update_flags, have_old, msg, &err))
201+
if (ref_transaction_update(transaction, refname,
202+
new_sha1, have_old ? old_sha1 : NULL,
203+
update_flags, msg, &err))
203204
die("%s", err.buf);
204205

205206
update_flags = 0;
@@ -264,8 +265,9 @@ static const char *parse_cmd_delete(struct ref_transaction *transaction,
264265
if (*next != line_termination)
265266
die("delete %s: extra input: %s", refname, next);
266267

267-
if (ref_transaction_delete(transaction, refname, old_sha1,
268-
update_flags, have_old, msg, &err))
268+
if (ref_transaction_delete(transaction, refname,
269+
have_old ? old_sha1 : NULL,
270+
update_flags, msg, &err))
269271
die("%s", err.buf);
270272

271273
update_flags = 0;
@@ -280,7 +282,6 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
280282
{
281283
struct strbuf err = STRBUF_INIT;
282284
char *refname;
283-
unsigned char new_sha1[20];
284285
unsigned char old_sha1[20];
285286

286287
refname = parse_refname(input, &next);
@@ -291,13 +292,11 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
291292
PARSE_SHA1_OLD))
292293
hashclr(old_sha1);
293294

294-
hashcpy(new_sha1, old_sha1);
295-
296295
if (*next != line_termination)
297296
die("verify %s: extra input: %s", refname, next);
298297

299-
if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,
300-
update_flags, 1, msg, &err))
298+
if (ref_transaction_verify(transaction, refname, old_sha1,
299+
update_flags, &err))
301300
die("%s", err.buf);
302301

303302
update_flags = 0;
@@ -353,7 +352,8 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
353352
{
354353
const char *refname, *oldval;
355354
unsigned char sha1[20], oldsha1[20];
356-
int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0;
355+
int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0;
356+
unsigned int flags = 0;
357357
struct option options[] = {
358358
OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
359359
OPT_BOOL('d', NULL, &delete, N_("delete the reference")),

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ extern void update_index_if_able(struct index_state *, struct lock_file *);
568568
extern int hold_locked_index(struct lock_file *, int);
569569
extern void set_alternate_index_output(const char *);
570570

571-
extern int delete_ref(const char *, const unsigned char *sha1, int delopt);
571+
extern int delete_ref(const char *, const unsigned char *sha1, unsigned int flags);
572572

573573
/* Environment bits from configuration mechanism */
574574
extern int trust_executable_bit;

fast-import.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ static int update_branch(struct branch *b)
17201720
transaction = ref_transaction_begin(&err);
17211721
if (!transaction ||
17221722
ref_transaction_update(transaction, b->name, b->sha1, old_sha1,
1723-
0, 1, msg, &err) ||
1723+
0, msg, &err) ||
17241724
ref_transaction_commit(transaction, &err)) {
17251725
ref_transaction_free(transaction);
17261726
error("%s", err.buf);
@@ -1760,8 +1760,8 @@ static void dump_tags(void)
17601760
strbuf_reset(&ref_name);
17611761
strbuf_addf(&ref_name, "refs/tags/%s", t->name);
17621762

1763-
if (ref_transaction_update(transaction, ref_name.buf, t->sha1,
1764-
NULL, 0, 0, msg, &err)) {
1763+
if (ref_transaction_update(transaction, ref_name.buf,
1764+
t->sha1, NULL, 0, msg, &err)) {
17651765
failure |= error("%s", err.buf);
17661766
goto cleanup;
17671767
}

0 commit comments

Comments
 (0)