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

Re:* [BUG] "git clean" does not pay attention to its parameters

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 5, 2007, 07:55 UTC
Message-ID
<7veje1zibm.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<200712050654.lB56scKk000311@mi0.bluebottle.com>
Nanako Shiraishi <nanako3@bluebottle.com> writes:
Show 5 quoted lines
> In a repository with LaTeX documents, I tried to see what *.aux files are left behind after formatting, by running "git clean -n" with the latest git (1.5.3.7-1005-gdada0c1):
>
>   % git clean -n '*.aux'
>
> This however showed more than just '*.aux' files.  With the released version 1.5.3.6, the output is correctly limited to the files that match the pattern.
Yuck.  People actually use git-clean?
But thanks for reporting.

Comparing the corresponding part from builtin-ls-files.c and what builtin-clean.c does, it does look broken.

Does this patch help? I am not sure why the directory side of the code is written that way, but I have a suspicion that "was a directory explicitly given as one of the pathspec" check is also bogus, although I did not touch that part.

-- >8 -- [PATCH] git-clean: Honor pathspec.

git-clean "*.rej" should attempt to look at only paths that match pattern "*.rej", but rewrite to C broke it.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-clean.c |   31 ++++++++++++++++++++-----------
 1 files changed, 20 insertions(+), 11 deletions(-)
diff --git a/builtin-clean.c b/builtin-clean.c
index 56ae4eb..7dd901e 100644
--- a/builtin-clean.c
+++ b/builtin-clean.c
@@ -27,13 +27,14 @@ static int git_clean_config(const char *var, const char *value)
 
 int cmd_clean(int argc, const char **argv, const char *prefix)
 {
-	int j;
+	int i;
 	int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
 	int ignored_only = 0, baselen = 0, config_set = 0;
 	struct strbuf directory;
 	struct dir_struct dir;
 	const char *path, *base;
 	static const char **pathspec;
+	char *seen = NULL;
 	struct option options[] = {
 		OPT__QUIET(&quiet),
 		OPT__DRY_RUN(&show_only),
@@ -85,12 +86,17 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 	read_directory(&dir, path, base, baselen, pathspec);
 	strbuf_init(&directory, 0);
 
-	for (j = 0; j < dir.nr; ++j) {
-		struct dir_entry *ent = dir.entries[j];
-		int len, pos, specs;
+	if (pathspec) {
+		for (i = 0; pathspec[i]; i++)
+			; /* nothing */
+		seen = xmalloc();
+	}
+
+	for (i = 0; i < dir.nr; i++) {
+		struct dir_entry *ent = dir.entries[i];
+		int len, pos;
 		struct cache_entry *ce;
 		struct stat st;
-		char *seen;
 
 		/*
 		 * Remove the '/' at the end that directory
@@ -114,15 +120,13 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 			int matched_path = 0;
 			strbuf_addstr(&directory, ent->name);
 			if (pathspec) {
-				for (specs =0; pathspec[specs]; ++specs)
-					/* nothing */;
-				seen = xcalloc(specs, 1);
-				/* Check if directory was explictly passed as
-				 * pathspec.  If so we want to remove it */
+				/*
+				 * Check if directory was explictly passed as
+				 * pathspec. If so we want to remove it.
+				 */
 				if (match_pathspec(pathspec, ent->name, ent->len,
 						   baselen, seen))
 					matched_path = 1;
-				free(seen);
 			}
 			if (show_only && (remove_directories || matched_path)) {
 				printf("Would remove %s\n", directory.buf);
@@ -138,6 +142,10 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 			}
 			strbuf_reset(&directory);
 		} else {
+			if (pathspec &&
+			    !match_pathspec(pathspec, ent->name, ent->len,
+					    baselen, seen))
+				continue; /* excluded */
 			if (show_only) {
 				printf("Would remove %s\n", ent->name);
 				continue;
@@ -147,6 +155,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 			unlink(ent->name);
 		}
 	}
+	free(seen);
 
 	strbuf_release(&directory);
 	return 0;
-- 
1.5.3.7-2115-geb804
Previous: Nanako ShiraishiNext: Johannes Schindelin
Message 2 of 9 in “[BUG] "git clean" does not pay attention to its parameters”
  1. Nanako ShiraishiDec 5, 2007
  2. Junio C HamanoDec 5, 2007
  3. Johannes SchindelinDec 5, 2007
  4. Junio C HamanoDec 5, 2007
  5. Sam RavnborgDec 5, 2007
  6. Shawn BohrerDec 5, 2007
  7. Junio C HamanoDec 6, 2007
  8. Jeff KingDec 5, 2007
  9. Jeff KingDec 6, 2007

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.