From: Junio C Hamano <gitster@pobox.com>
To: "brian m. carlson" <sandals@crustytoothpaste.net>
Cc: <git@vger.kernel.org>, Phillip Wood <phillip.wood123@gmail.com>
Subject: Re: [PATCH v5 2/4] builtin/stash: factor out revision parsing into a function
Date: Fri, 09 May 2025 08:27:20 -0700 [thread overview]
Message-ID: <xmqqldr53iuf.fsf@gitster.g> (raw)
In-Reply-To: <20250508234458.3665894-3-sandals@crustytoothpaste.net> (brian m. carlson's message of "Thu, 8 May 2025 23:44:55 +0000")
"brian m. carlson" <sandals@crustytoothpaste.net> writes:
> We allow several special forms of stash names in this code. In the
> future, we'll want to allow these same forms without parsing a stash
> commit, so let's refactor this code out into a function for reuse.
>
> Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
> ---
> builtin/stash.c | 34 +++++++++++++++++++++++-----------
> 1 file changed, 23 insertions(+), 11 deletions(-)
>
> diff --git a/builtin/stash.c b/builtin/stash.c
> index cfbd92852a..8ee6752efa 100644
> --- a/builtin/stash.c
> +++ b/builtin/stash.c
> @@ -169,6 +169,25 @@ static void assert_stash_like(struct stash_info *info, const char *revision)
> die(_("'%s' is not a stash-like commit"), revision);
> }
>
> +static int parse_revision(struct strbuf *revision, const char *commit, int quiet)
> +{
> + strbuf_reset(revision);
> + if (!commit) {
> + if (!refs_ref_exists(get_main_ref_store(the_repository), ref_stash)) {
> + if (!quiet)
> + fprintf_ln(stderr, _("No stash entries found."));
> + return -1;
> + }
> +
> + strbuf_addf(revision, "%s@{0}", ref_stash);
> + } else if (strspn(commit, "0123456789") == strlen(commit)) {
> + strbuf_addf(revision, "%s@{%s}", ref_stash, commit);
> + } else {
> + strbuf_addstr(revision, commit);
> + }
> + return 0;
> +}
> +
> static int get_stash_info(struct stash_info *info, int argc, const char **argv)
> {
> int ret;
> @@ -196,17 +215,10 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
> if (argc == 1)
> commit = argv[0];
>
> - if (!commit) {
> - if (!refs_ref_exists(get_main_ref_store(the_repository), ref_stash)) {
> - fprintf_ln(stderr, _("No stash entries found."));
> - return -1;
> - }
> -
> - strbuf_addf(&info->revision, "%s@{0}", ref_stash);
> - } else if (strspn(commit, "0123456789") == strlen(commit)) {
> - strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
> - } else {
> - strbuf_addstr(&info->revision, commit);
> + strbuf_init(&info->revision, 0);
> + if (parse_revision(&info->revision, commit, 0)) {
> + free_stash_info(info);
> + return -1;
> }
Two comments:
- It is file-scope static so it is not a huge deal, but it is a bit
confusing that parse_revision() sounds like a helper function you
would have in revision.c and call from everywhere.
- The call to free_stash_info() from inside get_stash_info() is
probably wrong. The early error return when ref_stash is missing
leaves info intact, and the callers of it share this pattern:
if (get_stash_info(&info, argc - 1, argv + 1))
goto cleanup;
...
cleanup:
free_stash_info(&info);
return ret;
Even if free_stah_info() happen to be idempotent right now, I do
not think we want our code to rely on it.
next prev parent reply other threads:[~2025-05-09 15:27 UTC|newest]
Thread overview: 72+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-08 23:44 [PATCH v5 0/4] Importing and exporting stashes to refs brian m. carlson
2025-05-08 23:44 ` [PATCH v5 1/4] object-name: make get_oid quietly return an error brian m. carlson
2025-05-09 1:55 ` Junio C Hamano
2025-05-09 19:50 ` brian m. carlson
2025-05-08 23:44 ` [PATCH v5 2/4] builtin/stash: factor out revision parsing into a function brian m. carlson
2025-05-09 15:27 ` Junio C Hamano [this message]
2025-05-08 23:44 ` [PATCH v5 3/4] builtin/stash: provide a way to export stashes to a ref brian m. carlson
2025-05-09 16:38 ` Junio C Hamano
2025-05-09 19:31 ` Junio C Hamano
2025-05-10 21:24 ` Jeff King
2025-05-12 9:10 ` Phillip Wood
2025-05-12 15:53 ` Junio C Hamano
2025-05-08 23:44 ` [PATCH v5 4/4] builtin/stash: provide a way to import stashes from " brian m. carlson
2025-05-09 19:54 ` Junio C Hamano
2025-05-11 23:44 ` brian m. carlson
2025-05-10 17:21 ` Jeff King
2025-05-12 12:42 ` Junio C Hamano
2025-05-12 12:58 ` Jeff King
2025-05-12 16:05 ` Junio C Hamano
2025-05-12 21:19 ` brian m. carlson
2025-05-10 21:33 ` Jeff King
2025-05-12 9:10 ` Phillip Wood
2025-05-09 1:10 ` [PATCH v5 0/4] Importing and exporting stashes to refs Junio C Hamano
2025-05-09 20:16 ` brian m. carlson
2025-05-09 16:53 ` D. Ben Knoble
2025-05-09 20:15 ` brian m. carlson
2025-05-10 19:13 ` D. Ben Knoble
2025-05-22 18:55 ` [PATCH] Makefile: avoid constant rebuilds with compilation database brian m. carlson
2025-05-22 18:55 ` [PATCH v6 0/5] Importing and exporting stashes to refs brian m. carlson
2025-06-01 22:32 ` [PATCH v7 0/4] " brian m. carlson
2025-06-01 22:32 ` [PATCH v7 1/4] object-name: make get_oid quietly return an error brian m. carlson
2025-06-01 22:32 ` [PATCH v7 2/4] builtin/stash: factor out revision parsing into a function brian m. carlson
2025-06-01 22:32 ` [PATCH v7 3/4] builtin/stash: provide a way to export stashes to a ref brian m. carlson
2025-06-05 9:25 ` Phillip Wood
2025-06-11 11:31 ` Kristoffer Haugsbakk
2025-06-11 23:35 ` brian m. carlson
2025-06-01 22:32 ` [PATCH v7 4/4] builtin/stash: provide a way to import stashes from " brian m. carlson
2025-06-05 9:38 ` [PATCH v7 0/4] Importing and exporting stashes to refs Phillip Wood
2025-06-12 1:12 ` [PATCH v8 " brian m. carlson
2025-06-12 1:12 ` [PATCH v8 1/4] object-name: make get_oid quietly return an error brian m. carlson
2025-06-12 1:12 ` [PATCH v8 2/4] builtin/stash: factor out revision parsing into a function brian m. carlson
2025-06-12 1:12 ` [PATCH v8 3/4] builtin/stash: provide a way to export stashes to a ref brian m. carlson
2025-06-12 1:12 ` [PATCH v8 4/4] builtin/stash: provide a way to import stashes from " brian m. carlson
2025-06-25 8:40 ` [PATCH v8 0/4] Importing and exporting stashes to refs Phillip Wood
2025-06-25 16:30 ` Junio C Hamano
2025-05-22 18:55 ` [PATCH v6 1/5] object-name: make get_oid quietly return an error brian m. carlson
2025-05-22 19:27 ` Junio C Hamano
2025-05-22 18:55 ` [PATCH v6 2/5] reflog-walk: expose read_complete_reflog brian m. carlson
2025-05-22 21:53 ` Ramsay Jones
2025-05-23 23:22 ` brian m. carlson
2025-05-24 1:09 ` Ramsay Jones
2025-05-26 19:55 ` brian m. carlson
2025-05-29 16:01 ` Phillip Wood
2025-05-29 21:59 ` Junio C Hamano
2025-05-22 18:55 ` [PATCH v6 3/5] builtin/stash: factor out revision parsing into a function brian m. carlson
2025-05-22 20:34 ` Junio C Hamano
2025-05-23 23:25 ` brian m. carlson
2025-05-24 0:23 ` Junio C Hamano
2025-05-26 19:36 ` brian m. carlson
2025-05-22 18:55 ` [PATCH v6 4/5] builtin/stash: provide a way to export stashes to a ref brian m. carlson
2025-05-22 20:51 ` Junio C Hamano
2025-05-26 19:42 ` brian m. carlson
2025-05-29 16:01 ` Phillip Wood
2025-05-22 18:55 ` [PATCH v6 5/5] builtin/stash: provide a way to import stashes from " brian m. carlson
2025-05-22 21:09 ` Junio C Hamano
2025-05-26 20:03 ` brian m. carlson
2025-05-22 21:15 ` Junio C Hamano
2025-05-23 13:17 ` Phillip Wood
2025-05-22 19:00 ` [PATCH] Makefile: avoid constant rebuilds with compilation database brian m. carlson
2025-05-22 19:08 ` Junio C Hamano
2025-06-11 11:35 ` [PATCH v5 0/4] Importing and exporting stashes to refs Kristoffer Haugsbakk
2025-06-12 0:45 ` brian m. carlson
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=xmqqldr53iuf.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=phillip.wood123@gmail.com \
--cc=sandals@crustytoothpaste.net \
/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).