add--interactive: ignore unmerged entries in patch mode

When "add -p" sees an unmerged entry, it shows the combined diff and then immediately skips the hunk. This can be confusing in a variety of ways, depending on whether there are other changes to stage (in which case you get the superfluous combined diff output in between other hunks) or not (in which case you get the combined diff and the program exits immediately, rather than seeing "No changes"). The current behavior was not planned, and is just what the implementation happens to do. Instead, let's explicitly remove unmerged entries from our list of modified files, and print a warning that we are ignoring them. We can cheaply find which entries are unmerged by adding "--raw" output to the "diff-files --numstat" we already run. There is one non-obvious thing we must change when parsing this combined output. Before this patch, when we saw a numstat line for a file that did not have index changes, we would create a new record with 'unchanged' in the 'INDEX' field. Because "--raw" comes before "--numstat", we must move this special-case down to the raw-line case (and it is sufficient to move it rather than handle it in both places, since any file which has a --numstat will also have a --raw entry). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 5, 2012 at 08:30 UTC 4066bd6797a36ece00ffcb4814edc11e5ed25f68
2 files changed +44 -7
git-add--interactive.perl
+18 -7
@@ -268,6 +268,7 @@ sub get_empty_tree {
268 # FILE: is file different from index?
269 # INDEX_ADDDEL: is it add/delete between HEAD and index?
270 # FILE_ADDDEL: is it add/delete between index and file?
271 +# UNMERGED: is the path unmerged
272
273 sub list_modified {
274 my ($only) = @_;
@@ -318,16 +319,10 @@ sub list_modified {
319 }
320 }
321
321 - for (run_cmd_pipe(qw(git diff-files --numstat --summary --), @tracked)) {
322 + for (run_cmd_pipe(qw(git diff-files --numstat --summary --raw --), @tracked)) {
323 if (($add, $del, $file) =
324 /^([-\d]+) ([-\d]+) (.*)/) {
325 $file = unquote_path($file);
325 - if (!exists $data{$file}) {
326 - $data{$file} = +{
327 - INDEX => 'unchanged',
328 - BINARY => 0,
329 - };
330 - }
326 my ($change, $bin);
327 if ($add eq '-' && $del eq '-') {
328 $change = 'binary';
@@ -346,6 +341,18 @@ sub list_modified {
341 $file = unquote_path($file);
342 $data{$file}{FILE_ADDDEL} = $adddel;
343 }
344 + elsif (/^:[0-7]+ [0-7]+ [0-9a-f]+ [0-9a-f]+ (.) (.*)$/) {
345 + $file = unquote_path($2);
346 + if (!exists $data{$file}) {
347 + $data{$file} = +{
348 + INDEX => 'unchanged',
349 + BINARY => 0,
350 + };
351 + }
352 + if ($1 eq 'U') {
353 + $data{$file}{UNMERGED} = 1;
354 + }
355 + }
356 }
357
358 for (sort keys %data) {
@@ -1190,6 +1197,10 @@ sub apply_patch_for_checkout_commit {
1197
1198 sub patch_update_cmd {
1199 my @all_mods = list_modified($patch_mode_flavour{FILTER});
1200 + error_msg "ignoring unmerged: $_->{VALUE}\n"
1201 + for grep { $_->{UNMERGED} } @all_mods;
1202 + @all_mods = grep { !$_->{UNMERGED} } @all_mods;
1203 +
1204 my @mods = grep { !($_->{BINARY}) } @all_mods;
1205 my @them;
1206
t/t3701-add-interactive.sh
+26
@@ -330,4 +330,30 @@ test_expect_success PERL 'split hunk "add -p (edit)"' '
330 ! grep "^+15" actual
331 '
332
333 +test_expect_success 'patch mode ignores unmerged entries' '
334 + git reset --hard &&
335 + test_commit conflict &&
336 + test_commit non-conflict &&
337 + git checkout -b side &&
338 + test_commit side conflict.t &&
339 + git checkout master &&
340 + test_commit master conflict.t &&
341 + test_must_fail git merge side &&
342 + echo changed >non-conflict.t &&
343 + echo y | git add -p >output &&
344 + ! grep a/conflict.t output &&
345 + cat >expected <<-\EOF &&
346 + * Unmerged path conflict.t
347 + diff --git a/non-conflict.t b/non-conflict.t
348 + index f766221..5ea2ed4 100644
349 + --- a/non-conflict.t
350 + +++ b/non-conflict.t
351 + @@ -1 +1 @@
352 + -non-conflict
353 + +changed
354 + EOF
355 + git diff --cached >diff &&
356 + test_cmp expected diff
357 +'
358 +
359 test_done