Subject: Re: git diff prints long usage help if not run in git repo

From: Junio C Hamano <[email-masked]>
Date: Mon, 22 Aug 2022 12:43:54 -0700

Mohamed Akram <[email-masked]> writes:

> When running `git diff` in a directory that’s not a git repo, it
> prints a warning that this is the case, followed by a very long
> help text that, on your average terminal, causes it to scroll so
> much that the warning isn’t even visible. It should only print
> the warning and nothing else like other git commands. This was
> tested with git version 2.37.2 on macOS.

Thanks for a clearly written feature request.  I think it is not
unreasonable to stop after telling the user to use the --no-index
option.

But on the other hand, not using "--no-index" is not what causes
such a "git diff" outside any repository fail.  If the user said

     $ cd / && git diff

it is clear that the user does not know they need to say what they
want to compare against what.  The reason why the command fails is
not because the user did not say

     $ cd / && git diff --no-index

but because the user did not say

     $ cd / && git diff --no-index etc/passwd etc/group

So, I dunno.

Perhaps somebody can try the following patch and tell us if it is an
improvement.  I know it is for the specific case "cd / && git diff"
but it really depends on the reason why (1) the command was run
outside any repository and why (2) the command was run without any
arguments.  A shallow answer to both of them are "by mistake", but
what I am trying to get at is that understanding the reason why such
a mistake was made is probably the key to evaluate if the behaviour
updated by the attached patch is a good idea.

Thanks.

 diff-no-index.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git c/diff-no-index.c w/diff-no-index.c
index 9a8b09346b..c8f2cdce05 100644
--- c/diff-no-index.c
+++ w/diff-no-index.c
@@ -259,9 +259,10 @@ int diff_no_index(struct rev_info *revs,
 			     diff_no_index_usage, 0);
 	if (argc != 2) {
 		if (implicit_no_index)
-			warning(_("Not a git repository. Use --no-index to "
-				  "compare two paths outside a working tree"));
-		usage_with_options(diff_no_index_usage, options);
+			die(_("Not a git repository. Use --no-index to "
+			      "compare two paths outside a working tree"));
+		else
+			usage_with_options(diff_no_index_usage, options);
 	}
 	FREE_AND_NULL(options);
 	for (i = 0; i < 2; i++) {