Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit f34b205

Browse files
pcloudsgitster
authored andcommitted
diff: do not quit early on stat-dirty files
When QUICK is set (i.e. with --quiet) we try to do as little work as possible, stopping after seeing the first change. stat-dirty is considered a "change" but it may turn out not, if no actual content is changed. The actual content test is performed too late in the process and the shortcut may be taken prematurely, leading to incorrect return code. Assume we do "git diff --quiet". If we have a stat-dirty file "a" and a really dirty file "b". We break the loop in run_diff_files() and stop after "a" because we have got a "change". Later in diffcore_skip_stat_unmatch() we find out "a" is actually not changed. But there's nothing else in the diff queue, we incorrectly declare "no change", ignoring the fact that "b" is changed. This also happens to "git diff --quiet HEAD" when it hits diff_can_quit_early() in oneway_diff(). This patch does the content test earlier in order to keep going if "a" is unchanged. The test result is cached so that when diffcore_skip_stat_unmatch() is done in the end, we spend no cycles on re-testing "a". Reported-by: IWAMOTO Toshihiro <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fceb907 commit f34b205

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

diff.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4594,6 +4594,11 @@ static int diff_filespec_is_identical(struct diff_filespec *one,
45944594

45954595
static int diff_filespec_check_stat_unmatch(struct diff_filepair *p)
45964596
{
4597+
if (p->done_skip_stat_unmatch)
4598+
return p->skip_stat_unmatch_result;
4599+
4600+
p->done_skip_stat_unmatch = 1;
4601+
p->skip_stat_unmatch_result = 0;
45974602
/*
45984603
* 1. Entries that come from stat info dirtiness
45994604
* always have both sides (iow, not create/delete),
@@ -4615,8 +4620,8 @@ static int diff_filespec_check_stat_unmatch(struct diff_filepair *p)
46154620
diff_populate_filespec(p->two, 1) ||
46164621
(p->one->size != p->two->size) ||
46174622
!diff_filespec_is_identical(p->one, p->two)) /* (2) */
4618-
return 1;
4619-
return 0;
4623+
p->skip_stat_unmatch_result = 1;
4624+
return p->skip_stat_unmatch_result;
46204625
}
46214626

46224627
static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
@@ -4792,6 +4797,7 @@ void diff_change(struct diff_options *options,
47924797
unsigned old_dirty_submodule, unsigned new_dirty_submodule)
47934798
{
47944799
struct diff_filespec *one, *two;
4800+
struct diff_filepair *p;
47954801

47964802
if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) &&
47974803
is_submodule_ignored(concatpath, options))
@@ -4818,10 +4824,16 @@ void diff_change(struct diff_options *options,
48184824
fill_filespec(two, new_sha1, new_sha1_valid, new_mode);
48194825
one->dirty_submodule = old_dirty_submodule;
48204826
two->dirty_submodule = new_dirty_submodule;
4827+
p = diff_queue(&diff_queued_diff, one, two);
48214828

4822-
diff_queue(&diff_queued_diff, one, two);
4823-
if (!DIFF_OPT_TST(options, DIFF_FROM_CONTENTS))
4824-
DIFF_OPT_SET(options, HAS_CHANGES);
4829+
if (DIFF_OPT_TST(options, DIFF_FROM_CONTENTS))
4830+
return;
4831+
4832+
if (DIFF_OPT_TST(options, QUICK) && options->skip_stat_unmatch &&
4833+
!diff_filespec_check_stat_unmatch(p))
4834+
return;
4835+
4836+
DIFF_OPT_SET(options, HAS_CHANGES);
48254837
}
48264838

48274839
struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path)

diffcore.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ struct diff_filepair {
7070
unsigned broken_pair : 1;
7171
unsigned renamed_pair : 1;
7272
unsigned is_unmerged : 1;
73+
unsigned done_skip_stat_unmatch : 1;
74+
unsigned skip_stat_unmatch_result : 1;
7375
};
7476
#define DIFF_PAIR_UNMERGED(p) ((p)->is_unmerged)
7577

t/t4035-diff-quiet.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,10 @@ test_expect_success 'git diff --ignore-all-space, both files outside repo' '
148148
)
149149
'
150150

151+
test_expect_success 'git diff --quiet ignores stat-change only entries' '
152+
test-chmtime +10 a &&
153+
echo modified >>b &&
154+
test_expect_code 1 git diff --quiet
155+
'
156+
151157
test_done

0 commit comments

Comments
 (0)