threads / patch / 6106

patch, 5 partsgit-add: add ignored files when asked explicitly.

Subject: [PATCH 5/5] git-add: add ignored files when asked explicitly.

## tl;dr

11 messages between Dec 25, 2006 and Jan 4, 2007. Diffs are folded; open one to read it.

replies: 10people: 5as markdown or json

Junio C Hamano· Dec 25, 2006, 11:13 UTC · lore

One thing many people found confusing about git-add was that a file whose name matches an ignored pattern could not be added to the index. With this, such a file can be added by explicitly spelling its name to git-add.

Fileglobs and recursive behaviour do not add ignored files to the index. That is, if a pattern '*.o' is in .gitignore, and two files foo.o, bar/baz.o are in the working tree:

    $ git add foo.o
    $ git add '*.o'
    $ git add bar
Only the first form adds foo.o to the index.
Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 Documentation/git-add.txt |   11 ++++++++---
 builtin-add.c             |   11 ++++++++++-
 2 files changed, 18 insertions(+), 4 deletions(-)
Show changes to 2 files +18 −4

Documentation/git-add.txt, builtin-add.c

diff --git a/Documentation/git-add.txt b/Documentation/git-add.txt
index d86c0e7..a8ed459 100644
--- a/Documentation/git-add.txt
+++ b/Documentation/git-add.txt
@@ -25,8 +25,9 @@ the commit.
 The 'git status' command can be used to obtain a summary of what is included
 for the next commit.
 
-This command only adds non-ignored files, to add ignored files use
-"git update-index --add".
+This command can be used to add ignored files, but they have to be
+explicitly and exactly specified from the command line.  File globbing
+and recursive behaviour do not add ignored files.
 
 Please see gitlink:git-commit[1] for alternative ways to add content to a
 commit.
@@ -35,7 +36,11 @@ commit.
 OPTIONS
 -------
 <file>...::
-	Files to add content from.
+	Files to add content from.  Fileglobs (e.g. `*.c`) can
+	be given to add all matching files.  Also a
+	leading directory name (e.g. `dir` to add `dir/file1`
+	and `dir/file2`) can be given to add all files in the
+	directory, recursively.
 
 -n::
         Don't actually add the file(s), just show if they exist.
diff --git a/builtin-add.c b/builtin-add.c
index f306f82..389c106 100644
--- a/builtin-add.c
+++ b/builtin-add.c
@@ -25,7 +25,14 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p
 	i = dir->nr;
 	while (--i >= 0) {
 		struct dir_entry *entry = *src++;
-		if (!match_pathspec(pathspec, entry->name, entry->len, prefix, seen)) {
+		int how = match_pathspec(pathspec, entry->name, entry->len,
+					 prefix, seen);
+		/*
+		 * ignored entries can be added with exact match,
+		 * but not with glob nor recursive.
+		 */
+		if (!how ||
+		    (entry->ignored_entry && how != MATCHED_EXACTLY)) {
 			free(entry);
 			continue;
 		}
@@ -54,6 +61,8 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec)
 
 	/* Set up the default git porcelain excludes */
 	memset(dir, 0, sizeof(*dir));
+	if (pathspec)
+		dir->show_both = 1;
 	dir->exclude_per_dir = ".gitignore";
 	path = git_path("info/exclude");
 	if (!access(path, R_OK))
-- 
1.4.4.3.g50da
Johannes Schindelin· Dec 25, 2006, 13:47 UTC · re: Junio C Hamano · lore

Re: [PATCH 5/5] git-add: add ignored files when asked explicitly.

Hi,
On Mon, 25 Dec 2006, Junio C Hamano wrote:
>     $ git add foo.o
>     $ git add '*.o'
Most people do
	$ git add *.o

instead, where bash expands the expression. Maybe this new behaviour should be hidden between a "-f" option?

Ciao, Dscho

Junio C Hamano· Dec 25, 2006, 17:27 UTC · re: Johannes Schindelin · lore

Re: [PATCH 5/5] git-add: add ignored files when asked explicitly.

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 11 quoted lines
> On Mon, 25 Dec 2006, Junio C Hamano wrote:
>
>>     $ git add foo.o
>>     $ git add '*.o'
>
> Most people do
>
> 	$ git add *.o
>
> instead, where bash expands the expression. Maybe this new behaviour 
> should be hidden between a "-f" option?
When would anybody do "git add *.o"?

A more plausible situation is that you have '*.o' in .gitignore because you do not want to keep track of object files generated from your source, but your project needs to keep track of one third-party object file that you do not have the source to, and helping that situation is what this patch is about.

An alternative is to use the mechanism I added here to _detect_ the attempt to add an ignored file with explicitly spelled out pathspec, and issue an info message that says something like:

	Path 'xyzzy/filfre.o' is not being ignored by one of
	your .gitignore files.  If you really want to add it,
	please add this entry to .gitignore file:
        !/xyzzy/filfre.o

One advantage of this is that it would help guiding the user in the right direction, giving a reusable piece of knowledge, without changing the behaviour of the command (what is refused is refused). But I can already see people's complaints: if the tool knows how to fix that situation why forces the user to do so?

Although the reason why the alternative does not do so is "The user earlier said *.o files are uninteresting but came back with a conflicting request to add xyzzy/filfre.o, which could be a mistake. We ask for a confirmation", which is very sensible, another alternative would be to add the path anyway and issue an warning, like this:

	$ ls xyzzy
        filfre.c	filfre.o
	$ git add xyzzy/filfre.?
	added ignored path xyzzy/filfre.o
Junio C Hamano· Dec 25, 2006, 18:39 UTC · re: Junio C Hamano · lore

[PATCH] git-add: warn when adding an ignored file with an explicit request.

We allow otherwise ignored paths to be added to the index by spelling its path out on the command line, but we would warn the user about them when we do so.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 * This is on top of what I sent out last night.
 builtin-add.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
Show changes to builtin-add.c +3 −0
diff --git a/builtin-add.c b/builtin-add.c
index 822075a..c54c694 100644
--- a/builtin-add.c
+++ b/builtin-add.c
@@ -37,6 +37,9 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p
 			free(entry);
 			continue;
 		}
