Skip to content

Commit f7d0605

Browse files
ungpsdscho
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: Junio C Hamano <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 0e5156b commit f7d0605

File tree

2 files changed

+65
-41
lines changed

2 files changed

+65
-41
lines changed

builtin/stash--helper.c

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

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

@@ -731,6 +736,62 @@ static int show_stash(int argc, const char **argv, const char *prefix)
731736
return diff_result_code(&rev.diffopt, 0);
732737
}
733738

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

769832
usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
770833
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)