Skip to content

Commit

Permalink
dir.c(common_prefix): Fix two bugs
Browse files Browse the repository at this point in the history
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>
Signed-off-by: Junio C Hamano <junkio@cox.net>
  • Loading branch information
dscho authored and Junio C Hamano committed Apr 23, 2007
1 parent 9731706 commit c7f34c1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,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;
Expand Down
6 changes: 6 additions & 0 deletions t/t3700-add.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c7f34c1

Please sign in to comment.