1717
1818#define INCLUDE_ALL_FILES 2
1919
20- static const char * const git_stash_helper_usage [] = {
21- N_ ("git stash--helper list [<options>]" ),
22- N_ ("git stash--helper show [<options>] [<stash>]" ),
23- N_ ("git stash--helper drop [-q|--quiet] [<stash>]" ),
24- N_ ("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]" ),
25- N_ ("git stash--helper branch <branchname> [<stash>]" ),
26- N_ ("git stash--helper clear" ),
27- N_ ("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
20+ static const char * const git_stash_usage [] = {
21+ N_ ("git stash list [<options>]" ),
22+ N_ ("git stash show [<options>] [<stash>]" ),
23+ N_ ("git stash drop [-q|--quiet] [<stash>]" ),
24+ N_ ("git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]" ),
25+ N_ ("git stash branch <branchname> [<stash>]" ),
26+ N_ ("git stash clear" ),
27+ N_ ("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
2828 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
2929 " [--] [<pathspec>...]]" ),
30- N_ ("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
30+ N_ ("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
3131 " [-u|--include-untracked] [-a|--all] [<message>]" ),
3232 NULL
3333};
3434
35- static const char * const git_stash_helper_list_usage [] = {
36- N_ ("git stash--helper list [<options>]" ),
35+ static const char * const git_stash_list_usage [] = {
36+ N_ ("git stash list [<options>]" ),
3737 NULL
3838};
3939
40- static const char * const git_stash_helper_show_usage [] = {
41- N_ ("git stash--helper show [<options>] [<stash>]" ),
40+ static const char * const git_stash_show_usage [] = {
41+ N_ ("git stash show [<options>] [<stash>]" ),
4242 NULL
4343};
4444
45- static const char * const git_stash_helper_drop_usage [] = {
46- N_ ("git stash--helper drop [-q|--quiet] [<stash>]" ),
45+ static const char * const git_stash_drop_usage [] = {
46+ N_ ("git stash drop [-q|--quiet] [<stash>]" ),
4747 NULL
4848};
4949
50- static const char * const git_stash_helper_pop_usage [] = {
51- N_ ("git stash--helper pop [--index] [-q|--quiet] [<stash>]" ),
50+ static const char * const git_stash_pop_usage [] = {
51+ N_ ("git stash pop [--index] [-q|--quiet] [<stash>]" ),
5252 NULL
5353};
5454
55- static const char * const git_stash_helper_apply_usage [] = {
56- N_ ("git stash--helper apply [--index] [-q|--quiet] [<stash>]" ),
55+ static const char * const git_stash_apply_usage [] = {
56+ N_ ("git stash apply [--index] [-q|--quiet] [<stash>]" ),
5757 NULL
5858};
5959
60- static const char * const git_stash_helper_branch_usage [] = {
61- N_ ("git stash--helper branch <branchname> [<stash>]" ),
60+ static const char * const git_stash_branch_usage [] = {
61+ N_ ("git stash branch <branchname> [<stash>]" ),
6262 NULL
6363};
6464
65- static const char * const git_stash_helper_clear_usage [] = {
66- N_ ("git stash--helper clear" ),
65+ static const char * const git_stash_clear_usage [] = {
66+ N_ ("git stash clear" ),
6767 NULL
6868};
6969
70- static const char * const git_stash_helper_store_usage [] = {
71- N_ ("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>" ),
70+ static const char * const git_stash_store_usage [] = {
71+ N_ ("git stash store [-m|--message <message>] [-q|--quiet] <commit>" ),
7272 NULL
7373};
7474
75- static const char * const git_stash_helper_create_usage [] = {
76- N_ ("git stash--helper create [<message>]" ),
77- NULL
78- };
79-
80- static const char * const git_stash_helper_push_usage [] = {
81- N_ ("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
75+ static const char * const git_stash_push_usage [] = {
76+ N_ ("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
8277 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
8378 " [--] [<pathspec>...]]" ),
8479 NULL
8580};
8681
87- static const char * const git_stash_helper_save_usage [] = {
88- N_ ("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
82+ static const char * const git_stash_save_usage [] = {
83+ N_ ("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
8984 " [-u|--include-untracked] [-a|--all] [<message>]" ),
9085 NULL
9186};
@@ -222,7 +217,7 @@ static int clear_stash(int argc, const char **argv, const char *prefix)
222217 };
223218
224219 argc = parse_options (argc , argv , prefix , options ,
225- git_stash_helper_clear_usage ,
220+ git_stash_clear_usage ,
226221 PARSE_OPT_STOP_AT_NON_OPTION );
227222
228223 if (argc )
@@ -527,7 +522,7 @@ static int apply_stash(int argc, const char **argv, const char *prefix)
527522 };
528523
529524 argc = parse_options (argc , argv , prefix , options ,
530- git_stash_helper_apply_usage , 0 );
525+ git_stash_apply_usage , 0 );
531526
532527 if (get_stash_info (& info , argc , argv ))
533528 return -1 ;
@@ -600,7 +595,7 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
600595 };
601596
602597 argc = parse_options (argc , argv , prefix , options ,
603- git_stash_helper_drop_usage , 0 );
598+ git_stash_drop_usage , 0 );
604599
605600 if (get_stash_info (& info , argc , argv ))
606601 return -1 ;
@@ -626,7 +621,7 @@ static int pop_stash(int argc, const char **argv, const char *prefix)
626621 };
627622
628623 argc = parse_options (argc , argv , prefix , options ,
629- git_stash_helper_pop_usage , 0 );
624+ git_stash_pop_usage , 0 );
630625
631626 if (get_stash_info (& info , argc , argv ))
632627 return -1 ;
@@ -653,7 +648,7 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
653648 };
654649
655650 argc = parse_options (argc , argv , prefix , options ,
656- git_stash_helper_branch_usage , 0 );
651+ git_stash_branch_usage , 0 );
657652
658653 if (!argc ) {
659654 fprintf_ln (stderr , _ ("No branch name specified" ));
@@ -688,7 +683,7 @@ static int list_stash(int argc, const char **argv, const char *prefix)
688683 };
689684
690685 argc = parse_options (argc , argv , prefix , options ,
691- git_stash_helper_list_usage ,
686+ git_stash_list_usage ,
692687 PARSE_OPT_KEEP_UNKNOWN );
693688
694689 if (!ref_exists (ref_stash ))
@@ -768,7 +763,7 @@ static int show_stash(int argc, const char **argv, const char *prefix)
768763 argc = setup_revisions (argc , argv , & rev , NULL );
769764 if (argc > 1 ) {
770765 free_stash_info (& info );
771- usage_with_options (git_stash_helper_show_usage , options );
766+ usage_with_options (git_stash_show_usage , options );
772767 }
773768
774769 rev .diffopt .flags .recursive = 1 ;
@@ -814,7 +809,7 @@ static int store_stash(int argc, const char **argv, const char *prefix)
814809 };
815810
816811 argc = parse_options (argc , argv , prefix , options ,
817- git_stash_helper_store_usage ,
812+ git_stash_store_usage ,
818813 PARSE_OPT_KEEP_UNKNOWN );
819814
820815 if (argc != 1 ) {
@@ -1229,29 +1224,18 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
12291224
12301225static int create_stash (int argc , const char * * argv , const char * prefix )
12311226{
1232- int include_untracked = 0 ;
12331227 int ret = 0 ;
1234- const char * stash_msg = NULL ;
12351228 struct strbuf stash_msg_buf = STRBUF_INIT ;
12361229 struct stash_info info ;
12371230 struct pathspec ps ;
1238- struct option options [] = {
1239- OPT_BOOL ('u' , "include-untracked" , & include_untracked ,
1240- N_ ("include untracked files in stash" )),
1241- OPT_STRING ('m' , "message" , & stash_msg , N_ ("message" ),
1242- N_ ("stash message" )),
1243- OPT_END ()
1244- };
12451231
1246- argc = parse_options (argc , argv , prefix , options ,
1247- git_stash_helper_create_usage ,
1248- 0 );
1232+ /* Starting with argv[1], since argv[0] is "create" */
1233+ strbuf_join_argv (& stash_msg_buf , argc - 1 , ++ argv , ' ' );
12491234
12501235 memset (& ps , 0 , sizeof (ps ));
12511236 if (!check_changes_tracked_files (ps ))
12521237 return 0 ;
12531238
1254- strbuf_addstr (& stash_msg_buf , stash_msg );
12551239 if (!(ret = do_create_stash (ps , & stash_msg_buf , 0 , 0 , & info , NULL , 0 )))
12561240 printf_ln ("%s" , oid_to_hex (& info .w_commit ));
12571241
@@ -1481,9 +1465,10 @@ static int push_stash(int argc, const char **argv, const char *prefix)
14811465 OPT_END ()
14821466 };
14831467
1484- argc = parse_options (argc , argv , prefix , options ,
1485- git_stash_helper_push_usage ,
1486- 0 );
1468+ if (argc )
1469+ argc = parse_options (argc , argv , prefix , options ,
1470+ git_stash_push_usage ,
1471+ 0 );
14871472
14881473 parse_pathspec (& ps , 0 , PATHSPEC_PREFER_FULL , prefix , argv );
14891474 return do_push_stash (ps , stash_msg , quiet , keep_index , patch_mode ,
@@ -1516,7 +1501,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
15161501 };
15171502
15181503 argc = parse_options (argc , argv , prefix , options ,
1519- git_stash_helper_save_usage ,
1504+ git_stash_save_usage ,
15201505 PARSE_OPT_KEEP_DASHDASH );
15211506
15221507 if (argc )
@@ -1530,27 +1515,29 @@ static int save_stash(int argc, const char **argv, const char *prefix)
15301515 return ret ;
15311516}
15321517
1533- int cmd_stash__helper (int argc , const char * * argv , const char * prefix )
1518+ int cmd_stash (int argc , const char * * argv , const char * prefix )
15341519{
1520+ int i = -1 ;
15351521 pid_t pid = getpid ();
15361522 const char * index_file ;
1523+ struct argv_array args = ARGV_ARRAY_INIT ;
15371524
15381525 struct option options [] = {
15391526 OPT_END ()
15401527 };
15411528
15421529 git_config (git_diff_basic_config , NULL );
15431530
1544- argc = parse_options (argc , argv , prefix , options , git_stash_helper_usage ,
1531+ argc = parse_options (argc , argv , prefix , options , git_stash_usage ,
15451532 PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH );
15461533
15471534 index_file = get_index_file ();
15481535 strbuf_addf (& stash_index_path , "%s.stash.%" PRIuMAX , index_file ,
15491536 (uintmax_t )pid );
15501537
1551- if (argc < 1 )
1552- usage_with_options ( git_stash_helper_usage , options );
1553- if (!strcmp (argv [0 ], "apply" ))
1538+ if (! argc )
1539+ return !! push_stash ( 0 , NULL , prefix );
1540+ else if (!strcmp (argv [0 ], "apply" ))
15541541 return !!apply_stash (argc , argv , prefix );
15551542 else if (!strcmp (argv [0 ], "clear" ))
15561543 return !!clear_stash (argc , argv , prefix );
@@ -1572,7 +1559,42 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
15721559 return !!push_stash (argc , argv , prefix );
15731560 else if (!strcmp (argv [0 ], "save" ))
15741561 return !!save_stash (argc , argv , prefix );
1562+ else if (* argv [0 ] != '-' )
1563+ usage_msg_opt (xstrfmt (_ ("unknown subcommand: %s" ), argv [0 ]),
1564+ git_stash_usage , options );
1565+
1566+ if (strcmp (argv [0 ], "-p" )) {
1567+ while (++ i < argc && strcmp (argv [i ], "--" )) {
1568+ /*
1569+ * `akpqu` is a string which contains all short options,
1570+ * except `-m` which is verified separately.
1571+ */
1572+ if ((strlen (argv [i ]) == 2 ) && * argv [i ] == '-' &&
1573+ strchr ("akpqu" , argv [i ][1 ]))
1574+ continue ;
1575+
1576+ if (!strcmp (argv [i ], "--all" ) ||
1577+ !strcmp (argv [i ], "--keep-index" ) ||
1578+ !strcmp (argv [i ], "--no-keep-index" ) ||
1579+ !strcmp (argv [i ], "--patch" ) ||
1580+ !strcmp (argv [i ], "--quiet" ) ||
1581+ !strcmp (argv [i ], "--include-untracked" ))
1582+ continue ;
1583+
1584+ /*
1585+ * `-m` and `--message=` are verified separately because
1586+ * they need to be immediately followed by a string
1587+ * (i.e.`-m"foobar"` or `--message="foobar"`).
1588+ */
1589+ if (starts_with (argv [i ], "-m" ) ||
1590+ starts_with (argv [i ], "--message=" ))
1591+ continue ;
1592+
1593+ usage_with_options (git_stash_usage , options );
1594+ }
1595+ }
15751596
1576- usage_msg_opt (xstrfmt (_ ("unknown subcommand: %s" ), argv [0 ]),
1577- git_stash_helper_usage , options );
1597+ argv_array_push (& args , "push" );
1598+ argv_array_pushv (& args , argv );
1599+ return !!push_stash (args .argc , args .argv , prefix );
15781600}
0 commit comments