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 1/2] builtin/refs: add 'exists' subcommand
Date: Thu, 21 Aug 2025 12:21:34 +0200	[thread overview]
Message-ID: <aKbzLhqryk5d-zgh@pks.im> (raw)
In-Reply-To: <20250821085246.929307-2-meetsoni3017@gmail.com>

On Thu, Aug 21, 2025 at 02:22:45PM +0530, Meet Soni wrote:
> As part of the ongoing effort to consolidate reference handling,
> introduce a new `exists` subcommand. This command provides the same
> functionality and exit-code behavior as `git show-ref --exists`, serving
> as its modern replacement.
> 
> The logic for `show-ref --exists` is minimal. Rather than creating a
> shared helper function which would be overkill for ~20 lines of code,
> its implementation is intentionally duplicated here. This contrasts with
> `git refs list`, where sharing the larger implementation of
> `for-each-ref` was necessary.

I agree with this decision. It doesn't really feel worth it to share
code for such trivial functionality.

> diff --git a/builtin/refs.c b/builtin/refs.c
> index 76224feba4..617d8ab138 100644
> --- a/builtin/refs.c
> +++ b/builtin/refs.c
> @@ -113,6 +117,48 @@ static int cmd_refs_list(int argc, const char **argv, const char *prefix,
>  	return for_each_ref_core(argc, argv, prefix, repo, refs_list_usage);
>  }
>  
> +static int cmd_refs_exists(int argc, const char **argv, const char *prefix,
> +			   struct repository *repo UNUSED)
> +{
> +	struct strbuf unused_referent = STRBUF_INIT;
> +	struct object_id unused_oid;
> +	unsigned int unused_type;
> +	int failure_errno = 0;
> +	const char *ref;
> +

Let's drop this empty newline.

> +	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)
> +		die("'git refs exists' requires a reference");
> +
> +	ref = *argv++;
> +	if (*argv)
> +		die("'git refs exists' requires exactly one reference");

We can combine these two error messages to just say `if (argc != 1)`.
Also, the strings should be marked for translation.

> +	if (refs_read_raw_ref(get_main_ref_store(the_repository), ref,
> +			      &unused_oid, &unused_referent, &unused_type,
> +			      &failure_errno)) {
> +		if (failure_errno == ENOENT || failure_errno == EISDIR) {
> +			error(_("reference does not exist"));
> +			return 2;
> +		} else {
> +			errno = failure_errno;
> +			error_errno(_("failed to look up reference"));
> +			return 1;

I'd personally prefer to se a common exit path and use `goto` so that
one doesn't have to worry about whether or not the `struct strbuf` needs
to be free'd in error cases. But I'll leave it up to you to decide
whether you want to do this change.

Patrick

  reply	other threads:[~2025-08-21 10:21 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-21  8:52 [GSoC][PATCH 0/2] Add refs exists subcommand Meet Soni
2025-08-21  8:52 ` [GSoC][PATCH 1/2] builtin/refs: add 'exists' subcommand Meet Soni
2025-08-21 10:21   ` Patrick Steinhardt [this message]
2025-08-21  8:52 ` [GSoC][PATCH 2/2] t: add test for git refs exists subcommand Meet Soni
2025-08-21 10:21   ` Patrick Steinhardt
2025-08-22  4:59     ` Meet Soni
2025-08-21 10:21 ` [GSoC][PATCH 0/2] Add " Patrick Steinhardt
2025-08-21 16:01 ` Junio C Hamano
2025-08-22  4:27   ` Meet Soni
2025-08-23  6:00 ` [GSoC][PATCH v2 0/4] " Meet Soni
2025-08-23  6:00   ` [GSoC][PATCH v2 1/4] builtin/refs: add 'exists' subcommand Meet Soni
2025-08-23  6:00   ` [GSoC][PATCH v2 2/4] t1403: split 'show-ref --exists' tests into a separate file Meet Soni
2025-08-23  6:00   ` [GSoC][PATCH v2 3/4] t1422: refactor tests to be shareable Meet Soni
2025-08-24 16:58     ` Patrick Steinhardt
2025-08-23  6:00   ` [GSoC][PATCH v2 4/4] t: add test for git refs exists subcommand Meet Soni
2025-08-24 16:58   ` [GSoC][PATCH v2 0/4] Add " Patrick Steinhardt
2025-08-26  6:41   ` [GSoC][PATCH v3 " Meet Soni
2025-08-26  6:41     ` [GSoC][PATCH v3 1/4] builtin/refs: add 'exists' subcommand Meet Soni
2025-08-26  6:41     ` [GSoC][PATCH v3 2/4] t1403: split 'show-ref --exists' tests into a separate file Meet Soni
2025-08-26  6:41     ` [GSoC][PATCH v3 3/4] t1422: refactor tests to be shareable Meet Soni
2025-08-26  6:41     ` [GSoC][PATCH v3 4/4] t: add test for git refs exists subcommand Meet Soni
2025-09-02 11:52     ` [GSoC][PATCH v3 0/4] Add " Patrick Steinhardt
2025-09-02 16:57     ` 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=aKbzLhqryk5d-zgh@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).