Skip to content

Commit 81f45e7

Browse files
committed
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 78d553b commit 81f45e7

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

builtin-add.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,19 @@ static void fill_pathspec_matches(const char **pathspec, char *seen, int specs)
4141
}
4242
}
4343

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)
4557
{
4658
char *seen;
4759
int i, specs;
@@ -61,13 +73,7 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p
6173
}
6274
dir->nr = dst - dir->entries;
6375
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;
7177
}
7278

7379
static void treat_gitlinks(const char **pathspec)
@@ -283,6 +289,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
283289
int flags;
284290
int add_new_files;
285291
int require_pathspec;
292+
char *seen = NULL;
286293

287294
git_config(add_config, NULL);
288295

@@ -342,14 +349,27 @@ int cmd_add(int argc, const char **argv, const char *prefix)
342349
/* This picks up the paths that are not tracked */
343350
baselen = fill_directory(&dir, pathspec);
344351
if (pathspec)
345-
prune_directory(&dir, pathspec, baselen);
352+
seen = prune_directory(&dir, pathspec, baselen);
346353
}
347354

348355
if (refresh_only) {
349356
refresh(verbose, pathspec);
350357
goto finish;
351358
}
352359

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+
353373
exit_status |= add_files_to_cache(prefix, pathspec, flags);
354374

355375
if (add_new_files)

0 commit comments

Comments
 (0)