aboutsummaryrefslogtreecommitdiff
path: root/tools/tools/commitsdb/query_commit_db
blob: e855efb067661a8e35b8d60cb08e6d1bceb1cd0b (plain) (blame)
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
#!/usr/bin/perl -w

# $FreeBSD$

# This script takes a filename and revision number as arguments
# and spits out a list of other files and their revisions that share
# the same log message.  This is done by referring to the database
# previously written by running make_commit_db.

use strict;
use Digest::MD5 qw(md5_hex);

my $dbname = "commitsdb";

# Take the filename and revision number from the command line.
# Also take a flag to say whether to generate a patch or not.
my ($file, $revision, $genpatch) = (shift, shift, shift);

# Find the checksum of the named revision.
my %possible_files;
open DB, "< $dbname" or die "$!\n";
my $cksum;
while (<DB>) {
	chomp;
	my ($name, $rev, $hash) = split;
	$name =~ s/^\.\///g;

	$possible_files{$name} = 1 if $file !~ /\// && $name =~ /^.*\/$file/;

	next unless $name eq $file and $rev eq $revision;
	$cksum = $hash;
}
close DB;

# Handle the fall-out if the file/revision wasn't matched.
unless ($cksum) {
	if (%possible_files) {
		print "Couldn't find the file. Maybe you meant:\n";
		foreach (sort keys %possible_files) {
			print "\t$_\n";
		}
	}
	die "Can't find $file rev $revision in database\n";
}


# Look for similar revisions.
my @results;
open DB, "< $dbname" or die "$!\n";
while (<DB>) {
	chomp;
	my ($name, $rev, $hash) = split;

	next unless $hash eq $cksum;

	push @results, "$name $rev";
}
close DB;

# May as well show the log message if we're producing a patch
print `cvs log -r$revision $file` if $genpatch;

# Show the commits that match, and their patches if required.
foreach my $r (sort @results) {
	print "$r\n";
	next unless $genpatch;

	my ($name, $rev) = split /\s/, $r, 2;
	my $prevrev = previous_revision($rev);
	print `cvs diff -u -r$prevrev -r$rev $name`;
	print "\n\n";
}

#
# Return the previous revision number.
#
sub previous_revision {
	my $rev = shift;

	$rev =~ /(?:(.*)\.)?([^\.]+)\.([^\.]+)$/;
	my ($base, $r1, $r2) = ($1, $2, $3);

	my $prevrev = "";
	if ($r2 == 1) {
		$prevrev = $base;
	} else {
		$prevrev = "$base." if $base;
		$prevrev .= "$r1." . ($r2 - 1);
	}
	return $prevrev;
}

#end