Skip to content

Commit ec9709c

Browse files
committed
Merge branch 'jc/diff-no-index-in-subdir' into jch
* jc/diff-no-index-in-subdir: diff: --no-index should ignore the worktree
2 parents bc82184 + e1d3d61 commit ec9709c

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

builtin/diff.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,21 @@ int cmd_diff(int argc,
487487

488488
init_diff_ui_defaults();
489489
repo_config(the_repository, git_diff_ui_config, NULL);
490+
491+
/*
492+
* If we are ignoring the fact that our current directory may
493+
* be part of a working tree controlled by a Git repository to
494+
* pretend to be a "better GNU diff", we should undo the
495+
* effect of the setup code that did a chdir() to the top of
496+
* the working tree. Where we came from is recorded in the
497+
* prefix.
498+
*/
499+
if (no_index && prefix) {
500+
if (chdir(prefix))
501+
die(_("cannot come back to cwd"));
502+
prefix = NULL;
503+
}
504+
490505
prefix = precompose_argv_prefix(argc, argv, prefix);
491506

492507
repo_init_revisions(the_repository, &rev, prefix);

t/t4053-diff-no-index.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ test_expect_success 'git diff --no-index directories' '
2626
test_line_count = 14 cnt
2727
'
2828

29+
test_expect_success 'git diff --no-index with -' '
30+
cat >expect <<-\EOF &&
31+
diff --git a/- b/-
32+
new file mode 100644
33+
--- /dev/null
34+
+++ b/-
35+
@@ -0,0 +1 @@
36+
+frotz
37+
EOF
38+
(
39+
cd a &&
40+
echo frotz |
41+
test_expect_code 1 git diff --no-index /dev/null - >../actual
42+
) &&
43+
test_cmp expect actual
44+
'
45+
2946
test_expect_success 'git diff --no-index relative path outside repo' '
3047
(
3148
cd repo &&

0 commit comments

Comments
 (0)