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

Paul-Sebastian Ungureanu committed Feb 25, 2019 at 23:16 UTC 41e0dd55c4300dc83ea2883bf04bfb234c071f98
2 files changed +64 -41
builtin/stash--helper.c
+62
@@ -58,6 +58,11 @@ static const char * const git_stash_helper_clear_usage[] = {
58 NULL
59 };
60
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 +
66 static const char *ref_stash = "refs/stash";
67 static struct strbuf stash_index_path = STRBUF_INIT;
68
@@ -729,6 +734,61 @@ static int show_stash(int argc, const char **argv, const char *prefix)
734 return diff_result_code(&rev.diffopt, 0);
735 }
736
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 +
792 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
793 {
794 pid_t pid = getpid();
@@ -763,6 +823,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
823 return !!list_stash(argc, argv, prefix);
824 else if (!strcmp(argv[0], "show"))
825 return !!show_stash(argc, argv, prefix);
826 + else if (!strcmp(argv[0], "store"))
827 + return !!store_stash(argc, argv, prefix);
828
829 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
830 git_stash_helper_usage, options);
git-stash.sh
+2 -41
@@ -208,45 +208,6 @@ create_stash () {
208 die "$(gettext "Cannot record working tree state")"
209 }
210
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 -
211 push_stash () {
212 keep_index=
213 patch_mode=
@@ -325,7 +286,7 @@ push_stash () {
286 clear_stash || die "$(gettext "Cannot initialize stash")"
287
288 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 ||
290 die "$(gettext "Cannot save the current status")"
291 say "$(eval_gettext "Saved working directory and index state \$stash_msg")"
292
@@ -485,7 +446,7 @@ create)
446 ;;
447 store)
448 shift
488 - store_stash "$@"
449 + git stash--helper store "$@"
450 ;;
451 drop)
452 shift