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 <shyamthakkar001@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ghanshyam Thakkar committed
Apr 3, 2024 at 23:44 UTC
7de13cfef3042478223012841e07cd91d7234d22
2 files changed
+18
-1
builtin/add.c
+8
-1
@@ -370,6 +370,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
370
int add_new_files;
371
int require_pathspec;
372
char *seen = NULL;
373
+ char *ps_matched = NULL;
374
struct lock_file lock_file = LOCK_INIT;
375
376
git_config(add_config, NULL);
@@ -549,13 +550,18 @@ int cmd_add(int argc, const char **argv, const char *prefix)
550
551
begin_odb_transaction();
552
553
+ ps_matched = xcalloc(pathspec.nr, 1);
554
if (add_renormalize)
555
exit_status |= renormalize_tracked_files(&pathspec, flags);
556
else
557
exit_status |= add_files_to_cache(the_repository, prefix,
556
- &pathspec, NULL,
558
+ &pathspec, ps_matched,
559
include_sparse, flags);
560
561
+ if (take_worktree_changes && !add_renormalize && !ignore_add_errors &&
562
+ report_path_error(ps_matched, &pathspec))
563
+ exit(128);
564
+
565
if (add_new_files)
566
exit_status |= add_files(&dir, flags);
567
@@ -568,6 +574,7 @@ finish:
574
COMMIT_LOCK | SKIP_IF_UNCHANGED))
575
die(_("unable to write new index file"));
576
577
+ free(ps_matched);
578
dir_clear(&dir);
579
clear_pathspec(&pathspec);
580
return exit_status;
t/t2200-add-update.sh
+10
@@ -65,6 +65,16 @@ test_expect_success 'update did not touch untracked files' '
65
test_must_be_empty out
66
'
67
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
+
78
test_expect_success 'cache tree has not been corrupted' '
79
80
git ls-files -s |