Skip to content

Commit 89b4183

Browse files
peffgitster
authored andcommitted
stash: pass --no-color to diff plumbing child processes
After a partial stash, we may clear out the working tree by capturing the output of diff-tree and piping it into git-apply (and likewise we may use diff-index to restore the index). So we most definitely do not want color diff output from that diff-tree process. And it normally would not produce any, since its stdout is not going to a tty, and the default value of color.ui is "auto". However, if GIT_PAGER_IN_USE is set in the environment, that overrides the tty check, and we'll produce a colorized diff that chokes git-apply: $ echo y | GIT_PAGER_IN_USE=1 git stash -p [...] Saved working directory and index state WIP on main: 4f2e2bb foo error: No valid patches in input (allow with "--allow-empty") Cannot remove worktree changes Setting this variable is a relatively silly thing to do, and not something most users would run into. But we sometimes do it in our tests to stimulate color. And it is a user-visible bug, so let's fix it rather than work around it in the tests. The root issue here is that diff-tree (and other diff plumbing) should probably not ever produce color by default. It does so not by parsing color.ui, but because of the baked-in "auto" default from 4c7f181 (make color.ui default to 'auto', 2013-06-10). But changing that is risky; we've had discussions back and forth on the topic over the years. E.g.: https://lore.kernel.org/git/[email protected]/. So let's accept that as the status quo for now and protect ourselves by passing --no-color to the child processes. This is the same thing we did for add-interactive itself in 1c6ffb5 (add--interactive.perl: specify --no-color explicitly, 2020-09-07). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c44beea commit 89b4183

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

builtin/stash.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
377377
* however it should be done together with apply_cached.
378378
*/
379379
cp.git_cmd = 1;
380-
strvec_pushl(&cp.args, "diff-tree", "--binary", NULL);
380+
strvec_pushl(&cp.args, "diff-tree", "--binary", "--no-color", NULL);
381381
strvec_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
382382

383383
return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
@@ -1283,6 +1283,7 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
12831283

12841284
cp_diff_tree.git_cmd = 1;
12851285
strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "--binary",
1286+
"--no-color",
12861287
"-U1", "HEAD", oid_to_hex(&info->w_tree), "--", NULL);
12871288
if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
12881289
ret = -1;
@@ -1345,6 +1346,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
13451346

13461347
cp_diff_tree.git_cmd = 1;
13471348
strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD",
1349+
"--no-color",
13481350
oid_to_hex(&info->w_tree), "--", NULL);
13491351
if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
13501352
ret = -1;
@@ -1719,6 +1721,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
17191721

17201722
cp_diff.git_cmd = 1;
17211723
strvec_pushl(&cp_diff.args, "diff-index", "-p",
1724+
"--no-color",
17221725
"--cached", "--binary", "HEAD", "--",
17231726
NULL);
17241727
add_pathspecs(&cp_diff.args, ps);

t/t3904-stash-patch.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,23 @@ test_expect_success 'stash -p with split hunk' '
107107
! grep "added line 2" test
108108
'
109109

110+
test_expect_success 'stash -p not confused by GIT_PAGER_IN_USE' '
111+
echo to-stash >test &&
112+
# Set both GIT_PAGER_IN_USE and TERM. Our goal is to entice any
113+
# diff subprocesses into thinking that they could output
114+
# color, even though their stdout is not going into a tty.
115+
echo y |
116+
GIT_PAGER_IN_USE=1 TERM=vt100 git stash -p &&
117+
git diff --exit-code
118+
'
119+
120+
test_expect_success 'index push not confused by GIT_PAGER_IN_USE' '
121+
echo index >test &&
122+
git add test &&
123+
echo working-tree >test &&
124+
# As above, we try to entice the child diff into using color.
125+
GIT_PAGER_IN_USE=1 TERM=vt100 git stash push test &&
126+
git diff --exit-code
127+
'
128+
110129
test_done

0 commit comments

Comments
 (0)