git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [GSoC][PATCH 1/2] builtin/refs: add 'exists' subcommand

From
Patrick Steinhardt <ps@pks.im>
Date
Aug 21, 2025, 10:21 UTC
Message-ID
<aKbzLhqryk5d-zgh@pks.im>
In-Reply-To
<20250821085246.929307-2-meetsoni3017@gmail.com>
On Thu, Aug 21, 2025 at 02:22:45PM +0530, Meet Soni wrote:
Show 10 quoted lines
> 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.

Show 17 quoted lines
> 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.
Show 15 quoted lines
> +	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.

Show 10 quoted lines
> +	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
Previous: Meet SoniNext: Meet Soni
Message 3 of 23 in “Add refs exists subcommand”
  1. Meet SoniAug 21, 2025
  2. [GSoC][PATCH 1/2] builtin/refs: add 'exists' subcommandMeet Soni, Aug 21, 2025
  3. Patrick SteinhardtAug 21, 2025
  4. [GSoC][PATCH 2/2] t: add test for git refs exists subcommandMeet Soni, Aug 21, 2025
  5. Patrick SteinhardtAug 21, 2025
  6. Meet SoniAug 22, 2025
  7. Patrick SteinhardtAug 21, 2025
  8. Junio C HamanoAug 21, 2025
  9. Meet SoniAug 22, 2025
  10. [GSoC][PATCH v2 0/4] Add refs exists subcommandMeet Soni, Aug 23, 2025
  11. [GSoC][PATCH v2 1/4] builtin/refs: add 'exists' subcommandMeet Soni, Aug 23, 2025
  12. [GSoC][PATCH v2 2/4] t1403: split 'show-ref --exists' tests into a separate fileMeet Soni, Aug 23, 2025
  13. [GSoC][PATCH v2 3/4] t1422: refactor tests to be shareableMeet Soni, Aug 23, 2025
  14. Patrick SteinhardtAug 24, 2025
  15. [GSoC][PATCH v2 4/4] t: add test for git refs exists subcommandMeet Soni, Aug 23, 2025
  16. Patrick SteinhardtAug 24, 2025
  17. [GSoC][PATCH v3 0/4] Add refs exists subcommandMeet Soni, Aug 26, 2025
  18. [GSoC][PATCH v3 1/4] builtin/refs: add 'exists' subcommandMeet Soni, Aug 26, 2025
  19. [GSoC][PATCH v3 2/4] t1403: split 'show-ref --exists' tests into a separate fileMeet Soni, Aug 26, 2025
  20. [GSoC][PATCH v3 3/4] t1422: refactor tests to be shareableMeet Soni, Aug 26, 2025
  21. [GSoC][PATCH v3 4/4] t: add test for git refs exists subcommandMeet Soni, Aug 26, 2025
  22. Patrick SteinhardtSep 2, 2025
  23. Junio C HamanoSep 2, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.