+		if (entry->ignored_entry)
+			fprintf(stderr, "warning: '%s' is an ignored path.\n",
+				entry->name);
 		*dst++ = entry;
 	}
 	dir->nr = dst - dir->entries;
-- 
1.4.4.3.g71b5
Jakub Narebski· Dec 25, 2006, 22:24 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-add: warn when adding an ignored file with an explicit request.

Junio C Hamano wrote:
> We allow otherwise ignored paths to be added to the index by
> spelling its path out on the command line, but we would warn the
> user about them when we do so.

I'm for warning user that the file to be added is ignored file and telling how to make an exception in .gitignore _AND_ that -f option can be used to bypass that check. And adding ignored path as above with -f option.

But otherwise: nice series of patches.
P.S. Were all the patches sent as replies to introductory letter?
-- 
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git
Johannes Schindelin· Dec 26, 2006, 16:19 UTC · re: Jakub Narebski · lore

Re: [PATCH] git-add: warn when adding an ignored file with an explicit request.

Hi,
On Mon, 25 Dec 2006, Jakub Narebski wrote:
> P.S. Were all the patches sent as replies to introductory letter?

I actually don't like that convention at all. It makes for a _huge_ thread of unrelated discussions, since most of the time the series is _exactly_ because the patches are somewhat independent (otherwise you could not separate them).

Ciao, Dscho

Nicolas Pitre· Dec 25, 2006, 19:57 UTC · re: Junio C Hamano · lore

Re: [PATCH 5/5] git-add: add ignored files when asked explicitly.

On Mon, 25 Dec 2006, Junio C Hamano wrote:
Show 9 quoted lines
> An alternative is to use the mechanism I added here to _detect_
> the attempt to add an ignored file with explicitly spelled out
> pathspec, and issue an info message that says something like:
> 
> 	Path 'xyzzy/filfre.o' is not being ignored by one of
> 	your .gitignore files.  If you really want to add it,
> 	please add this entry to .gitignore file:
> 
>         !/xyzzy/filfre.o
What about this instead:
	Path 'xyzzy/filfre.o' should be ignored according to one of
	your .gitignore files.  If you really want to add it, you must
	use the -f flag.
Nicolas
Johannes Schindelin· Dec 26, 2006, 15:34 UTC · re: Junio C Hamano · lore

Re: [PATCH 5/5] git-add: add ignored files when asked explicitly.

