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 <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 8, 2017 at 15:51 UTC 5e00a6c873981f87165adfecf29ad0ecc2c2c5df
1 file changed +21 -14
refs/files-backend.c
+21 -14
@@ -2636,6 +2636,27 @@ static int files_transaction_finish(struct ref_store *ref_store,
2636 }
2637 }
2638
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 +
2660 /*
2661 * Perform deletes now that updates are safely completed.
2662 *
@@ -2672,20 +2693,6 @@ static int files_transaction_finish(struct ref_store *ref_store,
2693 }
2694 }
2695
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 -
2696 clear_loose_ref_cache(refs);
2697
2698 cleanup: