@@ -23,6 +23,9 @@ static const char * const git_stash_helper_usage[] = {
23
23
N_ ("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]" ),
24
24
N_ ("git stash--helper branch <branchname> [<stash>]" ),
25
25
N_ ("git stash--helper clear" ),
26
+ N_ ("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
27
+ " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
28
+ " [--] [<pathspec>...]]" ),
26
29
NULL
27
30
};
28
31
@@ -71,6 +74,13 @@ static const char * const git_stash_helper_create_usage[] = {
71
74
NULL
72
75
};
73
76
77
+ static const char * const git_stash_helper_push_usage [] = {
78
+ N_ ("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
79
+ " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
80
+ " [--] [<pathspec>...]]" ),
81
+ NULL
82
+ };
83
+
74
84
static const char * ref_stash = "refs/stash" ;
75
85
static struct strbuf stash_index_path = STRBUF_INIT ;
76
86
@@ -1092,7 +1102,7 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
1092
1102
1093
1103
static int do_create_stash (struct pathspec ps , struct strbuf * stash_msg_buf ,
1094
1104
int include_untracked , int patch_mode ,
1095
- struct stash_info * info )
1105
+ struct stash_info * info , struct strbuf * patch )
1096
1106
{
1097
1107
int ret = 0 ;
1098
1108
int flags = 0 ;
@@ -1105,7 +1115,6 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
1105
1115
struct strbuf msg = STRBUF_INIT ;
1106
1116
struct strbuf commit_tree_label = STRBUF_INIT ;
1107
1117
struct strbuf untracked_files = STRBUF_INIT ;
1108
- struct strbuf patch = STRBUF_INIT ;
1109
1118
1110
1119
prepare_fallback_ident ("git stash" , "git@stash" );
1111
1120
@@ -1154,7 +1163,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
1154
1163
untracked_commit_option = 1 ;
1155
1164
}
1156
1165
if (patch_mode ) {
1157
- ret = stash_patch (info , ps , & patch );
1166
+ ret = stash_patch (info , ps , patch );
1158
1167
if (ret < 0 ) {
1159
1168
fprintf_ln (stderr , _ ("Cannot save the current "
1160
1169
"worktree state" ));
@@ -1225,7 +1234,8 @@ static int create_stash(int argc, const char **argv, const char *prefix)
1225
1234
1226
1235
memset (& ps , 0 , sizeof (ps ));
1227
1236
strbuf_addstr (& stash_msg_buf , stash_msg );
1228
- ret = do_create_stash (ps , & stash_msg_buf , include_untracked , 0 , & info );
1237
+ ret = do_create_stash (ps , & stash_msg_buf , include_untracked , 0 , & info ,
1238
+ NULL );
1229
1239
1230
1240
if (!ret )
1231
1241
printf_ln ("%s" , oid_to_hex (& info .w_commit ));
@@ -1239,6 +1249,231 @@ static int create_stash(int argc, const char **argv, const char *prefix)
1239
1249
return ret < 0 ;
1240
1250
}
1241
1251
1252
+ static int do_push_stash (struct pathspec ps , const char * stash_msg , int quiet ,
1253
+ int keep_index , int patch_mode , int include_untracked )
1254
+ {
1255
+ int ret = 0 ;
1256
+ struct stash_info info ;
1257
+ struct strbuf patch = STRBUF_INIT ;
1258
+ struct strbuf stash_msg_buf = STRBUF_INIT ;
1259
+
1260
+ if (patch_mode && keep_index == -1 )
1261
+ keep_index = 1 ;
1262
+
1263
+ if (patch_mode && include_untracked ) {
1264
+ fprintf_ln (stderr , _ ("Can't use --patch and --include-untracked"
1265
+ " or --all at the same time" ));
1266
+ ret = -1 ;
1267
+ goto done ;
1268
+ }
1269
+
1270
+ read_cache_preload (NULL );
1271
+ if (!include_untracked && ps .nr ) {
1272
+ int i ;
1273
+ char * ps_matched = xcalloc (ps .nr , 1 );
1274
+
1275
+ for (i = 0 ; i < active_nr ; i ++ )
1276
+ ce_path_match (& the_index , active_cache [i ], & ps ,
1277
+ ps_matched );
1278
+
1279
+ if (report_path_error (ps_matched , & ps , NULL )) {
1280
+ fprintf_ln (stderr , _ ("Did you forget to 'git add'?" ));
1281
+ ret = -1 ;
1282
+ free (ps_matched );
1283
+ goto done ;
1284
+ }
1285
+ free (ps_matched );
1286
+ }
1287
+
1288
+ if (refresh_cache (REFRESH_QUIET )) {
1289
+ ret = -1 ;
1290
+ goto done ;
1291
+ }
1292
+
1293
+ if (!check_changes (ps , include_untracked )) {
1294
+ if (!quiet )
1295
+ printf_ln (_ ("No local changes to save" ));
1296
+ goto done ;
1297
+ }
1298
+
1299
+ if (!reflog_exists (ref_stash ) && do_clear_stash ()) {
1300
+ ret = -1 ;
1301
+ fprintf_ln (stderr , _ ("Cannot initialize stash" ));
1302
+ goto done ;
1303
+ }
1304
+
1305
+ if (stash_msg )
1306
+ strbuf_addstr (& stash_msg_buf , stash_msg );
1307
+ if (do_create_stash (ps , & stash_msg_buf , include_untracked , patch_mode ,
1308
+ & info , & patch )) {
1309
+ ret = -1 ;
1310
+ goto done ;
1311
+ }
1312
+
1313
+ if (do_store_stash (& info .w_commit , stash_msg_buf .buf , 1 )) {
1314
+ ret = -1 ;
1315
+ fprintf_ln (stderr , _ ("Cannot save the current status" ));
1316
+ goto done ;
1317
+ }
1318
+
1319
+ printf_ln (_ ("Saved working directory and index state %s" ),
1320
+ stash_msg_buf .buf );
1321
+
1322
+ if (!patch_mode ) {
1323
+ if (include_untracked && !ps .nr ) {
1324
+ struct child_process cp = CHILD_PROCESS_INIT ;
1325
+
1326
+ cp .git_cmd = 1 ;
1327
+ argv_array_pushl (& cp .args , "clean" , "--force" ,
1328
+ "--quiet" , "-d" , NULL );
1329
+ if (include_untracked == INCLUDE_ALL_FILES )
1330
+ argv_array_push (& cp .args , "-x" );
1331
+ if (run_command (& cp )) {
1332
+ ret = -1 ;
1333
+ goto done ;
1334
+ }
1335
+ }
1336
+ if (ps .nr ) {
1337
+ struct child_process cp_add = CHILD_PROCESS_INIT ;
1338
+ struct child_process cp_diff = CHILD_PROCESS_INIT ;
1339
+ struct child_process cp_apply = CHILD_PROCESS_INIT ;
1340
+ struct strbuf out = STRBUF_INIT ;
1341
+
1342
+ cp_add .git_cmd = 1 ;
1343
+ argv_array_push (& cp_add .args , "add" );
1344
+ if (!include_untracked )
1345
+ argv_array_push (& cp_add .args , "-u" );
1346
+ if (include_untracked == INCLUDE_ALL_FILES )
1347
+ argv_array_push (& cp_add .args , "--force" );
1348
+ argv_array_push (& cp_add .args , "--" );
1349
+ add_pathspecs (& cp_add .args , ps );
1350
+ if (run_command (& cp_add )) {
1351
+ ret = -1 ;
1352
+ goto done ;
1353
+ }
1354
+
1355
+ cp_diff .git_cmd = 1 ;
1356
+ argv_array_pushl (& cp_diff .args , "diff-index" , "-p" ,
1357
+ "--cached" , "--binary" , "HEAD" , "--" ,
1358
+ NULL );
1359
+ add_pathspecs (& cp_diff .args , ps );
1360
+ if (pipe_command (& cp_diff , NULL , 0 , & out , 0 , NULL , 0 )) {
1361
+ ret = -1 ;
1362
+ goto done ;
1363
+ }
1364
+
1365
+ cp_apply .git_cmd = 1 ;
1366
+ argv_array_pushl (& cp_apply .args , "apply" , "--index" ,
1367
+ "-R" , NULL );
1368
+ if (pipe_command (& cp_apply , out .buf , out .len , NULL , 0 ,
1369
+ NULL , 0 )) {
1370
+ ret = -1 ;
1371
+ goto done ;
1372
+ }
1373
+ } else {
1374
+ struct child_process cp = CHILD_PROCESS_INIT ;
1375
+ cp .git_cmd = 1 ;
1376
+ argv_array_pushl (& cp .args , "reset" , "--hard" , "-q" ,
1377
+ NULL );
1378
+ if (run_command (& cp )) {
1379
+ ret = -1 ;
1380
+ goto done ;
1381
+ }
1382
+ }
1383
+
1384
+ if (keep_index == 1 && !is_null_oid (& info .i_tree )) {
1385
+ struct child_process cp_ls = CHILD_PROCESS_INIT ;
1386
+ struct child_process cp_checkout = CHILD_PROCESS_INIT ;
1387
+ struct strbuf out = STRBUF_INIT ;
1388
+
1389
+ if (reset_tree (& info .i_tree , 0 , 1 )) {
1390
+ ret = -1 ;
1391
+ goto done ;
1392
+ }
1393
+
1394
+ cp_ls .git_cmd = 1 ;
1395
+ argv_array_pushl (& cp_ls .args , "ls-files" , "-z" ,
1396
+ "--modified" , "--" , NULL );
1397
+
1398
+ add_pathspecs (& cp_ls .args , ps );
1399
+ if (pipe_command (& cp_ls , NULL , 0 , & out , 0 , NULL , 0 )) {
1400
+ ret = -1 ;
1401
+ goto done ;
1402
+ }
1403
+
1404
+ cp_checkout .git_cmd = 1 ;
1405
+ argv_array_pushl (& cp_checkout .args , "checkout-index" ,
1406
+ "-z" , "--force" , "--stdin" , NULL );
1407
+ if (pipe_command (& cp_checkout , out .buf , out .len , NULL ,
1408
+ 0 , NULL , 0 )) {
1409
+ ret = -1 ;
1410
+ goto done ;
1411
+ }
1412
+ }
1413
+ goto done ;
1414
+ } else {
1415
+ struct child_process cp = CHILD_PROCESS_INIT ;
1416
+
1417
+ cp .git_cmd = 1 ;
1418
+ argv_array_pushl (& cp .args , "apply" , "-R" , NULL );
1419
+
1420
+ if (pipe_command (& cp , patch .buf , patch .len , NULL , 0 , NULL , 0 )) {
1421
+ fprintf_ln (stderr , _ ("Cannot remove worktree changes" ));
1422
+ ret = -1 ;
1423
+ goto done ;
1424
+ }
1425
+
1426
+ if (keep_index < 1 ) {
1427
+ struct child_process cp = CHILD_PROCESS_INIT ;
1428
+
1429
+ cp .git_cmd = 1 ;
1430
+ argv_array_pushl (& cp .args , "reset" , "-q" , "--" , NULL );
1431
+ add_pathspecs (& cp .args , ps );
1432
+ if (run_command (& cp )) {
1433
+ ret = -1 ;
1434
+ goto done ;
1435
+ }
1436
+ }
1437
+ goto done ;
1438
+ }
1439
+
1440
+ done :
1441
+ strbuf_release (& stash_msg_buf );
1442
+ return ret ;
1443
+ }
1444
+
1445
+ static int push_stash (int argc , const char * * argv , const char * prefix )
1446
+ {
1447
+ int keep_index = -1 ;
1448
+ int patch_mode = 0 ;
1449
+ int include_untracked = 0 ;
1450
+ int quiet = 0 ;
1451
+ const char * stash_msg = NULL ;
1452
+ struct pathspec ps ;
1453
+ struct option options [] = {
1454
+ OPT_BOOL ('k' , "keep-index" , & keep_index ,
1455
+ N_ ("keep index" )),
1456
+ OPT_BOOL ('p' , "patch" , & patch_mode ,
1457
+ N_ ("stash in patch mode" )),
1458
+ OPT__QUIET (& quiet , N_ ("quiet mode" )),
1459
+ OPT_BOOL ('u' , "include-untracked" , & include_untracked ,
1460
+ N_ ("include untracked files in stash" )),
1461
+ OPT_SET_INT ('a' , "all" , & include_untracked ,
1462
+ N_ ("include ignore files" ), 2 ),
1463
+ OPT_STRING ('m' , "message" , & stash_msg , N_ ("message" ),
1464
+ N_ ("stash message" )),
1465
+ OPT_END ()
1466
+ };
1467
+
1468
+ argc = parse_options (argc , argv , prefix , options ,
1469
+ git_stash_helper_push_usage ,
1470
+ 0 );
1471
+
1472
+ parse_pathspec (& ps , 0 , PATHSPEC_PREFER_FULL , prefix , argv );
1473
+ return do_push_stash (ps , stash_msg , quiet , keep_index , patch_mode ,
1474
+ include_untracked );
1475
+ }
1476
+
1242
1477
int cmd_stash__helper (int argc , const char * * argv , const char * prefix )
1243
1478
{
1244
1479
pid_t pid = getpid ();
@@ -1277,6 +1512,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
1277
1512
return !!store_stash (argc , argv , prefix );
1278
1513
else if (!strcmp (argv [0 ], "create" ))
1279
1514
return !!create_stash (argc , argv , prefix );
1515
+ else if (!strcmp (argv [0 ], "push" ))
1516
+ return !!push_stash (argc , argv , prefix );
1280
1517
1281
1518
usage_msg_opt (xstrfmt (_ ("unknown subcommand: %s" ), argv [0 ]),
1282
1519
git_stash_helper_usage , options );
0 commit comments