Permalink
Browse files

Proper option and revision parsing instead of passing directly to git…

… diff

This makes expanding a single commit into a range of one an easy, robust
feature.
  • Loading branch information...
1 parent bf56476 commit 93ff80e6b60bd5f24d262fafe0aeb56d13f5ddc2 @tsibley committed Feb 18, 2014
Showing with 41 additions and 2 deletions.
  1. +41 −2 git-hitlist
View
@@ -4,8 +4,45 @@ use warnings;
use open qw/ :std :encoding(UTF-8) /;
use autodie;
use File::Temp 'tempfile';
+use Getopt::Long;
-open my $diff, "-|", qw(git diff -U0), @ARGV;
+sub usage {
+ return <<USAGE;
+git-hitlist [--cached]
+git-hitlist <commit> [<commit>]
+
+Open vim with all the files touched by the unstaged changes, staged changes
+(with --cached), or the specified commit. If two commits are specified, the
+diff of the two will be used. The quickfix list is populated with the first
+line of each patch hunk for easier jumping around.
+
+--cached Use staged changes instead of unstaged changes
+USAGE
+}
+
+my (@opts, $use_staged);
+GetOptions(
+ 'cached' => \$use_staged,
+ 'help' => sub { print usage(); exit 0 },
+) or die usage();
+
+if ($use_staged) {
+ if (@ARGV) {
+ warn "Commit not allowed when --cached is specified.\n";
+ die usage();
+ }
+ push @opts, "--cached";
+}
+elsif (@ARGV) {
+ open my $revparse, "-|", qw(git rev-parse --revs-only), @ARGV;
+ @opts = map { chomp; $_ } <$revparse>;
+
+ # Turn a single commit into a range of one.
+ unshift @opts, "$opts[0]~"
+ if @opts == 1;
+}
+
+open my $diff, "-|", qw(git diff -U0), @opts;
my (@hitlist, %file, %line);
while (<$diff>) {
@@ -25,7 +62,9 @@ while (<$diff>) {
close $diff;
unless (@hitlist) {
- warn "Nothing to whack. (Perhaps you meant to use --cached?)\n";
+ warn "Nothing to whack.",
+ ($use_staged ? "" : " (Perhaps you meant to use --cached?)"),
+ "\n";
exit;
}

0 comments on commit 93ff80e

Please sign in to comment.