Skip to content

Commit 1bc4cc3

Browse files
KarthikNayakgitster
authored andcommitted
refs: accept symref values in ref_transaction_update()
The function `ref_transaction_update()` obtains ref information and flags to create a `ref_update` and add them to the transaction at hand. To extend symref support in transactions, we need to also accept the old and new ref targets and process it. This commit adds the required parameters to the function and modifies all call sites. The two parameters added are `new_target` and `old_target`. The `new_target` is used to denote what the reference should point to when the transaction is applied. Some functions allow this parameter to be NULL, meaning that the reference is not changed. The `old_target` denotes the value the reference must have before the update. Some functions allow this parameter to be NULL, meaning that the old value of the reference is not checked. We also update the internal function `ref_transaction_add_update()` similarly to take the two new parameters. Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 436d4e5 commit 1bc4cc3

File tree

14 files changed

+71
-24
lines changed

14 files changed

+71
-24
lines changed

branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ void create_branch(struct repository *r,
627627
if (!transaction ||
628628
ref_transaction_update(transaction, ref.buf,
629629
&oid, forcing ? NULL : null_oid(),
630-
0, msg, &err) ||
630+
NULL, NULL, 0, msg, &err) ||
631631
ref_transaction_commit(transaction, &err))
632632
die("%s", err.buf);
633633
ref_transaction_free(transaction);

builtin/fast-import.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@ static int update_branch(struct branch *b)
16341634
transaction = ref_transaction_begin(&err);
16351635
if (!transaction ||
16361636
ref_transaction_update(transaction, b->name, &b->oid, &old_oid,
1637-
0, msg, &err) ||
1637+
NULL, NULL, 0, msg, &err) ||
16381638
ref_transaction_commit(transaction, &err)) {
16391639
ref_transaction_free(transaction);
16401640
error("%s", err.buf);
@@ -1675,7 +1675,8 @@ static void dump_tags(void)
16751675
strbuf_addf(&ref_name, "refs/tags/%s", t->name);
16761676

16771677
if (ref_transaction_update(transaction, ref_name.buf,
1678-
&t->oid, NULL, 0, msg, &err)) {
1678+
&t->oid, NULL, NULL, NULL,
1679+
0, msg, &err)) {
16791680
failure |= error("%s", err.buf);
16801681
goto cleanup;
16811682
}

