Skip to content

Commit 0558cbb

Browse files
SyntevoAlexhomo-programmatis
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]>
1 parent 04e2fd5 commit 0558cbb

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
@@ -1451,8 +1451,10 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
14511451
return ret;
14521452
}
14531453

1454-
static int push_stash(int argc, const char **argv, const char *prefix)
1454+
static int push_stash(int argc, const char **argv, const char *prefix,
1455+
int push_assumed)
14551456
{
1457+
int force_assume = 0;
14561458
int keep_index = -1;
14571459
int patch_mode = 0;
14581460
int include_untracked = 0;
@@ -1474,10 +1476,22 @@ static int push_stash(int argc, const char **argv, const char *prefix)
14741476
OPT_END()
14751477
};
14761478

1477-
if (argc)
1479+
if (argc) {
1480+
force_assume = !strcmp(argv[0], "-p");
14781481
argc = parse_options(argc, argv, prefix, options,
14791482
git_stash_push_usage,
1480-
0);
1483+
PARSE_OPT_KEEP_DASHDASH);
1484+
}
1485+
1486+
if (argc) {
1487+
if (!strcmp(argv[0], "--")) {
1488+
argc--;
1489+
argv++;
1490+
} else if (push_assumed && !force_assume) {
1491+
die("subcommand wasn't specified; 'push' can't be assumed due to unexpected token '%s'",
1492+
argv[0]);
1493+
}
1494+
}
14811495

14821496
parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
14831497
prefix, argv);
@@ -1550,7 +1564,6 @@ static int use_builtin_stash(void)
15501564

15511565
int cmd_stash(int argc, const char **argv, const char *prefix)
15521566
{
1553-
int i = -1;
15541567
pid_t pid = getpid();
15551568
const char *index_file;
15561569
struct argv_array args = ARGV_ARRAY_INIT;
@@ -1583,7 +1596,7 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
15831596
(uintmax_t)pid);
15841597

15851598
if (!argc)
1586-
return !!push_stash(0, NULL, prefix);
1599+
return !!push_stash(0, NULL, prefix, 0);
15871600
else if (!strcmp(argv[0], "apply"))
15881601
return !!apply_stash(argc, argv, prefix);
15891602
else if (!strcmp(argv[0], "clear"))
@@ -1603,45 +1616,15 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
16031616
else if (!strcmp(argv[0], "create"))
16041617
return !!create_stash(argc, argv, prefix);
16051618
else if (!strcmp(argv[0], "push"))
1606-
return !!push_stash(argc, argv, prefix);
1619+
return !!push_stash(argc, argv, prefix, 0);
16071620
else if (!strcmp(argv[0], "save"))
16081621
return !!save_stash(argc, argv, prefix);
16091622
else if (*argv[0] != '-')
16101623
usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
16111624
git_stash_usage, options);
16121625

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

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)