rerere: refactor "replay" part of do_plain_rerere()
Extract the body of a loop that attempts to replay recorded resolution for each conflicted path into a helper function, not because I want to call it from multiple places later, but because the logic has become too deeply nested and hard to read. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Jun 30, 2015 at 19:36 UTC
8e7768b2de8bfdf82cde565d2f42e8d7f91e74e0
1 file changed
+40
-35
rerere.c
+40
-35
@@ -629,6 +629,44 @@ static void update_paths(struct string_list *update)
629
rollback_lock_file(&index_lock);
630
}
631
632
+/*
633
+ * The path indicated by rr_item may still have conflict for which we
634
+ * have a recorded resolution, in which case replay it and optionally
635
+ * update it. Or it may have been resolved by the user and we may
636
+ * only have the preimage for that conflict, in which case the result
637
+ * needs to be recorded as a resolution in a postimage file.
638
+ */
639
+static void do_rerere_one_path(struct string_list_item *rr_item,
640
+ struct string_list *update)
641
+{
642
+ const char *path = rr_item->string;
643
+ const char *name = (const char *)rr_item->util;
644
+
645
+ /* Is there a recorded resolution we could attempt to apply? */
646
+ if (has_rerere_resolution(name)) {
647
+ if (merge(name, path))
648
+ return; /* failed to replay */
649
+
650
+ if (rerere_autoupdate)
651
+ string_list_insert(update, path);
652
+ else
653
+ fprintf(stderr,
654
+ "Resolved '%s' using previous resolution.\n",
655
+ path);
656
+ goto mark_resolved;
657
+ }
658
+
659
+ /* Let's see if the user has resolved it. */
660
+ if (handle_file(path, NULL, NULL))
661
+ return; /* not yet resolved */
662
+
663
+ copy_file(rerere_path(name, "postimage"), path, 0666);
664
+ fprintf(stderr, "Recorded resolution for '%s'.\n", path);
665
+mark_resolved:
666
+ free(rr_item->util);
667
+ rr_item->util = NULL;
668
+}
669
+
670
static int do_plain_rerere(struct string_list *rr, int fd)
671
{
672
struct string_list conflict = STRING_LIST_INIT_DUP;
@@ -682,41 +720,8 @@ static int do_plain_rerere(struct string_list *rr, int fd)
720
}
721
}
722
685
- /*
686
- * Some of the paths that had conflicts earlier might have
687
- * been resolved by the user. Others may be similar to a
688
- * conflict already that was resolved before.
689
- */
690
- for (i = 0; i < rr->nr; i++) {
691
- int ret;
692
- const char *path = rr->items[i].string;
693
- const char *name = (const char *)rr->items[i].util;
694
-
695
- /* Is there a recorded resolution we could attempt to apply? */
696
- if (has_rerere_resolution(name)) {
697
- if (merge(name, path))
698
- continue;
699
-
700
- if (rerere_autoupdate)
701
- string_list_insert(&update, path);
702
- else
703
- fprintf(stderr,
704
- "Resolved '%s' using previous resolution.\n",
705
- path);
706
- goto mark_resolved;
707
- }
708
-
709
- /* Let's see if the user has resolved it. */
710
- ret = handle_file(path, NULL, NULL);
711
- if (ret)
712
- continue;
713
-
714
- copy_file(rerere_path(name, "postimage"), path, 0666);
715
- fprintf(stderr, "Recorded resolution for '%s'.\n", path);
716
- mark_resolved:
717
- free(rr->items[i].util);
718
- rr->items[i].util = NULL;
719
- }
723
+ for (i = 0; i < rr->nr; i++)
724
+ do_rerere_one_path(&rr->items[i], &update);
725
726
if (update.nr)
727
update_paths(&update);