Re: Blamming a diff between two commits?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Samuel Lucas Vaz de Mello wrote:
> Is there any way to git blame (or annotate) a diff between two
> commits?

Piecing it together from existing tools isn't really hard, and made
for a nice distraction.

Call it as './git-blame-diff.perl HEAD^ HEAD' or so.

This lacks proper argument checking and a chdir to the repository top
level.  Maybe you could fill in the gaps and shape it as a contrib
patch?  For bonus points, change it so that the workdir version can be
used as the new side of the diff, by omitting the second argument.

--- 8< ---
#!/usr/bin/perl -w

sub parse_hunk_header {
	my ($line) = @_;
	my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
	    $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
	$o_cnt = 1 unless defined $o_cnt;
	$n_cnt = 1 unless defined $n_cnt;
	return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
}

sub get_blame_prefix {
	my ($line) = @_;
	$line =~ /^([0-9a-f]+\s+\([^\)]+\))/ or die "bad blame output: $line";
	return $1;
}

my ($oldrev, $newrev) = @ARGV;
open($diff, '-|', 'git', '--no-pager', 'diff', $oldrev, $newrev) or die;

my ($pre, $post);
my $filename;
while (<$diff>) {
	if (m{^diff --git ./(.*) ./\1$}) {
		$filename = $1;
	} elsif (m{^(\+\+\+|---) ./$filename$}) {
		# ignore
	} elsif (m{^@@ }) {
		my ($o_ofs, $o_cnt, $n_ofs, $n_cnt)
			= parse_hunk_header($_);
		my $o_end = $o_ofs + $o_cnt;
		my $n_end = $n_ofs + $n_cnt;
		open($pre, '-|', 'git', 'blame', "-L$o_ofs,$o_end",
		     $oldrev, '--', $filename) or die;
		open($post, '-|', 'git', 'blame', "-L$n_ofs,$n_end",
		     $newrev, '--', $filename) or die;
	} elsif (m{^ }) {
		print get_blame_prefix(scalar <$pre>), "\t", $_;
		scalar <$post>; # discard
	} elsif (m{^\-}) {
		print get_blame_prefix(scalar <$pre>), "\t", $_;
	} elsif (m{^\+}) {
		print get_blame_prefix(scalar <$post>), "\t", $_;
	} 
}

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

Attachment: signature.asc
Description: This is a digitally signed message part.


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux