Skip to content

Commit 8c3713c

Browse files
SyntevoAlexgitster
authored andcommitted
stash: eliminate crude option parsing
Eliminate crude option parsing and rely on real parsing instead, because 1) Crude parsing is crude, for example it's not capable of handling things like `git stash -m Message` 2) Adding options in two places is inconvenient and prone to bugs As a side result, the case of `git stash -m Message` gets fixed. Also give a good error message instead of just throwing usage at user. ---- Some review of what's been happening to this code: Before [1], `git-stash.sh` only verified that all args begin with `-` : # The default command is "push" if nothing but options are given seen_non_option= for opt do case "$opt" in --) break ;; -*) ;; *) seen_non_option=t; break ;; esac done Later, [1] introduced the duplicate code I'm now removing, also making the previous test more strict by white-listing options. ---- [1] Commit 40af146 ("stash: convert `stash--helper.c` into `stash.c`" 2019-02-26) Signed-off-by: Alexandr Miloslavskiy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3f3d806 commit 8c3713c

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

builtin/stash.c

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,8 +1448,10 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
14481448
return ret;
14491449
}
14501450

1451-
static int push_stash(int argc, const char **argv, const char *prefix)
1451+
static int push_stash(int argc, const char **argv, const char *prefix,
1452+
int push_assumed)
14521453
{
1454+
int force_assume = 0;
14531455
int keep_index = -1;
14541456
int patch_mode = 0;
14551457
int include_untracked = 0;
@@ -1471,10 +1473,22 @@ static int push_stash(int argc, const char **argv, const char *prefix)
14711473
OPT_END()
14721474
};
14731475

1474-
if (argc)
1476+
if (argc) {
1477+
force_assume = !strcmp(argv[0], "-p");
14751478
argc = parse_options(argc, argv, prefix, options,
14761479
git_stash_push_usage,
1477-
0);
1480+
PARSE_OPT_KEEP_DASHDASH);
1481+
}
1482+
1483+
if (argc) {
1484+
if (!strcmp(argv[0], "--")) {
1485+
argc--;
1486+
argv++;
1487+
} else if (push_assumed && !force_assume) {
1488+
die("subcommand wasn't specified; 'push' can't be assumed due to unexpected token '%s'",
1489+
argv[0]);
1490+
}
1491+
}
14781492

14791493
parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
14801494
prefix, argv);
@@ -1547,7 +1561,6 @@ static int use_builtin_stash(void)
15471561

15481562
int cmd_stash(int argc, const char **argv, const char *prefix)
15491563
{
1550-
int i = -1;
15511564
pid_t pid = getpid();
15521565
const char *index_file;
15531566
struct argv_array args = ARGV_ARRAY_INIT;
@@ -1580,7 +1593,7 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
15801593
(uintmax_t)pid);
15811594

15821595
if (!argc)
1583-
return !!push_stash(0, NULL, prefix);
1596+
return !!push_stash(0, NULL, prefix, 0);
15841597
else if (!strcmp(argv[0], "apply"))
15851598
return !!apply_stash(argc, argv, prefix);
15861599
else if (!strcmp(argv[0], "clear"))
@@ -1600,45 +1613,15 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
16001613
else if (!strcmp(argv[0], "create"))
16011614
return !!create_stash(argc, argv, prefix);
16021615
else if (!strcmp(argv[0], "push"))
1603-
return !!push_stash(argc, argv, prefix);
1616+
return !!push_stash(argc, argv, prefix, 0);
16041617
else if (!strcmp(argv[0], "save"))
16051618
return !!save_stash(argc, argv, prefix);
16061619
else if (*argv[0] != '-')
16071620
usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
16081621
git_stash_usage, options);
16091622

1610-
if (strcmp(argv[0], "-p")) {
1611-
while (++i < argc && strcmp(argv[i], "--")) {
1612-
/*
1613-
* `akpqu` is a string which contains all short options,
1614-
* except `-m` which is verified separately.
1615-
*/
1616-
if ((strlen(argv[i]) == 2) && *argv[i] == '-' &&
1617-
strchr("akpqu", argv[i][1]))
1618-
continue;
1619-
1620-
if (!strcmp(argv[i], "--all") ||
1621-
!strcmp(argv[i], "--keep-index") ||
1622-
!strcmp(argv[i], "--no-keep-index") ||
1623-
!strcmp(argv[i], "--patch") ||
1624-
!strcmp(argv[i], "--quiet") ||
1625-
!strcmp(argv[i], "--include-untracked"))
1626-
continue;
1627-
1628-
/*
1629-
* `-m` and `--message=` are verified separately because
1630-
* they need to be immediately followed by a string
1631-
* (i.e.`-m"foobar"` or `--message="foobar"`).
1632-
*/
1633-
if (starts_with(argv[i], "-m") ||
1634-
starts_with(argv[i], "--message="))
1635-
continue;
1636-
1637-
usage_with_options(git_stash_usage, options);
1638-
}
1639-
}
1640-
1623+
/* Assume 'stash push' */
16411624
argv_array_push(&args, "push");
16421625
argv_array_pushv(&args, argv);
1643-
return !!push_stash(args.argc, args.argv, prefix);
1626+
return !!push_stash(args.argc, args.argv, prefix, 1);
16441627
}

t/t3903-stash.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ test_expect_success 'stash --no-keep-index' '
285285
test bar,bar2 = $(cat file),$(cat file2)
286286
'
287287

288+
test_expect_success 'dont assume push with non-option args' '
289+
test_must_fail git stash -q drop 2>err &&
290+
test_i18ngrep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
291+
'
292+
288293
test_expect_success 'stash --invalid-option' '
289294
echo bar5 >file &&
290295
echo bar6 >file2 &&

0 commit comments

Comments
 (0)