* git cat-file -e behavior
@ 2016-02-29 9:58 Michal Čihař
2016-02-29 11:44 ` Jeff King
0 siblings, 1 reply; 5+ messages in thread
From: Michal Čihař @ 2016-02-29 9:58 UTC (permalink / raw)
To: git
[-- Attachment #1: Type: text/plain, Size: 529 bytes --]
Hi
the documentation for "git cat-file -e" says:
> Suppress all output; instead exit with zero status if <object> exists
and is a valid object.
However running it on non existing object will complain "fatal: Not a
valid object name".
$ git cat-file -e master:README.rst
$ echo $?
0
$ git cat-file -e master:foo
fatal: Not a valid object name master:foo
$ echo $?
128
Is the output in this case expected?
I'm currently running 2.7.0.
--
Michal Čihař | http://cihar.com | http://blog.cihar.com
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: git cat-file -e behavior 2016-02-29 9:58 git cat-file -e behavior Michal Čihař @ 2016-02-29 11:44 ` Jeff King 2016-02-29 12:16 ` Michal Čihař 2016-02-29 12:40 ` Jeff King 0 siblings, 2 replies; 5+ messages in thread From: Jeff King @ 2016-02-29 11:44 UTC (permalink / raw) To: Michal Čihař; +Cc: git On Mon, Feb 29, 2016 at 10:58:29AM +0100, Michal Čihař wrote: > the documentation for "git cat-file -e" says: > > > Suppress all output; instead exit with zero status if <object> exists > and is a valid object. > > However running it on non existing object will complain "fatal: Not a > valid object name". > > $ git cat-file -e master:README.rst > $ echo $? > 0 > $ git cat-file -e master:foo > fatal: Not a valid object name master:foo > $ echo $? > 128 > > Is the output in this case expected? > > I'm currently running 2.7.0. It looks like it has been this way forever. The first thing we do with the object is resolve its name to a sha1, and that's where the error you see comes from. And then we actually check whether we have the object. I think the intended use was to feed it a sha1 to see if it exists. Then the name-resolution step is a noop. I'm not sure if the behavior you are seeing is all that bad (the documentation could be read as suppressing the normal stdout output, but error messages remain), but it would be easy to change: diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 54db118..afde169 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -35,6 +35,9 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, if (unknown_type) flags |= LOOKUP_UNKNOWN_OBJECT; + if (opt == 'e') + return !has_sha1_file(sha1); + if (get_sha1_with_context(obj_name, 0, sha1, &obj_context)) die("Not a valid object name %s", obj_name); @@ -58,9 +61,6 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, printf("%lu\n", size); return 0; - case 'e': - return !has_sha1_file(sha1); - case 'c': if (!obj_context.path[0]) die("git cat-file --textconv %s: <object> must be <sha1:path>", ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: git cat-file -e behavior 2016-02-29 11:44 ` Jeff King @ 2016-02-29 12:16 ` Michal Čihař 2016-02-29 12:36 ` Jeff King 2016-02-29 12:40 ` Jeff King 1 sibling, 1 reply; 5+ messages in thread From: Michal Čihař @ 2016-02-29 12:16 UTC (permalink / raw) To: Jeff King; +Cc: git [-- Attachment #1: Type: text/plain, Size: 892 bytes --] Hi Dne 29.2.2016 v 12:44 Jeff King napsal(a): > It looks like it has been this way forever. The first thing we do with > the object is resolve its name to a sha1, and that's where the error you > see comes from. And then we actually check whether we have the object. > > I think the intended use was to feed it a sha1 to see if it exists. Then > the name-resolution step is a noop. I found this as best way to check whether file exists in branch. Checking git ls-tree output seems less error prone than checking return value of git cat-file -e... > I'm not sure if the behavior you are seeing is all that bad (the > documentation could be read as suppressing the normal stdout output, but > error messages remain), I understand this, that's why I'm asking whether it's expected output or not :-). -- Michal Čihař | http://cihar.com | http://blog.cihar.com [-- Attachment #2: OpenPGP digital signature --] [-- Type: application/pgp-signature, Size: 819 bytes --] ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: git cat-file -e behavior 2016-02-29 12:16 ` Michal Čihař @ 2016-02-29 12:36 ` Jeff King 0 siblings, 0 replies; 5+ messages in thread From: Jeff King @ 2016-02-29 12:36 UTC (permalink / raw) To: Michal Čihař; +Cc: git On Mon, Feb 29, 2016 at 01:16:34PM +0100, Michal Čihař wrote: > Dne 29.2.2016 v 12:44 Jeff King napsal(a): > > It looks like it has been this way forever. The first thing we do with > > the object is resolve its name to a sha1, and that's where the error you > > see comes from. And then we actually check whether we have the object. > > > > I think the intended use was to feed it a sha1 to see if it exists. Then > > the name-resolution step is a noop. > > I found this as best way to check whether file exists in branch. > Checking git ls-tree output seems less error prone than checking return > value of git cat-file -e... I think the usual way to do that would be: git rev-parse --verify $branch:$path >/dev/null which just resolves the name (not that what you are doing is wrong, I just think rev-parse is the way we usually do it in our scripts). That _will_ write a message to stderr when the name doesn't resolve. -Peff ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: git cat-file -e behavior 2016-02-29 11:44 ` Jeff King 2016-02-29 12:16 ` Michal Čihař @ 2016-02-29 12:40 ` Jeff King 1 sibling, 0 replies; 5+ messages in thread From: Jeff King @ 2016-02-29 12:40 UTC (permalink / raw) To: Michal Čihař; +Cc: git On Mon, Feb 29, 2016 at 06:44:55AM -0500, Jeff King wrote: > [...]but it would be easy to change: > > diff --git a/builtin/cat-file.c b/builtin/cat-file.c > index 54db118..afde169 100644 > --- a/builtin/cat-file.c > +++ b/builtin/cat-file.c > @@ -35,6 +35,9 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, > if (unknown_type) > flags |= LOOKUP_UNKNOWN_OBJECT; > > + if (opt == 'e') > + return !has_sha1_file(sha1); > + > if (get_sha1_with_context(obj_name, 0, sha1, &obj_context)) > die("Not a valid object name %s", obj_name); > > @@ -58,9 +61,6 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, > printf("%lu\n", size); > return 0; > > - case 'e': > - return !has_sha1_file(sha1); > - > case 'c': > if (!obj_context.path[0]) > die("git cat-file --textconv %s: <object> must be <sha1:path>", This is wrong, of course. We still need to call get_sha1, but just need to not die. So it's more like this (totally untested, naturally): diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 54db118..44add8c 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -35,8 +35,11 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, if (unknown_type) flags |= LOOKUP_UNKNOWN_OBJECT; - if (get_sha1_with_context(obj_name, 0, sha1, &obj_context)) + if (get_sha1_with_context(obj_name, 0, sha1, &obj_context)) { + if (opt == 'e') + return 1; die("Not a valid object name %s", obj_name); + } buf = NULL; switch (opt) { -Peff ^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2016-02-29 12:40 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2016-02-29 9:58 git cat-file -e behavior Michal Čihař 2016-02-29 11:44 ` Jeff King 2016-02-29 12:16 ` Michal Čihař 2016-02-29 12:36 ` Jeff King 2016-02-29 12:40 ` Jeff King
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).