git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] builtin/add: error out when passing untracked path with -u

From
Ghanshyam Thakkar <shyamthakkar001@gmail.com>
Date
Mar 18, 2024, 15:52 UTC
Message-ID
<20240318155219.494206-6-shyamthakkar001@gmail.com>
In-Reply-To
<20240318155219.494206-2-shyamthakkar001@gmail.com>

Currently when we pass a pathspec which does not match any tracked path along side --update, it silently succeeds, unlike without --update. As --update only touches known paths, match the pathspec against the index and error out when no match found. And ensure that the index is fully expanded before matching the pathspec. Also add a testcase to check for the error.

Signed-off-by: Ghanshyam Thakkar <shyamthakkar001@gmail.com>
---
 builtin/add.c         | 16 ++++++++++++++++
 t/t2200-add-update.sh |  5 +++++
 2 files changed, 21 insertions(+)
diff --git a/builtin/add.c b/builtin/add.c
index 393c10cbcf..7ec5ea4a3e 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -24,6 +24,7 @@
 #include "strvec.h"
 #include "submodule.h"
 #include "add-interactive.h"
+#include "sparse-index.h"
 
 static const char * const builtin_add_usage[] = {
 	N_("git add [<options>] [--] <pathspec>..."),
@@ -536,6 +537,21 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 			}
 		}
 
+		if (take_worktree_changes && pathspec.nr) {
+			int i, ret;
+			char *ps_matched = xcalloc(pathspec.nr, 1);
+
+			/* TODO: audit for interaction with sparse-index. */
+			ensure_full_index(&the_index);
+			for (i = 0; i < the_index.cache_nr; i++)
+				ce_path_match(&the_index, the_index.cache[i],
+					      &pathspec, ps_matched);
+
+			ret = report_path_error(ps_matched, &pathspec);
+			free(ps_matched);
+			if (ret)
+				exit(1);
+		}
 
 		if (only_match_skip_worktree.nr) {
 			advise_on_updating_sparse_paths(&only_match_skip_worktree);
diff --git a/t/t2200-add-update.sh b/t/t2200-add-update.sh
index c01492f33f..f6a9615d1b 100755
--- a/t/t2200-add-update.sh
+++ b/t/t2200-add-update.sh
@@ -65,6 +65,11 @@ test_expect_success 'update did not touch untracked files' '
 	test_must_be_empty out
 '
 
+test_expect_success 'error out when given untracked path' '
+	test_must_fail git add -u dir2/other 2>err &&
+	test_grep -e "error: pathspec .dir2/other. did not match any file(s) known to git" err
+'
+
 test_expect_success 'cache tree has not been corrupted' '
 
 	git ls-files -s |
-- 
2.44.0
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 31 in “fix certain cases of add and commit with untracked path not erroring out”
  1. 0/2 fix certain cases of add and commit with untracked path not erroring outGhanshyam Thakkar, Mar 18, 2024
  2. 1/2 builtin/commit: error out when passing untracked path with -iGhanshyam Thakkar, Mar 18, 2024
  3. Junio C HamanoMar 18, 2024
  4. 2/2 builtin/add: error out when passing untracked path with -uGhanshyam Thakkar, Mar 18, 2024
  5. Junio C HamanoMar 18, 2024
  6. 0/3 commit, add: error out when passing untracked pathGhanshyam Thakkar, Mar 29, 2024
  7. 0/3 commit, add: error out when passing untracked pathsGhanshyam Thakkar, Apr 2, 2024
  8. 0/3 commit,add: error out when passing untracked pathGhanshyam Thakkar, Apr 3, 2024
  9. 1/3 revision: optionally record matches with pathspec elementsGhanshyam Thakkar, Apr 3, 2024
  10. 2/3 builtin/commit: error out when passing untracked path with -iGhanshyam Thakkar, Apr 3, 2024
  11. 3/3 builtin/add: error out when passing untracked path with -uGhanshyam Thakkar, Apr 3, 2024
  12. 1/3 revision: optionally record matches with pathspec elementsGhanshyam Thakkar, Apr 2, 2024
  13. 2/3 builtin/commit: error out when passing untracked path with -iGhanshyam Thakkar, Apr 2, 2024
  14. Junio C HamanoApr 2, 2024
  15. Ghanshyam ThakkarApr 2, 2024
  16. 3/3 builtin/add: error out when passing untracked path with -uGhanshyam Thakkar, Apr 2, 2024
  17. Junio C HamanoApr 2, 2024
  18. Ghanshyam ThakkarApr 2, 2024
  19. 1/3 read-cache: optionally collect pathspec matching infoGhanshyam Thakkar, Mar 29, 2024
  20. Junio C HamanoMar 29, 2024
  21. Junio C HamanoMar 29, 2024
  22. Ghanshyam ThakkarMar 30, 2024
  23. Junio C HamanoMar 30, 2024
  24. 2/3 builtin/commit: error out when passing untracked path with -iGhanshyam Thakkar, Mar 29, 2024
  25. Junio C HamanoMar 29, 2024
  26. 3/3 builtin/add: error out when passing untracked path with -uGhanshyam Thakkar, Mar 29, 2024
  27. Junio C HamanoMar 29, 2024
  28. Ghanshyam ThakkarMar 30, 2024
  29. Junio C HamanoMar 30, 2024
  30. Ghanshyam ThakkarApr 1, 2024
  31. Junio C HamanoApr 1, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.