Skip to content

Commit cd8c891

Browse files
committed
Merge branch 'dw/diff-no-index-doc'
When the user types "git diff" outside a working tree, thinking he is inside one, the current error message that is a single-liner "usage: git diff --no-index <path> <path>" may not be sufficient to make him realize the mistake. Add "Not a git repository" to the error message when we fell into the "--no-index" mode without an explicit command line option to instruct us to do so. * dw/diff-no-index-doc: diff --no-index: describe in a separate paragraph diff --no-index: clarify operation when not inside a repository
2 parents 8fbb07e + 286bc12 commit cd8c891

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

Documentation/git-diff.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,15 @@ two blob objects, or changes between two files on disk.
2828
words, the differences are what you _could_ tell Git to
2929
further add to the index but you still haven't. You can
3030
stage these changes by using linkgit:git-add[1].
31-
+
32-
If exactly two paths are given and at least one points outside
33-
the current repository, 'git diff' will compare the two files /
34-
directories. This behavior can be forced by --no-index.
31+
32+
'git diff' --no-index [--options] [--] [<path>...]::
33+
34+
This form is to compare the given two paths on the
35+
filesystem. You can omit the `--no-index` option when
36+
running the command in a working tree controlled by Git and
37+
at least one of the paths points outside the working tree,
38+
or when running the command outside a working tree
39+
controlled by Git.
3540

3641
'git diff' [--options] --cached [<commit>] [--] [<path>...]::
3742

diff-no-index.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,21 @@ void diff_no_index(struct rev_info *revs,
215215
path_inside_repo(prefix, argv[i+1])))
216216
return;
217217
}
218-
if (argc != i + 2)
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. */
219230
usagef("git diff %s <path> <path>",
220231
no_index ? "--no-index" : "[--no-index]");
232+
}
221233

222234
diff_setup(&revs->diffopt);
223235
for (i = 1; i < argc - 2; ) {

0 commit comments

Comments
 (0)