threads / patch / 7774

patch, 3 partsTeach cat-file a --quiet option

Subject: [PATCH 3/3] Teach cat-file a --quiet option

## tl;dr

6 messages between Apr 22, 2007 and Apr 22, 2007. Diffs are folded; open one to read it.

replies: 5people: 2as markdown or json

Shawn O. Pearce· Apr 22, 2007, 01:14 UTC · lore

Sometimes when you get the content of a file in a script you don't want an error message for missing files; instead its OK to treat a missing file the same as one whose content was empty.

This is especially true if the script is using something like `cat-file blob HEAD:path/to/file` to look at an optional file's content.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 Documentation/git-cat-file.txt |    4 ++++
 builtin-cat-file.c             |   26 ++++++++++++++++++++------
 2 files changed, 24 insertions(+), 6 deletions(-)
Show changes to 2 files +24 −6

Documentation/git-cat-file.txt, builtin-cat-file.c

diff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt
index 075c0d0..f6edb1a 100644
--- a/Documentation/git-cat-file.txt
+++ b/Documentation/git-cat-file.txt
@@ -46,6 +46,10 @@ OPTIONS
 	or to ask for a "blob" with <object> being a tag object that
 	points at it.
 
+--quiet::
+	Don't print an error message if the object doesn't exist;
+	instead exit with non-zero status like -e.
+
 OUTPUT
 ------
 If '-t' is specified, one of the <type>.
diff --git a/builtin-cat-file.c b/builtin-cat-file.c
index b2437fe..8807a61 100644
--- a/builtin-cat-file.c
+++ b/builtin-cat-file.c
@@ -82,13 +82,15 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
 	enum object_type type;
 	void *buf;
 	unsigned long size;
-	int opt = 0, i;
+	int opt = 0, quiet = 0, i;
 	const char *exp_type = NULL, *obj_name = NULL;
 
 	git_config(git_default_config);
 	for (i = 1; i < argc; i++) {
 		const char *a = argv[i];
-		if (!opt && a[0] == '-' && a[1])
+		if (!strcmp(a, "--quiet"))
+			quiet = 1;
+		else if (!opt && a[0] == '-' && a[1])
 			opt = a[1];
 		else if (!opt && !exp_type)
 			exp_type = a;
@@ -99,8 +101,11 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
 	}
 	if (!obj_name)
 		usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
-	if (get_sha1(obj_name, sha1))
+	if (get_sha1(obj_name, sha1)) {
+		if (quiet || opt == 'e')
+			return 1;
 		die("Not a valid object name %s", obj_name);
+	}
 
 	buf = NULL;
 	switch (opt) {
@@ -125,8 +130,11 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
 
 	case 'p':
 		type = sha1_object_info(sha1, NULL);
-		if (type < 0)
+		if (type < 0) {
+			if (quiet)
+				return 1;
 			die("Not a valid object name %s", obj_name);
+		}
 
 		/* custom pretty-print here */
 		if (type == OBJ_TREE) {
@@ -135,8 +143,11 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
 		}
 
 		buf = read_sha1_file(sha1, &type, &size);
-		if (!buf)
+		if (!buf) {
+			if (quiet)
+				return 1;
 			die("Cannot read object %s", obj_name);
+		}
 		if (type == OBJ_TAG) {
 			pprint_tag(sha1, buf, size);
 			return 0;
@@ -152,8 +163,11 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
 		die("git-cat-file: unknown option: %s\n", exp_type);
 	}
 
-	if (!buf)
+	if (!buf) {
+		if (quiet)
+			return 1;
 		die("git-cat-file %s: bad file", obj_name);
+	}
 
 	write_or_die(1, buf, size);
 	return 0;
-- 
1.5.1.1.135.gf948
Junio C Hamano· Apr 22, 2007, 07:56 UTC · re: Shawn O. Pearce · lore

Re: [PATCH 3/3] Teach cat-file a --quiet option

"Shawn O. Pearce" <spearce@spearce.org> writes:
Show 7 quoted lines
> Sometimes when you get the content of a file in a script you don't
> want an error message for missing files; instead its OK to treat
> a missing file the same as one whose content was empty.
>
> This is especially true if the script is using something like
> `cat-file blob HEAD:path/to/file` to look at an optional file's
> content.

I am not sure if I agree with this logic. How is this different from discarding stderr to /dev/null?

Shawn O. Pearce· Apr 22, 2007, 08:11 UTC · re: Junio C Hamano · lore

Re: [PATCH 3/3] Teach cat-file a --quiet option

Junio C Hamano <junkio@cox.net> wrote:
Show 5 quoted lines
> "Shawn O. Pearce" <spearce@spearce.org> writes:
> 
> > Sometimes when you get the content of a file in a script you don't
> > want an error message for missing files; instead its OK to treat
> > a missing file the same as one whose content was empty.
...
> I am not sure if I agree with this logic.  How is this different
> from discarding stderr to /dev/null?
 
Its not any different.  But it means I can do:
	open(I,'-|','git','cat-file','--quiet','blob',"HEAD:users/$who");

and not worry about redirection to silence the case of when $who is not in the users subtree of HEAD. Sure, I could redirect that, but then that's something more like:

	if (open(I,'-|')) {
		open STDERR, ">/dev/null";
		exec 'git','cat-file','--quiet','blob',"HEAD:users/$who";
		exit 1;
	}

and uh, why, that's really annoying. And my Perl is rusty enough that I'm not even sure I did that right, I'd have to go look it up danngit. And didn't we just add a --quiet to git-diff? This is different, but not that much different..

What's also annoying is cat-file today prints an error with -e if you use the "branch:path" syntax, but not if you supply the 40 byte hex SHA-1 of the blob in question. Again, you have to redirect the one syntax, but not the other, even for just a simple -e. Which makes -e slightly less useful. So I also fixed that...

Anyway... if you really don't like it, drop it, I'll just have to go digging through the Perl manual... ;-)

