Skip to content

Commit aecbf91

Browse files
committed
git-diff: resurrect the traditional empty "diff --git" behaviour
The warning message to suggest "Consider running git-status" from "git-diff" that we experimented with during the 1.5.3 cycle turns out to be a bad idea. It robbed cache-dirty information from people who valued it, while still asking users to run "update-index --refresh". It was hoped that the new behaviour would at least have some educational value, but not showing the cache-dirty paths like before meant that the user would not even know easily which paths were cache-dirty, and it made the need to refresh the index look like even more unnecessary chore. This commit reinstates the traditional behaviour, but with a twist. By default, the empty "diff --git" output is totally squelched out from "git diff" output. At the end of the command, it automatically runs "update-index --refresh" as needed, without even bothering the user. In other words, people who do not care about the cache-dirtyness do not even have to see the warning. The traditional behaviour to see the stat-dirty output and to bypassing the overhead of content comparison can be specified by setting the configuration variable diff.autorefreshindex to false. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 18e32b5 commit aecbf91

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

Documentation/config.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,16 @@ color.status.<slot>::
396396
commit.template::
397397
Specify a file to use as the template for new commit messages.
398398

399+
diff.autorefreshindex::
400+
When using `git diff` to compare with work tree
401+
files, do not consider stat-only change as changed.
402+
Instead, silently run `git update-index --refresh` to
403+
update the cached stat information for paths whose
404+
contents in the work tree match the contents in the
405+
index. This option defaults to true. Note that this
406+
affects only `git diff` Porcelain, and not lower level
407+
`diff` commands, such as `git diff-files`.
408+
399409
diff.renameLimit::
400410
The number of files to consider when performing the copy/rename
401411
detection; equivalent to the git diff option '-l'.

builtin-diff.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,30 @@ void add_head(struct rev_info *revs)
188188
add_pending_object(revs, obj, "HEAD");
189189
}
190190

191+
static void refresh_index_quietly(void)
192+
{
193+
struct lock_file *lock_file;
194+
int fd;
195+
196+
lock_file = xcalloc(1, sizeof(struct lock_file));
197+
fd = hold_locked_index(lock_file, 0);
198+
if (fd < 0)
199+
return;
200+
discard_cache();
201+
read_cache();
202+
refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
203+
if (active_cache_changed) {
204+
if (write_cache(fd, active_cache, active_nr) ||
205+
close(fd) ||
206+
commit_locked_index(lock_file))
207+
; /*
208+
* silently ignore it -- we haven't mucked
209+
* with the real index.
210+
*/
211+
}
212+
rollback_lock_file(lock_file);
213+
}
214+
191215
int cmd_diff(int argc, const char **argv, const char *prefix)
192216
{
193217
int i;
@@ -222,7 +246,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
222246
prefix = setup_git_directory_gently(&nongit);
223247
git_config(git_diff_ui_config);
224248
init_revisions(&rev, prefix);
225-
rev.diffopt.skip_stat_unmatch = 1;
249+
rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
226250

227251
if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
228252
argc = 0;
@@ -346,11 +370,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
346370
if (rev.diffopt.exit_with_status)
347371
result = rev.diffopt.has_changes;
348372

349-
if ((rev.diffopt.output_format & DIFF_FORMAT_PATCH)
350-
&& (1 < rev.diffopt.skip_stat_unmatch))
351-
printf("Warning: %d path%s touched but unmodified. "
352-
"Consider running git-status.\n",
353-
rev.diffopt.skip_stat_unmatch - 1,
354-
rev.diffopt.skip_stat_unmatch == 2 ? "" : "s");
373+
if (1 < rev.diffopt.skip_stat_unmatch)
374+
refresh_index_quietly();
355375
return result;
356376
}

cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,9 @@ extern char *convert_to_git(const char *path, const char *src, unsigned long *si
594594
extern char *convert_to_working_tree(const char *path, const char *src, unsigned long *sizep);
595595
extern void *convert_sha1_file(const char *path, const unsigned char *sha1, unsigned int mode, enum object_type *type, unsigned long *size);
596596

597+
/* diff.c */
598+
extern int diff_auto_refresh_index;
599+
597600
/* match-trees.c */
598601
void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int);
599602

diff.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
static int diff_detect_rename_default;
2020
static int diff_rename_limit_default = -1;
2121
static int diff_use_color_default;
22+
int diff_auto_refresh_index = 1;
2223

2324
static char diff_colors[][COLOR_MAXLEN] = {
2425
"\033[m", /* reset */
@@ -166,6 +167,10 @@ int git_diff_ui_config(const char *var, const char *value)
166167
diff_detect_rename_default = DIFF_DETECT_RENAME;
167168
return 0;
168169
}
170+
if (!strcmp(var, "diff.autorefreshindex")) {
171+
diff_auto_refresh_index = git_config_bool(var, value);
172+
return 0;
173+
}
169174
if (!prefixcmp(var, "diff.")) {
170175
const char *ep = strrchr(var, '.');
171176

0 commit comments

Comments
 (0)