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

Re: git add -u nonexistent-file

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 9, 2010, 21:58 UTC
Message-ID
<7v7hqm2hs7.fsf@alter.siamese.dyndns.org>
In-Reply-To
<20100209003958.GA4065@coredump.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 24 quoted lines
> It's not ignoring the extra parameters. They limit the scope of the
> operation. So:
>
>   $ git init
>   $ touch file && mkdir subdir && touch subdir/file
>   $ git add . && git commit -m one
>   $ echo changes >file && echo changes >subdir/file
>   $ git add -u subdir
>   $ git status
>   # On branch master
>   # Changes to be committed:
>   #       modified:   subdir/file
>   #
>   # Changed but not updated:
>   #       modified:   file
>   #
>
> That being said, you noticed that the regular add case notes unused
> pathspecs on the command line:
>
>   $ git add bogus
>   fatal: pathspec 'bogus' did not match any files
>
> We could probably do the same here.

It won't be entirely trivial to do so efficiently but it shouldn't be a rocket surgery.

Something like this (untested of course)?
 builtin-add.c |   38 +++++++++++++++++++++++++++++---------
 1 files changed, 29 insertions(+), 9 deletions(-)
diff --git a/builtin-add.c b/builtin-add.c
index 2705f8d..87d2980 100644
--- a/builtin-add.c
+++ b/builtin-add.c
@@ -117,7 +117,19 @@ static void fill_pathspec_matches(const char **pathspec, char *seen, int specs)
 	}
 }
 
-static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
+static char *find_used_pathspec(const char **pathspec)
+{
+	char *seen;
+	int i;
+
+	for (i = 0; pathspec[i];  i++)
+		; /* just counting */
+	seen = xcalloc(i, 1);
+	fill_pathspec_matches(pathspec, seen, i);
+	return seen;
+}
+
+static char *prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
 {
 	char *seen;
 	int i, specs;
@@ -137,13 +149,7 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p
 	}
 	dir->nr = dst - dir->entries;
 	fill_pathspec_matches(pathspec, seen, specs);
-
-	for (i = 0; i < specs; i++) {
-		if (!seen[i] && pathspec[i][0] && !file_exists(pathspec[i]))
-			die("pathspec '%s' did not match any files",
-					pathspec[i]);
-	}
-        free(seen);
+	return seen;
 }
 
 static void treat_gitlinks(const char **pathspec)
@@ -359,6 +365,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 	int flags;
 	int add_new_files;
 	int require_pathspec;
+	char *seen = NULL;
 
 	git_config(add_config, NULL);
 
@@ -418,7 +425,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 		/* This picks up the paths that are not tracked */
 		baselen = fill_directory(&dir, pathspec);
 		if (pathspec)
-			prune_directory(&dir, pathspec, baselen);
+			seen = prune_directory(&dir, pathspec, baselen);
 	}
 
 	if (refresh_only) {
@@ -426,6 +433,19 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 		goto finish;
 	}
 
+	if (pathspec) {
+		int i;
+		if (!seen)
+			seen = find_used_pathspec(pathspec);
+		for (i = 0; pathspec[i]; i++) {
+			if (!seen[i] && pathspec[i][0]
+			    && !file_exists(pathspec[i]))
+				die("pathspec '%s' did not match any files",
+				    pathspec[i]);
+		}
+		free(seen);
+	}
+
 	exit_status |= add_files_to_cache(prefix, pathspec, flags);
 
 	if (add_new_files)
Previous: Chris PackhamNext: Chris Packham
Message 6 of 13 in “git add -u nonexistent-file”
  1. SZEDER GáborFeb 8, 2010
  2. Chris PackhamFeb 8, 2010
  3. Jeff KingFeb 9, 2010
  4. Chris PackhamFeb 9, 2010
  5. test for add with non-existent pathspecChris Packham, Feb 9, 2010
  6. Junio C HamanoFeb 9, 2010
  7. Chris PackhamFeb 9, 2010
  8. Chris PackhamFeb 9, 2010
  9. 1/3 test for add with non-existent pathspecChris Packham, Feb 9, 2010
  10. 2/3 git add -u: give an error if pathspec unmatchedChris Packham, Feb 9, 2010
  11. 3/3 t2200-add-update.sh: change expected fail to successChris Packham, Feb 9, 2010
  12. Junio C HamanoFeb 9, 2010
  13. Jeff KingFeb 10, 2010

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.