Hi,
On Mon, 25 Dec 2006, Junio C Hamano wrote:
Show 15 quoted lines
> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> 
> > On Mon, 25 Dec 2006, Junio C Hamano wrote:
> >
> >>     $ git add foo.o
> >>     $ git add '*.o'
> >
> > Most people do
> >
> > 	$ git add *.o
> >
> > instead, where bash expands the expression. Maybe this new behaviour 
> > should be hidden between a "-f" option?
> 
> When would anybody do "git add *.o"?

Not exactly "git add *.o", but when I see in git-status that there are only a couple of files which are untracked, and I want them added, I lazily do "git add *".

Ciao, Dscho

Junio C Hamano· Dec 26, 2006, 19:05 UTC · re: Johannes Schindelin · lore

Re: [PATCH 5/5] git-add: add ignored files when asked explicitly.

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 12 quoted lines
>> > Most people do
>> >
>> > 	$ git add *.o
>> >
>> > instead, where bash expands the expression. Maybe this new behaviour 
>> > should be hidden between a "-f" option?
>> 
>> When would anybody do "git add *.o"?
>
> Not exactly "git add *.o", but when I see in git-status that there are 
> only a couple of files which are untracked, and I want them added, I 
> lazily do "git add *".

I changed it to warn and refuse and made it only add under -f flag.

Johannes Schindelin· Dec 26, 2006, 22:48 UTC · re: Junio C Hamano · lore

Re: [PATCH 5/5] git-add: add ignored files when asked explicitly.

Hi,
On Tue, 26 Dec 2006, Junio C Hamano wrote:
Show 17 quoted lines
> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> 
> >> > Most people do
> >> >
> >> > 	$ git add *.o
> >> >
> >> > instead, where bash expands the expression. Maybe this new behaviour 
> >> > should be hidden between a "-f" option?
> >> 
> >> When would anybody do "git add *.o"?
> >
> > Not exactly "git add *.o", but when I see in git-status that there are 
> > only a couple of files which are untracked, and I want them added, I 
> > lazily do "git add *".
> 
> I changed it to warn and refuse and made it only add under -f
> flag.
Thanks!

Ciao, Dscho

Andreas Ericsson· Jan 4, 2007, 13:58 UTC · re: Junio C Hamano · lore

Re: [PATCH 5/5] git-add: add ignored files when asked explicitly.

Junio C Hamano wrote:
Show 15 quoted lines
> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> 
>> On Mon, 25 Dec 2006, Junio C Hamano wrote:
>>
>>>     $ git add foo.o
>>>     $ git add '*.o'
>> Most people do
>>
>> 	$ git add *.o
>>
>> instead, where bash expands the expression. Maybe this new behaviour 
>> should be hidden between a "-f" option?
> 
> When would anybody do "git add *.o"?
> 

Make that "git add *.c" then, in a directory normally containing a lot of generated C-files.

Show 11 quoted lines
> 
> An alternative is to use the mechanism I added here to _detect_
> the attempt to add an ignored file with explicitly spelled out
> pathspec, and issue an info message that says something like:
> 
> 	Path 'xyzzy/filfre.o' is not being ignored by one of
> 	your .gitignore files.  If you really want to add it,
> 	please add this entry to .gitignore file:
> 
>         !/xyzzy/filfre.o
> 

Sounds very sensible to me, although I assume you meant "path xyzzy/filfre.o is being ignored" (ie, "is not being ignored" (sic) was a typo).

Show 11 quoted lines
> One advantage of this is that it would help guiding the user in
> the right direction, giving a reusable piece of knowledge,
> without changing the behaviour of the command (what is refused
> is refused).  But I can already see people's complaints: if the
> tool knows how to fix that situation why forces the user to do
> so?
> 
> Although the reason why the alternative does not do so is "The
> user earlier said *.o files are uninteresting but came back with
> a conflicting request to add xyzzy/filfre.o, which could be a
> mistake.  We ask for a confirmation", which is very sensible,

Very sensible indeed. If you tell a cabdriver "go-left-go-right" (very fast) he'll (hopefully) stop and ask you where you really wanted to go.

Show 8 quoted lines
> another alternative would be to add the path anyway and issue an
> warning, like this:
> 
> 	$ ls xyzzy
>         filfre.c	filfre.o
> 	$ git add xyzzy/filfre.?
> 	added ignored path xyzzy/filfre.o
> 
I like the "you did something weird. Education served" option better.
-- 
Andreas Ericsson                   andreas.ericsson@op5.se
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231

← back to recent threads