git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: Meet Soni <meetsoni3017@gmail.com>
Cc: git@vger.kernel.org, shejialuo@gmail.com
Subject: Re: [GSoC][PATCH] builtin/refs: add 'get' subcommand
Date: Wed, 24 Sep 2025 08:32:43 +0200	[thread overview]
Message-ID: <aNOQi04mS0uXD4iv@pks.im> (raw)
In-Reply-To: <20250923104533.21165-1-meetsoni3017@gmail.com>

On Tue, Sep 23, 2025 at 04:15:33PM +0530, Meet Soni wrote:
> diff --git a/Documentation/git-refs.adoc b/Documentation/git-refs.adoc
> index bfa9b3ea2d..f07fe8c864 100644
> --- a/Documentation/git-refs.adoc
> +++ b/Documentation/git-refs.adoc
> @@ -19,6 +19,7 @@ git refs list [--count=<count>] [--shell|--perl|--python|--tcl]
>  		   [(--exclude=<pattern>)...] [--start-after=<marker>]
>  		   [ --stdin | (<pattern>...)]
>  git refs exists <ref>
> +git refs get <ref>
>  
>  DESCRIPTION
>  -----------
> @@ -45,6 +46,12 @@ exists::
>  	failed with an error other than the reference being missing. This does
>  	not verify whether the reference resolves to an actual object.
>  
> +get::
> +	Reads the raw value of a single, exact reference. Instead of

Let's say "fully qualified" instead of "exact".

> +	recursively dereferencing symbolic references, this command prints the
> +	direct target of the symref (e.g., ref: refs/heads/main). For regular
> +	references, it prints the object ID (SHA-1) they point to.

I'd drop the reference to SHA1 here, as it may be any object hash.

> diff --git a/builtin/refs.c b/builtin/refs.c
> index 91548783b7..b473a78e18 100644
> --- a/builtin/refs.c
> +++ b/builtin/refs.c
> @@ -159,6 +163,43 @@ static int cmd_refs_exists(int argc, const char **argv, const char *prefix,
>  	return ret;
>  }
>  
> +static int cmd_refs_get(int argc, const char **argv, const char *prefix,
> +			struct repository *repo UNUSED)
> +{
> +	const char *refname;
> +	struct object_id oid;
> +	unsigned int type;
> +	int failure_errno = 0;
> +	struct strbuf referent = STRBUF_INIT;

Tiny nit: we typically order variables that aren't accessed by options
after the options array.

> +	const char * const exists_usage[] = {
> +		REFS_EXISTS_USAGE,
> +		NULL,
> +	};
> +	struct option options[] = {
> +		OPT_END(),
> +	};
> +
> +	argc = parse_options(argc, argv, prefix, options, exists_usage, 0);
> +	if (argc != 1)
> +		die("refs get requires exactly one reference");

This should be translatable. Furthermore, we can probably use `usagef()`
instead to have a "usage:" prefix instead of "fatal:".

> +	refname = *argv++;
> +	if (refs_read_raw_ref(get_main_ref_store(the_repository), refname,
> +			      &oid, &referent, &type, &failure_errno)) {
> +		die("'%s' - not a valid ref", refname);

We should discern by `failure_errno` here. Most importantly, I think we
should handle `ENOENT` and `EISDIR` specially to both mean that the
reference does not exist. So, e.g.:

	if (refs_read_raw_ref(get_main_ref_store(the_repository), refname,
			      &oid, &referent, &type, &failure_errno)) {
		if (failure_errno == ENOENT || failure_errno == EISDIR)
			die(_("reference does not exist"));
		else
			die_errno(_("failed to look up reference"));
	}

> +	}
> +
> +	if (type & REF_ISSYMREF) {
> +		printf("ref: %s\n", referent.buf);
> +	} else {
> +		printf("%s\n", oid_to_hex(&oid));
> +	}

We can drop the curly braces around single-line bodies.

Patrick

      parent reply	other threads:[~2025-09-24  6:32 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-23 10:45 [GSoC][PATCH] builtin/refs: add 'get' subcommand Meet Soni
2025-09-23 16:57 ` Ben Knoble
2025-09-24  6:32   ` Patrick Steinhardt
2025-09-23 21:50 ` Junio C Hamano
2025-09-24  6:32   ` Patrick Steinhardt
2025-09-24 15:29     ` Ben Knoble
2025-09-24 17:11       ` Junio C Hamano
2025-09-25  6:25         ` Patrick Steinhardt
2025-09-25 18:08           ` D. Ben Knoble
2025-09-25 18:43             ` Junio C Hamano
2025-09-24  6:32 ` Patrick Steinhardt [this message]

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=aNOQi04mS0uXD4iv@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=meetsoni3017@gmail.com \
    --cc=shejialuo@gmail.com \
    /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 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).