From: Junio C Hamano Date: Mon, 10 Jul 2017 20:58:58 +0000 (-0700) Subject: Merge branch 'jk/add-p-commentchar-fix' into maint X-Git-Tag: v2.13.3~19 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/f904494574035f6ff2725b0e97951c55ccd9dee3?ds=inline;hp=-c Merge branch 'jk/add-p-commentchar-fix' into maint "git add -p" were updated in 2.12 timeframe to cope with custom core.commentchar but the implementation was buggy and a metacharacter like $ and * did not work. * jk/add-p-commentchar-fix: add--interactive: quote commentChar regex add--interactive: handle EOF in prompt_yesno --- f904494574035f6ff2725b0e97951c55ccd9dee3 diff --combined git-add--interactive.perl index 709a5f6ce6,395dd5eb4b..c55c612455 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@@ -47,6 -47,7 +47,6 @@@ my $normal_color = $repo->get_color("" my $diff_algorithm = $repo->config('diff.algorithm'); my $diff_indent_heuristic = $repo->config_bool('diff.indentheuristic'); -my $diff_compaction_heuristic = $repo->config_bool('diff.compactionheuristic'); my $diff_filter = $repo->config('interactive.difffilter'); my $use_readkey = 0; @@@ -92,7 -93,7 +92,7 @@@ sub colored } # command line options -my $cmd; +my $patch_mode_only; my $patch_mode; my $patch_mode_revision; @@@ -275,11 -276,20 +275,11 @@@ sub list_modified my ($only) = @_; my (%data, @return); my ($add, $del, $adddel, $file); - my @tracked = (); - - if (@ARGV) { - @tracked = map { - chomp $_; - unquote_path($_); - } run_cmd_pipe(qw(git ls-files --), @ARGV); - return if (!@tracked); - } my $reference = get_diff_reference($patch_mode_revision); for (run_cmd_pipe(qw(git diff-index --cached --numstat --summary), $reference, - '--', @tracked)) { + '--', @ARGV)) { if (($add, $del, $file) = /^([-\d]+) ([-\d]+) (.*)/) { my ($change, $bin); @@@ -304,7 -314,7 +304,7 @@@ } } - for (run_cmd_pipe(qw(git diff-files --numstat --summary --raw --), @tracked)) { + for (run_cmd_pipe(qw(git diff-files --numstat --summary --raw --), @ARGV)) { if (($add, $del, $file) = /^([-\d]+) ([-\d]+) (.*)/) { $file = unquote_path($file); @@@ -732,6 -742,8 +732,6 @@@ sub parse_diff } if ($diff_indent_heuristic) { splice @diff_cmd, 1, 0, "--indent-heuristic"; - } elsif ($diff_compaction_heuristic) { - splice @diff_cmd, 1, 0, "--compaction-heuristic"; } if (defined $patch_mode_revision) { push @diff_cmd, get_diff_reference($patch_mode_revision); @@@ -1040,7 -1052,7 +1040,7 @@@ marked for unstaging.") marked for applying."), checkout_index => N__( "If the patch applies cleanly, the edited hunk will immediately be -marked for discarding"), +marked for discarding."), checkout_head => N__( "If the patch applies cleanly, the edited hunk will immediately be marked for discarding."), @@@ -1085,7 -1097,7 +1085,7 @@@ EOF open $fh, '<', $hunkfile or die sprintf(__("failed to open hunk edit file for reading: %s"), $!); - my @newtext = grep { !/^$comment_line_char/ } <$fh>; + my @newtext = grep { !/^\Q$comment_line_char\E/ } <$fh>; close $fh; unlink $hunkfile; @@@ -1140,6 -1152,7 +1140,7 @@@ sub prompt_yesno while (1) { print colored $prompt_color, $prompt; my $line = prompt_single_character; + return undef unless defined $line; return 0 if $line =~ /^n/i; return 1 if $line =~ /^y/i; } @@@ -1290,7 -1303,7 +1291,7 @@@ sub patch_update_cmd } return 0; } - if ($patch_mode) { + if ($patch_mode_only) { @them = @mods; } else { @@@ -1669,7 -1682,7 +1670,7 @@@ status - show paths with change update - add working tree state to the staged set of changes revert - revert staged set of changes back to the HEAD version patch - pick hunks and update selectively -diff - view diff between HEAD and index +diff - view diff between HEAD and index add untracked - add contents of untracked files to the staged set of changes EOF } @@@ -1712,7 -1725,7 +1713,7 @@@ sub process_args die sprintf(__("invalid argument %s, expecting --"), $arg) unless $arg eq "--"; %patch_mode_flavour = %{$patch_modes{$patch_mode}}; - $cmd = 1; + $patch_mode_only = 1; } elsif ($arg ne "--") { die sprintf(__("invalid argument %s, expecting --"), $arg); @@@ -1749,7 -1762,7 +1750,7 @@@ sub main_loop process_args(); refresh(); -if ($cmd) { +if ($patch_mode_only) { patch_update_cmd(); } else { diff --combined t/t3701-add-interactive.sh index 2ecb43a616,2bfd41f06e..2f3e7cea64 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@@ -380,101 -380,12 +380,109 @@@ test_expect_success 'patch mode ignore test_cmp expected diff ' +test_expect_success 'diffs can be colorized' ' + git reset --hard && + + # force color even though the test script has no terminal + test_config color.ui always && + + echo content >test && + printf y | git add -p >output 2>&1 && + + # We do not want to depend on the exact coloring scheme + # git uses for diffs, so just check that we saw some kind of color. + grep "$(printf "\\033")" output +' + +test_expect_success 'patch-mode via -i prompts for files' ' + git reset --hard && + + echo one >file && + echo two >test && + git add -i <<-\EOF && + patch + test + + y + quit + EOF + + echo test >expect && + git diff --cached --name-only >actual && + test_cmp expect actual +' + +test_expect_success 'add -p handles globs' ' + git reset --hard && + + mkdir -p subdir && + echo base >one.c && + echo base >subdir/two.c && + git add "*.c" && + git commit -m base && + + echo change >one.c && + echo change >subdir/two.c && + git add -p "*.c" <<-\EOF && + y + y + EOF + + cat >expect <<-\EOF && + one.c + subdir/two.c + EOF + git diff --cached --name-only >actual && + test_cmp expect actual +' + +test_expect_success 'add -p handles relative paths' ' + git reset --hard && + + echo base >relpath.c && + git add "*.c" && + git commit -m relpath && + + echo change >relpath.c && + mkdir -p subdir && + git -C subdir add -p .. 2>error <<-\EOF && + y + EOF + + test_must_be_empty error && + + cat >expect <<-\EOF && + relpath.c + EOF + git diff --cached --name-only >actual && + test_cmp expect actual +' + +test_expect_success 'add -p does not expand argument lists' ' + git reset --hard && + + echo content >not-changed && + git add not-changed && + git commit -m "add not-changed file" && + + echo change >file && + GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF && + y + EOF + + # we know that "file" must be mentioned since we actually + # update it, but we want to be sure that our "." pathspec + # was not expanded into the argument list of any command. + # So look only for "not-changed". + ! grep not-changed trace.out +' + + test_expect_success 'hunk-editing handles custom comment char' ' + git reset --hard && + echo change >>file && + test_config core.commentChar "\$" && + echo e | GIT_EDITOR=true git add -p && + git diff --exit-code + ' + test_done