Skip to content

Commit 6ce68ab

Browse files
ungpsdscho
authored andcommitted
stash: convert push to builtin
Add stash push to the helper. Signed-off-by: Paul-Sebastian Ungureanu <[email protected]>
1 parent f466436 commit 6ce68ab

File tree

2 files changed

+245
-6
lines changed

2 files changed

+245
-6
lines changed

builtin/stash--helper.c

+241-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ static const char * const git_stash_helper_usage[] = {
2323
N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
2424
N_("git stash--helper branch <branchname> [<stash>]"),
2525
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>...]]"),
2629
NULL
2730
};
2831

@@ -71,6 +74,13 @@ static const char * const git_stash_helper_create_usage[] = {
7174
NULL
7275
};
7376

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+
7484
static const char *ref_stash = "refs/stash";
7585
static struct strbuf stash_index_path = STRBUF_INIT;
7686

@@ -1092,7 +1102,7 @@ static int stash_working_tree(struct stash_info *info, struct pathspec ps)
10921102

10931103
static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
10941104
int include_untracked, int patch_mode,
1095-
struct stash_info *info)
1105+
struct stash_info *info, struct strbuf *patch)
10961106
{
10971107
int ret = 0;
10981108
int flags = 0;
@@ -1105,7 +1115,6 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11051115
struct strbuf msg = STRBUF_INIT;
11061116
struct strbuf commit_tree_label = STRBUF_INIT;
11071117
struct strbuf untracked_files = STRBUF_INIT;
1108-
struct strbuf patch = STRBUF_INIT;
11091118

11101119
read_cache_preload(NULL);
11111120
refresh_cache(REFRESH_QUIET);
@@ -1152,7 +1161,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11521161
untracked_commit_option = 1;
11531162
}
11541163
if (patch_mode) {
1155-
ret = stash_patch(info, ps, &patch);
1164+
ret = stash_patch(info, ps, patch);
11561165
if (ret < 0) {
11571166
fprintf_ln(stderr, _("Cannot save the current "
11581167
"worktree state"));
@@ -1229,7 +1238,8 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12291238

12301239
memset(&ps, 0, sizeof(ps));
12311240
strbuf_addstr(&stash_msg_buf, stash_msg);
1232-
ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info);
1241+
ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
1242+
NULL);
12331243

12341244
if (!ret)
12351245
printf_ln("%s", oid_to_hex(&info.w_commit));
@@ -1243,6 +1253,231 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12431253
return ret < 0;
12441254
}
12451255

