2
0

hashfinder 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/perl
  2. # Copyright (c) 2007-2014, Anthony Minessale II
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. #
  9. # Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. #
  12. # Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in the
  14. # documentation and/or other materials provided with the distribution.
  15. #
  16. # Neither the name of the original author; nor the names of any contributors
  17. # may be used to endorse or promote products derived from this software
  18. # without specific prior written permission.
  19. #
  20. #
  21. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  25. # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. #
  33. # Contributors:
  34. #
  35. # hashfinder - Find origin of a particular line of code
  36. #
  37. my $file = shift;
  38. my $regex = shift;
  39. my $alt_file = shift;
  40. my $delim = " ";
  41. $file and $regex or die "missing params. Syntax: <file> <regex>";
  42. sub doit($$) {
  43. my $rev = shift;
  44. my $pattern = shift;
  45. my $loops = shift || 0;
  46. my $linematch = 0;
  47. if ($pattern =~ /^\~(.*)/) {
  48. $pattern = $1;
  49. } else {
  50. $pattern = quotemeta $pattern;
  51. }
  52. if ($pattern =~ /^(\d+)$/) {
  53. $linematch = 1;
  54. }
  55. retry:
  56. open GIT, "git blame -n $file $rev 2>&1|";
  57. my $mc = 0;
  58. my @matches = ();
  59. while (<GIT>) {
  60. my $matched = 0;
  61. if (/fatal:/) {
  62. if ($alt_file) {
  63. $file = $alt_file;
  64. $alt_file = undef;
  65. goto retry;
  66. }
  67. }
  68. if ($linematch) {
  69. $matched = (/^\S+\s+$pattern\s+/);
  70. } else {
  71. $matched = (/$pattern/);
  72. }
  73. if ($matched) {
  74. s/[\r\n]//g;
  75. push @matches, $_;
  76. $mc++;
  77. }
  78. }
  79. close(GIT);
  80. if ($mc > 5) {
  81. print $delim x $loops;
  82. print "$mc matches; Maybe more specific?\n";
  83. foreach (@matches) {
  84. print "$_\n";
  85. }
  86. return;
  87. }
  88. my $x = 0;
  89. foreach (@matches) {
  90. my ($hash, $lno, $author, $line);
  91. my $done = 0;
  92. if (/\//) {
  93. ($hash, $lno, $author, $line) = /(\S+)\s+\S+\s+(\S+)\s+(\([^\)]+\))\s*(.*)/;
  94. $done = 1;
  95. } else {
  96. die $_;
  97. ($hash, $lno, $author, $line) = /(\S+)\s+(\S+)\s+(\([^\)]+\))\s*(.*)/;
  98. }
  99. if ($hash) {
  100. $line =~ s/^\s+//g;
  101. print "\n";
  102. print $delim x $loops;
  103. my $msg = `git log -1 --pretty=format:%B $hash`;
  104. print "[$hash] [$lno] [$author] [$line]\n";
  105. print $delim x $loops;
  106. print ":: $msg\n";
  107. doit("$hash" . "^", $line, $loops+1);
  108. print "\n";
  109. }
  110. last if $done;
  111. }
  112. }
  113. doit(undef, $regex);
  114. # For Emacs:
  115. # Local Variables:
  116. # mode:perl
  117. # indent-tabs-mode:t
  118. # tab-width:4
  119. # c-basic-offset:4
  120. # End:
  121. # For VIM:
  122. # vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: