Skip to content

Commit 7de13cf

Browse files
spectre10gitster
authored andcommitted
builtin/add: error out when passing untracked path with -u
When passing untracked path with -u option, it silently succeeds. There is no error message and the exit code is zero. This is inconsistent with other instances of git commands where the expected argument is a known path. In those other instances, we error out when the path is not known. Fix this by passing a character array to add_files_to_cache() to collect the pathspec matching information and report the error if a pathspec does not match any cache entry. Also add a testcase to cover this scenario. Signed-off-by: Ghanshyam Thakkar <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ac5946e commit 7de13cf

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

builtin/add.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
370370
int add_new_files;
371371
int require_pathspec;
372372
char *seen = NULL;
373+
char *ps_matched = NULL;
373374
struct lock_file lock_file = LOCK_INIT;
374375

375376
git_config(add_config, NULL);
@@ -549,13 +550,18 @@ int cmd_add(int argc, const char **argv, const char *prefix)
549550

550551
begin_odb_transaction();
551552

553+
ps_matched = xcalloc(pathspec.nr, 1);
552554
if (add_renormalize)
553555
exit_status |= renormalize_tracked_files(&pathspec, flags);
554556
else
555557
exit_status |= add_files_to_cache(the_repository, prefix,
556-
&pathspec, NULL,
558+
&pathspec, ps_matched,
557559
include_sparse, flags);
558560

561+
if (take_worktree_changes && !add_renormalize && !ignore_add_errors &&
562+
report_path_error(ps_matched, &pathspec))
563+
exit(128);
564+
559565
if (add_new_files)
560566
exit_status |= add_files(&dir, flags);
561567

@@ -568,6 +574,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
568574
COMMIT_LOCK | SKIP_IF_UNCHANGED))
569575
die(_("unable to write new index file"));
570576

577+
free(ps_matched);
571578
dir_clear(&dir);
572579
clear_pathspec(&pathspec);
573580
return exit_status;

t/t2200-add-update.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ test_expect_success 'update did not touch untracked files' '
6565
test_must_be_empty out
6666
'
6767

68+
test_expect_success 'error out when passing untracked path' '
69+
git reset --hard &&
70+
echo content >>baz &&
71+
echo content >>top &&
72+
test_must_fail git add -u baz top 2>err &&
73+
test_grep -e "error: pathspec .baz. did not match any file(s) known to git" err &&
74+
git diff --cached --name-only >actual &&
75+
test_must_be_empty actual
76+
'
77+
6878
test_expect_success 'cache tree has not been corrupted' '
6979
7080
git ls-files -s |

0 commit comments

Comments
 (0)