-- 
Shawn.
Junio C Hamano· Apr 22, 2007, 09:20 UTC · re: Shawn O. Pearce · lore

Re: [PATCH 3/3] Teach cat-file a --quiet option

"Shawn O. Pearce" <spearce@spearce.org> writes:
> What's also annoying is cat-file today prints an error with -e if
> you use the "branch:path" syntax, but not if you supply the 40 byte
> hex SHA-1 of the blob in question.

I _think_ the intent of -e or any other "silently check" option is to signal "status" with exit code, while still indicating an error with the message.

For the particular example of "cat-file -e", whose purpose is to accept an object name string and report if such an object exists in the repository:

	$ git cat-file -e 45354a57ee7e3e42c7137db6c94fa968c6babe8d; echo $?
        0
	$ git cat-file -e 45354a57ee7e3e42c7137db6c94fa968c6babe8e; echo $?
        1
	$ git cat-file -e HEAD:NO-SUCH-PATH
	fatal: Not a valid object name HEAD:NO-SUCH-PATH

The input of the above two _look_ like valid object names and they return Ok/Bad because the user is asking "is there such an object?".

Technically speaking, with the last case, the user did not give a correctly formatted object name (treeish colon needs to be followed by a path that exists in the treeish to be considered a valid object name), so it can be argued that complaining is the right thing to do, but that is too literal to the law and I would agree that it probably is on the wrong side of the borderline.

Shawn O. Pearce· Apr 22, 2007, 08:15 UTC · re: Junio C Hamano · lore

Re: [PATCH 3/3] Teach cat-file a --quiet option

Junio C Hamano <junkio@cox.net> wrote:
Show 7 quoted lines
> "Shawn O. Pearce" <spearce@spearce.org> writes:
> > This is especially true if the script is using something like
> > `cat-file blob HEAD:path/to/file` to look at an optional file's
> > content.
> 
> I am not sure if I agree with this logic.  How is this different
> from discarding stderr to /dev/null?

Also, a redirect of stderr to /dev/null kills other error messages that might be interesting to see on stderr of the caller, like if a packfile isn't readable... while my --quiet patch doesn't.j The --quiet flag only kills errors that occurred because the requested object doesn't exist.

-- 
Shawn.
Junio C Hamano· Apr 22, 2007, 09:20 UTC · re: Shawn O. Pearce · lore

Re: [PATCH 3/3] Teach cat-file a --quiet option

"Shawn O. Pearce" <spearce@spearce.org> writes:
Show 11 quoted lines
> Junio C Hamano <junkio@cox.net> wrote:
>> "Shawn O. Pearce" <spearce@spearce.org> writes:
>> > This is especially true if the script is using something like
>> > `cat-file blob HEAD:path/to/file` to look at an optional file's
>> > content.
>> 
>> I am not sure if I agree with this logic.  How is this different
>> from discarding stderr to /dev/null?
>
> Also, a redirect of stderr to /dev/null kills other error messages
> that might be interesting to see on stderr of the caller,...

I think that quite depends on what your definition of "other" is; see my other message on this subject.

← back to recent threads