Skip to contentSkip to conversation

git-ls-files in subdirectories ignore higher-up .gitignore

PATCH In review Pavel Roskin 3 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Hello!

git-ls-files appears to behave in a way that may be unexpected to the users. When run in a subdirectory, git-ls-files never reads .gitignore or whatever is specified by --exclude-per-directory in the parent directories.

This can be demonstrated in git's own repository:

$ touch t/test.o
$ git-ls-files --others --exclude-per-directory=.gitignore
$ cd t
$ git-ls-files --others --exclude-per-directory=.gitignore
test.o
$

Before I attempt to fix it, I'd like to make sure it's a bug, not a feature.

Signature · 2 lines
Regards,
Pavel Roskin
#1 lore raw
Quoted 16 lines · Pavel Roskin

Hello!

git-ls-files appears to behave in a way that may be unexpected to the users. When run in a subdirectory, git-ls-files never reads .gitignore or whatever is specified by --exclude-per-directory in the parent directories.

This can be demonstrated in git's own repository:

$ touch t/test.o
$ git-ls-files --others --exclude-per-directory=.gitignore
$ cd t
$ git-ls-files --others --exclude-per-directory=.gitignore
test.o
$
Quoted 2 lines

Before I attempt to fix it, I'd like to make sure it's a bug, not a feature.

I for one consider it a bug.
I expect exactly same output if I do
git ls-files t/
or
cd t; git ls-files

Very usefull when I limit grep to some part of the kernel tree for example.

Sam

#5 lore raw
In reply to Pavel Roskin
Quoted 11 lines · Pavel Roskin

This can be demonstrated in git's own repository:

$ touch t/test.o
$ git-ls-files --others --exclude-per-directory=.gitignore
$ cd t
$ git-ls-files --others --exclude-per-directory=.gitignore
test.o
$

Before I attempt to fix it, I'd like to make sure it's a bug, not a feature.

I am not sure if that is a bug or a feature, but you are right. We do not bother to go uplevel and look for .gitignore in the current code.

It would probably be a welcome addition if your fix made these two sequences work similarly modulo leading paths from the command output. I think it is natural to expect they are moral equivalents:

(Sequence 1)
$ touch t/test.o t/garbage
$ git-ls-files -o --exclude-per-directory=.gitignore t/

(Sequence 2)
$ touch t/test.o t/garbage
$ cd t
$ git-ls-files -o --exclude-per-directory=.gitignore

#2 lore raw

PATCH 1/1 ls-files: honour per-directory ignore file from higher directories.

In reply to Sam Ravnborg

When git-ls-files -o --exclude-per-directory=.gitignore is run from a subdirectory, it did not read from .gitignore from its parent directory. Reading from them makes output from these two commands consistent:

    $ git ls-files -o --exclude-per-directory=.gitignore Documentation
    $ cd Documentation &&
      git ls-files -o --exclude-per-directory=.gitignore

---

* If there are positive feedbacks on this one, I consider it a safe enough candidate to be included in 1.2.0 release.

 ls-files.c |   22 +++++++++++++++++++++-
 1 files changed, 21 insertions(+), 1 deletions(-)

701ca744e386c2429ca44072ea987bbb4bdac7ce

ls-files.c +21 -1
index 6af3b09..7024cf1 100644
@@ -474,8 +474,28 @@ static void show_files(void)
 		const char *path = ".", *base = "";
 		int baselen = prefix_len;
 
-		if (baselen)
+		if (baselen) {
 			path = base = prefix;
+			if (exclude_per_dir) {
+				char *p, *pp = xmalloc(baselen+1);
+				memcpy(pp, prefix, baselen+1);
+				p = pp;
+				while (1) {
+					char save = *p;
+					*p = 0;
+					push_exclude_per_directory(pp, p-pp);
+					*p++ = save;
+					if (!save)
+						break;
+					p = strchr(p, '/');
+					if (p)
+						p++;
+					else
+						p = pp + baselen;
+				}
+				free(pp);
+			}
+		}
 		read_directory(path, base, baselen);
 		qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
 		if (show_others)
Signature · 1 line
1.1.6.gbb042
#3 lore raw

Re: [PATCH] ls-files: honour per-directory ignore file from higher directories.

Hello, Junio!

Quoted 4 lines · Junio C Hamano

When git-ls-files -o --exclude-per-directory=.gitignore is run from a subdirectory, it did not read from .gitignore from its parent directory. Reading from them makes output from these two commands consistent:

You beat me at that. Thank you!

With this patch, the simplified version of cg-clean passes the cogito testsuite. That's where the original bug was caught.

Quoted 2 lines

* If there are positive feedbacks on this one, I consider it a safe enough candidate to be included in 1.2.0 release.

Yes, it would be nice to see it released.

Signature · 2 lines
Regards,
Pavel Roskin
#4 lore raw

Re: [PATCH] ls-files: honour per-directory ignore file from higher directories.

Quoted 2 lines · Pavel Roskin

With this patch, the simplified version of cg-clean passes the cogito testsuite. That's where the original bug was caught.

Thanks for testing it.