Skip to content

Commit 41e0dd5

Browse files
ungpsgitster
authored andcommitted
stash: convert store to builtin
Add stash store to the helper and delete the store_stash function from the shell script. Signed-off-by: Paul-Sebastian Ungureanu <[email protected]> Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dc7bd38 commit 41e0dd5

File tree

2 files changed

+64
-41
lines changed

2 files changed

+64
-41
lines changed

builtin/stash--helper.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ static const char * const git_stash_helper_clear_usage[] = {
5858
NULL
5959
};
6060

61+
static const char * const git_stash_helper_store_usage[] = {
62+
N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
63+
NULL
64+
};
65+
6166
static const char *ref_stash = "refs/stash";
6267
static struct strbuf stash_index_path = STRBUF_INIT;
6368

@@ -729,6 +734,61 @@ static int show_stash(int argc, const char **argv, const char *prefix)
729734
return diff_result_code(&rev.diffopt, 0);
730735
}
731736

737+
static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
738+
int quiet)
739+
{
740+
if (!stash_msg)
741+
stash_msg = "Created via \"git stash store\".";
742+
743+
if (update_ref(stash_msg, ref_stash, w_commit, NULL,
744+
REF_FORCE_CREATE_REFLOG,
745+
quiet ? UPDATE_REFS_QUIET_ON_ERR :
746+
UPDATE_REFS_MSG_ON_ERR)) {
747+
if (!quiet) {
748+
fprintf_ln(stderr, _("Cannot update %s with %s"),
749+
ref_stash, oid_to_hex(w_commit));
750+
}
751+
return -1;
752+
}
753+
754+
return 0;
755+
}
756+
757+
static int store_stash(int argc, const char **argv, const char *prefix)
758+
{
759+
int quiet = 0;
760+
const char *stash_msg = NULL;
761+
struct object_id obj;
762+
struct object_context dummy;
763+
struct option options[] = {
764+
OPT__QUIET(&quiet, N_("be quiet")),
765+
OPT_STRING('m', "message", &stash_msg, "message",
766+
N_("stash message")),
767+
OPT_END()
768+
};
769+
770+
argc = parse_options(argc, argv, prefix, options,
771+
git_stash_helper_store_usage,
772+
PARSE_OPT_KEEP_UNKNOWN);
773+
774+
if (argc != 1) {
775+
if (!quiet)
776+
fprintf_ln(stderr, _("\"git stash store\" requires one "
777+
"<commit> argument"));
778+
return -1;
779+
}
780+
781+
if (get_oid_with_context(argv[0], quiet ? GET_OID_QUIETLY : 0, &obj,
782+
&dummy)) {
783+
if (!quiet)
784+
fprintf_ln(stderr, _("Cannot update %s with %s"),
785+
ref_stash, argv[0]);
786+
return -1;
787+
}
788+
789+
return do_store_stash(&obj, stash_msg, quiet);
790+
}
791+
732792
int cmd_stash__helper(int argc, const char **argv, const char *prefix)
733793
{
734794
pid_t pid = getpid();
@@ -763,6 +823,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
763823
return !!list_stash(argc, argv, prefix);
764824
else if (!strcmp(argv[0], "show"))
765825
return !!show_stash(argc, argv, prefix);
826+
else if (!strcmp(argv[0], "store"))
827+
return !!store_stash(argc, argv, prefix);
766828

767829
usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
768830
git_stash_helper_usage, options);

git-stash.sh

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -208,45 +208,6 @@ create_stash () {
208208
die "$(gettext "Cannot record working tree state")"
209209
}
210210

211-
store_stash () {
212-
while test $# != 0
213-
do
214-
case "$1" in
215-
-m|--message)
216-
shift
217-
stash_msg="$1"
218-
;;
219-
-m*)
220-
stash_msg=${1#-m}
221-
;;
222-
--message=*)
223-
stash_msg=${1#--message=}
224-
;;
225-
-q|--quiet)
226-
quiet=t
227-
;;
228-
*)
229-
break
230-
;;
231-
esac
232-
shift
233-
done
234-
test $# = 1 ||
235-
die "$(eval_gettext "\"$dashless store\" requires one <commit> argument")"
236-
237-
w_commit="$1"
238-
if test -z "$stash_msg"
239-
then
240-
stash_msg="Created via \"git stash store\"."
241-
fi
242-
243-
git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit
244-
ret=$?
245-
test $ret != 0 && test -z "$quiet" &&
246-
die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
247-
return $ret
248-
}
249-
250211
push_stash () {
251212
keep_index=
252213
patch_mode=
@@ -325,7 +286,7 @@ push_stash () {
325286
clear_stash || die "$(gettext "Cannot initialize stash")"
326287

327288
create_stash -m "$stash_msg" -u "$untracked" -- "$@"
328-
store_stash -m "$stash_msg" -q $w_commit ||
289+
git stash--helper store -m "$stash_msg" -q $w_commit ||
329290
die "$(gettext "Cannot save the current status")"
330291
say "$(eval_gettext "Saved working directory and index state \$stash_msg")"
331292

@@ -485,7 +446,7 @@ create)
485446
;;
486447
store)
487448
shift
488-
store_stash "$@"
449+
git stash--helper store "$@"
489450
;;
490451
drop)
491452
shift

0 commit comments

Comments
 (0)