git add -u: die on unmatched pathspec
If a pathspec is supplied to 'git add -u' and no path matches the pattern, fail with an approriate error message and exit code. Tested-by: Chris Packham <judge.packham@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Feb 9, 2010 at 17:30 UTC
81f45e7dc4e930ffc17dce3e377e6adc3eb3d8de
1 file changed
+29
-9
builtin-add.c
+29
-9
@@ -41,7 +41,19 @@ static void fill_pathspec_matches(const char **pathspec, char *seen, int specs)
41
}
42
}
43
44
-static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
44
+static char *find_used_pathspec(const char **pathspec)
45
+{
46
+ char *seen;
47
+ int i;
48
+
49
+ for (i = 0; pathspec[i]; i++)
50
+ ; /* just counting */
51
+ seen = xcalloc(i, 1);
52
+ fill_pathspec_matches(pathspec, seen, i);
53
+ return seen;
54
+}
55
+
56
+static char *prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
57
{
58
char *seen;
59
int i, specs;
@@ -61,13 +73,7 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p
73
}
74
dir->nr = dst - dir->entries;
75
fill_pathspec_matches(pathspec, seen, specs);
64
-
65
- for (i = 0; i < specs; i++) {
66
- if (!seen[i] && pathspec[i][0] && !file_exists(pathspec[i]))
67
- die("pathspec '%s' did not match any files",
68
- pathspec[i]);
69
- }
70
- free(seen);
76
+ return seen;
77
}
78
79
static void treat_gitlinks(const char **pathspec)
@@ -283,6 +289,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
289
int flags;
290
int add_new_files;
291
int require_pathspec;
292
+ char *seen = NULL;
293
294
git_config(add_config, NULL);
295
@@ -342,7 +349,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
349
/* This picks up the paths that are not tracked */
350
baselen = fill_directory(&dir, pathspec);
351
if (pathspec)
345
- prune_directory(&dir, pathspec, baselen);
352
+ seen = prune_directory(&dir, pathspec, baselen);
353
}
354
355
if (refresh_only) {
@@ -350,6 +357,19 @@ int cmd_add(int argc, const char **argv, const char *prefix)
357
goto finish;
358
}
359
360
+ if (pathspec) {
361
+ int i;
362
+ if (!seen)
363
+ seen = find_used_pathspec(pathspec);
364
+ for (i = 0; pathspec[i]; i++) {
365
+ if (!seen[i] && pathspec[i][0]
366
+ && !file_exists(pathspec[i]))
367
+ die("pathspec '%s' did not match any files",
368
+ pathspec[i]);
369
+ }
370
+ free(seen);
371
+ }
372
+
373
exit_status |= add_files_to_cache(prefix, pathspec, flags);
374
375
if (add_new_files)