findMergedChanges.pl 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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", "--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] changes/*
  19. A change is "merged" if it has ever been merged to release-0.2.2 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.2 and it
  22. has had changes in master.
  23. A change is "weird" if it has been merged to release-0.2.2 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.2";
  30. while (@ARGV and $ARGV[0] =~ /^--/) {
  31. my $flag = shift @ARGV;
  32. if ($flag =~ /^--(weird|merged|unmerged|list)/) {
  33. $look_for_type = $1;
  34. } elsif ($flag =~ /^--branch=(\S+)/) {
  35. $target_branch = $1;
  36. } else {
  37. die "Unrecognized flag $flag";
  38. }
  39. }
  40. for my $changefile (@ARGV) {
  41. my $n_merged = nChanges($target_branch, $changefile);
  42. my $n_postmerged = nChanges("${target_branch}..origin/master", $changefile);
  43. my $type;
  44. if ($n_merged != 0 and $n_postmerged == 0) {
  45. $type = "merged";
  46. } elsif ($n_merged == 0 and $n_postmerged != 0) {
  47. $type = "unmerged";
  48. } else {
  49. $type = "weird";
  50. }
  51. if ($type eq $look_for_type) {
  52. print "$changefile\n";
  53. } elsif ($look_for_type eq 'list') {
  54. printf "% 8s: %s\n", $type, $changefile;
  55. }
  56. }