@@ -27,11 +27,11 @@ static const char * const git_stash_usage[] = {
27
27
N_ ("git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]" ),
28
28
N_ ("git stash branch <branchname> [<stash>]" ),
29
29
"git stash clear" ,
30
- N_ ("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
30
+ N_ ("git stash [push [-p|--patch] [-S|--staged] [- k|--[no-]keep-index] [-q|--quiet]\n"
31
31
" [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
32
32
" [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
33
33
" [--] [<pathspec>...]]" ),
34
- N_ ("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
34
+ N_ ("git stash save [-p|--patch] [-S|--staged] [- k|--[no-]keep-index] [-q|--quiet]\n"
35
35
" [-u|--include-untracked] [-a|--all] [<message>]" ),
36
36
NULL
37
37
};
@@ -1132,6 +1132,38 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
1132
1132
return ret ;
1133
1133
}
1134
1134
1135
+ static int stash_staged (struct stash_info * info , struct strbuf * out_patch ,
1136
+ int quiet )
1137
+ {
1138
+ int ret = 0 ;
1139
+ struct child_process cp_diff_tree = CHILD_PROCESS_INIT ;
1140
+ struct index_state istate = { NULL };
1141
+
1142
+ if (write_index_as_tree (& info -> w_tree , & istate , the_repository -> index_file ,
1143
+ 0 , NULL )) {
1144
+ ret = -1 ;
1145
+ goto done ;
1146
+ }
1147
+
1148
+ cp_diff_tree .git_cmd = 1 ;
1149
+ strvec_pushl (& cp_diff_tree .args , "diff-tree" , "-p" , "-U1" , "HEAD" ,
1150
+ oid_to_hex (& info -> w_tree ), "--" , NULL );
1151
+ if (pipe_command (& cp_diff_tree , NULL , 0 , out_patch , 0 , NULL , 0 )) {
1152
+ ret = -1 ;
1153
+ goto done ;
1154
+ }
1155
+
1156
+ if (!out_patch -> len ) {
1157
+ if (!quiet )
1158
+ fprintf_ln (stderr , _ ("No staged changes" ));
1159
+ ret = 1 ;
1160
+ }
1161
+
1162
+ done :
1163
+ discard_index (& istate );
1164
+ return ret ;
1165
+ }
1166
+
1135
1167
static int stash_patch (struct stash_info * info , const struct pathspec * ps ,
1136
1168
struct strbuf * out_patch , int quiet )
1137
1169
{
@@ -1258,7 +1290,7 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
1258
1290
}
1259
1291
1260
1292
static int do_create_stash (const struct pathspec * ps , struct strbuf * stash_msg_buf ,
1261
- int include_untracked , int patch_mode ,
1293
+ int include_untracked , int patch_mode , int only_staged ,
1262
1294
struct stash_info * info , struct strbuf * patch ,
1263
1295
int quiet )
1264
1296
{
@@ -1337,6 +1369,16 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
1337
1369
} else if (ret > 0 ) {
1338
1370
goto done ;
1339
1371
}
1372
+ } else if (only_staged ) {
1373
+ ret = stash_staged (info , patch , quiet );
1374
+ if (ret < 0 ) {
1375
+ if (!quiet )
1376
+ fprintf_ln (stderr , _ ("Cannot save the current "
1377
+ "staged state" ));
1378
+ goto done ;
1379
+ } else if (ret > 0 ) {
1380
+ goto done ;
1381
+ }
1340
1382
} else {
1341
1383
if (stash_working_tree (info , ps )) {
1342
1384
if (!quiet )
@@ -1395,7 +1437,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
1395
1437
if (!check_changes_tracked_files (& ps ))
1396
1438
return 0 ;
1397
1439
1398
- ret = do_create_stash (& ps , & stash_msg_buf , 0 , 0 , & info ,
1440
+ ret = do_create_stash (& ps , & stash_msg_buf , 0 , 0 , 0 , & info ,
1399
1441
NULL , 0 );
1400
1442
if (!ret )
1401
1443
printf_ln ("%s" , oid_to_hex (& info .w_commit ));
@@ -1405,7 +1447,7 @@ static int create_stash(int argc, const char **argv, const char *prefix)
1405
1447
}
1406
1448
1407
1449
static int do_push_stash (const struct pathspec * ps , const char * stash_msg , int quiet ,
1408
- int keep_index , int patch_mode , int include_untracked )
1450
+ int keep_index , int patch_mode , int include_untracked , int only_staged )
1409
1451
{
1410
1452
int ret = 0 ;
1411
1453
struct stash_info info ;
@@ -1423,6 +1465,17 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
1423
1465
goto done ;
1424
1466
}
1425
1467
1468
+ /* --patch overrides --staged */
1469
+ if (patch_mode )
1470
+ only_staged = 0 ;
1471
+
1472
+ if (only_staged && include_untracked ) {
1473
+ fprintf_ln (stderr , _ ("Can't use --staged and --include-untracked"
1474
+ " or --all at the same time" ));
1475
+ ret = -1 ;
1476
+ goto done ;
1477
+ }
1478
+
1426
1479
read_cache_preload (NULL );
1427
1480
if (!include_untracked && ps -> nr ) {
1428
1481
int i ;
@@ -1463,7 +1516,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
1463
1516
1464
1517
if (stash_msg )
1465
1518
strbuf_addstr (& stash_msg_buf , stash_msg );
1466
- if (do_create_stash (ps , & stash_msg_buf , include_untracked , patch_mode ,
1519
+ if (do_create_stash (ps , & stash_msg_buf , include_untracked , patch_mode , only_staged ,
1467
1520
& info , & patch , quiet )) {
1468
1521
ret = -1 ;
1469
1522
goto done ;
@@ -1480,7 +1533,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
1480
1533
printf_ln (_ ("Saved working directory and index state %s" ),
1481
1534
stash_msg_buf .buf );
1482
1535
1483
- if (!patch_mode ) {
1536
+ if (!( patch_mode || only_staged ) ) {
1484
1537
if (include_untracked && !ps -> nr ) {
1485
1538
struct child_process cp = CHILD_PROCESS_INIT ;
1486
1539
@@ -1598,6 +1651,7 @@ static int push_stash(int argc, const char **argv, const char *prefix,
1598
1651
{
1599
1652
int force_assume = 0 ;
1600
1653
int keep_index = -1 ;
1654
+ int only_staged = 0 ;
1601
1655
int patch_mode = 0 ;
1602
1656
int include_untracked = 0 ;
1603
1657
int quiet = 0 ;
@@ -1608,6 +1662,8 @@ static int push_stash(int argc, const char **argv, const char *prefix,
1608
1662
struct option options [] = {
1609
1663
OPT_BOOL ('k' , "keep-index" , & keep_index ,
1610
1664
N_ ("keep index" )),
1665
+ OPT_BOOL ('S' , "staged" , & only_staged ,
1666
+ N_ ("stash staged changes only" )),
1611
1667
OPT_BOOL ('p' , "patch" , & patch_mode ,
1612
1668
N_ ("stash in patch mode" )),
1613
1669
OPT__QUIET (& quiet , N_ ("quiet mode" )),
@@ -1646,6 +1702,9 @@ static int push_stash(int argc, const char **argv, const char *prefix,
1646
1702
if (patch_mode )
1647
1703
die (_ ("--pathspec-from-file is incompatible with --patch" ));
1648
1704
1705
+ if (only_staged )
1706
+ die (_ ("--pathspec-from-file is incompatible with --staged" ));
1707
+
1649
1708
if (ps .nr )
1650
1709
die (_ ("--pathspec-from-file is incompatible with pathspec arguments" ));
1651
1710
@@ -1657,12 +1716,13 @@ static int push_stash(int argc, const char **argv, const char *prefix,
1657
1716
}
1658
1717
1659
1718
return do_push_stash (& ps , stash_msg , quiet , keep_index , patch_mode ,
1660
- include_untracked );
1719
+ include_untracked , only_staged );
1661
1720
}
1662
1721
1663
1722
static int save_stash (int argc , const char * * argv , const char * prefix )
1664
1723
{
1665
1724
int keep_index = -1 ;
1725
+ int only_staged = 0 ;
1666
1726
int patch_mode = 0 ;
1667
1727
int include_untracked = 0 ;
1668
1728
int quiet = 0 ;
@@ -1673,6 +1733,8 @@ static int save_stash(int argc, const char **argv, const char *prefix)
1673
1733
struct option options [] = {
1674
1734
OPT_BOOL ('k' , "keep-index" , & keep_index ,
1675
1735
N_ ("keep index" )),
1736
+ OPT_BOOL ('S' , "staged" , & only_staged ,
1737
+ N_ ("stash staged changes only" )),
1676
1738
OPT_BOOL ('p' , "patch" , & patch_mode ,
1677
1739
N_ ("stash in patch mode" )),
1678
1740
OPT__QUIET (& quiet , N_ ("quiet mode" )),
@@ -1694,7 +1756,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
1694
1756
1695
1757
memset (& ps , 0 , sizeof (ps ));
1696
1758
ret = do_push_stash (& ps , stash_msg , quiet , keep_index ,
1697
- patch_mode , include_untracked );
1759
+ patch_mode , include_untracked , only_staged );
1698
1760
1699
1761
strbuf_release (& stash_msg_buf );
1700
1762
return ret ;
0 commit comments