[PATCH] archimport - add merge detection
We now keep track of the patches merged in each branch since they have diverged, using the records that the Arch "logs" provide. Merge parents for a commit are defined if we are merging a series of patches that starts from the mergebase. If patches from a related branch are merged out-of-order, we keep track of how much has been merged sequentially -- the tip of that sequential merge is our new parent from that branch. This mechanism works very well for branches that merge in dovetail and/or flying fish patterns, probably less well for others. Signed-off-by: Martin Langhoff <martin@catalyst.net.nz> Signed-off-by: Junio C Hamano <junkio@cox.net>
martin@catalyst.net.nz committed
Sep 10, 2005 at 23:42 UTC
b779d5f009f8e12df13d650ad8a57e63068a9c82
1 file changed
+180
-4
git-archimport.perl
+180
-4
@@ -61,7 +61,7 @@ END
61
exit(1);
62
}
63
64
-getopts("hviC:t:") or usage();
64
+getopts("ThviC:t:") or usage();
65
usage if $opt_h;
66
67
@ARGV >= 1 or usage();
@@ -76,6 +76,10 @@ $git_tree ||= ".";
76
77
78
my @psets = (); # the collection
79
+my %psets = (); # the collection, by name
80
+
81
+my %rptags = (); # my reverse private tags
82
+ # to map a SHA1 to a commitid
83
84
foreach my $root (@arch_roots) {
85
my ($arepo, $abranch) = split(m!/!, $root);
@@ -96,6 +100,7 @@ foreach my $root (@arch_roots) {
100
if (%ps) {
101
my %temp = %ps; # break references
102
push (@psets, \%temp);
103
+ $psets{$temp{id}} = \%temp;
104
%ps = ();
105
}
106
@@ -158,7 +163,8 @@ foreach my $root (@arch_roots) {
163
164
if (%ps) {
165
my %temp = %ps; # break references
161
- push (@psets, \%temp);
166
+ push (@psets, \%temp);
167
+ $psets{ $temp{id} } = \%temp;
168
%ps = ();
169
}
170
close ABROWSE;
@@ -183,6 +189,24 @@ unless (-d '.git') { # initial import
189
} else {
190
die "Need to start from an import or a tag -- cannot use $psets[0]{id}";
191
}
192
+} else { # progressing an import
193
+ # load the rptags
194
+ opendir(DIR, ".git/archimport/tags")
195
+ || die "can't opendir: $!";
196
+ while (my $file = readdir(DIR)) {
197
+ # skip non-interesting-files
198
+ next unless -f ".git/archimport/tags/$file";
199
+ next if $file =~ m/--base-0$/; # don't care for base-0
200
+ my $sha = ptag($file);
201
+ chomp $sha;
202
+ # reconvert the 3rd '--' sequence from the end
203
+ # into a slash
204
+ # $file = reverse $file;
205
+ # $file =~ s!^(.+?--.+?--.+?--.+?)--(.+)$!$1/$2!;
206
+ # $file = reverse $file;
207
+ $rptags{$sha} = $file;
208
+ }
209
+ closedir DIR;
210
}
211
212
# process patchsets
@@ -345,6 +369,9 @@ foreach my $ps (@psets) {
369
}
370
}
371
372
+ if ($ps->{merges}) {
373
+ push @par, find_parents($ps);
374
+ }
375
my $par = join (' ', @par);
376
377
#
@@ -391,7 +418,8 @@ foreach my $ps (@psets) {
418
print " * Committed $ps->{id}\n";
419
print " + tree $tree\n";
420
print " + commit $commitid\n";
394
- # print " + commit date is $ps->{date} \n";
421
+ $opt_v && print " + commit date is $ps->{date} \n";
422
+ $opt_v && print " + parents: $par \n";
423
}
424
425
sub branchname {
@@ -556,7 +584,7 @@ sub tag {
584
or die "Cannot write tag $tag: $!\n";
585
close(C)
586
or die "Cannot write tag $tag: $!\n";
559
- print "Created tag '$tag' on '$commit'\n" if $opt_v;
587
+ print " * Created tag ' $tag' on '$commit'\n" if $opt_v;
588
} else { # read
589
open(C,"<.git/refs/tags/$tag")
590
or die "Cannot read tag $tag: $!\n";
@@ -587,6 +615,8 @@ sub ptag {
615
or die "Cannot write tag $tag: $!\n";
616
close(C)
617
or die "Cannot write tag $tag: $!\n";
618
+ $rptags{$commit} = $tag
619
+ unless $tag =~ m/--base-0$/;
620
} else { # read
621
# if the tag isn't there, return 0
622
unless ( -s ".git/archimport/tags/$tag") {
@@ -599,6 +629,152 @@ sub ptag {
629
die "Error reading tag $tag: $!\n" unless length $commit == 40;
630
close(C)
631
or die "Cannot read tag $tag: $!\n";
632
+ unless (defined $rptags{$commit}) {
633
+ $rptags{$commit} = $tag;
634
+ }
635
return $commit;
636
}
637
}
638
+
639
+sub find_parents {
640
+ #
641
+ # Identify what branches are merging into me
642
+ # and whether we are fully merged
643
+ # git-merge-base <headsha> <headsha> should tell
644
+ # me what the base of the merge should be
645
+ #
646
+ my $ps = shift;
647
+
648
+ my %branches; # holds an arrayref per branch
649
+ # the arrayref contains a list of
650
+ # merged patches between the base
651
+ # of the merge and the current head
652
+
653
+ my @parents; # parents found for this commit
654
+
655
+ # simple loop to split the merges
656
+ # per branch
657
+ foreach my $merge (@{$ps->{merges}}) {
658
+ my $branch = branchname($merge);
659
+ unless (defined $branches{$branch} ){
660
+ $branches{$branch} = [];
661
+ }
662
+ push @{$branches{$branch}}, $merge;
663
+ }
664
+
665
+ #
666
+ # foreach branch find a merge base and walk it to the
667
+ # head where we are, collecting the merged patchsets that
668
+ # Arch has recorded. Keep that in @have
669
+ # Compare that with the commits on the other branch
670
+ # between merge-base and the tip of the branch (@need)
671
+ # and see if we have a series of consecutive patches
672
+ # starting from the merge base. The tip of the series
673
+ # of consecutive patches merged is our new parent for
674
+ # that branch.
675
+ #
676
+ foreach my $branch (keys %branches) {
677
+ my $mergebase = `git-merge-base $branch $ps->{branch}`;
678
+ die "Cannot find merge base for $branch and $ps->{branch}" if $?;
679
+ chomp $mergebase;
680
+
681
+ # now walk up to the mergepoint collecting what patches we have
682
+ my $branchtip = git_rev_parse($ps->{branch});
683
+ my @ancestors = `git-rev-list --merge-order $branchtip ^$mergebase`;
684
+ my %have; # collected merges this branch has
685
+ foreach my $merge (@{$ps->{merges}}) {
686
+ $have{$merge} = 1;
687
+ }
688
+ my %ancestorshave;
689
+ foreach my $par (@ancestors) {
690
+ $par = commitid2pset($par);
691
+ if (defined $par->{merges}) {
692
+ foreach my $merge (@{$par->{merges}}) {
693
+ $ancestorshave{$merge}=1;
694
+ }
695
+ }
696
+ }
697
+ # print "++++ Merges in $ps->{id} are....\n";
698
+ # my @have = sort keys %have; print Dumper(\@have);
699
+
700
+ # merge what we have with what ancestors have
701
+ %have = (%have, %ancestorshave);
702
+
703
+ # see what the remote branch has - these are the merges we
704
+ # will want to have in a consecutive series from the mergebase
705
+ my $otherbranchtip = git_rev_parse($branch);
706
+ my @needraw = `git-rev-list --merge-order $otherbranchtip ^$mergebase`;
707
+ my @need;
708
+ foreach my $needps (@needraw) { # get the psets
709
+ $needps = commitid2pset($needps);
710
+ # git-rev-list will also
711
+ # list commits merged in via earlier
712
+ # merges. we are only interested in commits
713
+ # from the branch we're looking at
714
+ if ($branch eq $needps->{branch}) {
715
+ push @need, $needps->{id};
716
+ }
717
+ }
718
+
719
+ # print "++++ Merges from $branch we want are....\n";
720
+ # print Dumper(\@need);
721
+
722
+ my $newparent;
723
+ while (my $needed_commit = pop @need) {
724
+ if ($have{$needed_commit}) {
725
+ $newparent = $needed_commit;
726
+ } else {
727
+ last; # break out of the while
728
+ }
729
+ }
730
+ if ($newparent) {
731
+ push @parents, $newparent;
732
+ }
733
+
734
+
735
+ } # end foreach branch
736
+
737
+ # prune redundant parents
738
+ my %parents;
739
+ foreach my $p (@parents) {
740
+ $parents{$p} = 1;
741
+ }
742
+ foreach my $p (@parents) {
743
+ next unless exists $psets{$p}{merges};
744
+ next unless ref $psets{$p}{merges};
745
+ my @merges = @{$psets{$p}{merges}};
746
+ foreach my $merge (@merges) {
747
+ if ($parents{$merge}) {
748
+ delete $parents{$merge};
749
+ }
750
+ }
751
+ }
752
+ @parents = keys %parents;
753
+ @parents = map { " -p " . ptag($_) } @parents;
754
+ return @parents;
755
+}
756
+
757
+sub git_rev_parse {
758
+ my $name = shift;
759
+ my $val = `git-rev-parse $name`;
760
+ die "Error: git-rev-parse $name" if $?;
761
+ chomp $val;
762
+ return $val;
763
+}
764
+
765
+# resolve a SHA1 to a known patchset
766
+sub commitid2pset {
767
+ my $commitid = shift;
768
+ chomp $commitid;
769
+ my $name = $rptags{$commitid}
770
+ || die "Cannot find reverse tag mapping for $commitid";
771
+ # the keys in %rptag are slightly munged; unmunge
772
+ # reconvert the 3rd '--' sequence from the end
773
+ # into a slash
774
+ $name = reverse $name;
775
+ $name =~ s!^(.+?--.+?--.+?--.+?)--(.+)$!$1/$2!;
776
+ $name = reverse $name;
777
+ my $ps = $psets{$name}
778
+ || (print Dumper(sort keys %psets)) && die "Cannot find patchset for $name";
779
+ return $ps;
780
+}