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};
@@ -220,7 +215,7 @@ static int clear_stash(int argc, const char **argv, const char *prefix)
220215 };
221216
222217 argc = parse_options (argc , argv , prefix , options ,
223- git_stash_helper_clear_usage ,
218+ git_stash_clear_usage ,
224219 PARSE_OPT_STOP_AT_NON_OPTION );
225220
226221 if (argc )
@@ -521,7 +516,7 @@ static int apply_stash(int argc, const char **argv, const char *prefix)
521516 };
522517
523518 argc = parse_options (argc , argv , prefix , options ,
524- git_stash_helper_apply_usage , 0 );
519+ git_stash_apply_usage , 0 );
525520
526521 if (get_stash_info (& info , argc , argv ))
527522 return -1 ;
@@ -594,7 +589,7 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
594589 };
595590
596591 argc = parse_options (argc , argv , prefix , options ,
597- git_stash_helper_drop_usage , 0 );
592+ git_stash_drop_usage , 0 );
598593
599594 if (get_stash_info (& info , argc , argv ))
600595 return -1 ;
@@ -620,7 +615,7 @@ static int pop_stash(int argc, const char **argv, const char *prefix)
620615 };
621616
622617 argc = parse_options (argc , argv , prefix , options ,
623- git_stash_helper_pop_usage , 0 );
618+ git_stash_pop_usage , 0 );
624619
625620 if (get_stash_info (& info , argc , argv ))
626621 return -1 ;
@@ -647,7 +642,7 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
647642 };
648643
649644 argc = parse_options (argc , argv , prefix , options ,
650- git_stash_helper_branch_usage , 0 );
645+ git_stash_branch_usage , 0 );
651646
652647 if (!argc ) {
653648 fprintf_ln (stderr , _ ("No branch name specified" ));
@@ -682,7 +677,7 @@ static int list_stash(int argc, const char **argv, const char *prefix)
682677 };
683678
684679 argc = parse_options (argc , argv , prefix , options ,
685- git_stash_helper_list_usage ,
680+ git_stash_list_usage ,
686681 PARSE_OPT_KEEP_UNKNOWN );
687682
688683 if (!ref_exists (ref_stash ))
@@ -762,7 +757,7 @@ static int show_stash(int argc, const char **argv, const char *prefix)
762757 argc = setup_revisions (argc , argv , & rev , NULL );
763758 if (argc > 1 ) {
764759 free_stash_info (& info );
765- usage_with_options (git_stash_helper_show_usage , options );
760+ usage_with_options (git_stash_show_usage , options );
766761 }
767762
768763 rev .diffopt .flags .recursive = 1 ;
@@ -808,7 +803,7 @@ static int store_stash(int argc, const char **argv, const char *prefix)
808803 };
809804
810805 argc = parse_options (argc , argv , prefix , options ,
811- git_stash_helper_store_usage ,
806+ git_stash_store_usage ,
812807 PARSE_OPT_KEEP_UNKNOWN );
813808
814809 if (argc != 1 ) {
@@ -1221,30 +1216,19 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
12211216
12221217static int create_stash (int argc , const char * * argv , const char * prefix )
12231218{
1224- int include_untracked = 0 ;
12251219 int ret = 0 ;
1226- const char * stash_msg = NULL ;
12271220 struct strbuf stash_msg_buf = STRBUF_INIT ;
12281221 struct stash_info info ;
12291222 struct pathspec ps ;
1230- struct option options [] = {
1231- OPT_BOOL ('u' , "include-untracked" , & include_untracked ,
1232- N_ ("include untracked files in stash" )),
1233- OPT_STRING ('m' , "message" , & stash_msg , N_ ("message" ),
1234- N_ ("stash message" )),
1235- OPT_END ()
1236- };
12371223
1238- argc = parse_options (argc , argv , prefix , options ,
1239- git_stash_helper_create_usage ,
1240- 0 );
1224+ /* Starting with argv[1], since argv[0] is "create" */
1225+ strbuf_join_argv (& stash_msg_buf , argc - 1 , ++ argv , ' ' );
12411226
12421227 memset (& ps , 0 , sizeof (ps ));
12431228 if (!check_changes_tracked_files (ps ))
12441229 return 0 ;
12451230
1246- strbuf_addstr (& stash_msg_buf , stash_msg );
1247- ret = do_create_stash (ps , & stash_msg_buf , include_untracked , 0 , & info ,
1231+ ret = do_create_stash (ps , & stash_msg_buf , 0 , 0 , & info ,
12481232 NULL , 0 );
12491233 if (!ret )
12501234 printf_ln ("%s" , oid_to_hex (& info .w_commit ));
@@ -1476,9 +1460,10 @@ static int push_stash(int argc, const char **argv, const char *prefix)
14761460 OPT_END ()
14771461 };
14781462
1479- argc = parse_options (argc , argv , prefix , options ,
1480- git_stash_helper_push_usage ,
1481- 0 );
1463+ if (argc )
1464+ argc = parse_options (argc , argv , prefix , options ,
1465+ git_stash_push_usage ,
1466+ 0 );
14821467
14831468 parse_pathspec (& ps , 0 , PATHSPEC_PREFER_FULL , prefix , argv );
14841469 return do_push_stash (ps , stash_msg , quiet , keep_index , patch_mode ,
@@ -1511,7 +1496,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
15111496 };
15121497
15131498 argc = parse_options (argc , argv , prefix , options ,
1514- git_stash_helper_save_usage ,
1499+ git_stash_save_usage ,
15151500 PARSE_OPT_KEEP_DASHDASH );
15161501
15171502 if (argc )
@@ -1525,27 +1510,29 @@ static int save_stash(int argc, const char **argv, const char *prefix)
15251510 return ret ;
15261511}
15271512
1528- int cmd_stash__helper (int argc , const char * * argv , const char * prefix )
1513+ int cmd_stash (int argc , const char * * argv , const char * prefix )
15291514{
1515+ int i = -1 ;
15301516 pid_t pid = getpid ();
15311517 const char * index_file ;
1518+ struct argv_array args = ARGV_ARRAY_INIT ;
15321519
15331520 struct option options [] = {
15341521 OPT_END ()
15351522 };
15361523
15371524 git_config (git_diff_basic_config , NULL );
15381525
1539- argc = parse_options (argc , argv , prefix , options , git_stash_helper_usage ,
1526+ argc = parse_options (argc , argv , prefix , options , git_stash_usage ,
15401527 PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH );
15411528
15421529 index_file = get_index_file ();
15431530 strbuf_addf (& stash_index_path , "%s.stash.%" PRIuMAX , index_file ,
15441531 (uintmax_t )pid );
15451532
1546- if (argc < 1 )
1547- usage_with_options ( git_stash_helper_usage , options );
1548- if (!strcmp (argv [0 ], "apply" ))
1533+ if (! argc )
1534+ return !! push_stash ( 0 , NULL , prefix );
1535+ else if (!strcmp (argv [0 ], "apply" ))
15491536 return !!apply_stash (argc , argv , prefix );
15501537 else if (!strcmp (argv [0 ], "clear" ))
15511538 return !!clear_stash (argc , argv , prefix );
@@ -1567,7 +1554,42 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
15671554 return !!push_stash (argc , argv , prefix );
15681555 else if (!strcmp (argv [0 ], "save" ))
15691556 return !!save_stash (argc , argv , prefix );
1557+ else if (* argv [0 ] != '-' )
1558+ usage_msg_opt (xstrfmt (_ ("unknown subcommand: %s" ), argv [0 ]),
1559+ git_stash_usage , options );
1560+
1561+ if (strcmp (argv [0 ], "-p" )) {
1562+ while (++ i < argc && strcmp (argv [i ], "--" )) {
1563+ /*
1564+ * `akpqu` is a string which contains all short options,
1565+ * except `-m` which is verified separately.
1566+ */
1567+ if ((strlen (argv [i ]) == 2 ) && * argv [i ] == '-' &&
1568+ strchr ("akpqu" , argv [i ][1 ]))
1569+ continue ;
1570+
1571+ if (!strcmp (argv [i ], "--all" ) ||
1572+ !strcmp (argv [i ], "--keep-index" ) ||
1573+ !strcmp (argv [i ], "--no-keep-index" ) ||
1574+ !strcmp (argv [i ], "--patch" ) ||
1575+ !strcmp (argv [i ], "--quiet" ) ||
1576+ !strcmp (argv [i ], "--include-untracked" ))
1577+ continue ;
1578+
1579+ /*
1580+ * `-m` and `--message=` are verified separately because
1581+ * they need to be immediately followed by a string
1582+ * (i.e.`-m"foobar"` or `--message="foobar"`).
1583+ */
1584+ if (starts_with (argv [i ], "-m" ) ||
1585+ starts_with (argv [i ], "--message=" ))
1586+ continue ;
1587+
1588+ usage_with_options (git_stash_usage , options );
1589+ }
1590+ }
15701591
1571- usage_msg_opt (xstrfmt (_ ("unknown subcommand: %s" ), argv [0 ]),
1572- git_stash_helper_usage , options );
1592+ argv_array_push (& args , "push" );
1593+ argv_array_pushv (& args , argv );
1594+ return !!push_stash (args .argc , args .argv , prefix );
15731595}
0 commit comments