builtin/stash: factor out revision parsing into a function

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Jun 12, 2025 at 01:12 UTC 7572e59b3d89a8ff15a2d81de7d5f1814181d981
1 file changed +22 -11
builtin/stash.c
+22 -11
@@ -169,6 +169,25 @@ static void assert_stash_like(struct stash_info *info, const char *revision)
169 die(_("'%s' is not a stash-like commit"), revision);
170 }
171
172 +static int parse_stash_revision(struct strbuf *revision, const char *commit, int quiet)
173 +{
174 + strbuf_reset(revision);
175 + if (!commit) {
176 + if (!refs_ref_exists(get_main_ref_store(the_repository), ref_stash)) {
177 + if (!quiet)
178 + fprintf_ln(stderr, _("No stash entries found."));
179 + return -1;
180 + }
181 +
182 + strbuf_addf(revision, "%s@{0}", ref_stash);
183 + } else if (strspn(commit, "0123456789") == strlen(commit)) {
184 + strbuf_addf(revision, "%s@{%s}", ref_stash, commit);
185 + } else {
186 + strbuf_addstr(revision, commit);
187 + }
188 + return 0;
189 +}
190 +
191 static int get_stash_info(struct stash_info *info, int argc, const char **argv)
192 {
193 int ret;
@@ -196,17 +215,9 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
215 if (argc == 1)
216 commit = argv[0];
217
199 - if (!commit) {
200 - if (!refs_ref_exists(get_main_ref_store(the_repository), ref_stash)) {
201 - fprintf_ln(stderr, _("No stash entries found."));
202 - return -1;
203 - }
204 -
205 - strbuf_addf(&info->revision, "%s@{0}", ref_stash);
206 - } else if (strspn(commit, "0123456789") == strlen(commit)) {
207 - strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
208 - } else {
209 - strbuf_addstr(&info->revision, commit);
218 + strbuf_init(&info->revision, 0);
219 + if (parse_stash_revision(&info->revision, commit, 0)) {
220 + return -1;
221 }
222
223 revision = info->revision.buf;