Skip to content

Commit 69840cc

Browse files
pks-tgitster
authored andcommitted
refs: extract packed_refs_delete_refs() to allow control of transaction
When deleting loose refs, then we also have to delete the refs in the packed backend. This is done by calling `refs_delete_refs()`, which then uses the packed-backend's logic to delete refs. This doesn't allow us to exercise any control over the reference transaction which is being created in the packed backend, which is required in a subsequent commit. Extract a new function `packed_refs_delete_refs()`, which hosts most of the logic to delete refs except for creating the transaction itself. Like this, we can easily create the transaction in the files backend and thus exert more control over it. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e83ba64 commit 69840cc

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

refs/files-backend.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,7 @@ static int files_delete_refs(struct ref_store *ref_store, const char *msg,
12491249
{
12501250
struct files_ref_store *refs =
12511251
files_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
1252+
struct ref_transaction *transaction = NULL;
12521253
struct strbuf err = STRBUF_INIT;
12531254
int i, result = 0;
12541255

@@ -1258,10 +1259,14 @@ static int files_delete_refs(struct ref_store *ref_store, const char *msg,
12581259
if (packed_refs_lock(refs->packed_ref_store, 0, &err))
12591260
goto error;
12601261

1261-
if (refs_delete_refs(refs->packed_ref_store, msg, refnames, flags)) {
1262-
packed_refs_unlock(refs->packed_ref_store);
1262+
transaction = ref_store_transaction_begin(refs->packed_ref_store, &err);
1263+
if (!transaction)
1264+
goto error;
1265+
1266+
result = packed_refs_delete_refs(refs->packed_ref_store,
1267+
transaction, msg, refnames, flags);
1268+
if (result)
12631269
goto error;
1264-
}
12651270

12661271
packed_refs_unlock(refs->packed_ref_store);
12671272

@@ -1272,6 +1277,7 @@ static int files_delete_refs(struct ref_store *ref_store, const char *msg,
12721277
result |= error(_("could not remove reference %s"), refname);
12731278
}
12741279

1280+
ref_transaction_free(transaction);
12751281
strbuf_release(&err);
12761282
return result;
12771283

@@ -1288,6 +1294,7 @@ static int files_delete_refs(struct ref_store *ref_store, const char *msg,
12881294
else
12891295
error(_("could not delete references: %s"), err.buf);
12901296

1297+
ref_transaction_free(transaction);
12911298
strbuf_release(&err);
12921299
return -1;
12931300
}

refs/packed-backend.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,15 +1522,10 @@ static int packed_initial_transaction_commit(struct ref_store *ref_store,
15221522
static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
15231523
struct string_list *refnames, unsigned int flags)
15241524
{
1525-
struct packed_ref_store *refs =
1526-
packed_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
15271525
struct strbuf err = STRBUF_INIT;
15281526
struct ref_transaction *transaction;
1529-
struct string_list_item *item;
15301527
int ret;
15311528

1532-
(void)refs; /* We need the check above, but don't use the variable */
1533-
15341529
if (!refnames->nr)
15351530
return 0;
15361531

@@ -1544,6 +1539,26 @@ static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
15441539
if (!transaction)
15451540
return -1;
15461541

1542+
ret = packed_refs_delete_refs(ref_store, transaction,
1543+
msg, refnames, flags);
1544+
1545+
ref_transaction_free(transaction);
1546+
return ret;
1547+
}
1548+
1549+
int packed_refs_delete_refs(struct ref_store *ref_store,
1550+
struct ref_transaction *transaction,
1551+
const char *msg,
1552+
struct string_list *refnames,
1553+
unsigned int flags)
1554+
{
1555+
struct strbuf err = STRBUF_INIT;
1556+
struct string_list_item *item;
1557+
int ret;
1558+
1559+
/* Assert that the ref store refers to a packed backend. */
1560+
packed_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
1561+
15471562
for_each_string_list_item(item, refnames) {
15481563
if (ref_transaction_delete(transaction, item->string, NULL,
15491564
flags, msg, &err)) {
@@ -1563,7 +1578,6 @@ static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
15631578
error(_("could not delete references: %s"), err.buf);
15641579
}
15651580

1566-
ref_transaction_free(transaction);
15671581
strbuf_release(&err);
15681582
return ret;
15691583
}

refs/packed-backend.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
struct repository;
55
struct ref_transaction;
6+
struct string_list;
67

78
/*
89
* Support for storing references in a `packed-refs` file.
@@ -27,6 +28,12 @@ int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
2728
void packed_refs_unlock(struct ref_store *ref_store);
2829
int packed_refs_is_locked(struct ref_store *ref_store);
2930

31+
int packed_refs_delete_refs(struct ref_store *ref_store,
32+
struct ref_transaction *transaction,
33+
const char *msg,
34+
struct string_list *refnames,
35+
unsigned int flags);
36+
3037
/*
3138
* Return true if `transaction` really needs to be carried out against
3239
* the specified packed_ref_store, or false if it can be skipped

0 commit comments

Comments
 (0)