# git ls-files --error-unmatch weirdness

3 messages from 2011-08-12 to 2011-08-12. Participants: SZEDER Gábor, Nguyen Thai Ngoc Duy, Junio C Hamano.
Thread: https://gitlist.dev/t/28077

## SZEDER Gábor, 2011-08-12 13:24

Subject: git ls-files --error-unmatch weirdness
Message-ID: <20110812132436.GA12147@goldbirke>
URL: https://gitlist.dev/e/20110812132436.GA12147%40goldbirke

```
Hi,

I was looking for untracked files when noticed this:

repo$ git init
Initialized empty Git repository in /home/szeder/tmp/git/repo/.git/
repo (master)$ mkdir dir
repo (master)$ touch dir/file
repo (master)$ git add dir/file
repo (master)$ git commit -m 'first'
[master (root-commit) 2c0990c] first
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 dir/file
repo (master)$ git ls-files --others --error-unmatch
repo/dir (master)$ echo $?
0
repo (master)$ cd dir
repo/dir (master)$ git ls-files --others --error-unmatch
error: pathspec '' did not match any file(s) known to git.
Did you forget to 'git add'?
repo/dir (master)$ echo $?
1

So, the same 'git ls-files' command is OK when run at the top of the
worktree, but errors out in a subdirectory.  What's going on?  Is this
a bug?  If not, why not?


Thanks,
Gábor

```

## Nguyen Thai Ngoc Duy, 2011-08-12 14:17

Subject: Re: git ls-files --error-unmatch weirdness
Message-ID: <CACsJy8D0-SqSY0q2hJ0+w_NeT_s0Uza=Bi8-y37EKWE2VZhYeQ@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8D0-SqSY0q2hJ0%2Bw_NeT_s0Uza%3DBi8-y37EKWE2VZhYeQ%40mail.gmail.com
In-Reply-To: <20110812132436.GA12147@goldbirke>

```
2011/8/12 SZEDER Gábor <szeder@ira.uka.de>:
> So, the same 'git ls-files' command is OK when run at the top of the
> worktree, but errors out in a subdirectory.  What's going on?  Is this
> a bug?  If not, why not?

report_path_error() in ls-files.c is meant to check pathspecs given by
user. However when in a subdir, get_pathspec() automatically adds a
pathspec (that is prefix). This new pathspec confuses
report_path_error(). The following patch may help (possibly corrupted
because I paste in gmail, but you get the idea)

diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index 0e98bff..e0611ac 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -545,7 +545,8 @@ int cmd_ls_files(int argc, const char **argv,
const char *cmd_prefix)
 	max_prefix_len = max_prefix ? strlen(max_prefix) : 0;

 	/* Treat unmatching pathspec elements as errors */
-	if (pathspec && error_unmatch) {
+	if (pathspec && error_unmatch &&
+	    (!prefix || pathspec[1])) {
 		int num;
 		for (num = 0; pathspec[num]; num++)
 			;

-- 
Duy

```

## Junio C Hamano, 2011-08-12 17:56

Subject: Re: git ls-files --error-unmatch weirdness
Message-ID: <7vaabeo6nl.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vaabeo6nl.fsf%40alter.siamese.dyndns.org
In-Reply-To: <20110812132436.GA12147@goldbirke>

```
SZEDER Gábor <szeder@ira.uka.de> writes:

> repo (master)$ git ls-files --others --error-unmatch

The "--error-unmatch" is about reporting errors in pathspecs you gave from
the command line. The behaviour is undefined if you do not give any, like
the above command line.

Having said that, I wouldn't be surprised if it triggered when run from an
empty subdirectory.

```
