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

Re: [PATCH] Remove duplicate pathspecs from ls-files command line

From
Alex Riesen <raa.lkml@gmail.com>
Date
Aug 29, 2007, 21:15 UTC
Message-ID
<20070829211519.GE11824@steel.home>
In-Reply-To
<7v4piioyu1.fsf@gitster.siamese.dyndns.org>
Junio C Hamano, Wed, Aug 29, 2007 22:44:22 +0200:
> 
> That loop is plain old O(n^2) that penalizes everybody.
> 

Maybe something in pathspec matching code could be reused to notice the duplications? It has to go through all of them anyway...

Show 7 quoted lines
> Please do not penalize sane callers when you try to improve
> support of mistaken usage.  Move expensive error recovery in the
> error path when possible, and have _only_ mistaken users pay the
> price.
> 
> Like this perhaps.
> 

I just would write it shorter (except for that ugly label before closing brace).

diff --git a/builtin-ls-files.c b/builtin-ls-files.c
index d36181a..258868e 100644
--- a/builtin-ls-files.c
+++ b/builtin-ls-files.c
@@ -511,11 +511,28 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
 		 */
 		int num, errors = 0;
 		for (num = 0; pathspec[num]; num++) {
+			int other;
+
 			if (ps_matched[num])
 				continue;
+			/*
+			 * The caller might have fed identical pathspec
+			 * twice.  Do not barf on such a mistake.
+			 */
+			for (other = 0; pathspec[other]; other++) {
+				if (other == num || !ps_matched[other])
+					continue;
+				if (!strcmp(pathspec[other], pathspec[num]))
+					/*
+					 * Ok, we have a match already.
+					 */
+					goto found_dup;
+			}
+
 			error("pathspec '%s' did not match any file(s) known to git.",
 			      pathspec[num] + prefix_offset);
 			errors++;
+		found_dup:;
 		}
 
 		if (errors)
-- 
1.5.3.rc7.26.g5f7e4
Previous: martin f krafftNext: David Kastrup
Message 5 of 9 in “double occurrence of filenames on command lines”
  1. martin f krafftAug 29, 2007
  2. Remove duplicate pathspecs from ls-files command lineAlex Riesen, Aug 29, 2007
  3. Junio C HamanoAug 29, 2007
  4. martin f krafftAug 29, 2007
  5. Alex RiesenAug 29, 2007
  6. David KastrupAug 29, 2007
  7. Junio C HamanoAug 30, 2007
  8. David KastrupAug 30, 2007
  9. martin f krafftAug 29, 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.