All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Shawn O. Pearce" <spearce@spearce.org>
To: Junio C Hamano <junkio@cox.net>
Cc: git@vger.kernel.org
Subject: [PATCH 3/3] Teach cat-file a --quiet option
Date: Sat, 21 Apr 2007 21:14:47 -0400	[thread overview]
Message-ID: <20070422011447.GC2910@spearce.org> (raw)

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(-)

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

             reply	other threads:[~2007-04-22  1:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-22  1:14 Shawn O. Pearce [this message]
2007-04-22  7:56 ` [PATCH 3/3] Teach cat-file a --quiet option Junio C Hamano
2007-04-22  8:11   ` Shawn O. Pearce
2007-04-22  9:20     ` Junio C Hamano
2007-04-22  8:15   ` Shawn O. Pearce
2007-04-22  9:20     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070422011447.GC2910@spearce.org \
    --to=spearce@spearce.org \
    --cc=git@vger.kernel.org \
    --cc=junkio@cox.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.