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 ) {
@@ -1237,28 +1232,16 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
12371232
12381233static int create_stash (int argc , const char * * argv , const char * prefix )
12391234{
1240- int include_untracked = 0 ;
12411235 int ret = 0 ;
1242- const char * stash_msg = NULL ;
12431236 struct strbuf stash_msg_buf = STRBUF_INIT ;
12441237 struct stash_info info ;
12451238 struct pathspec ps ;
1246- struct option options [] = {
1247- OPT_BOOL ('u' , "include-untracked" , & include_untracked ,
1248- N_ ("include untracked files in stash" )),
1249- OPT_STRING ('m' , "message" , & stash_msg , N_ ("message" ),
1250- N_ ("stash message" )),
1251- OPT_END ()
1252- };
12531239
1254- argc = parse_options (argc , argv , prefix , options ,
1255- git_stash_helper_create_usage ,
1256- 0 );
1240+ /* Starting with argv[1], since argv[0] is "create" */
1241+ strbuf_join_argv (& stash_msg_buf , argc - 1 , ++ argv , ' ' );
12571242
12581243 memset (& ps , 0 , sizeof (ps ));
1259- strbuf_addstr (& stash_msg_buf , stash_msg );
1260- ret = do_create_stash (ps , & stash_msg_buf , include_untracked , 0 , & info ,
1261- NULL , 0 );
1244+ ret = do_create_stash (ps , & stash_msg_buf , 0 , 0 , & info , NULL , 0 );
12621245
12631246 if (!ret )
12641247 printf_ln ("%s" , oid_to_hex (& info .w_commit ));
@@ -1493,9 +1476,10 @@ static int push_stash(int argc, const char **argv, const char *prefix)
14931476 OPT_END ()
14941477 };
14951478
1496- argc = parse_options (argc , argv , prefix , options ,
1497- git_stash_helper_push_usage ,
1498- 0 );
1479+ if (argc )
1480+ argc = parse_options (argc , argv , prefix , options ,
1481+ git_stash_push_usage ,
1482+ 0 );
14991483
15001484 parse_pathspec (& ps , 0 , PATHSPEC_PREFER_FULL , prefix , argv );
15011485 return do_push_stash (ps , stash_msg , quiet , keep_index , patch_mode ,
@@ -1528,7 +1512,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
15281512 };
15291513
15301514 argc = parse_options (argc , argv , prefix , options ,
1531- git_stash_helper_save_usage ,
1515+ git_stash_save_usage ,
15321516 PARSE_OPT_KEEP_DASHDASH );
15331517
15341518 if (argc )
@@ -1542,27 +1526,29 @@ static int save_stash(int argc, const char **argv, const char *prefix)
15421526 return ret ;
15431527}
15441528
1545- int cmd_stash__helper (int argc , const char * * argv , const char * prefix )
1529+ int cmd_stash (int argc , const char * * argv , const char * prefix )
15461530{
1531+ int i = -1 ;
15471532 pid_t pid = getpid ();
15481533 const char * index_file ;
1534+ struct argv_array args = ARGV_ARRAY_INIT ;
15491535
15501536 struct option options [] = {
15511537 OPT_END ()
15521538 };
15531539
15541540 git_config (git_diff_basic_config , NULL );
15551541
1556- argc = parse_options (argc , argv , prefix , options , git_stash_helper_usage ,
1542+ argc = parse_options (argc , argv , prefix , options , git_stash_usage ,
15571543 PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH );
15581544
15591545 index_file = get_index_file ();
15601546 strbuf_addf (& stash_index_path , "%s.stash.%" PRIuMAX , index_file ,
15611547 (uintmax_t )pid );
15621548
1563- if (argc < 1 )
1564- usage_with_options ( git_stash_helper_usage , options );
1565- if (!strcmp (argv [0 ], "apply" ))
1549+ if (! argc )
1550+ return !! push_stash ( 0 , NULL , prefix );
1551+ else if (!strcmp (argv [0 ], "apply" ))
15661552 return !!apply_stash (argc , argv , prefix );
15671553 else if (!strcmp (argv [0 ], "clear" ))
15681554 return !!clear_stash (argc , argv , prefix );
@@ -1584,7 +1570,42 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
15841570 return !!push_stash (argc , argv , prefix );
15851571 else if (!strcmp (argv [0 ], "save" ))
15861572 return !!save_stash (argc , argv , prefix );
1573+ else if (* argv [0 ] != '-' )
1574+ usage_msg_opt (xstrfmt (_ ("unknown subcommand: %s" ), argv [0 ]),
1575+ git_stash_usage , options );
1576+
1577+ if (strcmp (argv [0 ], "-p" )) {
1578+ while (++ i < argc && strcmp (argv [i ], "--" )) {
1579+ /*
1580+ * `akpqu` is a string which contains all short options,
1581+ * except `-m` which is verified separately.
1582+ */
1583+ if ((strlen (argv [i ]) == 2 ) && * argv [i ] == '-' &&
1584+ strchr ("akpqu" , argv [i ][1 ]))
1585+ continue ;
1586+
1587+ if (!strcmp (argv [i ], "--all" ) ||
1588+ !strcmp (argv [i ], "--keep-index" ) ||
1589+ !strcmp (argv [i ], "--no-keep-index" ) ||
1590+ !strcmp (argv [i ], "--patch" ) ||
1591+ !strcmp (argv [i ], "--quiet" ) ||
1592+ !strcmp (argv [i ], "--include-untracked" ))
1593+ continue ;
1594+
1595+ /*
1596+ * `-m` and `--message=` are verified separately because
1597+ * they need to be immediately followed by a string
1598+ * (i.e.`-m"foobar"` or `--message="foobar"`).
1599+ */
1600+ if (starts_with (argv [i ], "-m" ) ||
1601+ starts_with (argv [i ], "--message=" ))
1602+ continue ;
1603+
1604+ usage_with_options (git_stash_usage , options );
1605+ }
1606+ }
15871607
1588- usage_msg_opt (xstrfmt (_ ("unknown subcommand: %s" ), argv [0 ]),
1589- git_stash_helper_usage , options );
1608+ argv_array_push (& args , "push" );
1609+ argv_array_pushv (& args , argv );
1610+ return !!push_stash (args .argc , args .argv , prefix );
15901611}
0 commit comments