git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

search-svnlog.pl 3.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#!/usr/bin/perl -w

# ====================================================================
# Show log messages matching a certain pattern.  Usage:
#
#    search-svnlog.pl [-v] [-f LOGFILE] REGEXP
#
# See &usage() for details.
#
# ====================================================================
# Copyright (c) 2000-2004 CollabNet.  All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.  The terms
# are also available at http://subversion.tigris.org/license-1.html.
# If newer versions of this license are posted there, you may use a
# newer version instead, at your option.
#
# This software consists of voluntary contributions made by many
# individuals.  For exact contribution history, see the revision
# history and logs, available at http://subversion.tigris.org/.
# ====================================================================

use strict;
use Getopt::Long;

my $log_file;
my $invert = 0;
my $caseless = 0;

GetOptions('f|file=s' => \$log_file,
           'v|invert' => \$invert,
           'i|caseinsensitive' => \$caseless) or &usage;

&usage("$0: too few arguments") unless @ARGV;
&usage("$0: too many arguments") if @ARGV > 1;

my $filter = shift;
$filter = '(?i)' . $filter if $caseless;

my $log_cmd = "svn log -v";

my $log_separator = '-' x 72 . "\n";

my $open_string = defined $log_file ? $log_file : "$log_cmd |";
open(LOGDATA, $open_string) or
  die "$0: cannot open `$open_string' for reading: $!\n";

my $this_entry_accum = "";
my $this_rev = -1;
my $this_lines = 0;
my $seen_blank_line;  # A blank line separates headers from body.

while (<LOGDATA>)
{
  if (/^r([0-9]+) \| [^\|]* \| [^\|]* \| ([0-9]+) (line|lines)$/)
  {
    $this_rev = $1;
    $this_lines = $2 + 1;  # Compensate for blank line preceding body.

    $this_entry_accum .= $_;
  }
  elsif ($this_lines == 0)  # Reached end of msg.  Looking at log separator?
  {
    if (! ($_ eq $log_separator))
    {
      die "$0: wrong number of lines for log message!\n${this_entry_accum}\n";
    }

    if ($this_entry_accum =~ /$filter/og ^ $invert)
    {
      print "${this_entry_accum}${log_separator}";
    }

    # Reset accumulators.
    $seen_blank_line = 0;
    $this_entry_accum = "";
    $this_rev = -1;
  }
  elsif ($this_lines < 0)
  {
    die "$0: line weirdness parsing log.\n";
  }
  else   # Just continue accumulating.
  {
    $this_entry_accum .= $_;

    if ($seen_blank_line)
    {
      $this_lines--;
    }
    elsif (/^$/)
    {
      $seen_blank_line = 1;
      $this_lines--;
    }
  }
}

close(LOGDATA) or
  die "$0: closing `$open_string' failed: $!\n";

exit 0;

sub usage {
  warn "@_\n" if @_;
  die "usage: $0: [-v] [-i] [-f LOGFILE] REGEXP\n",
      "\n",
      "Print only log messages matching REGEXP, either by running 'svn log'\n",
      "in the current working directory, or if '-f LOGFILE' is passed, then\n",
      "read the log data from LOGFILE (which should be in the same format\n",
      "as the output of 'svn log').\n",
      "\n",
      "If '-v' is given, the matching is inverted (like 'grep -v').\n",
      "\n",
      "If '-i' is given, the matching is case-insensitive (like 'grep -i').\n";
}