Skip to content

Commit 470faf9

Browse files
tgummerergitster
authored andcommitted
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>
1 parent d2446df commit 470faf9

File tree

3 files changed

+51
-47
lines changed

3 files changed

+51
-47
lines changed

builtin/diff.c

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include "submodule.h"
1717
#include "sha1-array.h"
1818

19+
#define DIFF_NO_INDEX_EXPLICIT 1
20+
#define DIFF_NO_INDEX_IMPLICIT 2
21+
1922
struct blobinfo {
2023
unsigned char sha1[20];
2124
const char *name;
@@ -257,7 +260,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
257260
int blobs = 0, paths = 0;
258261
const char *path = NULL;
259262
struct blobinfo blob[2];
260-
int nongit;
263+
int nongit = 0, no_index = 0;
261264
int result = 0;
262265

263266
/*
@@ -283,14 +286,57 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
283286
* Other cases are errors.
284287
*/
285288

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+
286301
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+
287314
gitmodules_config();
288315
git_config(git_diff_ui_config, NULL);
289316

290317
init_revisions(&rev, prefix);
291318

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+
}
294340

295341
/* Otherwise, we are doing the usual "git" diff */
296342
rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;

diff-no-index.c

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -183,54 +183,12 @@ static int queue_diff(struct diff_options *o,
183183

184184
void diff_no_index(struct rev_info *revs,
185185
int argc, const char **argv,
186-
int nongit, const char *prefix)
186+
const char *prefix)
187187
{
188188
int i, prefixlen;
189-
int no_index = 0;
190189
unsigned deprecated_show_diff_q_option_used = 0;
191190
const char *paths[2];
192191

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-
234192
diff_setup(&revs->diffopt);
235193
for (i = 1; i < argc - 2; ) {
236194
int j;

diff.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ extern int diff_flush_patch_id(struct diff_options *, unsigned char *);
330330

331331
extern int diff_result_code(struct diff_options *, int);
332332

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 *);
334334

335335
extern int index_differs_from(const char *def, int diff_flags);
336336

0 commit comments

Comments
 (0)