Skip to content

Commit 5e00a6c

Browse files
mhaggergitster
authored andcommitted
files_transaction_finish(): delete reflogs before references
If the deletion steps unexpectedly fail, it is less bad to leave a reference without its reflog than it is to leave a reflog without its reference, since the latter is an invalid repository state. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9939b33 commit 5e00a6c

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

refs/files-backend.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,6 +2636,27 @@ static int files_transaction_finish(struct ref_store *ref_store,
26362636
}
26372637
}
26382638

2639+
/*
2640+
* Now that updates are safely completed, we can perform
2641+
* deletes. First delete the reflogs of any references that
2642+
* will be deleted, since (in the unexpected event of an
2643+
* error) leaving a reference without a reflog is less bad
2644+
* than leaving a reflog without a reference (the latter is a
2645+
* mildly invalid repository state):
2646+
*/
2647+
for (i = 0; i < transaction->nr; i++) {
2648+
struct ref_update *update = transaction->updates[i];
2649+
if (update->flags & REF_DELETING &&
2650+
!(update->flags & REF_LOG_ONLY) &&
2651+
!(update->flags & REF_ISPRUNING)) {
2652+
strbuf_reset(&sb);
2653+
files_reflog_path(refs, &sb, update->refname);
2654+
if (!unlink_or_warn(sb.buf))
2655+
try_remove_empty_parents(refs, update->refname,
2656+
REMOVE_EMPTY_PARENTS_REFLOG);
2657+
}
2658+
}
2659+
26392660
/*
26402661
* Perform deletes now that updates are safely completed.
26412662
*
@@ -2672,20 +2693,6 @@ static int files_transaction_finish(struct ref_store *ref_store,
26722693
}
26732694
}
26742695

2675-
/* Delete the reflogs of any references that were deleted: */
2676-
for (i = 0; i < transaction->nr; i++) {
2677-
struct ref_update *update = transaction->updates[i];
2678-
if (update->flags & REF_DELETING &&
2679-
!(update->flags & REF_LOG_ONLY) &&
2680-
!(update->flags & REF_ISPRUNING)) {
2681-
strbuf_reset(&sb);
2682-
files_reflog_path(refs, &sb, update->refname);
2683-
if (!unlink_or_warn(sb.buf))
2684-
try_remove_empty_parents(refs, update->refname,
2685-
REMOVE_EMPTY_PARENTS_REFLOG);
2686-
}
2687-
}
2688-
26892696
clear_loose_ref_cache(refs);
26902697

26912698
cleanup:

0 commit comments

Comments
 (0)