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

bug with git-diff --quiet

From
ITIWAMOTO Toshihiro <iwamoto@valinux.co.jp>
Date
Jan 23, 2014, 02:45 UTC
Message-ID
<20140123024525.B726248918@mail.valinux.co.jp>

I found "git-diff --quiet" returns a zero exit status even if there's a change. The following sequence reproduces the bug:

  $ mkdir foo
  $ cd foo
  $ git init
  $ echo a > a.txt
  $ echo b >b.txt
  $ git add ?.txt
  $ git commit
  $ echo b >> b.txt
  $ touch a.txt
  $ git diff --quiet; echo $?
  
Interestingly, if you issue "git-diff --quiet" again, it returns the
expected exit status 1.

The problem is in the optimization code in run_diff_files(). The function finds a.txt has different stat(2) data from .git/index and calls diff_change(), which sets DIFF_OPT_HAS_CHANGES. As the flag makes diff_can_quit_early() return 1, run_diff_files()'s loop finishes without calling diff_change() for b.txt.

Then, diffcore_std() examines diff_queued_diff and clears DIFF_OPT_HAS_CHANGES, because a.txt is unchanged. This is how a change in b.txt is ignored by git-diff --quiet.

Here's a obvious fix for this bug, but I think you can find a better fix. Thanks in advance.

diff --git a/diff-lib.c b/diff-lib.c
index 346cac6..0b8c58d 100644
--- a/diff-lib.c
+++ b/diff-lib.c
@@ -105,9 +105,6 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
 		int changed;
 		unsigned dirty_submodule = 0;
 
-		if (diff_can_quit_early(&revs->diffopt))
-			break;
-
 		if (!ce_path_match(ce, &revs->prune_data))
 			continue;
 
--
IWAMOTO Toshihiro
Next: Duy Nguyen
Message 1 of 12 in “bug with git-diff --quiet”
  1. IWAMOTO ToshihiroJan 23, 2014
  2. Duy NguyenJan 25, 2014
  3. 1/3 Move diffcore_skip_stat_unmatch core logic out for reuse laterNguyễn Thái Ngọc Duy, Jan 25, 2014
  4. 2/3 diff: do not quit early on stat-dirty filesNguyễn Thái Ngọc Duy, Jan 25, 2014
  5. 3/3 diff: turn off skip_stat_unmatch on "diff --cached"Nguyễn Thái Ngọc Duy, Jan 25, 2014
  6. 3/3 diff: turn skip_stat_unmatch on selectivelyNguyễn Thái Ngọc Duy, Jan 27, 2014
  7. Junio C HamanoJan 27, 2014
  8. Junio C HamanoJan 28, 2014
  9. Duy NguyenJan 28, 2014
  10. Junio C HamanoJan 29, 2014
  11. Duy NguyenJan 30, 2014
  12. Junio C HamanoJan 31, 2014

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.