builtin/fetch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ static int s_update_ref(const char *action,
667667

668668
ret = ref_transaction_update(transaction, ref->name, &ref->new_oid,
669669
check_old ? &ref->old_oid : NULL,
670-
0, msg, &err);
670+
NULL, NULL, 0, msg, &err);
671671
if (ret) {
672672
ret = STORE_REF_ERROR_OTHER;
673673
goto out;

builtin/receive-pack.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
15951595
if (ref_transaction_update(transaction,
15961596
namespaced_name,
15971597
new_oid, old_oid,
1598+
NULL, NULL,
15981599
0, "push",
15991600
&err)) {
16001601
rp_error("%s", err.buf);

builtin/replace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ static int replace_object_oid(const char *object_ref,
201201
transaction = ref_transaction_begin(&err);
202202
if (!transaction ||
203203
ref_transaction_update(transaction, ref.buf, repl, &prev,
204-
0, NULL, &err) ||
204+
NULL, NULL, 0, NULL, &err) ||
205205
ref_transaction_commit(transaction, &err))
206206
res = error("%s", err.buf);
207207

builtin/tag.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
660660
transaction = ref_transaction_begin(&err);
661661
if (!transaction ||
662662
ref_transaction_update(transaction, ref.buf, &object, &prev,
663+
NULL, NULL,
663664
create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
664665
reflog_msg.buf, &err) ||
665666
ref_transaction_commit(transaction, &err)) {

builtin/update-ref.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ static void parse_cmd_update(struct ref_transaction *transaction,
204204

205205
if (ref_transaction_update(transaction, refname,
206206
&new_oid, have_old ? &old_oid : NULL,
207+
NULL, NULL,
207208
update_flags | create_reflog_flag,
208209
msg, &err))
209210
die("%s", err.buf);

refs.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,13 +1228,19 @@ struct ref_update *ref_transaction_add_update(
12281228
const char *refname, unsigned int flags,
12291229
const struct object_id *new_oid,
12301230
const struct object_id *old_oid,
1231+
const char *new_target, const char *old_target,
12311232
const char *msg)
12321233
{
12331234
struct ref_update *update;
12341235

12351236
if (transaction->state != REF_TRANSACTION_OPEN)
12361237
BUG("update called for transaction that is not open");
12371238

1239+
if (old_oid && old_target)
1240+
BUG("only one of old_oid and old_target should be non NULL");
1241+
if (new_oid && new_target)
1242+
BUG("only one of new_oid and new_target should be non NULL");
1243+
12381244
FLEX_ALLOC_STR(update, refname, refname);
12391245
ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
12401246
transaction->updates[transaction->nr++] = update;
@@ -1253,6 +1259,8 @@ int ref_transaction_update(struct ref_transaction *transaction,
12531259
const char *refname,
12541260
const struct object_id *new_oid,
12551261
const struct object_id *old_oid,
1262+
const char *new_target,
1263+
const char *old_target,
12561264
unsigned int flags, const char *msg,
12571265
struct strbuf *err)
12581266
{
@@ -1280,7 +1288,8 @@ int ref_transaction_update(struct ref_transaction *transaction,
12801288
flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0);
12811289

12821290
ref_transaction_add_update(transaction, refname, flags,
1283-
new_oid, old_oid, msg);
1291+
new_oid, old_oid, new_target,
1292+
old_target, msg);
12841293
return 0;
12851294
}
12861295

@@ -1295,7 +1304,8 @@ int ref_transaction_create(struct ref_transaction *transaction,
12951304
return 1;
12961305
}
12971306
return ref_transaction_update(transaction, refname, new_oid,
1298-
null_oid(), flags, msg, err);
1307+
null_oid(), NULL, NULL, flags,
1308+
msg, err);
12991309
}
13001310

13011311
int ref_transaction_delete(struct ref_transaction *transaction,
@@ -1308,7 +1318,8 @@ int ref_transaction_delete(struct ref_transaction *transaction,
13081318
BUG("delete called with old_oid set to zeros");
13091319
return ref_transaction_update(transaction, refname,
13101320
null_oid(), old_oid,
1311-
flags, msg, err);
1321+
NULL, NULL, flags,
1322+
msg, err);
13121323
}
13131324

13141325
int ref_transaction_verify(struct ref_transaction *transaction,
@@ -1321,6 +1332,7 @@ int ref_transaction_verify(struct ref_transaction *transaction,
13211332
BUG("verify called with old_oid set to NULL");
13221333
return ref_transaction_update(transaction, refname,
13231334
NULL, old_oid,
1335+
NULL, NULL,
13241336
flags, NULL, err);
13251337
}
13261338

@@ -1335,8 +1347,8 @@ int refs_update_ref(struct ref_store *refs, const char *msg,
13351347

13361348
t = ref_store_transaction_begin(refs, &err);
13371349
if (!t ||
1338-
ref_transaction_update(t, refname, new_oid, old_oid, flags, msg,
1339-
&err) ||
1350+
ref_transaction_update(t, refname, new_oid, old_oid, NULL, NULL,
1351+
flags, msg, &err) ||
13401352
ref_transaction_commit(t, &err)) {
13411353
ret = 1;
13421354
ref_transaction_free(t);

refs.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,16 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
648648
* before the update. A copy of this value is made in the
649649
* transaction.
650650
*
651+
* new_target -- the target reference that the reference will be
652+
* updated to point to. If the reference is a regular reference,
653+
* it will be converted to a symbolic reference. Cannot be set
654+
* together with `new_oid`. A copy of this value is made in the
655+
* transaction.
656+
*
657+
* old_target -- the reference that the reference must be pointing to.
658+
* Canont be set together with `old_oid`. A copy of this value is
659+
* made in the transaction.
660+
*
651661
* flags -- flags affecting the update, passed to
652662
* update_ref_lock(). Possible flags: REF_NO_DEREF,
653663
* REF_FORCE_CREATE_REFLOG. See those constants for more
@@ -713,7 +723,11 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
713723
* beforehand. The old value is checked after the lock is taken to
714724
* prevent races. If the old value doesn't agree with old_oid, the
715725
* whole transaction fails. If old_oid is NULL, then the previous
716-
* value is not checked.
726+
* value is not checked. If `old_target` is not NULL, treat the reference
727+
* as a symbolic ref and validate that its target before the update is
728+
* `old_target`. If the `new_target` is not NULL, then the reference
729+
* will be updated to a symbolic ref which targets `new_target`.
730+
* Together, these allow us to update between regular refs and symrefs.
717731
*
718732
* See the above comment "Reference transaction updates" for more
719733
* information.
@@ -722,6 +736,8 @@ int ref_transaction_update(struct ref_transaction *transaction,
722736
const char *refname,
723737
const struct object_id *new_oid,
724738
const struct object_id *old_oid,
739+
const char *new_target,
740+
const char *old_target,
725741
unsigned int flags, const char *msg,
726742
struct strbuf *err);
727743

refs/files-backend.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
11981198
ref_transaction_add_update(
11991199
transaction, r->name,
12001200
REF_NO_DEREF | REF_HAVE_NEW | REF_HAVE_OLD | REF_IS_PRUNING,
1201-
null_oid(), &r->oid, NULL);
1201+
null_oid(), &r->oid, NULL, NULL, NULL);
12021202
if (ref_transaction_commit(transaction, &err))
12031203
goto cleanup;
12041204

@@ -1292,7 +1292,7 @@ static int files_pack_refs(struct ref_store *ref_store,
12921292
* packed-refs transaction:
12931293
*/
12941294
if (ref_transaction_update(transaction, iter->refname,
1295-
iter->oid, NULL,
1295+
iter->oid, NULL, NULL, NULL,
12961296
REF_NO_DEREF, NULL, &err))
12971297
die("failure preparing to create packed reference %s: %s",
12981298
iter->refname, err.buf);
@@ -2309,7 +2309,7 @@ static int split_head_update(struct ref_update *update,
23092309
transaction, "HEAD",
23102310
update->flags | REF_LOG_ONLY | REF_NO_DEREF,
23112311
&update->new_oid, &update->old_oid,
2312-
update->msg);
2312+
NULL, NULL, update->msg);
23132313

23142314
/*
23152315
* Add "HEAD". This insertion is O(N) in the transaction
@@ -2372,7 +2372,7 @@ static int split_symref_update(struct ref_update *update,
23722372
new_update = ref_transaction_add_update(
23732373
transaction, referent, new_flags,
23742374
&update->new_oid, &update->old_oid,
2375-
update->msg);
2375+
NULL, NULL, update->msg);
23762376

23772377
new_update->parent_update = update;
23782378

@@ -2763,7 +2763,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
27632763
packed_transaction, update->refname,
27642764
REF_HAVE_NEW | REF_NO_DEREF,
27652765
&update->new_oid, NULL,
2766-
NULL);
2766+
NULL, NULL, NULL);
27672767
}
27682768
}
27692769

@@ -3048,7 +3048,7 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
30483048
ref_transaction_add_update(packed_transaction, update->refname,
30493049
update->flags & ~REF_HAVE_OLD,
30503050
&update->new_oid, &update->old_oid,
3051-
NULL);
3051+
NULL, NULL, NULL);
30523052
}
30533053

30543054
if (packed_refs_lock(refs->packed_ref_store, 0, err)) {

0 commit comments

Comments
 (0)