git-add: add ignored files when asked explicitly.
PATCH 5/5 git-add: add ignored files when asked explicitly.
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 barOnly the first form adds foo.o to the index.
--- Documentation/git-add.txt | 11 ++++++++--- builtin-add.c | 11 ++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-)
Documentation/git-add.txt +8 -3
@@ -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.
builtin-add.c +10 -1
@@ -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 ))
Signature · 1 line
1.4.4.3.g50da
Hi,
Quoted 2 lines · Junio C Hamano
$ 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
Quoted 11 lines · Johannes Schindelin
Quoted 2 lines · Junio C Hamano
$ git add foo.o $ git add '*.o'Most people do
$ git add *.oinstead, 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.oPATCH 1/1 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.
---
* This is on top of what I sent out last night.
builtin-add.c | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
builtin-add.c +3 -0
@@ -37,6 +37,9 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int pfree (entry);continue ; } +if (entry->ignored_entry) +fprintf (stderr,"warning: '%s' is an ignored path.\n" , + entry->name); *dst++ = entry; } dir->nr = dst - dir->entries;
Signature · 1 line
1.4.4.3.g71b5
Re: [PATCH] git-add: warn when adding an ignored file with an explicit request.
Quoted 3 lines · Junio C Hamano
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?
Signature · 3 lines
Jakub Narebski Warsaw, Poland ShadeHawk on #git
Re: [PATCH] git-add: warn when adding an ignored file with an explicit request.
Hi,
Quoted 1 line · Jakub Narebski
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
Hi,
Quoted 7 lines · Junio C Hamano
Quoted 5 lines · Johannes Schindelin
Quoted 2 lines · Junio C Hamano
$ git add foo.o $ git add '*.o'
Quoted 8 lines
Quoted 6 lines
Most people do
$ git add *.oinstead, 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
Quoted 4 lines · Johannes Schindelin
Quoted 4 lines
Quoted 4 lines
Most people do
$ git add *.o
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
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.
Hi,
Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Johannes Schindelin
Quoted 7 lines
Quoted 6 lines
Most people do
$ git add *.oinstead, where bash expands the expression. Maybe this new behaviour should be hidden between a "-f" option?
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
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
Quoted 9 lines · Junio C Hamano
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
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Johannes Schindelin
Quoted 2 lines · Junio C Hamano
$ git add foo.o $ git add '*.o'
Quoted 8 lines
Quoted 5 lines
Most people do
$ git add *.oinstead, 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.
Quoted 11 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).
Quoted 11 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.
Quoted 8 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.
Signature · 3 lines
Andreas Ericsson [email protected] OP5 AB www.op5.se Tel: +46 8-230225 Fax: +46 8-230231