@matheusbsbs / matheus-bagliano / commits / aecbf914c4
Fork 0

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>

Junio C Hamano committed Aug 31, 2007 at 13:13 UTC aecbf914c43ab76e055fa8a25b12456ae74d91bc
4 files changed +45 -7
Documentation/config.txt
+10
@@ -396,6 +396,16 @@ color.status.<slot>::
396 commit.template::
397 Specify a file to use as the template for new commit messages.
398
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 +
409 diff.renameLimit::
410 The number of files to consider when performing the copy/rename
411 detection; equivalent to the git diff option '-l'.
builtin-diff.c
+27 -7
@@ -188,6 +188,30 @@ void add_head(struct rev_info *revs)
188 add_pending_object(revs, obj, "HEAD");
189 }
190
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 +
215 int cmd_diff(int argc, const char **argv, const char *prefix)
216 {
217 int i;
@@ -222,7 +246,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
246 prefix = setup_git_directory_gently(&nongit);
247 git_config(git_diff_ui_config);
248 init_revisions(&rev, prefix);
225 - rev.diffopt.skip_stat_unmatch = 1;
249 + rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
250
251 if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
252 argc = 0;
@@ -346,11 +370,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
370 if (rev.diffopt.exit_with_status)
371 result = rev.diffopt.has_changes;
372
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();
375 return result;
376 }
cache.h
+3
@@ -594,6 +594,9 @@ extern char *convert_to_git(const char *path, const char *src, unsigned long *si
594 extern char *convert_to_working_tree(const char *path, const char *src, unsigned long *sizep);
595 extern void *convert_sha1_file(const char *path, const unsigned char *sha1, unsigned int mode, enum object_type *type, unsigned long *size);
596
597 +/* diff.c */
598 +extern int diff_auto_refresh_index;
599 +
600 /* match-trees.c */
601 void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int);
602
diff.c
+5
@@ -19,6 +19,7 @@
19 static int diff_detect_rename_default;
20 static int diff_rename_limit_default = -1;
21 static int diff_use_color_default;
22 +int diff_auto_refresh_index = 1;
23
24 static char diff_colors[][COLOR_MAXLEN] = {
25 "\033[m", /* reset */
@@ -166,6 +167,10 @@ int git_diff_ui_config(const char *var, const char *value)
167 diff_detect_rename_default = DIFF_DETECT_RENAME;
168 return 0;
169 }
170 + if (!strcmp(var, "diff.autorefreshindex")) {
171 + diff_auto_refresh_index = git_config_bool(var, value);
172 + return 0;
173 + }
174 if (!prefixcmp(var, "diff.")) {
175 const char *ep = strrchr(var, '.');
176