summaryrefslogtreecommitdiff
path: root/git-add--interactive.perl
diff options
context:
space:
mode:
Diffstat (limited to 'git-add--interactive.perl')
-rwxr-xr-xgit-add--interactive.perl183
1 files changed, 136 insertions, 47 deletions
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index 69aeaf0..77876d4 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -1,6 +1,8 @@
-#!/usr/bin/perl -w
+#!/usr/bin/perl
+use 5.008;
use strict;
+use warnings;
use Git;
binmode(STDOUT, ":raw");
@@ -42,7 +44,12 @@ my ($diff_new_color) =
my $normal_color = $repo->get_color("", "reset");
+my $diff_algorithm = $repo->config('diff.algorithm');
+
my $use_readkey = 0;
+my $use_termcap = 0;
+my %term_escapes;
+
sub ReadMode;
sub ReadKey;
if ($repo->config_bool("interactive.singlekey")) {
@@ -51,6 +58,17 @@ if ($repo->config_bool("interactive.singlekey")) {
Term::ReadKey->import;
$use_readkey = 1;
};
+ if (!$use_readkey) {
+ print STDERR "missing Term::ReadKey, disabling interactive.singlekey\n";
+ }
+ eval {
+ require Term::Cap;
+ my $termcap = Term::Cap->Tgetent;
+ foreach (values %$termcap) {
+ $term_escapes{$_} = 1 if /^\e/;
+ }
+ $use_termcap = 1;
+ };
}
sub colored {
@@ -87,6 +105,7 @@ my %patch_modes = (
TARGET => '',
PARTICIPLE => 'staging',
FILTER => 'file-only',
+ IS_REVERSE => 0,
},
'stash' => {
DIFF => 'diff-index -p HEAD',
@@ -96,6 +115,7 @@ my %patch_modes = (
TARGET => '',
PARTICIPLE => 'stashing',
FILTER => undef,
+ IS_REVERSE => 0,
},
'reset_head' => {
DIFF => 'diff-index -p --cached',
@@ -105,6 +125,7 @@ my %patch_modes = (
TARGET => '',
PARTICIPLE => 'unstaging',
FILTER => 'index-only',
+ IS_REVERSE => 1,
},
'reset_nothead' => {
DIFF => 'diff-index -R -p --cached',
@@ -114,6 +135,7 @@ my %patch_modes = (
TARGET => ' to index',
PARTICIPLE => 'applying',
FILTER => 'index-only',
+ IS_REVERSE => 0,
},
'checkout_index' => {
DIFF => 'diff-files -p',
@@ -123,6 +145,7 @@ my %patch_modes = (
TARGET => ' from worktree',
PARTICIPLE => 'discarding',
FILTER => 'file-only',
+ IS_REVERSE => 1,
},
'checkout_head' => {
DIFF => 'diff-index -p',
@@ -132,6 +155,7 @@ my %patch_modes = (
TARGET => ' from index and worktree',
PARTICIPLE => 'discarding',
FILTER => undef,
+ IS_REVERSE => 1,
},
'checkout_nothead' => {
DIFF => 'diff-index -R -p',
@@ -141,13 +165,14 @@ my %patch_modes = (
TARGET => ' to index and worktree',
PARTICIPLE => 'applying',
FILTER => undef,
+ IS_REVERSE => 0,
},
);
my %patch_mode_flavour = %{$patch_modes{stage}};
sub run_cmd_pipe {
- if ($^O eq 'MSWin32' || $^O eq 'msys') {
+ if ($^O eq 'MSWin32') {
my @invalid = grep {m/[":*]/} @_;
die "$^O does not support: @invalid\n" if @invalid;
my @args = map { m/ /o ? "\"$_\"": $_ } @_;
@@ -241,6 +266,17 @@ sub get_empty_tree {
return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
}
+sub get_diff_reference {
+ my $ref = shift;
+ if (defined $ref and $ref ne 'HEAD') {
+ return $ref;
+ } elsif (is_initial_commit()) {
+ return get_empty_tree();
+ } else {
+ return 'HEAD';
+ }
+}
+
# Returns list of hashes, contents of each of which are:
# VALUE: pathname
# BINARY: is a binary path
@@ -248,6 +284,7 @@ sub get_empty_tree {
# FILE: is file different from index?
# INDEX_ADDDEL: is it add/delete between HEAD and index?
# FILE_ADDDEL: is it add/delete between index and file?
+# UNMERGED: is the path unmerged
sub list_modified {
my ($only) = @_;
@@ -263,14 +300,7 @@ sub list_modified {
return if (!@tracked);
}
- my $reference;
- if (defined $patch_mode_revision and $patch_mode_revision ne 'HEAD') {
- $reference = $patch_mode_revision;
- } elsif (is_initial_commit()) {
- $reference = get_empty_tree();
- } else {
- $reference = 'HEAD';
- }
+ my $reference = get_diff_reference($patch_mode_revision);
for (run_cmd_pipe(qw(git diff-index --cached
--numstat --summary), $reference,
'--', @tracked)) {
@@ -298,16 +328,10 @@ sub list_modified {
}
}
- for (run_cmd_pipe(qw(git diff-files --numstat --summary --), @tracked)) {
+ for (run_cmd_pipe(qw(git diff-files --numstat --summary --raw --), @tracked)) {
if (($add, $del, $file) =
/^([-\d]+) ([-\d]+) (.*)/) {
$file = unquote_path($file);
- if (!exists $data{$file}) {
- $data{$file} = +{
- INDEX => 'unchanged',
- BINARY => 0,
- };
- }
my ($change, $bin);
if ($add eq '-' && $del eq '-') {
$change = 'binary';
@@ -326,6 +350,18 @@ sub list_modified {
$file = unquote_path($file);
$data{$file}{FILE_ADDDEL} = $adddel;
}
+ elsif (/^:[0-7]+ [0-7]+ [0-9a-f]+ [0-9a-f]+ (.) (.*)$/) {
+ $file = unquote_path($2);
+ if (!exists $data{$file}) {
+ $data{$file} = +{
+ INDEX => 'unchanged',
+ BINARY => 0,
+ };
+ }
+ if ($1 eq 'U') {
+ $data{$file}{UNMERGED} = 1;
+ }
+ }
}
for (sort keys %data) {
@@ -479,6 +515,9 @@ sub error_msg {
sub list_and_choose {
my ($opts, @stuff) = @_;
my (@chosen, @return);
+ if (!@stuff) {
+ return @return;
+ }
my $i;
my @prefixes = find_unique_prefixes(@stuff) unless $opts->{LIST_ONLY};
@@ -689,6 +728,8 @@ sub add_untracked_cmd {
if (@add) {
system(qw(git update-index --add --), @add);
say_n_paths('added', @add);
+ } else {
+ print "No untracked files.\n";
}
print "\n";
}
@@ -696,7 +737,7 @@ sub add_untracked_cmd {
sub run_git_apply {
my $cmd = shift;
my $fh;
- open $fh, '| git ' . $cmd;
+ open $fh, '| git ' . $cmd . " --recount --allow-overlap";
print $fh @_;
return close $fh;
}
@@ -704,8 +745,11 @@ sub run_git_apply {
sub parse_diff {
my ($path) = @_;
my @diff_cmd = split(" ", $patch_mode_flavour{DIFF});
+ if (defined $diff_algorithm) {
+ splice @diff_cmd, 1, 0, "--diff-algorithm=${diff_algorithm}";
+ }
if (defined $patch_mode_revision) {
- push @diff_cmd, $patch_mode_revision;
+ push @diff_cmd, get_diff_reference($patch_mode_revision);
}
my @diff = run_cmd_pipe("git", @diff_cmd, "--", $path);
my @colored = ();
@@ -731,14 +775,17 @@ sub parse_diff_header {
my $head = { TEXT => [], DISPLAY => [], TYPE => 'header' };
my $mode = { TEXT => [], DISPLAY => [], TYPE => 'mode' };
+ my $deletion = { TEXT => [], DISPLAY => [], TYPE => 'deletion' };
for (my $i = 0; $i < @{$src->{TEXT}}; $i++) {
- my $dest = $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ?
- $mode : $head;
+ my $dest =
+ $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ? $mode :
+ $src->{TEXT}->[$i] =~ /^deleted file/ ? $deletion :
+ $head;
push @{$dest->{TEXT}}, $src->{TEXT}->[$i];
push @{$dest->{DISPLAY}}, $src->{DISPLAY}->[$i];
}
- return ($head, $mode);
+ return ($head, $mode, $deletion);
}
sub hunk_splittable {
@@ -954,6 +1001,28 @@ sub coalesce_overlapping_hunks {
return @out;
}
+sub reassemble_patch {
+ my $head = shift;
+ my @patch;
+
+ # Include everything in the header except the beginning of the diff.
+ push @patch, (grep { !/^[-+]{3}/ } @$head);
+
+ # Then include any headers from the hunk lines, which must
+ # come before any actual hunk.
+ while (@_ && $_[0] !~ /^@/) {
+ push @patch, shift;
+ }
+
+ # Then begin the diff.
+ push @patch, grep { /^[-+]{3}/ } @$head;
+
+ # And then the actual hunks.
+ push @patch, @_;
+
+ return @patch;
+}
+
sub color_diff {
return map {
colored((/^@/ ? $fraginfo_color :
@@ -974,10 +1043,12 @@ sub edit_hunk_manually {
print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n";
print $fh @$oldtext;
my $participle = $patch_mode_flavour{PARTICIPLE};
+ my $is_reverse = $patch_mode_flavour{IS_REVERSE};
+ my ($remove_plus, $remove_minus) = $is_reverse ? ('-', '+') : ('+', '-');
print $fh <<EOF;
# ---
-# To remove '-' lines, make them ' ' lines (context).
-# To remove '+' lines, delete them.
+# To remove '$remove_minus' lines, make them ' ' lines (context).
+# To remove '$remove_plus' lines, delete them.
# Lines starting with # will be removed.
#
# If the patch applies cleanly, the edited hunk will immediately be
@@ -987,8 +1058,7 @@ sub edit_hunk_manually {
EOF
close $fh;
- my $editor = $ENV{GIT_EDITOR} || $repo->config("core.editor")
- || $ENV{VISUAL} || $ENV{EDITOR} || "vi";
+ chomp(my $editor = run_cmd_pipe(qw(git var GIT_EDITOR)));
system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
if ($? != 0) {
@@ -1014,8 +1084,7 @@ EOF
}
sub diff_applies {
- my $fh;
- return run_git_apply($patch_mode_flavour{APPLY_CHECK} . ' --recount --check',
+ return run_git_apply($patch_mode_flavour{APPLY_CHECK} . ' --check',
map { @{$_->{TEXT}} } @_);
}
@@ -1032,6 +1101,14 @@ sub prompt_single_character {
ReadMode 'cbreak';
my $key = ReadKey 0;
ReadMode 'restore';
+ if ($use_termcap and $key eq "\e") {
+ while (!defined $term_escapes{$key}) {
+ my $next = ReadKey 0.5;
+ last if (!defined $next);
+ $key .= $next;
+ }
+ $key =~ s/\e/^[/;
+ }
print "$key" if defined $key;
print "\n";
return $key;
@@ -1087,9 +1164,9 @@ sub help_patch_cmd {
print colored $help_color, <<EOF ;
y - $verb this hunk$target
n - do not $verb this hunk$target
-q - quit, do not $verb this hunk nor any of the remaining ones
-a - $verb this and all the remaining hunks in the file
-d - do not $verb this hunk nor any of the remaining hunks in the file
+q - quit; do not $verb this hunk or any of the remaining ones
+a - $verb this hunk and all later hunks in the file
+d - do not $verb this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
@@ -1104,7 +1181,7 @@ EOF
sub apply_patch {
my $cmd = shift;
- my $ret = run_git_apply $cmd . ' --recount', @_;
+ my $ret = run_git_apply $cmd, @_;
if (!$ret) {
print STDERR @_;
}
@@ -1113,17 +1190,17 @@ sub apply_patch {
sub apply_patch_for_checkout_commit {
my $reverse = shift;
- my $applies_index = run_git_apply 'apply '.$reverse.' --cached --recount --check', @_;
- my $applies_worktree = run_git_apply 'apply '.$reverse.' --recount --check', @_;
+ my $applies_index = run_git_apply 'apply '.$reverse.' --cached --check', @_;
+ my $applies_worktree = run_git_apply 'apply '.$reverse.' --check', @_;
if ($applies_worktree && $applies_index) {
- run_git_apply 'apply '.$reverse.' --cached --recount', @_;
- run_git_apply 'apply '.$reverse.' --recount', @_;
+ run_git_apply 'apply '.$reverse.' --cached', @_;
+ run_git_apply 'apply '.$reverse, @_;
return 1;
} elsif (!$applies_index) {
print colored $error_color, "The selected hunks do not apply to the index!\n";
if (prompt_yesno "Apply them to the worktree anyway? ") {
- return run_git_apply 'apply '.$reverse.' --recount', @_;
+ return run_git_apply 'apply '.$reverse, @_;
} else {
print colored $error_color, "Nothing was applied.\n";
return 0;
@@ -1136,6 +1213,10 @@ sub apply_patch_for_checkout_commit {
sub patch_update_cmd {
my @all_mods = list_modified($patch_mode_flavour{FILTER});
+ error_msg "ignoring unmerged: $_->{VALUE}\n"
+ for grep { $_->{UNMERGED} } @all_mods;
+ @all_mods = grep { !$_->{UNMERGED} } @all_mods;
+
my @mods = grep { !($_->{BINARY}) } @all_mods;
my @them;
@@ -1183,7 +1264,7 @@ sub summarize_hunk {
# Print a one-line summary of each hunk in the array ref in
-# the first argument, starting wih the index in the 2nd.
+# the first argument, starting with the index in the 2nd.
sub display_hunks {
my ($hunks, $i) = @_;
my $ctr = 0;
@@ -1206,7 +1287,7 @@ sub patch_update_file {
my ($ix, $num);
my $path = shift;
my ($head, @hunk) = parse_diff($path);
- ($head, my $mode) = parse_diff_header($head);
+ ($head, my $mode, my $deletion) = parse_diff_header($head);
for (@{$head->{DISPLAY}}) {
print;
}
@@ -1214,6 +1295,13 @@ sub patch_update_file {
if (@{$mode->{TEXT}}) {
unshift @hunk, $mode;
}
+ if (@{$deletion->{TEXT}}) {
+ foreach my $hunk (@hunk) {
+ push @{$deletion->{TEXT}}, @{$hunk->{TEXT}};
+ push @{$deletion->{DISPLAY}}, @{$hunk->{DISPLAY}};
+ }
+ @hunk = ($deletion);
+ }
$num = scalar @hunk;
$ix = 0;
@@ -1267,10 +1355,13 @@ sub patch_update_file {
print;
}
print colored $prompt_color, $patch_mode_flavour{VERB},
- ($hunk[$ix]{TYPE} eq 'mode' ? ' mode change' : ' this hunk'),
+ ($hunk[$ix]{TYPE} eq 'mode' ? ' mode change' :
+ $hunk[$ix]{TYPE} eq 'deletion' ? ' deletion' :
+ ' this hunk'),
$patch_mode_flavour{TARGET},
" [y,n,q,a,d,/$other,?]? ";
my $line = prompt_single_character;
+ last unless defined $line;
if ($line) {
if ($line =~ /^y/i) {
$hunk[$ix]{USE} = 1;
@@ -1322,14 +1413,13 @@ sub patch_update_file {
next;
}
elsif ($line =~ /^q/i) {
- while ($ix < $num) {
- if (!defined $hunk[$ix]{USE}) {
- $hunk[$ix]{USE} = 0;
+ for ($i = 0; $i < $num; $i++) {
+ if (!defined $hunk[$i]{USE}) {
+ $hunk[$i]{USE} = 0;
}
- $ix++;
}
$quit = 1;
- next;
+ last;
}
elsif ($line =~ m|^/(.*)|) {
my $regex = $1;
@@ -1441,8 +1531,7 @@ sub patch_update_file {
}
if (@result) {
- my $fh;
- my @patch = (@{$head->{TEXT}}, @result);
+ my @patch = reassemble_patch($head->{TEXT}, @result);
my $apply_routine = $patch_mode_flavour{APPLY};
&$apply_routine(@patch);
refresh();