findMergedChanges.pl (1956B)
1 #!/usr/bin/perl 2 3 use warnings; 4 use strict; 5 6 sub nChanges { 7 my ($branches, $fname) = @_; 8 local *F; 9 # requires perl 5.8. Avoids shell issues if we ever get a changes 10 # file named by the parents of Little Johnny Tables. 11 open F, "-|", "git", "log", "--no-merges", "--pretty=format:%H", $branches, "--", $fname 12 or die "$!"; 13 my @changes = <F>; 14 return scalar @changes 15 } 16 17 my $look_for_type = "merged"; 18 19 if (! @ARGV) { 20 print <<EOF 21 Usage: 22 findMergedChanges.pl [--merged/--unmerged/--weird/--list] [--branch=<branchname] [--head=<branchname>] changes/* 23 24 A change is "merged" if it has ever been merged to release-0.2.4 and it has had 25 no subsequent changes in main. 26 27 A change is "unmerged" if it has never been merged to release-0.2.4 and it 28 has had changes in main. 29 30 A change is "weird" if it has been merged to release-0.2.4 and it *has* had 31 subsequent changes in main. 32 33 Suggested application: 34 findMergedChanges.pl --merged changes/* | xargs -n 1 git rm 35 36 EOF 37 } 38 39 my $target_branch = "origin/release-0.2.4"; 40 my $head = "origin/main"; 41 42 while (@ARGV and $ARGV[0] =~ /^--/) { 43 my $flag = shift @ARGV; 44 if ($flag =~ /^--(weird|merged|unmerged|list)/) { 45 $look_for_type = $1; 46 } elsif ($flag =~ /^--branch=(\S+)/) { 47 $target_branch = $1; 48 } elsif ($flag =~ /^--head=(\S+)/) { 49 $head = $1; 50 } else { 51 die "Unrecognized flag $flag"; 52 } 53 } 54 55 for my $changefile (@ARGV) { 56 my $n_merged = nChanges($target_branch, $changefile); 57 my $n_postmerged = nChanges("${target_branch}..${head}", $changefile); 58 my $type; 59 60 if ($n_merged != 0 and $n_postmerged == 0) { 61 $type = "merged"; 62 } elsif ($n_merged == 0 and $n_postmerged != 0) { 63 $type = "unmerged"; 64 } else { 65 $type = "weird"; 66 } 67 68 if ($type eq $look_for_type) { 69 print "$changefile\n"; 70 } elsif ($look_for_type eq 'list') { 71 printf "% 8s: %s\n", $type, $changefile; 72 } 73 }