revdiff 2.98 KB
Newer Older
Gerd Moellmann's avatar
Gerd Moellmann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#! /usr/bin/perl

# Copyright (C) 2001 Free Software Foundation, Inc.
#
# This file is part of GNU Emacs.
#
# GNU Emacs is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# GNU Emacs is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Emacs; see the file COPYING.  If not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

use File::Basename;

24
if (@ARGV < 3)
Gerd Moellmann's avatar
Gerd Moellmann committed
25 26 27
{
  print <<USAGE;
revdiff FILE OLD NEW
28

Gerd Moellmann's avatar
Gerd Moellmann committed
29
Get a diff of FILE between revisions OLD and NEW.  Store the
Gerd Moellmann's avatar
Gerd Moellmann committed
30 31 32 33 34 35 36
diff in a file named FILE-OLD-NEW.diff.

If OLD is `-' use FILE's current revision for OLD.  If OLD is
`-<number>', use the Nth revision before the current one for OLD.

If NEW is +<number> or -<number>, build diffs between revisions OLD
and OLD +/- <number>.
Gerd Moellmann's avatar
Gerd Moellmann committed
37 38 39 40

Examples:

revdiff FILE - -1      get the latest change of FILE
Gerd Moellmann's avatar
Gerd Moellmann committed
41
revdiff FILE -1 +1     also gets the latest change of FILE
Gerd Moellmann's avatar
Gerd Moellmann committed
42 43 44 45 46 47 48 49 50
revdiff FILE 1.500 +2  get diffs 1.500-1.501 and 1.501-1.502.

USAGE
  exit 1;
}

$file = shift @ARGV;
$old  = shift @ARGV;

51
sub diffit
Gerd Moellmann's avatar
Gerd Moellmann committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
{
  my ($old, $new) = @_;
  print "cvs diff -r$old -r$new $file >$file-$old-$new.diff\n";
  system "cvs diff -r$old -r$new $file >$file-$old-$new.diff";
}

sub current_revision ($)
{
  my ($file) = @_;
  my $dir = dirname ($file);
  my $base = basename ($file);
  my $entries = "$dir/CVS/Entries";
  die "Can't find $entries" unless -f $entries;
  open (IN, "<$entries") or die "Cannot open $entries";
  my $rev;
67
  while ($line = <IN>)
Gerd Moellmann's avatar
Gerd Moellmann committed
68
    {
69
      if ($line =~ m,/$base/([^/]+),)
Gerd Moellmann's avatar
Gerd Moellmann committed
70 71 72 73 74 75 76 77 78 79 80
	{
	  $rev = $1;
	  break;
	}
    }
  die "Cannot determine current revision of $file" unless $rev;
  close (IN);
  return $rev;
}

if ($old eq "-")
Gerd Moellmann's avatar
Gerd Moellmann committed
81 82 83
  {
    $old = current_revision ($file);
  }
84
elsif ($old =~ /^-(\d+)$/)
Gerd Moellmann's avatar
Gerd Moellmann committed
85 86 87 88 89 90 91
  {
    my $offset = $1;
    $old = current_revision ($file);
    die "Internal error" unless $old =~ /(.*)\.(\d+)$/;
    my $minor = $2 - $offset;
    $old = sprintf ("%d.%d", $1, $minor);
  }
Gerd Moellmann's avatar
Gerd Moellmann committed
92

93
while (@ARGV)
Gerd Moellmann's avatar
Gerd Moellmann committed
94 95 96 97 98
  {
    my $new = shift @ARGV;
    if ($new =~ /^[+]\d+$/)
      {
	my $n = $new;
99
	for ($i = 0; $i < $n; ++$i)
Gerd Moellmann's avatar
Gerd Moellmann committed
100
	  {
101
	    unless ($old =~ /(.*)\.(\d+)$/)
Gerd Moellmann's avatar
Gerd Moellmann committed
102 103 104 105 106 107 108 109
	      {
		die "Internal error";
	      }
	    my $j = $2 + 1;
	    $new = "$1.$j";
	    diffit ($old, $new);
	    $old = $new;
	  }
110 111
      }
    elsif ($new =~ /^[-]\d+$/)
Gerd Moellmann's avatar
Gerd Moellmann committed
112 113
      {
	my $n = - $new;
114
	for ($i = 0; $i < $n; ++$i)
Gerd Moellmann's avatar
Gerd Moellmann committed
115
	  {
116
	    unless ($old =~ /(.*)\.(\d+)$/)
Gerd Moellmann's avatar
Gerd Moellmann committed
117 118 119 120 121 122 123 124 125
	      {
		die "Internal error";
	      }
	    my $j = $2 - 1;
	    $new = "$1.$j";
	    diffit ($new, $old);
	    $old = $new;
	  }
      }
126
    else
Gerd Moellmann's avatar
Gerd Moellmann committed
127 128 129 130 131 132 133 134 135
      {
	diffit ($old, $new);
	$old = $new;
      }
  }

# Local Variables:
# mode: cperl
# End: