Skip to content

Commit 93c44d4

Browse files
peffJunio C Hamano
authored andcommitted
git-add: allow path limiting with -u
Rather than updating all working tree paths, we limit ourselves to paths listed on the command line. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 16a4c61 commit 93c44d4

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

builtin-add.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
static const char builtin_add_usage[] =
1717
"git-add [-n] [-v] [-f] [--interactive | -i] [-u] [--] <filepattern>...";
1818

19-
static int take_all_worktree_changes;
19+
static int take_worktree_changes;
2020
static const char *excludes_file;
2121

2222
static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
@@ -122,11 +122,12 @@ static void update_callback(struct diff_queue_struct *q,
122122
}
123123
}
124124

125-
static void update_all(int verbose)
125+
static void update(int verbose, const char **files)
126126
{
127127
struct rev_info rev;
128128
init_revisions(&rev, "");
129129
setup_revisions(0, NULL, &rev, NULL);
130+
rev.prune_data = get_pathspec(rev.prefix, files);
130131
rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
131132
rev.diffopt.format_callback = update_callback;
132133
rev.diffopt.format_callback_data = &verbose;
@@ -200,16 +201,14 @@ int cmd_add(int argc, const char **argv, const char *prefix)
200201
continue;
201202
}
202203
if (!strcmp(arg, "-u")) {
203-
take_all_worktree_changes = 1;
204+
take_worktree_changes = 1;
204205
continue;
205206
}
206207
usage(builtin_add_usage);
207208
}
208209

209-
if (take_all_worktree_changes) {
210-
if (i < argc)
211-
die("-u and explicit paths are incompatible");
212-
update_all(verbose);
210+
if (take_worktree_changes) {
211+
update(verbose, argv + i);
213212
goto finish;
214213
}
215214

t/t2200-add-update.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/sh
2+
3+
test_description='git-add -u with path limiting
4+
5+
This test creates a working tree state with three files:
6+
7+
top (previously committed, modified)
8+
dir/sub (previously committed, modified)
9+
dir/other (untracked)
10+
11+
and issues a git-add -u with path limiting on "dir" to add
12+
only the updates to dir/sub.'
13+
14+
. ./test-lib.sh
15+
16+
test_expect_success 'setup' '
17+
echo initial >top &&
18+
mkdir dir &&
19+
echo initial >dir/sub &&
20+
git-add dir/sub top &&
21+
git-commit -m initial &&
22+
echo changed >top &&
23+
echo changed >dir/sub &&
24+
echo other >dir/other
25+
'
26+
27+
test_expect_success 'update' 'git-add -u dir'
28+
29+
test_expect_success 'update touched correct path' \
30+
'test "`git-diff-files --name-status dir/sub`" = ""'
31+
32+
test_expect_success 'update did not touch other tracked files' \
33+
'test "`git-diff-files --name-status top`" = "M top"'
34+
35+
test_expect_success 'update did not touch untracked files' \
36+
'test "`git-diff-files --name-status dir/other`" = ""'
37+
38+
test_done

0 commit comments

Comments
 (0)