git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] diff -- file1 file2: do not default to --no-index inside a git repository

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
May 23, 2008, 22:49 UTC
Message-ID
<alpine.DEB.1.00.0805232309350.30431@racer>
In-Reply-To
<7vbq2wsxnk.fsf@gitster.siamese.dyndns.org>

When calling "git diff -- a b", as opposed to "git diff a b" without "--", do not default to --no-index inside a git repository.

For example,
	git mv a b && git diff -- a b
used to have an unexpected result.
Noticed by Catalin Marinas, diagnosed by Linus.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
	On Fri, 23 May 2008, Junio C Hamano wrote:
	> Linus Torvalds <torvalds@linux-foundation.org> writes:
	> 
	> > ... So I would suggest that we make it *much* harder to 
	> > trigger the "make it act like a traditional 'diff'" thing.
	> >
	> > I would suggest that we *not* invoce the traditional 'diff' 
	> > behaviour when:
	> >
	> >  - we're called as "git-diff-files". That's clearly a git 
	> >    thing. Don't try to make it act like an external non-git diff. 
	> >    Only do the special case for plain "git diff" itself.
	I am not so sure about that.
	> >  - even for plain "git diff", make it much harder to trigger 
	> >    non-git behaviour. Don't do it if the files don't exist.
	> >    Don't do it if there is '--' there.
	> >
	> > In fact, maybe we should remove that thing entirely, or 
	> > *require* a flag to enable it (at least if we're in a git
	> > directory).  It's a cute hack, but when the cute hack actually 
	> > makes it impossible to do certain real git operations, it's a 
	> > cute hack that is detrimental.
	> 
	> Very well said.  I've always wanted to rip that hack out of the 
	> normal "git diff with two pathspec parameters" codepath.
	Yes, I see how that cute hack does the wrong thing here.
	However, it is pretty convenient to be able to say "git diff a b" 
	and have it fall back to --no-index if either a or b is not in the 
	index.  Most of the time, I would actually expect it to be the 
	right thing, too.
	Hey, but if you disagree, just rip it out.  Actually, I did that 
	already, and will reply with that alternative patch.
 diff-lib.c            |    4 ++--
 t/t4002-diff-basic.sh |   31 +++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 2 deletions(-)
diff --git a/diff-lib.c b/diff-lib.c
index fe2ccec..b1daad6 100644
--- a/diff-lib.c
+++ b/diff-lib.c
@@ -257,8 +257,8 @@ int setup_diff_no_index(struct rev_info *revs,
 		if (argv[i][0] != '-' || argv[i][1] == '\0')
 			break;
 		else if (!strcmp(argv[i], "--")) {
-			i++;
-			break;
+			revs->max_count = -2;
+			return -1;
 		} else if (i < argc - 3 && !strcmp(argv[i], "--no-index")) {
 			i = argc - 3;
 			DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
diff --git a/t/t4002-diff-basic.sh b/t/t4002-diff-basic.sh
index a4cfde6..678c4a5 100755
--- a/t/t4002-diff-basic.sh
+++ b/t/t4002-diff-basic.sh
@@ -244,4 +244,35 @@ test_expect_success \
     git diff-tree -r -R $tree_A $tree_B >.test-b &&
     cmp -s .test-a .test-b'
 
+cat > expect << EOF
+diff --git a/AA b/AA
+index ccba72a..6aa2b53 100644
+--- a/AA
++++ b/AA
+@@ -1 +1 @@
+-This is added AA in the branch A.
++This is added AA in the branch B.
+EOF
+
+test_expect_success '"diff -- AA DM" does not imply --no-index' '
+
+	git diff -- AA DM > output &&
+	cmp -s output expect
+
+'
+
+cat > expect << EOF
+diff --git a/AA b/DM
+index 6aa2b53..3c4d8de 100644
+--- a/AA
++++ b/DM
+@@ -1 +1 @@
+-This is added AA in the branch B.
++This is modified DM in the branch B.
+EOF
+
+test_expect_success '"diff AA DM" implies --no-index' '
+
+'
+
 test_done
-- 
1.5.5.1.553.ga1dd7.dirty
Previous: Linus TorvaldsNext: Johannes Schindelin
Message 4 of 11 in “git diff-files weirdness (bug?)”
  1. Catalin MarinasMay 23, 2008
  2. Linus TorvaldsMay 23, 2008
  3. Linus TorvaldsMay 23, 2008
  4. diff -- file1 file2: do not default to --no-index inside a git repositoryJohannes Schindelin, May 23, 2008
  5. diff file1 file2: do not default to --no-index inside a git repositoryJohannes Schindelin, May 23, 2008
  6. Junio C HamanoMay 24, 2008
  7. 1/3 tests: do not use implicit "git diff --no-index"Junio C Hamano, May 24, 2008
  8. 2/3 diff-files: do not play --no-index gamesJunio C Hamano, May 24, 2008
  9. 3/3 "git diff": do not ignore index without --no-indexJunio C Hamano, May 24, 2008
  10. Junio C HamanoMay 24, 2008
  11. Junio C HamanoMay 24, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.