@matheusbsbs / matheus-bagliano / commits / caf4f582b2
Fork 0

Improve "git add" again.

This makes it possible to add paths that have funny characters (TAB and LF) in them, and makes adding many paths more efficient in general. New flag "--stdin" to update-index was initially added for different purpose, but it turns out to be a perfect match for feeding "ls-files --others -z" output to improve "git add". It also adds "--verbose" flag to update-index for use with "git add" command. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 14, 2005 at 21:56 UTC caf4f582b2bb4d5582540aa49d29749b2600cd03
2 files changed +49 -15
git-add.sh
+17 -13
@@ -6,10 +6,9 @@ while : ; do
6 case "$1" in
7 -n)
8 show_only=true
9 - verbose=true
9 ;;
10 -v)
12 - verbose=true
11 + verbose=--verbose
12 ;;
13 *)
14 break
@@ -19,14 +18,19 @@ while : ; do
18 done
19
20 GIT_DIR=$(git-rev-parse --git-dir) || exit
22 -global_exclude=
23 -if [ -f "$GIT_DIR/info/exclude" ]; then
24 - global_exclude="--exclude-from=$GIT_DIR/info/exclude"
25 -fi
26 -for i in $(git-ls-files --others \
27 - $global_exclude --exclude-per-directory=.gitignore \
28 - "$@")
29 -do
30 - [ "$verbose" ] && echo " $i"
31 - [ "$show_only" ] || git-update-index --add -- "$i" || exit
32 -done
21 +
22 +if test -f "$GIT_DIR/info/exclude"
23 +then
24 + git-ls-files -z \
25 + --exclude-from="$GIT_DIR/info/exclude" \
26 + --others --exclude-per-directory=.gitignore "$@"
27 +else
28 + git-ls-files -z \
29 + --others --exclude-per-directory=.gitignore "$@"
30 +fi |
31 +case "$show_only" in
32 +true)
33 + xargs -0 echo ;;
34 +*)
35 + git-update-index --add $verbose -z --stdin ;;
36 +esac
update-index.c
+32 -2
@@ -13,8 +13,15 @@
13 * like "git-update-index *" and suddenly having all the object
14 * files be revision controlled.
15 */
16 -static int allow_add = 0, allow_remove = 0, allow_replace = 0, allow_unmerged = 0, not_new = 0, quiet = 0, info_only = 0;
16 +static int allow_add;
17 +static int allow_remove;
18 +static int allow_replace;
19 +static int allow_unmerged; /* --refresh needing merge is not error */
20 +static int not_new; /* --refresh not having working tree files is not error */
21 +static int quiet; /* --refresh needing update is not error */
22 +static int info_only;
23 static int force_remove;
24 +static int verbose;
25
26 /* Three functions to allow overloaded pointer return; see linux/err.h */
27 static inline void *ERR_PTR(long error)
@@ -32,6 +39,19 @@ static inline long IS_ERR(const void *ptr)
39 return (unsigned long)ptr > (unsigned long)-1000L;
40 }
41
42 +static void report(const char *fmt, ...)
43 +{
44 + va_list vp;
45 +
46 + if (!verbose)
47 + return;
48 +
49 + va_start(vp, fmt);
50 + vprintf(fmt, vp);
51 + putchar('\n');
52 + va_end(vp);
53 +}
54 +
55 static int add_file_to_cache(const char *path)
56 {
57 int size, namelen, option, status;
@@ -260,7 +280,11 @@ static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3)
280 ce->ce_mode = create_ce_mode(mode);
281 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
282 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
263 - return add_cache_entry(ce, option);
283 + if (add_cache_entry(ce, option))
284 + return error("%s: cannot add to the index - missing --add option?",
285 + arg3);
286 + report("add '%s'", arg3);
287 + return 0;
288 }
289
290 static int chmod_path(int flip, const char *path)
@@ -300,10 +324,12 @@ static void update_one(const char *path, const char *prefix, int prefix_length)
324 if (force_remove) {
325 if (remove_file_from_cache(p))
326 die("git-update-index: unable to remove %s", path);
327 + report("remove '%s'", path);
328 return;
329 }
330 if (add_file_to_cache(p))
331 die("Unable to process file %s", path);
332 + report("add '%s'", path);
333 }
334
335 static void read_index_info(int line_termination)
@@ -447,6 +473,10 @@ int main(int argc, const char **argv)
473 not_new = 1;
474 continue;
475 }
476 + if (!strcmp(path, "--verbose")) {
477 + verbose = 1;
478 + continue;
479 + }
480 die("unknown option %s", path);
481 }
482 update_one(path, prefix, prefix_length);