Skip to content

Commit 7572e59

Browse files
bk2204gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 393bbb2 commit 7572e59

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

builtin/stash.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,25 @@ static void assert_stash_like(struct stash_info *info, const char *revision)
169169
die(_("'%s' is not a stash-like commit"), revision);
170170
}
171171

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+
172191
static int get_stash_info(struct stash_info *info, int argc, const char **argv)
173192
{
174193
int ret;
@@ -196,17 +215,9 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
196215
if (argc == 1)
197216
commit = argv[0];
198217

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;
210221
}
211222

212223
revision = info->revision.buf;

0 commit comments

Comments
 (0)