git-diff: allow --no-index semantics a bit more
Even when inside a git work tree, if two paths are given and at least one is clearly outside the work tree, it cannot be a request to diff a tracked path anyway; allow such an invocation to use --no-index semantics. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
May 26, 2008 at 21:54 UTC
040366076fa0dc540309531997a987cc6d83a577
1 file changed
+32
-7
diff-no-index.c
+32
-7
@@ -144,6 +144,25 @@ static int queue_diff(struct diff_options *o,
144
}
145
}
146
147
+static int path_outside_repo(const char *path)
148
+{
149
+ /*
150
+ * We have already done setup_git_directory_gently() so we
151
+ * know we are inside a git work tree already.
152
+ */
153
+ const char *work_tree;
154
+ size_t len;
155
+
156
+ if (!is_absolute_path(path))
157
+ return 0;
158
+ work_tree = get_git_work_tree();
159
+ len = strlen(work_tree);
160
+ if (strncmp(path, work_tree, len) ||
161
+ (path[len] != '\0' && path[len] != '/'))
162
+ return 1;
163
+ return 0;
164
+}
165
+
166
void diff_no_index(struct rev_info *revs,
167
int argc, const char **argv,
168
int nongit, const char *prefix)
@@ -162,13 +181,19 @@ void diff_no_index(struct rev_info *revs,
181
break;
182
}
183
165
- /*
166
- * No explicit --no-index, but "git diff --opts A B" outside
167
- * a git repository is a cute hack to support.
168
- */
169
- if (!no_index && !nongit)
170
- return;
171
-
184
+ if (!no_index && !nongit) {
185
+ /*
186
+ * Inside a git repository, without --no-index. Only
187
+ * when a path outside the repository is given,
188
+ * e.g. "git diff /var/tmp/[12]", or "git diff
189
+ * Makefile /var/tmp/Makefile", allow it to be used as
190
+ * a colourful "diff" replacement.
191
+ */
192
+ if ((argc != i + 2) ||
193
+ (!path_outside_repo(argv[i]) &&
194
+ !path_outside_repo(argv[i+1])))
195
+ return;
196
+ }
197
if (argc != i + 2)
198
die("git diff %s takes two paths",
199
no_index ? "--no-index" : "[--no-index]");