Skip to content

Commit 6a42cfe

Browse files
committed
Merge branch 'nd/worktree' into maint
* nd/worktree: setup_git_directory(): fix move to worktree toplevel directory update-index: fix worktree setup read-tree: setup worktree if merge is required grep: fix worktree setup diff*: fix worktree setup
2 parents 36f44a0 + bb52863 commit 6a42cfe

File tree

8 files changed

+81
-4
lines changed

8 files changed

+81
-4
lines changed

builtin-diff-index.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
3939
if (rev.pending.nr != 1 ||
4040
rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
4141
usage(diff_cache_usage);
42+
if (!cached)
43+
setup_work_tree();
4244
if (read_cache() < 0) {
4345
perror("read_cache");
4446
return -1;

builtin-diff.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ static int builtin_diff_index(struct rev_info *revs,
122122
usage(builtin_diff_usage);
123123
argv++; argc--;
124124
}
125+
if (!cached)
126+
setup_work_tree();
125127
/*
126128
* Make sure there is one revision (i.e. pending object),
127129
* and there is no revision filtering parameters.
@@ -225,6 +227,7 @@ static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv
225227
(revs->diffopt.output_format & DIFF_FORMAT_PATCH))
226228
revs->combine_merges = revs->dense_combined_merges = 1;
227229

230+
setup_work_tree();
228231
if (read_cache() < 0) {
229232
perror("read_cache");
230233
return -1;

builtin-grep.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
783783
paths[1] = NULL;
784784
}
785785

786-
if (!list.nr)
786+
if (!list.nr) {
787+
if (!cached)
788+
setup_work_tree();
787789
return !grep_cache(&opt, paths, cached);
790+
}
788791

789792
if (cached)
790793
die("both --cached and trees are given.");

builtin-read-tree.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
194194
usage(read_tree_usage);
195195
if ((opts.dir && !opts.update))
196196
die("--exclude-per-directory is meaningless unless -u");
197+
if (opts.merge && !opts.index_only)
198+
setup_work_tree();
197199

198200
if (opts.merge) {
199201
if (stage < 2)

builtin-update-index.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,10 +614,12 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
614614
continue;
615615
}
616616
if (!strcmp(path, "--refresh")) {
617+
setup_work_tree();
617618
has_errors |= refresh_cache(refresh_flags);
618619
continue;
619620
}
620621
if (!strcmp(path, "--really-refresh")) {
622+
setup_work_tree();
621623
has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
622624
continue;
623625
}
@@ -684,6 +686,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
684686
goto finish;
685687
}
686688
if (!strcmp(path, "--again") || !strcmp(path, "-g")) {
689+
setup_work_tree();
687690
has_errors = do_reupdate(argc - i, argv + i,
688691
prefix, prefix_length);
689692
if (has_errors)
@@ -702,6 +705,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
702705
usage(update_index_usage);
703706
die("unknown option %s", path);
704707
}
708+
setup_work_tree();
705709
p = prefix_path(prefix, prefix_length, path);
706710
update_one(p, NULL, 0);
707711
if (set_executable_bit)
@@ -714,6 +718,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
714718

715719
strbuf_init(&buf, 0);
716720
strbuf_init(&nbuf, 0);
721+
setup_work_tree();
717722
while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
718723
const char *p;
719724
if (line_termination && buf.buf[0] == '"') {

git.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ static void handle_internal_command(int argc, const char **argv)
286286
{ "count-objects", cmd_count_objects, RUN_SETUP },
287287
{ "describe", cmd_describe, RUN_SETUP },
288288
{ "diff", cmd_diff },
289-
{ "diff-files", cmd_diff_files, RUN_SETUP },
289+
{ "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
290290
{ "diff-index", cmd_diff_index, RUN_SETUP },
291291
{ "diff-tree", cmd_diff_tree, RUN_SETUP },
292292
{ "fast-export", cmd_fast_export, RUN_SETUP },

setup.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,8 @@ const char *setup_git_directory(void)
581581
if (retval && chdir(retval))
582582
die ("Could not jump back into original cwd");
583583
rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
584+
if (rel && *rel && chdir(get_git_work_tree()))
585+
die ("Could not jump to working directory");
584586
return rel && *rel ? strcat(rel, "/") : NULL;
585587
}
586588

t/t1501-worktree.sh

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ test_rev_parse() {
2828
[ $# -eq 0 ] && return
2929
}
3030

31+
EMPTY_TREE=$(git write-tree)
3132
mkdir -p work/sub/dir || exit 1
3233
mv .git repo.git || exit 1
3334

@@ -106,12 +107,71 @@ test_expect_success 'repo finds its work tree from work tree, too' '
106107
'
107108

108109
test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' '
109-
cd repo.git/work/sub/dir &&
110+
(cd repo.git/work/sub/dir &&
110111
GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
111112
git diff --exit-code tracked &&
112113
echo changed > tracked &&
113114
! GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
114-
git diff --exit-code tracked
115+
git diff --exit-code tracked)
116+
'
117+
cat > diff-index-cached.expected <<\EOF
118+
:000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A sub/dir/tracked
119+
EOF
120+
cat > diff-index.expected <<\EOF
121+
:000000 100644 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 A sub/dir/tracked
122+
EOF
123+
124+
125+
test_expect_success 'git diff-index' '
126+
GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work git diff-index $EMPTY_TREE > result &&
127+
test_cmp diff-index.expected result &&
128+
GIT_DIR=repo.git git diff-index --cached $EMPTY_TREE > result &&
129+
test_cmp diff-index-cached.expected result
130+
'
131+
cat >diff-files.expected <<\EOF
132+
:100644 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0000000000000000000000000000000000000000 M sub/dir/tracked
133+
EOF
134+
135+
test_expect_success 'git diff-files' '
136+
GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work git diff-files > result &&
137+
test_cmp diff-files.expected result
138+
'
139+
140+
cat >diff-TREE.expected <<\EOF
141+
diff --git a/sub/dir/tracked b/sub/dir/tracked
142+
new file mode 100644
143+
index 0000000..5ea2ed4
144+
--- /dev/null
145+
+++ b/sub/dir/tracked
146+
@@ -0,0 +1 @@
147+
+changed
148+
EOF
149+
cat >diff-TREE-cached.expected <<\EOF
150+
diff --git a/sub/dir/tracked b/sub/dir/tracked
151+
new file mode 100644
152+
index 0000000..e69de29
153+
EOF
154+
cat >diff-FILES.expected <<\EOF
155+
diff --git a/sub/dir/tracked b/sub/dir/tracked
156+
index e69de29..5ea2ed4 100644
157+
--- a/sub/dir/tracked
158+
+++ b/sub/dir/tracked
159+
@@ -0,0 +1 @@
160+
+changed
161+
EOF
162+
163+
test_expect_success 'git diff' '
164+
GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work git diff $EMPTY_TREE > result &&
165+
test_cmp diff-TREE.expected result &&
166+
GIT_DIR=repo.git git diff --cached $EMPTY_TREE > result &&
167+
test_cmp diff-TREE-cached.expected result &&
168+
GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work git diff > result &&
169+
test_cmp diff-FILES.expected result
170+
'
171+
172+
test_expect_success 'git grep' '
173+
(cd repo.git/work/sub &&
174+
GIT_DIR=../.. GIT_WORK_TREE=.. git grep -l changed | grep -q dir/tracked)
115175
'
116176

117177
test_done

0 commit comments

Comments
 (0)