diff: move no-index detection to builtin/diff.c

Currently the --no-index option is parsed in diff_no_index(). Move the detection if a no-index diff should be executed to builtin/diff.c, where we can use it for executing diff_no_index() conditionally. This will also allow us to execute other operations conditionally, which will be done in the next patch. There are no functional changes. Helped-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Dec 11, 2013 at 10:58 UTC 470faf96544c1844ad775c695dfa370fe0ef3756
3 files changed +51 -47
builtin/diff.c
+49 -3
@@ -16,6 +16,9 @@
16 #include "submodule.h"
17 #include "sha1-array.h"
18
19 +#define DIFF_NO_INDEX_EXPLICIT 1
20 +#define DIFF_NO_INDEX_IMPLICIT 2
21 +
22 struct blobinfo {
23 unsigned char sha1[20];
24 const char *name;
@@ -257,7 +260,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
260 int blobs = 0, paths = 0;
261 const char *path = NULL;
262 struct blobinfo blob[2];
260 - int nongit;
263 + int nongit = 0, no_index = 0;
264 int result = 0;
265
266 /*
@@ -283,14 +286,57 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
286 * Other cases are errors.
287 */
288
289 + /* Were we asked to do --no-index explicitly? */
290 + for (i = 1; i < argc; i++) {
291 + if (!strcmp(argv[i], "--")) {
292 + i++;
293 + break;
294 + }
295 + if (!strcmp(argv[i], "--no-index"))
296 + no_index = DIFF_NO_INDEX_EXPLICIT;
297 + if (argv[i][0] != '-')
298 + break;
299 + }
300 +
301 prefix = setup_git_directory_gently(&nongit);
302 + /*
303 + * Treat git diff with at least one path outside of the
304 + * repo the same as if the command would have been executed
305 + * outside of a git repository. In this case it behaves
306 + * the same way as "git diff --no-index <a> <b>", which acts
307 + * as a colourful "diff" replacement.
308 + */
309 + if (nongit || ((argc == i + 2) &&
310 + (!path_inside_repo(prefix, argv[i]) ||
311 + !path_inside_repo(prefix, argv[i + 1]))))
312 + no_index = DIFF_NO_INDEX_IMPLICIT;
313 +
314 gitmodules_config();
315 git_config(git_diff_ui_config, NULL);
316
317 init_revisions(&rev, prefix);
318
292 - /* If this is a no-index diff, just run it and exit there. */
293 - diff_no_index(&rev, argc, argv, nongit, prefix);
319 + if (no_index) {
320 + if (argc != i + 2) {
321 + if (no_index == DIFF_NO_INDEX_IMPLICIT) {
322 + /*
323 + * There was no --no-index and there were not two
324 + * paths. It is possible that the user intended
325 + * to do an inside-repository operation.
326 + */
327 + fprintf(stderr, "Not a git repository\n");
328 + fprintf(stderr,
329 + "To compare two paths outside a working tree:\n");
330 + }
331 + /* Give the usage message for non-repository usage and exit. */
332 + usagef("git diff %s <path> <path>",
333 + no_index == DIFF_NO_INDEX_EXPLICIT ?
334 + "--no-index" : "[--no-index]");
335 +
336 + }
337 + /* If this is a no-index diff, just run it and exit there. */
338 + diff_no_index(&rev, argc, argv, prefix);
339 + }
340
341 /* Otherwise, we are doing the usual "git" diff */
342 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
diff-no-index.c
+1 -43
@@ -183,54 +183,12 @@ static int queue_diff(struct diff_options *o,
183
184 void diff_no_index(struct rev_info *revs,
185 int argc, const char **argv,
186 - int nongit, const char *prefix)
186 + const char *prefix)
187 {
188 int i, prefixlen;
189 - int no_index = 0;
189 unsigned deprecated_show_diff_q_option_used = 0;
190 const char *paths[2];
191
193 - /* Were we asked to do --no-index explicitly? */
194 - for (i = 1; i < argc; i++) {
195 - if (!strcmp(argv[i], "--")) {
196 - i++;
197 - break;
198 - }
199 - if (!strcmp(argv[i], "--no-index"))
200 - no_index = 1;
201 - if (argv[i][0] != '-')
202 - break;
203 - }
204 -
205 - if (!no_index && !nongit) {
206 - /*
207 - * Inside a git repository, without --no-index. Only
208 - * when a path outside the repository is given,
209 - * e.g. "git diff /var/tmp/[12]", or "git diff
210 - * Makefile /var/tmp/Makefile", allow it to be used as
211 - * a colourful "diff" replacement.
212 - */
213 - if ((argc != i + 2) ||
214 - (path_inside_repo(prefix, argv[i]) &&
215 - path_inside_repo(prefix, argv[i+1])))
216 - return;
217 - }
218 - if (argc != i + 2) {
219 - if (!no_index) {
220 - /*
221 - * There was no --no-index and there were not two
222 - * paths. It is possible that the user intended
223 - * to do an inside-repository operation.
224 - */
225 - fprintf(stderr, "Not a git repository\n");
226 - fprintf(stderr,
227 - "To compare two paths outside a working tree:\n");
228 - }
229 - /* Give the usage message for non-repository usage and exit. */
230 - usagef("git diff %s <path> <path>",
231 - no_index ? "--no-index" : "[--no-index]");
232 - }
233 -
192 diff_setup(&revs->diffopt);
193 for (i = 1; i < argc - 2; ) {
194 int j;
diff.h
+1 -1
@@ -330,7 +330,7 @@ extern int diff_flush_patch_id(struct diff_options *, unsigned char *);
330
331 extern int diff_result_code(struct diff_options *, int);
332
333 -extern void diff_no_index(struct rev_info *, int, const char **, int, const char *);
333 +extern void diff_no_index(struct rev_info *, int, const char **, const char *);
334
335 extern int index_differs_from(const char *def, int diff_flags);
336