stash: convert `stash--helper.c` into `stash.c`

The old shell script `git-stash.sh` was removed and replaced entirely by `builtin/stash.c`. In order to do that, `create` and `push` were adapted to work without `stash.sh`. For example, before this commit, `git stash create` called `git stash--helper create --message "$*"`. If it called `git stash--helper create "$@"`, then some of these changes wouldn't have been necessary. This commit also removes the word `helper` since now stash is called directly and not by a 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 40af14683432285b94407e8488eab6942d0779dc
6 files changed +92 -225
.gitignore
-1
@@ -162,7 +162,6 @@
162 /git-show-ref
163 /git-stage
164 /git-stash
165 -/git-stash--helper
165 /git-status
166 /git-stripspace
167 /git-submodule
Makefile
+1 -2
@@ -619,7 +619,6 @@ SCRIPT_SH += git-quiltimport.sh
619 SCRIPT_SH += git-legacy-rebase.sh
620 SCRIPT_SH += git-remote-testgit.sh
621 SCRIPT_SH += git-request-pull.sh
622 -SCRIPT_SH += git-stash.sh
622 SCRIPT_SH += git-submodule.sh
623 SCRIPT_SH += git-web--browse.sh
624
@@ -1117,7 +1116,7 @@ BUILTIN_OBJS += builtin/shortlog.o
1116 BUILTIN_OBJS += builtin/show-branch.o
1117 BUILTIN_OBJS += builtin/show-index.o
1118 BUILTIN_OBJS += builtin/show-ref.o
1120 -BUILTIN_OBJS += builtin/stash--helper.o
1119 +BUILTIN_OBJS += builtin/stash.o
1120 BUILTIN_OBJS += builtin/stripspace.o
1121 BUILTIN_OBJS += builtin/submodule--helper.o
1122 BUILTIN_OBJS += builtin/symbolic-ref.o
builtin.h
+1 -1
@@ -225,7 +225,7 @@ extern int cmd_show(int argc, const char **argv, const char *prefix);
225 extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
226 extern int cmd_show_index(int argc, const char **argv, const char *prefix);
227 extern int cmd_status(int argc, const char **argv, const char *prefix);
228 -extern int cmd_stash__helper(int argc, const char **argv, const char *prefix);
228 +extern int cmd_stash(int argc, const char **argv, const char *prefix);
229 extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
230 extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix);
231 extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);
builtin/stash.c renamed
+89 -67
@@ -16,75 +16,70 @@
16
17 #define INCLUDE_ALL_FILES 2
18
19 -static const char * const git_stash_helper_usage[] = {
20 - N_("git stash--helper list [<options>]"),
21 - N_("git stash--helper show [<options>] [<stash>]"),
22 - N_("git stash--helper drop [-q|--quiet] [<stash>]"),
23 - N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
24 - N_("git stash--helper branch <branchname> [<stash>]"),
25 - N_("git stash--helper clear"),
26 - N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
19 +static const char * const git_stash_usage[] = {
20 + N_("git stash list [<options>]"),
21 + N_("git stash show [<options>] [<stash>]"),
22 + N_("git stash drop [-q|--quiet] [<stash>]"),
23 + N_("git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
24 + N_("git stash branch <branchname> [<stash>]"),
25 + N_("git stash clear"),
26 + N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
27 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
28 " [--] [<pathspec>...]]"),
29 - N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
29 + N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
30 " [-u|--include-untracked] [-a|--all] [<message>]"),
31 NULL
32 };
33
34 -static const char * const git_stash_helper_list_usage[] = {
35 - N_("git stash--helper list [<options>]"),
34 +static const char * const git_stash_list_usage[] = {
35 + N_("git stash list [<options>]"),
36 NULL
37 };
38
39 -static const char * const git_stash_helper_show_usage[] = {
40 - N_("git stash--helper show [<options>] [<stash>]"),
39 +static const char * const git_stash_show_usage[] = {
40 + N_("git stash show [<options>] [<stash>]"),
41 NULL
42 };
43
44 -static const char * const git_stash_helper_drop_usage[] = {
45 - N_("git stash--helper drop [-q|--quiet] [<stash>]"),
44 +static const char * const git_stash_drop_usage[] = {
45 + N_("git stash drop [-q|--quiet] [<stash>]"),
46 NULL
47 };
48
49 -static const char * const git_stash_helper_pop_usage[] = {
50 - N_("git stash--helper pop [--index] [-q|--quiet] [<stash>]"),
49 +static const char * const git_stash_pop_usage[] = {
50 + N_("git stash pop [--index] [-q|--quiet] [<stash>]"),
51 NULL
52 };
53
54 -static const char * const git_stash_helper_apply_usage[] = {
55 - N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
54 +static const char * const git_stash_apply_usage[] = {
55 + N_("git stash apply [--index] [-q|--quiet] [<stash>]"),
56 NULL
57 };
58
59 -static const char * const git_stash_helper_branch_usage[] = {
60 - N_("git stash--helper branch <branchname> [<stash>]"),
59 +static const char * const git_stash_branch_usage[] = {
60 + N_("git stash branch <branchname> [<stash>]"),
61 NULL
62 };
63
64 -static const char * const git_stash_helper_clear_usage[] = {
65 - N_("git stash--helper clear"),
64 +static const char * const git_stash_clear_usage[] = {
65 + N_("git stash clear"),
66 NULL
67 };
68
69 -static const char * const git_stash_helper_store_usage[] = {
70 - N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
69 +static const char * const git_stash_store_usage[] = {
70 + N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"),
71 NULL
72 };
73
74 -static const char * const git_stash_helper_create_usage[] = {
75 - N_("git stash--helper create [<message>]"),
76 - NULL
77 -};
78 -
79 -static const char * const git_stash_helper_push_usage[] = {
80 - N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
74 +static const char * const git_stash_push_usage[] = {
75 + N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
76 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
77 " [--] [<pathspec>...]]"),
78 NULL
79 };
80
86 -static const char * const git_stash_helper_save_usage[] = {
87 - N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
81 +static const char * const git_stash_save_usage[] = {
82 + N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
83 " [-u|--include-untracked] [-a|--all] [<message>]"),
84 NULL
85 };
@@ -220,7 +215,7 @@ static int clear_stash(int argc, const char **argv, const char *prefix)
215 };
216
217 argc = parse_options(argc, argv, prefix, options,
223 - git_stash_helper_clear_usage,
218 + git_stash_clear_usage,
219 PARSE_OPT_STOP_AT_NON_OPTION);
220
221 if (argc)
@@ -521,7 +516,7 @@ static int apply_stash(int argc, const char **argv, const char *prefix)
516 };
517
518 argc = parse_options(argc, argv, prefix, options,
524 - git_stash_helper_apply_usage, 0);
519 + git_stash_apply_usage, 0);
520
521 if (get_stash_info(&info, argc, argv))
522 return -1;
@@ -594,7 +589,7 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
589 };
590
591 argc = parse_options(argc, argv, prefix, options,
597 - git_stash_helper_drop_usage, 0);
592 + git_stash_drop_usage, 0);
593
594 if (get_stash_info(&info, argc, argv))
595 return -1;
@@ -620,7 +615,7 @@ static int pop_stash(int argc, const char **argv, const char *prefix)
615 };
616
617 argc = parse_options(argc, argv, prefix, options,
623 - git_stash_helper_pop_usage, 0);
618 + git_stash_pop_usage, 0);
619
620 if (get_stash_info(&info, argc, argv))
621 return -1;
@@ -647,7 +642,7 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
642 };
643
644 argc = parse_options(argc, argv, prefix, options,
650 - git_stash_helper_branch_usage, 0);
645 + git_stash_branch_usage, 0);
646
647 if (!argc) {
648 fprintf_ln(stderr, _("No branch name specified"));
@@ -682,7 +677,7 @@ static int list_stash(int argc, const char **argv, const char *prefix)
677 };
678
679 argc = parse_options(argc, argv, prefix, options,
685 - git_stash_helper_list_usage,
680 + git_stash_list_usage,
681 PARSE_OPT_KEEP_UNKNOWN);
682
683 if (!ref_exists(ref_stash))
@@ -762,7 +757,7 @@ static int show_stash(int argc, const char **argv, const char *prefix)
757 argc = setup_revisions(argc, argv, &rev, NULL);
758 if (argc > 1) {
759 free_stash_info(&info);
765 - usage_with_options(git_stash_helper_show_usage, options);
760 + usage_with_options(git_stash_show_usage, options);
761 }
762
763 rev.diffopt.flags.recursive = 1;
@@ -808,7 +803,7 @@ static int store_stash(int argc, const char **argv, const char *prefix)
803 };
804
805 argc = parse_options(argc, argv, prefix, options,
811 - git_stash_helper_store_usage,
806 + git_stash_store_usage,
807 PARSE_OPT_KEEP_UNKNOWN);
808
809 if (argc != 1) {
@@ -1221,30 +1216,19 @@ done:
1216
1217 static int create_stash(int argc, const char **argv, const char *prefix)
1218 {
1224 - int include_untracked = 0;
1219 int ret = 0;
1226 - const char *stash_msg = NULL;
1220 struct strbuf stash_msg_buf = STRBUF_INIT;
1221 struct stash_info info;
1222 struct pathspec ps;
1230 - struct option options[] = {
1231 - OPT_BOOL('u', "include-untracked", &include_untracked,
1232 - N_("include untracked files in stash")),
1233 - OPT_STRING('m', "message", &stash_msg, N_("message"),
1234 - N_("stash message")),
1235 - OPT_END()
1236 - };
1223
1238 - argc = parse_options(argc, argv, prefix, options,
1239 - git_stash_helper_create_usage,
1240 - 0);
1224 + /* Starting with argv[1], since argv[0] is "create" */
1225 + strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
1226
1227 memset(&ps, 0, sizeof(ps));
1228 if (!check_changes_tracked_files(ps))
1229 return 0;
1230
1246 - strbuf_addstr(&stash_msg_buf, stash_msg);
1247 - ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
1231 + ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info,
1232 NULL, 0);
1233 if (!ret)
1234 printf_ln("%s", oid_to_hex(&info.w_commit));
@@ -1476,9 +1460,10 @@ static int push_stash(int argc, const char **argv, const char *prefix)
1460 OPT_END()
1461 };
1462
1479 - argc = parse_options(argc, argv, prefix, options,
1480 - git_stash_helper_push_usage,
1481 - 0);
1463 + if (argc)
1464 + argc = parse_options(argc, argv, prefix, options,
1465 + git_stash_push_usage,
1466 + 0);
1467
1468 parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);
1469 return do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,
@@ -1511,7 +1496,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
1496 };
1497
1498 argc = parse_options(argc, argv, prefix, options,
1514 - git_stash_helper_save_usage,
1499 + git_stash_save_usage,
1500 PARSE_OPT_KEEP_DASHDASH);
1501
1502 if (argc)
@@ -1525,10 +1510,12 @@ static int save_stash(int argc, const char **argv, const char *prefix)
1510 return ret;
1511 }
1512
1528 -int cmd_stash__helper(int argc, const char **argv, const char *prefix)
1513 +int cmd_stash(int argc, const char **argv, const char *prefix)
1514 {
1515 + int i = -1;
1516 pid_t pid = getpid();
1517 const char *index_file;
1518 + struct argv_array args = ARGV_ARRAY_INIT;
1519
1520 struct option options[] = {
1521 OPT_END()
@@ -1536,16 +1523,16 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
1523
1524 git_config(git_diff_basic_config, NULL);
1525
1539 - argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,
1526 + argc = parse_options(argc, argv, prefix, options, git_stash_usage,
1527 PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
1528
1529 index_file = get_index_file();
1530 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1531 (uintmax_t)pid);
1532
1546 - if (argc < 1)
1547 - usage_with_options(git_stash_helper_usage, options);
1548 - if (!strcmp(argv[0], "apply"))
1533 + if (!argc)
1534 + return !!push_stash(0, NULL, prefix);
1535 + else if (!strcmp(argv[0], "apply"))
1536 return !!apply_stash(argc, argv, prefix);
1537 else if (!strcmp(argv[0], "clear"))
1538 return !!clear_stash(argc, argv, prefix);
@@ -1567,7 +1554,42 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
1554 return !!push_stash(argc, argv, prefix);
1555 else if (!strcmp(argv[0], "save"))
1556 return !!save_stash(argc, argv, prefix);
1557 + else if (*argv[0] != '-')
1558 + usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
1559 + git_stash_usage, options);
1560 +
1561 + if (strcmp(argv[0], "-p")) {
1562 + while (++i < argc && strcmp(argv[i], "--")) {
1563 + /*
1564 + * `akpqu` is a string which contains all short options,
1565 + * except `-m` which is verified separately.
1566 + */
1567 + if ((strlen(argv[i]) == 2) && *argv[i] == '-' &&
1568 + strchr("akpqu", argv[i][1]))
1569 + continue;
1570 +
1571 + if (!strcmp(argv[i], "--all") ||
1572 + !strcmp(argv[i], "--keep-index") ||
1573 + !strcmp(argv[i], "--no-keep-index") ||
1574 + !strcmp(argv[i], "--patch") ||
1575 + !strcmp(argv[i], "--quiet") ||
1576 + !strcmp(argv[i], "--include-untracked"))
1577 + continue;
1578 +
1579 + /*
1580 + * `-m` and `--message=` are verified separately because
1581 + * they need to be immediately followed by a string
1582 + * (i.e.`-m"foobar"` or `--message="foobar"`).
1583 + */
1584 + if (starts_with(argv[i], "-m") ||
1585 + starts_with(argv[i], "--message="))
1586 + continue;
1587 +
1588 + usage_with_options(git_stash_usage, options);
1589 + }
1590 + }
1591
1571 - usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
1572 - git_stash_helper_usage, options);
1592 + argv_array_push(&args, "push");
1593 + argv_array_pushv(&args, argv);
1594 + return !!push_stash(args.argc, args.argv, prefix);
1595 }
git-stash.sh deleted
-153
@@ -1,153 +0,0 @@
1 -#!/bin/sh
2 -# Copyright (c) 2007, Nanako Shiraishi
3 -
4 -dashless=$(basename "$0" | sed -e 's/-/ /')
5 -USAGE="list [<options>]
6 - or: $dashless show [<stash>]
7 - or: $dashless drop [-q|--quiet] [<stash>]
8 - or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]
9 - or: $dashless branch <branchname> [<stash>]
10 - or: $dashless save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
11 - [-u|--include-untracked] [-a|--all] [<message>]
12 - or: $dashless [push [--patch] [-k|--[no-]keep-index] [-q|--quiet]
13 - [-u|--include-untracked] [-a|--all] [-m <message>]
14 - [-- <pathspec>...]]
15 - or: $dashless clear"
16 -
17 -SUBDIRECTORY_OK=Yes
18 -OPTIONS_SPEC=
19 -START_DIR=$(pwd)
20 -. git-sh-setup
21 -require_work_tree
22 -prefix=$(git rev-parse --show-prefix) || exit 1
23 -cd_to_toplevel
24 -
25 -TMP="$GIT_DIR/.git-stash.$$"
26 -TMPindex=${GIT_INDEX_FILE-"$(git rev-parse --git-path index)"}.stash.$$
27 -trap 'rm -f "$TMP-"* "$TMPindex"' 0
28 -
29 -ref_stash=refs/stash
30 -
31 -if git config --get-colorbool color.interactive; then
32 - help_color="$(git config --get-color color.interactive.help 'red bold')"
33 - reset_color="$(git config --get-color '' reset)"
34 -else
35 - help_color=
36 - reset_color=
37 -fi
38 -
39 -#
40 -# Parses the remaining options looking for flags and
41 -# at most one revision defaulting to ${ref_stash}@{0}
42 -# if none found.
43 -#
44 -# Derives related tree and commit objects from the
45 -# revision, if one is found.
46 -#
47 -# stash records the work tree, and is a merge between the
48 -# base commit (first parent) and the index tree (second parent).
49 -#
50 -# REV is set to the symbolic version of the specified stash-like commit
51 -# IS_STASH_LIKE is non-blank if ${REV} looks like a stash
52 -# IS_STASH_REF is non-blank if the ${REV} looks like a stash ref
53 -# s is set to the SHA1 of the stash commit
54 -# w_commit is set to the commit containing the working tree
55 -# b_commit is set to the base commit
56 -# i_commit is set to the commit containing the index tree
57 -# u_commit is set to the commit containing the untracked files tree
58 -# w_tree is set to the working tree
59 -# b_tree is set to the base tree
60 -# i_tree is set to the index tree
61 -# u_tree is set to the untracked files tree
62 -#
63 -# GIT_QUIET is set to t if -q is specified
64 -# INDEX_OPTION is set to --index if --index is specified.
65 -# FLAGS is set to the remaining flags (if allowed)
66 -#
67 -# dies if:
68 -# * too many revisions specified
69 -# * no revision is specified and there is no stash stack
70 -# * a revision is specified which cannot be resolve to a SHA1
71 -# * a non-existent stash reference is specified
72 -# * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
73 -#
74 -
75 -test "$1" = "-p" && set "push" "$@"
76 -
77 -PARSE_CACHE='--not-parsed'
78 -# The default command is "push" if nothing but options are given
79 -seen_non_option=
80 -for opt
81 -do
82 - case "$opt" in
83 - --) break ;;
84 - -*) ;;
85 - *) seen_non_option=t; break ;;
86 - esac
87 -done
88 -
89 -test -n "$seen_non_option" || set "push" "$@"
90 -
91 -# Main command set
92 -case "$1" in
93 -list)
94 - shift
95 - git stash--helper list "$@"
96 - ;;
97 -show)
98 - shift
99 - git stash--helper show "$@"
100 - ;;
101 -save)
102 - shift
103 - cd "$START_DIR"
104 - git stash--helper save "$@"
105 - ;;
106 -push)
107 - shift
108 - cd "$START_DIR"
109 - git stash--helper push "$@"
110 - ;;
111 -apply)
112 - shift
113 - cd "$START_DIR"
114 - git stash--helper apply "$@"
115 - ;;
116 -clear)
117 - shift
118 - git stash--helper clear "$@"
119 - ;;
120 -create)
121 - shift
122 - git stash--helper create --message "$*"
123 - ;;
124 -store)
125 - shift
126 - git stash--helper store "$@"
127 - ;;
128 -drop)
129 - shift
130 - git stash--helper drop "$@"
131 - ;;
132 -pop)
133 - shift
134 - cd "$START_DIR"
135 - git stash--helper pop "$@"
136 - ;;
137 -branch)
138 - shift
139 - cd "$START_DIR"
140 - git stash--helper branch "$@"
141 - ;;
142 -*)
143 - case $# in
144 - 0)
145 - cd "$START_DIR"
146 - git stash--helper push &&
147 - say "$(gettext "(To restore them type \"git stash apply\")")"
148 - ;;
149 - *)
150 - usage
151 - esac
152 - ;;
153 -esac
git.c
+1 -1
@@ -554,7 +554,7 @@ static struct cmd_struct commands[] = {
554 { "show-index", cmd_show_index },
555 { "show-ref", cmd_show_ref, RUN_SETUP },
556 { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
557 - { "stash--helper", cmd_stash__helper, RUN_SETUP | NEED_WORK_TREE },
557 + { "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE },
558 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
559 { "stripspace", cmd_stripspace },
560 { "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT },