From: Junio C Hamano Date: Mon, 04 Mar 2024 23:23:21 GMT Subject: Re: [PATCH] show-ref: add --unresolved option Message-ID: In-Reply-To: "John Cai via GitGitGadget" writes: > From: John Cai > > For reftable development, it would be handy to have a tool to provide > the direct value of any ref whether it be a symbolic ref or not. > Currently there is git-symbolic-ref, which only works for symbolic refs, > and git-rev-parse, which will resolve the ref. Let's add a --unresolved > option that will only take one ref and return whatever it points to > without dereferencing it. The approach may be reasonble, but the above description can use some improvements. * Even though the title of the patch says show-ref, the last sentence is a bit too far from there and it was unclear to what you are adding a new feature at least to me during my first read. Let's teach show-ref a `--unresolved` optionthat will ... may make it easier to follow. * "Whatever it points to without dereferencing it" implied that it assumes what it is asked to show can be dereferenced, which invites a natural question: what happens to a thing that is not dereferenceable in the first place? The implementation seems to show either symbolic-ref target (for symbolic refs) or the object name (for others), but let's make it easier for readers. > Documentation/git-show-ref.txt | 8 ++++++ > builtin/show-ref.c | 33 ++++++++++++++++-------- > t/t1403-show-ref.sh | 47 ++++++++++++++++++++++++++++++++++ > 3 files changed, 77 insertions(+), 11 deletions(-) > > diff --git a/Documentation/git-show-ref.txt b/Documentation/git-show-ref.txt > index ba757470059..2f9b4de1346 100644 > --- a/Documentation/git-show-ref.txt > +++ b/Documentation/git-show-ref.txt > @@ -16,6 +16,7 @@ SYNOPSIS > [--] [...] > 'git show-ref' --exclude-existing[=] > 'git show-ref' --exists > +'git show-ref' --unresolved > > DESCRIPTION > ----------- > @@ -76,6 +77,13 @@ OPTIONS > it does, 2 if it is missing, and 1 in case looking up the reference > failed with an error other than the reference being missing. > > +--unresolved:: > + > + Prints out what the reference points to without resolving it. Returns > + an exit code of 0 if it does, 2 if it is missing, and 1 in case looking > + up the reference failed with an error other than the reference being > + missing. Exactly the same issue as in the proposed log message, i.e. what is printed for what kind of ref is not really clear. > -static int cmd_show_ref__exists(const char **refs) > +static int cmd_show_ref__raw(const char **refs, int show) > { > - struct strbuf unused_referent = STRBUF_INIT; > - struct object_id unused_oid; > - unsigned int unused_type; > + struct strbuf referent = STRBUF_INIT; > + struct object_id oid; > + unsigned int type; > int failure_errno = 0; > const char *ref; > int ret = 0; > @@ -236,7 +237,7 @@ static int cmd_show_ref__exists(const char **refs) > die("--exists requires exactly one reference"); > > if (refs_read_raw_ref(get_main_ref_store(the_repository), ref, > - &unused_oid, &unused_referent, &unused_type, > + &oid, &referent, &type, > &failure_errno)) { > if (failure_errno == ENOENT || failure_errno == EISDIR) { > error(_("reference does not exist")); > @@ -250,8 +251,16 @@ static int cmd_show_ref__exists(const char **refs) > goto out; > } > > + if (!show) > + goto out; > + > + if (type & REF_ISSYMREF) > + printf("ref: %s\n", referent.buf); > + else > + printf("ref: %s\n", oid_to_hex(&oid)); If I create a symbolic ref whose value is deadbeef....deadbeef 40-hex, I cannot tell from this output if it is a symbolic ref of a ref that stores an object whose name is that hash. Reserve the use of "ref: %s" to the symbolic refs (so that it will also match how the files backend stores them in modern Git), and use some other prefix (or no perfix). Actually, I am not sure if what is proposed is even a good interface. Given a repository with these few refs: $ git show-ref refs/heads/master b387623c12f3f4a376e4d35a610fd3e55d7ea907 refs/heads/master $ git show-ref refs/remotes/repo/HEAD b387623c12f3f4a376e4d35a610fd3e55d7ea907 refs/remotes/repo/HEAD $ git symbolic-ref refs/remotes/repo/HEAD refs/remotes/repo/master I would think that the second command above shows the gap in feature set our current "show-ref" has. If we could do $ git show-ref --