@@ -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
149155static 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 )
0 commit comments