Skip to content

Commit cc64c69

Browse files
committed
Merge branch 'cp/add-u-pathspec' into maint
* cp/add-u-pathspec: test for add with non-existent pathspec git add -u: die on unmatched pathspec
2 parents 4c367c6 + 1e7ef74 commit cc64c69

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

builtin-add.c

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

120-
static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
120+
static char *find_used_pathspec(const char **pathspec)
121+
{
122+
char *seen;
123+
int i;
124+
125+
for (i = 0; pathspec[i]; i++)
126+
; /* just counting */
127+
seen = xcalloc(i, 1);
128+
fill_pathspec_matches(pathspec, seen, i);
129+
return seen;
130+
}
131+
132+
static char *prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
121133
{
122134
char *seen;
123135
int i, specs;
@@ -137,13 +149,7 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p
137149
}
138150
dir->nr = dst - dir->entries;
139151
fill_pathspec_matches(pathspec, seen, specs);
140-
141-
for (i = 0; i < specs; i++) {
142-
if (!seen[i] && pathspec[i][0] && !file_exists(pathspec[i]))
143-
die("pathspec '%s' did not match any files",
144-
pathspec[i]);
145-
}
146-
free(seen);
152+
return seen;
147153
}
148154

149155
static void treat_gitlinks(const char **pathspec)
@@ -359,6 +365,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
359365
int flags;
360366
int add_new_files;
361367
int require_pathspec;
368+
char *seen = NULL;
362369

363370
git_config(add_config, NULL);
364371

@@ -418,14 +425,27 @@ int cmd_add(int argc, const char **argv, const char *prefix)
418425
/* This picks up the paths that are not tracked */
419426
baselen = fill_directory(&dir, pathspec);
420427
if (pathspec)
421-
prune_directory(&dir, pathspec, baselen);
428+
seen = prune_directory(&dir, pathspec, baselen);
422429
}
423430

424431
if (refresh_only) {
425432
refresh(verbose, pathspec);
426433
goto finish;
427434
}
428435

436+
if (pathspec) {
437+
int i;
438+
if (!seen)
439+
seen = find_used_pathspec(pathspec);
440+
for (i = 0; pathspec[i]; i++) {
441+
if (!seen[i] && pathspec[i][0]
442+
&& !file_exists(pathspec[i]))
443+
die("pathspec '%s' did not match any files",
444+
pathspec[i]);
445+
}
446+
free(seen);
447+
}
448+
429449
exit_status |= add_files_to_cache(prefix, pathspec, flags);
430450

431451
if (add_new_files)

t/t2200-add-update.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,9 @@ test_expect_success 'add -u resolves unmerged paths' '
176176
177177
'
178178

179+
test_expect_success '"add -u non-existent" should fail' '
180+
test_must_fail git add -u non-existent &&
181+
! (git ls-files | grep "non-existent")
182+
'
183+
179184
test_done

t/t3700-add.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,9 @@ test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
255255
git add track-this
256256
'
257257

258+
test_expect_success '"add non-existent" should fail' '
259+
test_must_fail git add non-existent &&
260+
! (git ls-files | grep "non-existent")
261+
'
262+
258263
test_done

0 commit comments

Comments
 (0)