Skip to content

Commit ea8a3de

Browse files
ungpsdscho
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5928e0b commit ea8a3de

File tree

6 files changed

+91
-224
lines changed

6 files changed

+91
-224
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@
162162
/git-show-ref
163163
/git-stage
164164
/git-stash
165-
/git-stash--helper
166165
/git-status
167166
/git-stripspace
168167
/git-submodule

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,6 @@ SCRIPT_SH += git-quiltimport.sh
619619
SCRIPT_SH += git-legacy-rebase.sh
620620
SCRIPT_SH += git-remote-testgit.sh
621621
SCRIPT_SH += git-request-pull.sh
622-
SCRIPT_SH += git-stash.sh
623622
SCRIPT_SH += git-submodule.sh
624623
SCRIPT_SH += git-web--browse.sh
625624

@@ -1117,7 +1116,7 @@ BUILTIN_OBJS += builtin/shortlog.o
11171116
BUILTIN_OBJS += builtin/show-branch.o
11181117
BUILTIN_OBJS += builtin/show-index.o
11191118
BUILTIN_OBJS += builtin/show-ref.o
1120-
BUILTIN_OBJS += builtin/stash--helper.o
1119+
BUILTIN_OBJS += builtin/stash.o
11211120
BUILTIN_OBJS += builtin/stripspace.o
11221121
BUILTIN_OBJS += builtin/submodule--helper.o
11231122
BUILTIN_OBJS += builtin/symbolic-ref.o

builtin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ extern int cmd_show(int argc, const char **argv, const char *prefix);
225225
extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
226226
extern int cmd_show_index(int argc, const char **argv, const char *prefix);
227227
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);
229229
extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
230230
extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix);
231231
extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix);

builtin/stash--helper.c renamed to builtin/stash.c

Lines changed: 88 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -16,75 +16,70 @@
1616

1717
#define INCLUDE_ALL_FILES 2
1818

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"
2727
" [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
2828
" [--] [<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"
3030
" [-u|--include-untracked] [-a|--all] [<message>]"),
3131
NULL
3232
};
3333

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>]"),
3636
NULL
3737
};
3838

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>]"),
4141
NULL
4242
};
4343

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>]"),
4646
NULL
4747
};
4848

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>]"),
5151
NULL
5252
};
5353

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>]"),
5656
NULL
5757
};
5858

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>]"),
6161
NULL
6262
};
6363

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"),
6666
NULL
6767
};
6868

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>"),
7171
NULL
7272
};
7373

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"
8176
" [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
8277
" [--] [<pathspec>...]]"),
8378
NULL
8479
};
8580

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"
8883
" [-u|--include-untracked] [-a|--all] [<message>]"),
8984
NULL
9085
};
@@ -221,7 +216,7 @@ static int clear_stash(int argc, const char **argv, const char *prefix)
221216
};
222217

223218
argc = parse_options(argc, argv, prefix, options,
224-
git_stash_helper_clear_usage,
219+
git_stash_clear_usage,
225220
PARSE_OPT_STOP_AT_NON_OPTION);
226221

227222
if (argc)
@@ -526,7 +521,7 @@ static int apply_stash(int argc, const char **argv, const char *prefix)
526521
};
527522

528523
argc = parse_options(argc, argv, prefix, options,
529-
git_stash_helper_apply_usage, 0);
524+
git_stash_apply_usage, 0);
530525

531526
if (get_stash_info(&info, argc, argv))
532527
return -1;
@@ -599,7 +594,7 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
599594
};
600595

601596
argc = parse_options(argc, argv, prefix, options,
602-
git_stash_helper_drop_usage, 0);
597+
git_stash_drop_usage, 0);
603598

604599
if (get_stash_info(&info, argc, argv))
605600
return -1;
@@ -625,7 +620,7 @@ static int pop_stash(int argc, const char **argv, const char *prefix)
625620
};
626621

627622
argc = parse_options(argc, argv, prefix, options,
628-
git_stash_helper_pop_usage, 0);
623+
git_stash_pop_usage, 0);
629624

630625
if (get_stash_info(&info, argc, argv))
631626
return -1;
@@ -652,7 +647,7 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
652647
};
653648

654649
argc = parse_options(argc, argv, prefix, options,
655-
git_stash_helper_branch_usage, 0);
650+
git_stash_branch_usage, 0);
656651

657652
if (!argc) {
658653
fprintf_ln(stderr, _("No branch name specified"));
@@ -687,7 +682,7 @@ static int list_stash(int argc, const char **argv, const char *prefix)
687682
};
688683

689684
argc = parse_options(argc, argv, prefix, options,
690-
git_stash_helper_list_usage,
685+
git_stash_list_usage,
691686
PARSE_OPT_KEEP_UNKNOWN);
692687

693688
if (!ref_exists(ref_stash))
@@ -767,7 +762,7 @@ static int show_stash(int argc, const char **argv, const char *prefix)
767762
argc = setup_revisions(argc, argv, &rev, NULL);
768763
if (argc > 1) {
769764
free_stash_info(&info);
770-
usage_with_options(git_stash_helper_show_usage, options);
765+
usage_with_options(git_stash_show_usage, options);
771766
}
772767

773768
rev.diffopt.flags.recursive = 1;
@@ -813,7 +808,7 @@ static int store_stash(int argc, const char **argv, const char *prefix)
813808
};
814809

815810
argc = parse_options(argc, argv, prefix, options,
816-
git_stash_helper_store_usage,
811+
git_stash_store_usage,
817812
PARSE_OPT_KEEP_UNKNOWN);
818813

