Skip to content

Commit 0a10167

Browse files
dschogitster
authored andcommitted
add -p: ignore dirty submodules
Thanks to always running `diff-index` and `diff-files` with the `--numstat` option (the latter with `--ignore-submodules=dirty`) before even generating any real diff to parse, the Perl version of `git add -p` simply ignored dirty submodules and does not even offer them up for staging. However, the built-in variant did not use that flag because it tries to run only one `diff` command, skipping the unneeded `diff-index`/`diff-files` invocation of the Perl variant and therefore only faithfully recapitulates what the Perl code does once it _does_ generate and parse the real diff. This causes a problem when running the built-in `add -p` with `diff-so-fancy` because that diff colorizer always inserts an empty line before the diff header to ensure that it produces 4 lines as expected by `git add -p` (the equivalent of the non-colorized `diff`, `index`, `---` and `+++` lines). But `git diff-files` does not produce any `index` line for dirty submodules. The underlying problem is not even the discrepancy in lines, but that `git add -p` presents diffs for dirty submodules: there is nothing that _can_ be staged for those. Let's fix that bug, and teach the built-in `add -p` to ignore dirty submodules, too. This _incidentally_ also fixes the `diff-so-fancy` problem ;-) Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fd3f7f6 commit 0a10167

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

add-patch.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
419419
}
420420
color_arg_index = args.nr;
421421
/* Use `--no-color` explicitly, just in case `diff.color = always`. */
422-
strvec_pushl(&args, "--no-color", "-p", "--", NULL);
422+
strvec_pushl(&args, "--no-color", "--ignore-submodules=dirty", "-p",
423+
"--", NULL);
423424
for (i = 0; i < ps->nr; i++)
424425
strvec_push(&args, ps->items[i].original);
425426

t/t3701-add-interactive.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,18 @@ test_expect_success 'status ignores dirty submodules (except HEAD)' '
942942
! grep dirty-otherwise output
943943
'
944944

945+
test_expect_success 'handle submodules' '
946+
echo 123 >>for-submodules/dirty-otherwise/initial.t &&
947+
948+
force_color git -C for-submodules add -p dirty-otherwise >output 2>&1 &&
949+
grep "No changes" output &&
950+
951+
force_color git -C for-submodules add -p dirty-head >output 2>&1 <y &&
952+
git -C for-submodules ls-files --stage dirty-head >actual &&
953+
rev="$(git -C for-submodules/dirty-head rev-parse HEAD)" &&
954+
grep "$rev" actual
955+
'
956+
945957
test_expect_success 'set up pathological context' '
946958
git reset --hard &&
947959
test_write_lines a a a a a a a a a a a >a &&

0 commit comments

Comments
 (0)