@@ -310,7 +310,7 @@ static const char ignore_error[] =
310310"The following paths are ignored by one of your .gitignore files:\n" ;
311311
312312static int verbose = 0 , show_only = 0 , ignored_too = 0 , refresh_only = 0 ;
313- static int ignore_add_errors , addremove , intent_to_add ;
313+ static int ignore_add_errors , addremove , intent_to_add , ignore_missing = 0 ;
314314
315315static struct option builtin_add_options [] = {
316316 OPT__DRY_RUN (& show_only ),
@@ -325,6 +325,7 @@ static struct option builtin_add_options[] = {
325325 OPT_BOOLEAN ('A' , "all" , & addremove , "add all, noticing removal of tracked files" ),
326326 OPT_BOOLEAN ( 0 , "refresh" , & refresh_only , "don't add, only refresh the index" ),
327327 OPT_BOOLEAN ( 0 , "ignore-errors" , & ignore_add_errors , "just skip files which cannot be added because of errors" ),
328+ OPT_BOOLEAN ( 0 , "ignore-missing" , & ignore_missing , "check if - even missing - files are ignored in dry run" ),
328329 OPT_END (),
329330};
330331
@@ -385,6 +386,8 @@ int cmd_add(int argc, const char **argv, const char *prefix)
385386
386387 if (addremove && take_worktree_changes )
387388 die ("-A and -u are mutually incompatible" );
389+ if (!show_only && ignore_missing )
390+ die ("Option --ignore-missing can only be used together with --dry-run" );
388391 if ((addremove || take_worktree_changes ) && !argc ) {
389392 static const char * here [2 ] = { "." , NULL };
390393 argc = 1 ;
@@ -441,9 +444,14 @@ int cmd_add(int argc, const char **argv, const char *prefix)
441444 seen = find_used_pathspec (pathspec );
442445 for (i = 0 ; pathspec [i ]; i ++ ) {
443446 if (!seen [i ] && pathspec [i ][0 ]
444- && !file_exists (pathspec [i ]))
445- die ("pathspec '%s' did not match any files" ,
446- pathspec [i ]);
447+ && !file_exists (pathspec [i ])) {
448+ if (ignore_missing ) {
449+ if (excluded (& dir , pathspec [i ], DT_UNKNOWN ))
450+ dir_add_ignored (& dir , pathspec [i ], strlen (pathspec [i ]));
451+ } else
452+ die ("pathspec '%s' did not match any files" ,
453+ pathspec [i ]);
454+ }
447455 }
448456 free (seen );
449457 }
0 commit comments