threads / discuss / 1852

Improved "git add"

Subject: Improved "git add"

## tl;dr

3 messages between Sep 18, 2005 and Sep 18, 2005.

replies: 2people: 3as markdown or json

Linus Torvalds· Sep 18, 2005, 18:27 UTC · lore

This fixes everybodys favourite complaint about "git add", namely that it doesn't take directories.

We use "git-ls-files --others" to generate an arbitrary list of filenames, and thus also automatically honor ignore-files while we're at it.

Side note: there's a lot of room for improvement here. In particular, if we have a long list of filenames (importing a big archive), this will just do a big stupid for-loop and add them one at a time. Maybe it should use

	generate-list | xargs -0 git-update-idex --add --
instead.

Also, I think we should have a default ignore list if we don't find a .git/info/exclude file. Ignoring "*.o" and ".*" by default would probably be the right thing to do.

But I think this is a good first step.

Use the "-n" flag to just show the list of files to be added without adding them.

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---

NOTE! This depends on the previous patch that added "--git-dir" to git-rev-parse

diff --git a/git-add.sh b/git-add.sh
--- a/git-add.sh
+++ b/git-add.sh
@@ -1,2 +1,32 @@
 #!/bin/sh
-git-update-index --add -- "$@"
+
+show_only=
+verbose=
+while : ; do
+  case "$1" in
+    -n)
+	show_only=true
+	verbose=true
+	;;
+    -v)
+	verbose=true
+	;;
+    *)
+	break
+	;;
+  esac
+  shift
+done
+
+GIT_DIR=$(git-rev-parse --git-dir) || exit
+global_exclude=
+if [ -f "$GIT_DIR/info/exclude" ]; then
+   global_exclude="--exclude-from=$GIT_DIR/info/exclude"
+fi
+for i in $(git-ls-files --others \
+	$global_exclude --exclude-per-directory=.gitignore \
+	"$@")
+do
+   [ "$verbose" ] && echo "  $i"
+   [ "$show_only" ] || git-update-index --add -- "$i" || exit
+done
Nico -telmich- Schottelius· Sep 18, 2005, 21:29 UTC · re: Linus Torvalds · lore

Re: Improved "git add"

Linus Torvalds [Sun, Sep 18, 2005 at 11:27:45AM -0700]:
> [git-add/directory fix]
Nice, first cogito and now git itself. You are pretty fast.
> [...]
> Also, I think we should have a default ignore list if we don't find a 
> .git/info/exclude file. Ignoring "*.o" and ".*" by default would probably 
> be the right thing to do.

I do use doc/.warning or similar in some projects files for outputting a warning when somebody simply types 'make', so he'll get instructions on what to do.

Other people may even want to include *.o in their tree, because it's used by their program for another purpose.

I do not think that a VCS should ignore anything from itself. If we generate a standard .git/info/excludes (or whatever the name of it is) and write those standard values in there, this is a different thing.

Nico
-- 
Latest project: cconfig (http://nico.schotteli.us/papers/linux/cconfig/)
Open Source nutures open minds and free, creative developers.
Petr Baudis· Sep 18, 2005, 22:17 UTC · re: Nico -telmich- Schottelius · lore

Re: Improved "git add"

Dear diary, on Sun, Sep 18, 2005 at 11:29:04PM CEST, I got a letter where Nico -telmich- Schottelius <nico-linux-git@schottelius.org> told me that...

Show 20 quoted lines
> Linus Torvalds [Sun, Sep 18, 2005 at 11:27:45AM -0700]:
> > [git-add/directory fix]
> 
> Nice, first cogito and now git itself. You are pretty fast.
> 
> > [...]
> > Also, I think we should have a default ignore list if we don't find a 
> > .git/info/exclude file. Ignoring "*.o" and ".*" by default would probably 
> > be the right thing to do.
> 
> I do use doc/.warning or similar in some projects files for outputting
> a warning when somebody simply types 'make', so he'll get instructions
> on what to do.
> 
> Other people may even want to include *.o in their tree, because it's
> used by their program for another purpose.
> 
> I do not think that a VCS should ignore anything from itself. If we
> generate a standard .git/info/excludes (or whatever the name of it is)
> and write those standard values in there, this is a different thing.

But .git/info/exclude mostly sucks, you really want these kinds of rules in .gitignore if anywhere. E.g. CVS by default ignores some stuff and it seems as a fine strategy for me, and is working out in the vast majority of cases. You can unignore files by doing '!pattern', so if you want to throw away all the defaults, just specify '!*'.

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
If you want the holes in your knowledge showing up try teaching
someone.  -- Alan Cox

← back to recent threads