@@ -206,6 +206,21 @@ static int is_cmarker(char *buf, int marker_char, int marker_size)
206
206
return isspace (* buf );
207
207
}
208
208
209
+ /*
210
+ * Read contents a file with conflicts, normalize the conflicts
211
+ * by (1) discarding the common ancestor version in diff3-style,
212
+ * (2) reordering our side and their side so that whichever sorts
213
+ * alphabetically earlier comes before the other one, while
214
+ * computing the "conflict ID", which is just an SHA-1 hash of
215
+ * one side of the conflict, NUL, the other side of the conflict,
216
+ * and NUL concatenated together.
217
+ *
218
+ * Return the number of conflict hunks found.
219
+ *
220
+ * NEEDSWORK: the logic and theory of operation behind this conflict
221
+ * normalization may deserve to be documented somewhere, perhaps in
222
+ * Documentation/technical/rerere.txt.
223
+ */
209
224
static int handle_path (unsigned char * sha1 , struct rerere_io * io , int marker_size )
210
225
{
211
226
git_SHA_CTX ctx ;
@@ -276,6 +291,10 @@ static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_siz
276
291
return hunk_no ;
277
292
}
278
293
294
+ /*
295
+ * Scan the path for conflicts, do the "handle_path()" thing above, and
296
+ * return the number of conflict hunks found.
297
+ */
279
298
static int handle_file (const char * path , unsigned char * sha1 , const char * output )
280
299
{
281
300
int hunk_no = 0 ;
@@ -515,29 +534,54 @@ int rerere_remaining(struct string_list *merge_rr)
515
534
return 0 ;
516
535
}
517
536
537
+ /*
538
+ * Find the conflict identified by "name"; the change between its
539
+ * "preimage" (i.e. a previous contents with conflict markers) and its
540
+ * "postimage" (i.e. the corresponding contents with conflicts
541
+ * resolved) may apply cleanly to the contents stored in "path", i.e.
542
+ * the conflict this time around.
543
+ *
544
+ * Returns 0 for successful replay of recorded resolution, or non-zero
545
+ * for failure.
546
+ */
518
547
static int merge (const char * name , const char * path )
519
548
{
520
549
int ret ;
521
550
mmfile_t cur = {NULL , 0 }, base = {NULL , 0 }, other = {NULL , 0 };
522
551
mmbuffer_t result = {NULL , 0 };
523
552
553
+ /*
554
+ * Normalize the conflicts in path and write it out to
555
+ * "thisimage" temporary file.
556
+ */
524
557
if (handle_file (path , NULL , rerere_path (name , "thisimage" )) < 0 )
525
558
return 1 ;
526
559
527
560
if (read_mmfile (& cur , rerere_path (name , "thisimage" )) ||
528
- read_mmfile (& base , rerere_path (name , "preimage" )) ||
529
- read_mmfile (& other , rerere_path (name , "postimage" ))) {
561
+ read_mmfile (& base , rerere_path (name , "preimage" )) ||
562
+ read_mmfile (& other , rerere_path (name , "postimage" ))) {
530
563
ret = 1 ;
531
564
goto out ;
532
565
}
566
+
567
+ /*
568
+ * A three-way merge. Note that this honors user-customizable
569
+ * low-level merge driver settings.
570
+ */
533
571
ret = ll_merge (& result , path , & base , NULL , & cur , "" , & other , "" , NULL );
534
572
if (!ret ) {
535
573
FILE * f ;
536
574
575
+ /*
576
+ * A successful replay of recorded resolution.
577
+ * Mark that "postimage" was used to help gc.
578
+ */
537
579
if (utime (rerere_path (name , "postimage" ), NULL ) < 0 )
538
580
warning ("failed utime() on %s: %s" ,
539
581
rerere_path (name , "postimage" ),
540
582
strerror (errno ));
583
+
584
+ /* Update "path" with the resolution */
541
585
f = fopen (path , "w" );
542
586
if (!f )
543
587
return error ("Could not open %s: %s" , path ,
@@ -590,41 +634,61 @@ static int do_plain_rerere(struct string_list *rr, int fd)
590
634
find_conflict (& conflict );
591
635
592
636
/*
593
- * MERGE_RR records paths with conflicts immediately after merge
594
- * failed. Some of the conflicted paths might have been hand resolved
595
- * in the working tree since then, but the initial run would catch all
596
- * and register their preimages.
637
+ * MERGE_RR records paths with conflicts immediately after
638
+ * merge failed. Some of the conflicted paths might have been
639
+ * hand resolved in the working tree since then, but the
640
+ * initial run would catch all and register their preimages.
597
641
*/
598
-
599
642
for (i = 0 ; i < conflict .nr ; i ++ ) {
600
643
const char * path = conflict .items [i ].string ;
601
644
if (!string_list_has_string (rr , path )) {
602
645
unsigned char sha1 [20 ];
603
646
char * hex ;
604
647
int ret ;
648
+
649
+ /*
650
+ * Ask handle_file() to scan and assign a
651
+ * conflict ID. No need to write anything out
652
+ * yet.
653
+ */
605
654
ret = handle_file (path , sha1 , NULL );
606
655
if (ret < 1 )
607
656
continue ;
608
657
hex = xstrdup (sha1_to_hex (sha1 ));
609
658
string_list_insert (rr , path )-> util = hex ;
659
+
660
+ /*
661
+ * If the directory does not exist, create
662
+ * it. mkdir_in_gitdir() will fail with
663
+ * EEXIST if there already is one.
664
+ *
665
+ * NEEDSWORK: make sure "gc" does not remove
666
+ * preimage without removing the directory.
667
+ */
610
668
if (mkdir_in_gitdir (git_path ("rr-cache/%s" , hex )))
611
669
continue ;
670
+
671
+ /*
672
+ * We are the first to encounter this
673
+ * conflict. Ask handle_file() to write the
674
+ * normalized contents to the "preimage" file.
675
+ */
612
676
handle_file (path , NULL , rerere_path (hex , "preimage" ));
613
677
fprintf (stderr , "Recorded preimage for '%s'\n" , path );
614
678
}
615
679
}
616
680
617
681
/*
618
- * Now some of the paths that had conflicts earlier might have been
619
- * hand resolved. Others may be similar to a conflict already that
620
- * was resolved before.
682
+ * Some of the paths that had conflicts earlier might have
683
+ * been resolved by the user . Others may be similar to a
684
+ * conflict already that was resolved before.
621
685
*/
622
-
623
686
for (i = 0 ; i < rr -> nr ; i ++ ) {
624
687
int ret ;
625
688
const char * path = rr -> items [i ].string ;
626
689
const char * name = (const char * )rr -> items [i ].util ;
627
690
691
+ /* Is there a recorded resolution we could attempt to apply? */
628
692
if (has_rerere_resolution (name )) {
629
693
if (merge (name , path ))
630
694
continue ;
@@ -638,13 +702,13 @@ static int do_plain_rerere(struct string_list *rr, int fd)
638
702
goto mark_resolved ;
639
703
}
640
704
641
- /* Let's see if we have resolved it. */
705
+ /* Let's see if the user has resolved it. */
642
706
ret = handle_file (path , NULL , NULL );
643
707
if (ret )
644
708
continue ;
645
709
646
- fprintf (stderr , "Recorded resolution for '%s'.\n" , path );
647
710
copy_file (rerere_path (name , "postimage" ), path , 0666 );
711
+ fprintf (stderr , "Recorded resolution for '%s'.\n" , path );
648
712
mark_resolved :
649
713
free (rr -> items [i ].util );
650
714
rr -> items [i ].util = NULL ;
@@ -698,6 +762,11 @@ int setup_rerere(struct string_list *merge_rr, int flags)
698
762
return fd ;
699
763
}
700
764
765
+ /*
766
+ * The main entry point that is called internally from codepaths that
767
+ * perform mergy operations, possibly leaving conflicted index entries
768
+ * and working tree files.
769
+ */
701
770
int rerere (int flags )
702
771
{
703
772
struct string_list merge_rr = STRING_LIST_INIT_DUP ;
0 commit comments