Skip to content

Commit caf4f58

Browse files
author
Junio C Hamano
committed
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>
1 parent 22ddf71 commit caf4f58

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

git-add.sh

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ while : ; do
66
case "$1" in
77
-n)
88
show_only=true
9-
verbose=true
109
;;
1110
-v)
12-
verbose=true
11+
verbose=--verbose
1312
;;
1413
*)
1514
break
@@ -19,14 +18,19 @@ while : ; do
1918
done
2019

2120
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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@
1313
* like "git-update-index *" and suddenly having all the object
1414
* files be revision controlled.
1515
*/
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;
1723
static int force_remove;
24+
static int verbose;
1825

1926
/* Three functions to allow overloaded pointer return; see linux/err.h */
2027
static inline void *ERR_PTR(long error)
@@ -32,6 +39,19 @@ static inline long IS_ERR(const void *ptr)
3239
return (unsigned long)ptr > (unsigned long)-1000L;
3340
}
3441

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+
3555
static int add_file_to_cache(const char *path)
3656
{
3757
int size, namelen, option, status;
@@ -260,7 +280,11 @@ static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3)
260280
ce->ce_mode = create_ce_mode(mode);
261281
option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
262282
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;
264288
}
265289

266290
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)
300324
if (force_remove) {
301325
if (remove_file_from_cache(p))
302326
die("git-update-index: unable to remove %s", path);
327+
report("remove '%s'", path);
303328
return;
304329
}
305330
if (add_file_to_cache(p))
306331
die("Unable to process file %s", path);
332+
report("add '%s'", path);
307333
}
308334

309335
static void read_index_info(int line_termination)
@@ -447,6 +473,10 @@ int main(int argc, const char **argv)
447473
not_new = 1;
448474
continue;
449475
}
476+
if (!strcmp(path, "--verbose")) {
477+
verbose = 1;
478+
continue;
479+
}
450480
die("unknown option %s", path);
451481
}
452482
update_one(path, prefix, prefix_length);

0 commit comments

Comments
 (0)