Skip to content

Commit ae6d5c1

Browse files
jlehmanngitster
authored andcommitted
Refactor dirty submodule detection in diff-lib.c
Moving duplicated code into the new function match_stat_with_submodule(). Replacing the implicit activation of detailed checks for the dirtiness of submodules when DIFF_FORMAT_PATCH was selected with explicitly setting the recently added DIFF_OPT_DIRTY_SUBMODULES option in diff_setup_done(). Signed-off-by: Jens Lehmann <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9297f77 commit ae6d5c1

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

diff-lib.c

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ static int check_removed(const struct cache_entry *ce, struct stat *st)
5555
return 0;
5656
}
5757

58+
/*
59+
* Has a file changed or has a submodule new commits or a dirty work tree?
60+
*
61+
* Return 1 when changes are detected, 0 otherwise. If the DIRTY_SUBMODULES
62+
* option is set, the caller does not only want to know if a submodule is
63+
* modified at all but wants to know all the conditions that are met (new
64+
* commits, untracked content and/or modified content).
65+
*/
66+
static int match_stat_with_submodule(struct diff_options *diffopt,
67+
struct cache_entry *ce, struct stat *st,
68+
unsigned ce_option, unsigned *dirty_submodule)
69+
{
70+
int changed = ce_match_stat(ce, st, ce_option);
71+
if (S_ISGITLINK(ce->ce_mode)
72+
&& !DIFF_OPT_TST(diffopt, IGNORE_SUBMODULES)
73+
&& (!changed || DIFF_OPT_TST(diffopt, DIRTY_SUBMODULES))) {
74+
*dirty_submodule = is_submodule_modified(ce->name);
75+
if (*dirty_submodule)
76+
changed = 1;
77+
}
78+
return changed;
79+
}
80+
5881
int run_diff_files(struct rev_info *revs, unsigned int option)
5982
{
6083
int entries, i;
@@ -177,15 +200,8 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
177200
ce->sha1, ce->name, 0);
178201
continue;
179202
}
180-
changed = ce_match_stat(ce, &st, ce_option);
181-
if (S_ISGITLINK(ce->ce_mode)
182-
&& !DIFF_OPT_TST(&revs->diffopt, IGNORE_SUBMODULES)
183-
&& (!changed || (revs->diffopt.output_format & DIFF_FORMAT_PATCH)
184-
|| DIFF_OPT_TST(&revs->diffopt, DIRTY_SUBMODULES))) {
185-
dirty_submodule = is_submodule_modified(ce->name);
186-
if (dirty_submodule)
187-
changed = 1;
188-
}
203+
changed = match_stat_with_submodule(&revs->diffopt, ce, &st,
204+
ce_option, &dirty_submodule);
189205
if (!changed) {
190206
ce_mark_uptodate(ce);
191207
if (!DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER))
@@ -241,15 +257,8 @@ static int get_stat_data(struct cache_entry *ce,
241257
}
242258
return -1;
243259
}
244-
changed = ce_match_stat(ce, &st, 0);
245-
if (S_ISGITLINK(ce->ce_mode)
246-
&& !DIFF_OPT_TST(diffopt, IGNORE_SUBMODULES)
247-
&& (!changed || (diffopt->output_format & DIFF_FORMAT_PATCH)
248-
|| DIFF_OPT_TST(diffopt, DIRTY_SUBMODULES))) {
249-
*dirty_submodule = is_submodule_modified(ce->name);
250-
if (*dirty_submodule)
251-
changed = 1;
252-
}
260+
changed = match_stat_with_submodule(diffopt, ce, &st,
261+
0, dirty_submodule);
253262
if (changed) {
254263
mode = ce_mode_from_stat(ce, st.st_mode);
255264
sha1 = null_sha1;

diff.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,6 +2628,12 @@ int diff_setup_done(struct diff_options *options)
26282628
*/
26292629
if (options->pickaxe)
26302630
DIFF_OPT_SET(options, RECURSIVE);
2631+
/*
2632+
* When patches are generated, submodules diffed against the work tree
2633+
* must be checked for dirtiness too so it can be shown in the output
2634+
*/
2635+
if (options->output_format & DIFF_FORMAT_PATCH)
2636+
DIFF_OPT_SET(options, DIRTY_SUBMODULES);
26312637

26322638
if (options->detect_rename && options->rename_limit < 0)
26332639
options->rename_limit = diff_rename_limit_default;

0 commit comments

Comments
 (0)