1256+
static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
1257+
int keep_index, int patch_mode, int include_untracked)
1258+
{
1259+
int ret = 0;
1260+
struct stash_info info;
1261+
struct strbuf patch = STRBUF_INIT;
1262+
struct strbuf stash_msg_buf = STRBUF_INIT;
1263+
1264+
if (patch_mode && keep_index == -1)
1265+
keep_index = 1;
1266+
1267+
if (patch_mode && include_untracked) {
1268+
fprintf_ln(stderr, _("Can't use --patch and --include-untracked"
1269+
" or --all at the same time"));
1270+
ret = -1;
1271+
goto done;
1272+
}
1273+
1274+
read_cache_preload(NULL);
1275+
if (!include_untracked && ps.nr) {
1276+
int i;
1277+
char *ps_matched = xcalloc(ps.nr, 1);
1278+
1279+
for (i = 0; i < active_nr; i++)
1280+
ce_path_match(&the_index, active_cache[i], &ps,
1281+
ps_matched);
1282+
1283+
if (report_path_error(ps_matched, &ps, NULL)) {
1284+
fprintf_ln(stderr, _("Did you forget to 'git add'?"));
1285+
ret = -1;
1286+
free(ps_matched);
1287+
goto done;
1288+
}
1289+
free(ps_matched);
1290+
}
1291+
1292+
if (refresh_cache(REFRESH_QUIET)) {
1293+
ret = -1;
1294+
goto done;
1295+
}
1296+
1297+
if (!check_changes(ps, include_untracked)) {
1298+
if (!quiet)
1299+
printf_ln(_("No local changes to save"));
1300+
goto done;
1301+
}
1302+
1303+
if (!reflog_exists(ref_stash) && do_clear_stash()) {
1304+
ret = -1;
1305+
fprintf_ln(stderr, _("Cannot initialize stash"));
1306+
goto done;
1307+
}
1308+
1309+
if (stash_msg)
1310+
strbuf_addstr(&stash_msg_buf, stash_msg);
1311+
if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1312+
&info, &patch)) {
1313+
ret = -1;
1314+
goto done;
1315+
}
1316+
1317+
if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
1318+
ret = -1;
1319+
fprintf_ln(stderr, _("Cannot save the current status"));
1320+
goto done;
1321+
}
1322+
1323+
printf_ln(_("Saved working directory and index state %s"),
1324+
stash_msg_buf.buf);
1325+
1326+
if (!patch_mode) {
1327+
if (include_untracked && !ps.nr) {
1328+
struct child_process cp = CHILD_PROCESS_INIT;
1329+
1330+
cp.git_cmd = 1;
1331+
argv_array_pushl(&cp.args, "clean", "--force",
1332+
"--quiet", "-d", NULL);
1333+
if (include_untracked == INCLUDE_ALL_FILES)
1334+
argv_array_push(&cp.args, "-x");
1335+
if (run_command(&cp)) {
1336+
ret = -1;
1337+
goto done;
1338+
}
1339+
}
1340+
if (ps.nr) {
1341+
struct child_process cp_add = CHILD_PROCESS_INIT;
1342+
struct child_process cp_diff = CHILD_PROCESS_INIT;
1343+
struct child_process cp_apply = CHILD_PROCESS_INIT;
1344+
struct strbuf out = STRBUF_INIT;
1345+
1346+
cp_add.git_cmd = 1;
1347+
argv_array_push(&cp_add.args, "add");
1348+
if (!include_untracked)
1349+
argv_array_push(&cp_add.args, "-u");
1350+
if (include_untracked == INCLUDE_ALL_FILES)
1351+
argv_array_push(&cp_add.args, "--force");
1352+
argv_array_push(&cp_add.args, "--");
1353+
add_pathspecs(&cp_add.args, ps);
1354+
if (run_command(&cp_add)) {
1355+
ret = -1;
1356+
goto done;
1357+
}
1358+
1359+
cp_diff.git_cmd = 1;
1360+
argv_array_pushl(&cp_diff.args, "diff-index", "-p",
1361+
"--cached", "--binary", "HEAD", "--",
1362+
NULL);
1363+
add_pathspecs(&cp_diff.args, ps);
1364+
if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {
1365+
ret = -1;
1366+
goto done;
1367+
}
1368+
1369+
cp_apply.git_cmd = 1;
1370+
argv_array_pushl(&cp_apply.args, "apply", "--index",
1371+
"-R", NULL);
1372+
if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,
1373+
NULL, 0)) {
1374+
ret = -1;
1375+
goto done;
1376+
}
1377+
} else {
1378+
struct child_process cp = CHILD_PROCESS_INIT;
1379+
cp.git_cmd = 1;
1380+
argv_array_pushl(&cp.args, "reset", "--hard", "-q",
1381+
NULL);
1382+
if (run_command(&cp)) {
1383+
ret = -1;
1384+
goto done;
1385+
}
1386+
}
1387+
1388+
if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1389+
struct child_process cp_ls = CHILD_PROCESS_INIT;
1390+
struct child_process cp_checkout = CHILD_PROCESS_INIT;
1391+
struct strbuf out = STRBUF_INIT;
1392+
1393+
if (reset_tree(&info.i_tree, 0, 1)) {
1394+
ret = -1;
1395+
goto done;
1396+
}
1397+
1398+
cp_ls.git_cmd = 1;
1399+
argv_array_pushl(&cp_ls.args, "ls-files", "-z",
1400+
"--modified", "--", NULL);
1401+
1402+
add_pathspecs(&cp_ls.args, ps);
1403+
if (pipe_command(&cp_ls, NULL, 0, &out, 0, NULL, 0)) {
1404+
ret = -1;
1405+
goto done;
1406+
}
1407+
1408+
cp_checkout.git_cmd = 1;
1409+
argv_array_pushl(&cp_checkout.args, "checkout-index",
1410+
"-z", "--force", "--stdin", NULL);
1411+
if (pipe_command(&cp_checkout, out.buf, out.len, NULL,
1412+
0, NULL, 0)) {
1413+
ret = -1;
1414+
goto done;
1415+
}
1416+
}
1417+
goto done;
1418+
} else {
1419+
struct child_process cp = CHILD_PROCESS_INIT;
1420+
1421+
cp.git_cmd = 1;
1422+
argv_array_pushl(&cp.args, "apply", "-R", NULL);
1423+
1424+
if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1425+
fprintf_ln(stderr, _("Cannot remove worktree changes"));
1426+
ret = -1;
1427+
goto done;
1428+
}
1429+
1430+
if (keep_index < 1) {
1431+
struct child_process cp = CHILD_PROCESS_INIT;
1432+
1433+
cp.git_cmd = 1;
1434+
argv_array_pushl(&cp.args, "reset", "-q", "--", NULL);
1435+
add_pathspecs(&cp.args, ps);
1436+
if (run_command(&cp)) {
1437+
ret = -1;
1438+
goto done;
1439+
}
1440+
}
1441+
goto done;
1442+
}
1443+
1444+
done:
1445+
strbuf_release(&stash_msg_buf);
1446+
return ret;
1447+
}
1448+
1449+
static int push_stash(int argc, const char **argv, const char *prefix)
1450+
{
1451+
int keep_index = -1;
1452+
int patch_mode = 0;
1453+
int include_untracked = 0;
1454+
int quiet = 0;
1455+
const char *stash_msg = NULL;
1456+
struct pathspec ps;
1457+
struct option options[] = {
1458+
OPT_BOOL('k', "keep-index", &keep_index,
1459+
N_("keep index")),
1460+
OPT_BOOL('p', "patch", &patch_mode,
1461+
N_("stash in patch mode")),
1462+
OPT__QUIET(&quiet, N_("quiet mode")),
1463+
OPT_BOOL('u', "include-untracked", &include_untracked,
1464+
N_("include untracked files in stash")),
1465+
OPT_SET_INT('a', "all", &include_untracked,
1466+
N_("include ignore files"), 2),
1467+
OPT_STRING('m', "message", &stash_msg, N_("message"),
1468+
N_("stash message")),
1469+
OPT_END()
1470+
};
1471+
1472+
argc = parse_options(argc, argv, prefix, options,
1473+
git_stash_helper_push_usage,
1474+
0);
1475+
1476+
parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);
1477+
return do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,
1478+
include_untracked);
1479+
}
1480+
12461481
int cmd_stash__helper(int argc, const char **argv, const char *prefix)
12471482
{
12481483
pid_t pid = getpid();
@@ -1281,6 +1516,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
12811516
return !!store_stash(argc, argv, prefix);
12821517
else if (!strcmp(argv[0], "create"))
12831518
return !!create_stash(argc, argv, prefix);
1519+
else if (!strcmp(argv[0], "push"))
1520+
return !!push_stash(argc, argv, prefix);
12841521

12851522
usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
12861523
git_stash_helper_usage, options);

git-stash.sh

+4-2
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ save)
412412
;;
413413
push)
414414
shift
415-
push_stash "$@"
415+
cd "$START_DIR"
416+
git stash--helper push "$@"
416417
;;
417418
apply)
418419
shift
@@ -448,7 +449,8 @@ branch)
448449
*)
449450
case $# in
450451
0)
451-
push_stash &&
452+
cd "$START_DIR"
453+
git stash--helper push &&
452454
say "$(gettext "(To restore them type \"git stash apply\")")"
453455
;;
454456
*)

0 commit comments

Comments
 (0)