# [RFC PATCH] Make rev-parse -q and --is-* quiet

2 messages from 2020-03-13 to 2020-03-13. Participants: Erlend Aasland, Jeff King.
Thread: https://gitlist.dev/t/52988

## Erlend Aasland, 2020-03-13 12:13

Subject: [RFC PATCH] Make rev-parse -q and --is-* quiet
Message-ID: <4B2A0C64-FD4A-457C-A7CF-5B680AF38BC9@innova.no>
URL: https://gitlist.dev/e/4B2A0C64-FD4A-457C-A7CF-5B680AF38BC9%40innova.no

```
If rev-parse is called with both -q and an --is-* option, the result is
provided as the return code of the command, iso. printed to stdout.

This simplifies using these queries in shell scripts:
git rev-parse --is-bare-repository && do_stuff
git rev-parse --is-shallow-repository && do_stuff

Signed-off-by: Erlend E. Aasland <erlend.aasland@innova.no>
---
builtin/rev-parse.c | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 7a00da8203..5a8b404ec7 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -874,24 +874,31 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
				continue;
			}
			if (!strcmp(arg, "--is-inside-git-dir")) {
-				printf("%s\n", is_inside_git_dir() ? "true"
-						: "false");
+				int is_set = is_inside_git_dir();
+				if (quiet)
+					return is_set ? 0 : 1;
+				printf("%s\n", is_set ? "true" : "false");
				continue;
			}
			if (!strcmp(arg, "--is-inside-work-tree")) {
-				printf("%s\n", is_inside_work_tree() ? "true"
-						: "false");
+				int is_set = is_inside_work_tree();
+				if (quiet)
+					return is_set ? 0 : 1;
+				printf("%s\n", is_set ? "true" : "false");
				continue;
			}
			if (!strcmp(arg, "--is-bare-repository")) {
-				printf("%s\n", is_bare_repository() ? "true"
-						: "false");
+				int is_set = is_bare_repository();
+				if (quiet)
+					return is_set ? 0 : 1;
+				printf("%s\n", is_set ? "true" : "false");
				continue;
			}
			if (!strcmp(arg, "--is-shallow-repository")) {
-				printf("%s\n",
-						is_repository_shallow(the_repository) ? "true"
-						: "false");
+				int is_set = is_repository_shallow(the_repository);
+				if (quiet)
+					return is_set ? 0 : 1;
+				printf("%s\n", is_set ? "true" : "false");
				continue;
			}
			if (!strcmp(arg, "--shared-index-path")) {
-- 
2.25.1


```

## Jeff King, 2020-03-13 17:52

Subject: Re: [RFC PATCH] Make rev-parse -q and --is-* quiet
Message-ID: <20200313175236.GB549554@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20200313175236.GB549554%40coredump.intra.peff.net
In-Reply-To: <4B2A0C64-FD4A-457C-A7CF-5B680AF38BC9@innova.no>

```
On Fri, Mar 13, 2020 at 12:13:52PM +0000, Erlend Aasland wrote:

> If rev-parse is called with both -q and an --is-* option, the result is
> provided as the return code of the command, iso. printed to stdout.
> 
> This simplifies using these queries in shell scripts:
> git rev-parse --is-bare-repository && do_stuff
> git rev-parse --is-shallow-repository && do_stuff

I left some comments in the other part of the thread, but another
potential downside here is that the exit code also tells us whether the
command was unable to run for unrelated reasons (i.e., the answer is
really a tristate: yes, no, or "we don't know the answer").

One obvious reason to exit non-zero would be that we're not in a
repository at all. I _think_ that works OK with all of these
conditionals, because if we're not in a repo, then we are obviously not
inside a git-dir, and so on.

Another reason is that the caller misspelled the option name. :) That
might be an acceptable risk, though. It's not like it isn't present in
other commands, probably people are doing:

  test "$(git rev-parse --is-whatever") = "true" && do_stuff

anyway, which has the same problem. Though perhaps the simplicity there
is an argument that we don't need the new exit-code mechanism.

-Peff

```