819814
if (argc != 1) {
@@ -1227,29 +1222,18 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
12271222

12281223
static int create_stash(int argc, const char **argv, const char *prefix)
12291224
{
1230-
int include_untracked = 0;
12311225
int ret = 0;
1232-
const char *stash_msg = NULL;
12331226
struct strbuf stash_msg_buf = STRBUF_INIT;
12341227
struct stash_info info;
12351228
struct pathspec ps;
1236-
struct option options[] = {
1237-
OPT_BOOL('u', "include-untracked", &include_untracked,
1238-
N_("include untracked files in stash")),
1239-
OPT_STRING('m', "message", &stash_msg, N_("message"),
1240-
N_("stash message")),
1241-
OPT_END()
1242-
};
12431229

1244-
argc = parse_options(argc, argv, prefix, options,
1245-
git_stash_helper_create_usage,
1246-
0);
1230+
/* Starting with argv[1], since argv[0] is "create" */
1231+
strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
12471232

12481233
memset(&ps, 0, sizeof(ps));
12491234
if (!check_changes_tracked_files(ps))
12501235
return 0;
12511236

1252-
strbuf_addstr(&stash_msg_buf, stash_msg);
12531237
if (!(ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info, NULL, 0)))
12541238
printf_ln("%s", oid_to_hex(&info.w_commit));
12551239

@@ -1479,9 +1463,10 @@ static int push_stash(int argc, const char **argv, const char *prefix)
14791463
OPT_END()
14801464
};
14811465

1482-
argc = parse_options(argc, argv, prefix, options,
1483-
git_stash_helper_push_usage,
1484-
0);
1466+
if (argc)
1467+
argc = parse_options(argc, argv, prefix, options,
1468+
git_stash_push_usage,
1469+
0);
14851470

14861471
parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);
14871472
return do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,
@@ -1514,7 +1499,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
15141499
};
15151500

15161501
argc = parse_options(argc, argv, prefix, options,
1517-
git_stash_helper_save_usage,
1502+
git_stash_save_usage,
15181503
PARSE_OPT_KEEP_DASHDASH);
15191504

15201505
if (argc)
@@ -1528,27 +1513,29 @@ static int save_stash(int argc, const char **argv, const char *prefix)
15281513
return ret;
15291514
}
15301515

1531-
int cmd_stash__helper(int argc, const char **argv, const char *prefix)
1516+
int cmd_stash(int argc, const char **argv, const char *prefix)
15321517
{
1518+
int i = -1;
15331519
pid_t pid = getpid();
15341520
const char *index_file;
1521+
struct argv_array args = ARGV_ARRAY_INIT;
15351522

15361523
struct option options[] = {
15371524
OPT_END()
15381525
};
15391526

15401527
git_config(git_diff_basic_config, NULL);
15411528

1542-
argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,
1529+
argc = parse_options(argc, argv, prefix, options, git_stash_usage,
15431530
PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
15441531

15451532
index_file = get_index_file();
15461533
strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
15471534
(uintmax_t)pid);
15481535

1549-
if (argc < 1)
1550-
usage_with_options(git_stash_helper_usage, options);
1551-
if (!strcmp(argv[0], "apply"))
1536+
if (!argc)
1537+
return !!push_stash(0, NULL, prefix);
1538+
else if (!strcmp(argv[0], "apply"))
15521539
return !!apply_stash(argc, argv, prefix);
15531540
else if (!strcmp(argv[0], "clear"))
15541541
return !!clear_stash(argc, argv, prefix);
@@ -1570,7 +1557,42 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
15701557
return !!push_stash(argc, argv, prefix);
15711558
else if (!strcmp(argv[0], "save"))
15721559
return !!save_stash(argc, argv, prefix);
1560+
else if (*argv[0] != '-')
1561+
usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
1562+
git_stash_usage, options);
1563+
1564+
if (strcmp(argv[0], "-p")) {
1565+
while (++i < argc && strcmp(argv[i], "--")) {
1566+
/*
1567+
* `akpqu` is a string which contains all short options,
1568+
* except `-m` which is verified separately.
1569+
*/
1570+
if ((strlen(argv[i]) == 2) && *argv[i] == '-' &&
1571+
strchr("akpqu", argv[i][1]))
1572+
continue;
1573+
1574+
if (!strcmp(argv[i], "--all") ||
1575+
!strcmp(argv[i], "--keep-index") ||
1576+
!strcmp(argv[i], "--no-keep-index") ||
1577+
!strcmp(argv[i], "--patch") ||
1578+
!strcmp(argv[i], "--quiet") ||
1579+
!strcmp(argv[i], "--include-untracked"))
1580+
continue;
1581+
1582+
/*
1583+
* `-m` and `--message=` are verified separately because
1584+
* they need to be immediately followed by a string
1585+
* (i.e.`-m"foobar"` or `--message="foobar"`).
1586+
*/
1587+
if (starts_with(argv[i], "-m") ||
1588+
starts_with(argv[i], "--message="))
1589+
continue;
1590+
1591+
usage_with_options(git_stash_usage, options);
1592+
}
1593+
}
15731594

1574-
usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
1575-
git_stash_helper_usage, options);
1595+
argv_array_push(&args, "push");
1596+
argv_array_pushv(&args, argv);
1597+
return !!push_stash(args.argc, args.argv, prefix);
15761598
}

0 commit comments

Comments
 (0)