Git development
 help / color / mirror / Atom feed
* [PATCH] dir.c(common_prefix): Fix two bugs
@ 2007-04-23  8:21 Johannes Schindelin
  2007-04-23  8:47 ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Johannes Schindelin @ 2007-04-23  8:21 UTC (permalink / raw)
  To: git, junkio


The function common_prefix() is used to find the common subdirectory of
a couple of pathnames. When checking if the next pathname matches up with
the prefix, it incorrectly checked the whole path, not just the prefix
(including the slash). Thus, the expensive part of the loop was executed
always.

The other bug is more serious: if the first and the last pathname in the
list have a longer common prefix than the common prefix for _all_ pathnames
in the list, the longer one would be chosen. This bug was probably hidden
by the fact that bash's wildcard expansion sorts the results, and the code
just so happens to work with sorted input.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---

	If you're up for a very surprising error message, run the test 
	without compiling git-add first...

 dir.c          |    3 ++-
 t/t3700-add.sh |    6 ++++++
 2 files changed, 8 insertions(+), 1 deletions(-)

diff --git a/dir.c b/dir.c
index 6564a92..d306352 100644
--- a/dir.c
+++ b/dir.c
@@ -34,8 +34,9 @@ int common_prefix(const char **pathspec)
 	prefix = slash - path + 1;
 	while ((next = *++pathspec) != NULL) {
 		int len = strlen(next);
-		if (len >= prefix && !memcmp(path, next, len))
+		if (len >= prefix && !memcmp(path, next, prefix))
 			continue;
+		len = prefix - 1;
 		for (;;) {
 			if (!len)
 				return 0;
diff --git a/t/t3700-add.sh b/t/t3700-add.sh
index 08e0352..ad8cc7d 100755
--- a/t/t3700-add.sh
+++ b/t/t3700-add.sh
@@ -104,4 +104,10 @@ test_expect_success 'add ignored ones with -f' '
 	git-ls-files --error-unmatch d.ig/d.if d.ig/d.ig
 '
 
+mkdir 1 1/2 1/3
+touch 1/2/a 1/3/b 1/2/c
+test_expect_success 'check correct prefix detection' '
+	git add 1/2/a 1/3/b 1/2/c
+'
+
 test_done
-- 
1.5.1.1.2671.g2bc0-dirty

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] dir.c(common_prefix): Fix two bugs
  2007-04-23  8:21 [PATCH] dir.c(common_prefix): Fix two bugs Johannes Schindelin
@ 2007-04-23  8:47 ` Junio C Hamano
  2007-04-23  9:12   ` Johannes Schindelin
  0 siblings, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2007-04-23  8:47 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: git

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> 	If you're up for a very surprising error message, run the test 
> 	without compiling git-add first...

$ PATH=/usr/bin:/bin sh t3700-add.sh -i -v
* expecting success: touch foo && git-add foo
./test-lib.sh: line 136: git-add: command not found
* FAIL 1: Test of git-add
        touch foo && git-add foo
$ echo $?
1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] dir.c(common_prefix): Fix two bugs
  2007-04-23  8:47 ` Junio C Hamano
@ 2007-04-23  9:12   ` Johannes Schindelin
  2007-04-23  9:22     ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Johannes Schindelin @ 2007-04-23  9:12 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

Hi,

On Mon, 23 Apr 2007, Junio C Hamano wrote:

> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> 
> > 	If you're up for a very surprising error message, run the test 
> > 	without compiling git-add first...
> 
> $ PATH=/usr/bin:/bin sh t3700-add.sh -i -v
> * expecting success: touch foo && git-add foo
> ./test-lib.sh: line 136: git-add: command not found
> * FAIL 1: Test of git-add
>         touch foo && git-add foo
> $ echo $?
> 1

;-)

I did not say you should "make clean" first... For the record: this is 
what the surprising error looks like on my machine:

	* expecting success:
        	git add 1/2/a 1/3/b 1/2/c
	
	The following paths are ignored by one of your .gitignore files:
	1/3/b
	Use -f if you really want to add them.
	* FAIL 15: check correct prefix detection

The surprising factor is, of course, that there is no .gitignore file.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] dir.c(common_prefix): Fix two bugs
  2007-04-23  9:12   ` Johannes Schindelin
@ 2007-04-23  9:22     ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2007-04-23  9:22 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: git

In any case, well spotted, thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-04-23  9:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-23  8:21 [PATCH] dir.c(common_prefix): Fix two bugs Johannes Schindelin
2007-04-23  8:47 ` Junio C Hamano
2007-04-23  9:12   ` Johannes Schindelin
2007-04-23  9:22     ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox