findMergedChanges.pl 1.9 KB

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