git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCHv2] git-diff: Clarify operation when not inside a repository.

From
Dale R. Worley <worley@alum.mit.edu>
Date
Aug 22, 2013, 20:31 UTC
Message-ID
<201308222031.r7MKVL6O028293@freeze.ariadne.com>
In-Reply-To
<xmqqwqneuc69.fsf@gitster.dls.corp.google.com>

Clarify documentation for git-diff: State that when not inside a repository, --no-index is implied (and thus two arguments are mandatory).

Clarify error message from diff-no-index to inform user that CWD is not inside a repository and thus two arguments are mandatory.

Signed-off-by: Dale Worley <worley@ariadne.com>
---

The error message has been updated from [PATCH]. "git diff" outside a repository now produces:

    Not a git repository
    To compare two paths outside a working tree:
    usage: git diff [--no-index] <path> <path>

This should inform the user of his error regardless of whether he intended to perform a within-repository "git diff" or an out-of-repository "git diff".

This message is closer to the message that other Git commands produce:
    fatal: Not a git repository (or any parent up to mount parent )
    Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

"git diff --no-index" produces the same message as before (since the user is clearly invoking the non-repository behavior):

    usage: git diff --no-index <path> <path>

Regarding the change to git-diff.txt, perhaps "forced ... by executing 'git diff' outside of a working tree" is not the best wording, but it should be clear to the reader that (1) it is possible to execute 'git diff' outside of a working tree, and (2) when doing so, the behavior will be as if '--no-index' was specified.

I've also added some comments for the new code.
 Documentation/git-diff.txt |    3 ++-
 diff-no-index.c            |   12 +++++++++++-
 2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/Documentation/git-diff.txt b/Documentation/git-diff.txt
index 78d6d50..9f74989 100644
--- a/Documentation/git-diff.txt
+++ b/Documentation/git-diff.txt
@@ -31,7 +31,8 @@ two blob objects, or changes between two files on disk.
 +
 If exactly two paths are given and at least one points outside
 the current repository, 'git diff' will compare the two files /
-directories. This behavior can be forced by --no-index.
+directories. This behavior can be forced by --no-index or by 
+executing 'git diff' outside of a working tree.
 
 'git diff' [--options] --cached [<commit>] [--] [<path>...]::
 
diff --git a/diff-no-index.c b/diff-no-index.c
index e66fdf3..9734ec3 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -215,9 +215,19 @@ void diff_no_index(struct rev_info *revs,
 		     path_inside_repo(prefix, argv[i+1])))
 			return;
 	}
-	if (argc != i + 2)
+	if (argc != i + 2) {
+	        if (!no_index) {
+		        /* There was no --no-index and there were not two
+			 * paths.  It is possible that the user intended
+			 * to do an inside-repository operation. */
+		        fprintf(stderr, "Not a git repository\n");
+		        fprintf(stderr,
+				"To compare two paths outside a working tree:\n");
+		}
+		/* Give the usage message for non-repository usage and exit. */
 		usagef("git diff %s <path> <path>",
 		       no_index ? "--no-index" : "[--no-index]");
+	}
 
 	diff_setup(&revs->diffopt);
 	for (i = 1; i < argc - 2; ) {
-- 
1.7.7.6
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 7 in “git-diff: Clarify operation when not inside a repository.”
  1. git-diff: Clarify operation when not inside a repository.Dale R. Worley, Aug 21, 2013
  2. Junio C HamanoAug 21, 2013
  3. [PATCHv2] git-diff: Clarify operation when not inside a repository.Dale R. Worley, Aug 22, 2013
  4. Junio C HamanoAug 22, 2013
  5. Dale R. WorleyAug 23, 2013
  6. Junio C HamanoAug 28, 2013
  7. Dale R. WorleyAug 29, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.