Skip to content

Commit

Permalink
Add support for passing options and specify filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
Håkon Løvdal authored and hlovdal committed Jan 29, 2022
1 parent 5bf930d commit cfce377
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

Annotates each line in a diff hunk with author and commit information like blame.

## Usage

```shell
git diff-blame [oldrev [newrev [options] [-- [file1] [file2] [file3] ...]]]
```
There is support for passing on options to `git diff` but they must be given
after `oldrev` and `newrev` if they are present. E.g.
```shell
git diff-blame HEAD^ HEAD -w
git diff-blame --cached -- myfile.c
```
## Example:
$ git diff-blame HEAD^
Expand Down
45 changes: 38 additions & 7 deletions git-diff-blame
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# binary, for any purpose, commercial or non-commercial, and by any
# means.

use List::Util qw(first);

sub parse_hunk_header {
my ($line) = @_;
my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
Expand All @@ -27,12 +29,36 @@ $git_root =~ s/^\s+//;
$git_root =~ s/\s+$//;
chdir($git_root) or die "$!";

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

# First check if file arguments are present.
my $index = first { $ARGV[$_] eq '--' } 0..$#ARGV;
if (defined $index) {
($oldrev, $newrev) = @ARGV[0 .. $index-1];
@REST = @ARGV[$index .. $#ARGV];
} else {
($oldrev, $newrev, @REST) = @ARGV;
}

# Then check if any of the revisons accidentally were assigned git diff option values.
if (defined $newrev && $newrev =~ /^-/) {
unshift(@REST, $newrev);
$newrev = undef;
}

if (defined $oldrev && $oldrev =~ /^-/) {
unshift(@REST, $oldrev);
$oldrev = undef;
}

if (defined $oldrev) {
if (defined $newrev) {
open($diff, '-|', 'git', '--no-pager', 'diff', $oldrev, $newrev, @REST) or die;
} else {
open($diff, '-|', 'git', '--no-pager', 'diff', $oldrev, @REST) or die;
}
} else {
open($diff, '-|', 'git', '--no-pager', 'diff', $oldrev) or die;
open($diff, '-|', 'git', '--no-pager', 'diff', @REST) or die;
}

my ($pre, $post);
Expand Down Expand Up @@ -63,8 +89,13 @@ while (<$diff>) {
my $o_end = $o_ofs + $o_cnt - 1;
my $n_end = $n_ofs + $n_cnt - 1;
if (!$create) {
open($pre, '-|', 'git', 'blame', '-M', "-L$o_ofs,$o_end",
$oldrev, '--', $prefilename) or die;
if ($oldrev) {
open($pre, '-|', 'git', 'blame', '-M', "-L$o_ofs,$o_end",
$oldrev, '--', $prefilename) or die;
} else {
open($pre, '-|', 'git', 'blame', '-M', "-L$o_ofs,$o_end",
'HEAD', '--', $prefilename) or die;
}
}
if (!$delete) {
if ($newrev) {
Expand Down

0 comments on commit cfce377

Please sign in to comment.