Skip to content

Commit a2fdd34

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 <ungureanupaulsebastian@gmail.com>
1 parent 4a36231 commit a2fdd34

File tree

2 files changed

+64
-41
lines changed

2 files changed

+64
-41
lines changed

builtin/stash--helper.c

+62
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

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

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

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

git-stash.sh

+2-41
Original file line numberDiff line numberDiff line change
@@ -191,45 +191,6 @@ create_stash () {
191191
die "$(gettext "Cannot record working tree state")"
192192
}
193193

194-
store_stash () {
195-
while test $# != 0
196-
do
197-
case "$1" in
198-
-m|--message)
199-
shift
200-
stash_msg="$1"
201-
;;
202-
-m*)
203-
stash_msg=${1#-m}
204-
;;
205-
--message=*)
206-
stash_msg=${1#--message=}
207-
;;
208-
-q|--quiet)
209-
quiet=t
210-
;;
211-
*)
212-
break
213-
;;
214-
esac
215-
shift
216-
done
217-
test $# = 1 ||
218-
die "$(eval_gettext "\"$dashless store\" requires one <commit> argument")"
219-
220-
w_commit="$1"
221-
if test -z "$stash_msg"
222-
then
223-
stash_msg="Created via \"git stash store\"."
224-
fi
225-
226-
git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit
227-
ret=$?
228-
test $ret != 0 && test -z "$quiet" &&
229-
die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
230-
return $ret
231-
}
232-
233194
push_stash () {
234195
keep_index=
235196
patch_mode=
@@ -308,7 +269,7 @@ push_stash () {
308269
clear_stash || die "$(gettext "Cannot initialize stash")"
309270

310271
create_stash -m "$stash_msg" -u "$untracked" -- "$@"
311-
store_stash -m "$stash_msg" -q $w_commit ||
272+
git stash--helper store -m "$stash_msg" -q $w_commit ||
312273
die "$(gettext "Cannot save the current status")"
313274
say "$(eval_gettext "Saved working directory and index state \$stash_msg")"
314275

@@ -468,7 +429,7 @@ create)
468429
;;
469430
store)
470431
shift
471-
store_stash "$@"
432+
git stash--helper store "$@"
472433
;;
473434
drop)
474435
shift

0 commit comments

Comments
 (0)