add -u: only show pathless 'add -u' warning when changes exist outside cwd

A common workflow in large projects is to chdir into a subdirectory of interest and only do work there: cd src vi foo.c make test git add -u git commit The upcoming change to 'git add -u' behavior would not affect such a workflow: when the only changes present are in the current directory, 'git add -u' will add all changes, and whether that happens via an implicit "." or implicit ":/" parameter is an unimportant implementation detail. The warning about use of 'git add -u' with no pathspec is annoying because it seemingly serves no purpose in this case. So suppress the warning unless there are changes outside the cwd that are not being added. A previous version of this patch ran two I/O-intensive diff-files passes: one to find changes outside the cwd, and another to find changes to add to the index within the cwd. This version runs one full-tree diff and decides for each change whether to add it or warn and suppress it in update_callback. As a result, even on very large repositories "git add -u" will not be significantly slower than the future default behavior ("git add -u :/"), and the slowdown relative to "git add -u ." should be a useful clue to users of such repositories to get into the habit of explicitly passing '.'. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Acked-by: Jeff King <peff@peff.net> Improved-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Mar 19, 2013 at 15:50 UTC 71c7b0538f8f037e49a04f415a6fbf0bba2c85ad
2 files changed +38 -4
builtin/add.c
+37 -4
@@ -26,6 +26,8 @@ static int take_worktree_changes;
26 struct update_callback_data {
27 int flags;
28 int add_errors;
29 + const char *implicit_dot;
30 + size_t implicit_dot_len;
31 };
32
33 static const char *option_with_implicit_dot;
@@ -96,10 +98,26 @@ static void update_callback(struct diff_queue_struct *q,
98 {
99 int i;
100 struct update_callback_data *data = cbdata;
101 + const char *implicit_dot = data->implicit_dot;
102 + size_t implicit_dot_len = data->implicit_dot_len;
103
104 for (i = 0; i < q->nr; i++) {
105 struct diff_filepair *p = q->queue[i];
106 const char *path = p->one->path;
107 + /*
108 + * Check if "git add -A" or "git add -u" was run from a
109 + * subdirectory with a modified file outside that directory,
110 + * and warn if so.
111 + *
112 + * "git add -u" will behave like "git add -u :/" instead of
113 + * "git add -u ." in the future. This warning prepares for
114 + * that change.
115 + */
116 + if (implicit_dot &&
117 + strncmp_icase(path, implicit_dot, implicit_dot_len)) {
118 + warn_pathless_add();
119 + continue;
120 + }
121 switch (fix_unmerged_status(p, data)) {
122 default:
123 die(_("unexpected diff status %c"), p->status);
@@ -127,13 +145,25 @@ int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
145 {
146 struct update_callback_data data;
147 struct rev_info rev;
148 +
149 + memset(&data, 0, sizeof(data));
150 + data.flags = flags & ~ADD_CACHE_IMPLICIT_DOT;
151 + if ((flags & ADD_CACHE_IMPLICIT_DOT) && prefix) {
152 + /*
153 + * Check for modified files throughout the worktree so
154 + * update_callback has a chance to warn about changes
155 + * outside the cwd.
156 + */
157 + data.implicit_dot = prefix;
158 + data.implicit_dot_len = strlen(prefix);
159 + pathspec = NULL;
160 + }
161 +
162 init_revisions(&rev, prefix);
163 setup_revisions(0, NULL, &rev, NULL);
164 init_pathspec(&rev.prune_data, pathspec);
165 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
166 rev.diffopt.format_callback = update_callback;
135 - data.flags = flags;
136 - data.add_errors = 0;
167 rev.diffopt.format_callback_data = &data;
168 rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
169 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
@@ -373,6 +403,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
403 int add_new_files;
404 int require_pathspec;
405 char *seen = NULL;
406 + int implicit_dot = 0;
407
408 git_config(add_config, NULL);
409
@@ -402,10 +433,11 @@ int cmd_add(int argc, const char **argv, const char *prefix)
433 }
434 if (option_with_implicit_dot && !argc) {
435 static const char *here[2] = { ".", NULL };
405 - if (prefix)
436 + if (prefix && addremove)
437 warn_pathless_add();
438 argc = 1;
439 argv = here;
440 + implicit_dot = 1;
441 }
442
443 add_new_files = !take_worktree_changes && !refresh_only;
@@ -418,7 +450,8 @@ int cmd_add(int argc, const char **argv, const char *prefix)
450 (intent_to_add ? ADD_CACHE_INTENT : 0) |
451 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
452 (!(addremove || take_worktree_changes)
421 - ? ADD_CACHE_IGNORE_REMOVAL : 0));
453 + ? ADD_CACHE_IGNORE_REMOVAL : 0)) |
454 + (implicit_dot ? ADD_CACHE_IMPLICIT_DOT : 0);
455
456 if (require_pathspec && argc == 0) {
457 fprintf(stderr, _("Nothing specified, nothing added.\n"));
cache.h
+1
@@ -459,6 +459,7 @@ extern int remove_file_from_index(struct index_state *, const char *path);
459 #define ADD_CACHE_IGNORE_ERRORS 4
460 #define ADD_CACHE_IGNORE_REMOVAL 8
461 #define ADD_CACHE_INTENT 16
462 +#define ADD_CACHE_IMPLICIT_DOT 32 /* internal to "git add -u/-A" */
463 extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
464 extern int add_file_to_index(struct index_state *, const char *path, int flags);
465 extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh);