@matheusbsbs / matheus-bagliano / commits / 4f38f6b5ba
Fork 0

diff*: fix worktree setup

This fixes "git diff", "git diff-files" and "git diff-index" to work correctly under worktree setup. Because diff* family works in many modes and not all of them require worktree, Junio made a nice summary (with a little modification from me): * diff-files is about comparing with work tree, so it obviously needs a work tree; * diff-index also does, except "diff-index --cached" or "diff --cached TREE" * no-index is about random files outside git context, so it obviously doesn't need any work tree; * comparing two (or more) trees doesn't; * comparing two blobs doesn't; * comparing a blob with a random file doesn't; Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Aug 28, 2008 at 20:02 UTC 4f38f6b5bafb1f7f85c7b54d0bb0a0e977cd947c
4 files changed +63 -3
builtin-diff-index.c
+2
@@ -39,6 +39,8 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
39 if (rev.pending.nr != 1 ||
40 rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
41 usage(diff_cache_usage);
42 + if (!cached)
43 + setup_work_tree();
44 if (read_cache() < 0) {
45 perror("read_cache");
46 return -1;
builtin-diff.c
+3
@@ -122,6 +122,8 @@ static int builtin_diff_index(struct rev_info *revs,
122 usage(builtin_diff_usage);
123 argv++; argc--;
124 }
125 + if (!cached)
126 + setup_work_tree();
127 /*
128 * Make sure there is one revision (i.e. pending object),
129 * 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
227 (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
228 revs->combine_merges = revs->dense_combined_merges = 1;
229
230 + setup_work_tree();
231 if (read_cache() < 0) {
232 perror("read_cache");
233 return -1;
git.c
+1 -1
@@ -286,7 +286,7 @@ static void handle_internal_command(int argc, const char **argv)
286 { "count-objects", cmd_count_objects, RUN_SETUP },
287 { "describe", cmd_describe, RUN_SETUP },
288 { "diff", cmd_diff },
289 - { "diff-files", cmd_diff_files, RUN_SETUP },
289 + { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
290 { "diff-index", cmd_diff_index, RUN_SETUP },
291 { "diff-tree", cmd_diff_tree, RUN_SETUP },
292 { "fast-export", cmd_fast_export, RUN_SETUP },
t/t1501-worktree.sh
+57 -2
@@ -28,6 +28,7 @@ test_rev_parse() {
28 [ $# -eq 0 ] && return
29 }
30
31 +EMPTY_TREE=$(git write-tree)
32 mkdir -p work/sub/dir || exit 1
33 mv .git repo.git || exit 1
34
@@ -106,12 +107,66 @@ test_expect_success 'repo finds its work tree from work tree, too' '
107 '
108
109 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 &&
111 GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
112 git diff --exit-code tracked &&
113 echo changed > tracked &&
114 ! 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_done