{"thread":{"id":"62086","subject":"[bug?] git diff --cached in a non-git directory","startedAt":"2024-09-09T14:33:11Z","lastAt":"2024-09-09T20:26:42Z","messageCount":6,"participants":["Okke Timm","rsbecker@nexbridge.com","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"502451","messageId":"2886189B-4BB5-4230-B7AF-94F97A13D72F@timm.sh","threadId":"62086","inReplyTo":null,"subject":"[bug?] git diff --cached in a non-git directory","fromName":"Okke Timm","fromEmail":"okke@timm.sh","sentAt":"2024-09-09T14:26:20Z","receivedAt":"2024-09-09T14:33:11Z","isPatch":false,"sender":{"key":"okke@timm.sh","avatar":null},"body":"Thank you for filling out a Git bug report!\nPlease answer the following questions to help us understand your issue.\n\nWhat did you do before the bug happened? (Steps to reproduce your issue)\nI changed into a non-git directory.\nThen I called git with 'git diff --cached', which produced this error:\n\nerror: unknown option `cached'\nusage: git diff --no-index [<options>] <path> <path>\n[...]\n\nWhat did you expect to happen? (Expected behavior)\nA helfpul error message.\n\nWhat happened instead? (Actual behavior)\nAn unhelpful error message saying that cached is an unknown option.\n\nWhat's different between what you expected and what actually happened?\nMe being confused why git suddenly forgot that `git diff --cached` is an allowed option.\n\nAnything else you want to add:\nPlease fix and thank you for this great tool!\n\nPlease review the rest of the bug report below.\nYou can delete any lines you don't wish to share.\n\n\n[System Info]\ngit version:\ngit version 2.46.0\ncpu: arm64\nno commit associated with this build\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nfeature: fsmonitor--daemon\nlibcurl: 8.4.0\nzlib: 1.2.12\nuname: Darwin 23.6.0 Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:21 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T8103 arm64\ncompiler info: clang: 15.0.0 (clang-1500.3.9.4)\nlibc info: no libc information available\n$SHELL (typically, interactive shell): /opt/homebrew/bin/bash\n\n\n[Enabled Hooks]\nnot run from a git repository - no hooks to show\n\n"},{"id":"502458","messageId":"05b501db02ce$3bd49e30$b37dda90$@nexbridge.com","threadId":"62086","inReplyTo":"2886189B-4BB5-4230-B7AF-94F97A13D72F@timm.sh","subject":"RE: [bug?] git diff --cached in a non-git directory","fromName":"","fromEmail":"rsbecker@nexbridge.com","sentAt":"2024-09-09T15:37:46Z","receivedAt":"2024-09-09T15:46:26Z","isPatch":false,"sender":{"key":"randall.becker@nexbridge.ca","avatar":"https://avatars.githubusercontent.com/u/28956764?v=4"},"body":"On September 9, 2024 10:26 AM, Okke Timm wrote:\n>What did you do before the bug happened? (Steps to reproduce your issue) I\n>changed into a non-git directory.\n>Then I called git with 'git diff --cached', which produced this error:\n>\n>error: unknown option `cached'\n>usage: git diff --no-index [<options>] <path> <path> [...]\n>\n>What did you expect to happen? (Expected behavior) A helfpul error message.\n>\n>What happened instead? (Actual behavior) An unhelpful error message saying\nthat\n>cached is an unknown option.\n>\n>What's different between what you expected and what actually happened?\n>Me being confused why git suddenly forgot that `git diff --cached` is an\nallowed\n>option.\n>\n>Anything else you want to add:\n>Please fix and thank you for this great tool!\n>\n>Please review the rest of the bug report below.\n>You can delete any lines you don't wish to share.\n>\n>\n>[System Info]\n>git version:\n>git version 2.46.0\n>cpu: arm64\n>no commit associated with this build\n>sizeof-long: 8\n>sizeof-size_t: 8\n>shell-path: /bin/sh\n>feature: fsmonitor--daemon\n>libcurl: 8.4.0\n>zlib: 1.2.12\n>uname: Darwin 23.6.0 Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:21 PDT\n>2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T8103 arm64 compiler info:\n>clang: 15.0.0 (clang-1500.3.9.4) libc info: no libc information available\n$SHELL\n>(typically, interactive shell): /opt/homebrew/bin/bash\n>\n>\n>[Enabled Hooks]\n>not run from a git repository - no hooks to show\n\nBecause you are not in a git repository clone, the --cached option has no\nmeaning. It refers to the staging area only, so when not in a clone, git\ndoes not consider --cached as supported because there is no connection\nbetween an external directory and an internal. If you want to perform a diff\nfrom cached vs. external, run the git diff from inside the clone referencing\nthe internal and external file after --.\n\n"},{"id":"502459","messageId":"A95B257B-2414-43C7-B041-AC22B03D9143@timm.sh","threadId":"62086","inReplyTo":"05b501db02ce$3bd49e30$b37dda90$@nexbridge.com","subject":"Re: [bug?] git diff --cached in a non-git directory","fromName":"Okke Timm","fromEmail":"okke@timm.sh","sentAt":"2024-09-09T15:51:22Z","receivedAt":"2024-09-09T15:51:34Z","isPatch":false,"sender":{"key":"okke@timm.sh","avatar":null},"body":"\n> Am 09.09.2024 um 17:37 schrieb <rsbecker@nexbridge.com> <rsbecker@nexbridge.com>:\n> \n> On September 9, 2024 10:26 AM, Okke Timm wrote:\n>> What did you do before the bug happened? (Steps to reproduce your issue) I\n>> changed into a non-git directory.\n>> Then I called git with 'git diff --cached', which produced this error:\n>> \n>> error: unknown option `cached'\n>> usage: git diff --no-index [<options>] <path> <path> [...]\n>> \n>> What did you expect to happen? (Expected behavior) A helfpul error message.\n>> \n>> What happened instead? (Actual behavior) An unhelpful error message saying\n> that\n>> cached is an unknown option.\n>> \n>> What's different between what you expected and what actually happened?\n>> Me being confused why git suddenly forgot that `git diff --cached` is an\n> allowed\n>> option.\n>> \n>> Anything else you want to add:\n>> Please fix and thank you for this great tool!\n>> \n>> Please review the rest of the bug report below.\n>> You can delete any lines you don't wish to share.\n>> \n>> \n>> [System Info]\n>> git version:\n>> git version 2.46.0\n>> cpu: arm64\n>> no commit associated with this build\n>> sizeof-long: 8\n>> sizeof-size_t: 8\n>> shell-path: /bin/sh\n>> feature: fsmonitor--daemon\n>> libcurl: 8.4.0\n>> zlib: 1.2.12\n>> uname: Darwin 23.6.0 Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:21 PDT\n>> 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T8103 arm64 compiler info:\n>> clang: 15.0.0 (clang-1500.3.9.4) libc info: no libc information available\n> $SHELL\n>> (typically, interactive shell): /opt/homebrew/bin/bash\n>> \n>> \n>> [Enabled Hooks]\n>> not run from a git repository - no hooks to show\n> \n> Because you are not in a git repository clone, the --cached option has no\n> meaning. It refers to the staging area only, so when not in a clone, git\n> does not consider --cached as supported because there is no connection\n> between an external directory and an internal. If you want to perform a diff\n> from cached vs. external, run the git diff from inside the clone referencing\n> the internal and external file after —.\n\n\ni criticized the error message in my bug report. If it would tell me that this is\nnot a git repository, I’d be happy. But telling me that `—cached` is not an\nallowed option for `git diff` is very unhelpful and confusing.\n\nThe reason why I reported this is that git gets run in tools these days and not\nall check for the presence of a git repository correctly all the time. Also\nsometimes permission issue hide the `.git` directory for some reason (reported\nfor Windows and VSCode)."},{"id":"502471","messageId":"xmqqy140pt67.fsf@gitster.g","threadId":"62086","inReplyTo":"05b501db02ce$3bd49e30$b37dda90$@nexbridge.com","subject":"Re: [bug?] git diff --cached in a non-git directory","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2024-09-09T18:40:16Z","receivedAt":"2024-09-09T18:40:19Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"<rsbecker@nexbridge.com> writes:\n\n>>error: unknown option `cached'\n>>usage: git diff --no-index [<options>] <path> <path> [...]\n> ...\n> Because you are not in a git repository clone, the --cached option has no\n> meaning.\n\nYes.  \"git diff\" unfortunately has a mode where a limited subset of\nits features is available and that is called the \"--no-index\" mode.\n\nBecause the command is capable of working outside a repository, \"You\nare not in a repository\" is a not a good error message in this\nsituation, either.  It is not that you stepped outside a repository\nthat is your error.  It is not that you used \"--cached\" that is your\nerror, either.  You used \"--cached\" outside a repository, that is\nwhere your error is.\n\nA patch to improve the error messages in such a situation is\ncertainly welcome.\n\nThanks.\n\n"},{"id":"502472","messageId":"05e201db02ee$84787380$8d695a80$@nexbridge.com","threadId":"62086","inReplyTo":"xmqqy140pt67.fsf@gitster.g","subject":"RE: [bug?] git diff --cached in a non-git directory","fromName":"","fromEmail":"rsbecker@nexbridge.com","sentAt":"2024-09-09T19:28:52Z","receivedAt":"2024-09-09T19:29:01Z","isPatch":false,"sender":{"key":"randall.becker@nexbridge.ca","avatar":"https://avatars.githubusercontent.com/u/28956764?v=4"},"body":"On September 9, 2024 2:40 PM, Junio C Hamano wrote:\n><rsbecker@nexbridge.com> writes:\n>\n>>>error: unknown option `cached'\n>>>usage: git diff --no-index [<options>] <path> <path> [...]\n>> ...\n>> Because you are not in a git repository clone, the --cached option has\n>> no meaning.\n>\n>Yes.  \"git diff\" unfortunately has a mode where a limited subset of its\nfeatures is\n>available and that is called the \"--no-index\" mode.\n>\n>Because the command is capable of working outside a repository, \"You are\nnot in a\n>repository\" is a not a good error message in this situation, either.  It is\nnot that you\n>stepped outside a repository that is your error.  It is not that you used\n\"--cached\"\n>that is your error, either.  You used \"--cached\" outside a repository, that\nis where\n>your error is.\n>\n>A patch to improve the error messages in such a situation is certainly\nwelcome.\n\nGlad to. Can you point me at where this is generated?\n\n"},{"id":"502474","messageId":"xmqqikv4po8v.fsf@gitster.g","threadId":"62086","inReplyTo":"xmqqy140pt67.fsf@gitster.g","subject":"Re: [bug?] git diff --cached in a non-git directory","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2024-09-09T20:26:40Z","receivedAt":"2024-09-09T20:26:42Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> <rsbecker@nexbridge.com> writes:\n>\n>>>error: unknown option `cached'\n>>>usage: git diff --no-index [<options>] <path> <path> [...]\n>> ...\n>> Because you are not in a git repository clone, the --cached option has no\n>> meaning.\n>\n> Yes.  \"git diff\" unfortunately has a mode where a limited subset of\n> its features is available and that is called the \"--no-index\" mode.\n>\n> Because the command is capable of working outside a repository, \"You\n> are not in a repository\" is a not a good error message in this\n> situation, either.  It is not that you stepped outside a repository\n> that is your error.  It is not that you used \"--cached\" that is your\n> error, either.  You used \"--cached\" outside a repository, that is\n> where your error is.\n>\n> A patch to improve the error messages in such a situation is\n> certainly welcome.\n\nHaving said that, there is already a warning() close by.\n\n\targc = parse_options(argc, argv, revs->prefix, options,\n\t\t\t     diff_no_index_usage, 0);\n\tif (argc != 2) {\n\t\tif (implicit_no_index)\n\t\t\twarning(_(\"Not a git repository. Use --no-index to \"\n\t\t\t\t  \"compare two paths outside a working tree\"));\n\t\tusage_with_options(diff_no_index_usage, options);\n\t}\n\nand this gives\n\n$ cd / && git diff foo 2>&1 | grep -e error: -e fatal: -e warning:\nwarning: Not a git repository. Use --no-index to compare two paths outside a working tree\n\na relevant warning.\n\nBut this is a bit too short to be effective for all failure modes.\nparse_options() would die if it sees an invalid option.  For that,\nI suspect that we'd need to pass the usage string conditionally,\ni.e.\n\n    argc = parse_options(argc, argv, revs->prefix, options,\n\t\t\t implicit_no_index \n\t\t\t ? diff_no_index_usage_with_non_repo_warning\n\t\t\t : diff_no_index_usage, 0);\n\nwith\n\n    static const char * const diff_no_index_usage_with_non_repo_warning[] = {\n            N_(\"git diff --no-index [<options>] <path> <path>\"),\n            N_(\"Not in a git repository; some options may not make sense\"),\n            NULL,\n    };\n\nnear where diff_no_index_usage[] is defined, or something silly like\nthat.\n\n"}]}