Skip to content

Commit 5914750

Browse files
ungpsdscho
authored andcommitted
stash: convert push to builtin
Add stash push to the helper. Signed-off-by: Paul-Sebastian Ungureanu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 24273f9 commit 5914750

File tree

2 files changed

+245
-6
lines changed

2 files changed

+245
-6
lines changed

builtin/stash--helper.c

Lines changed: 241 additions & 4 deletions
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
prepare_fallback_ident("git stash", "git@stash");
11111120

@@ -1154,7 +1163,7 @@ static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
11541163
untracked_commit_option = 1;
11551164
}
11561165
if (patch_mode) {
1157-
ret = stash_patch(info, ps, &patch);
1166+
ret = stash_patch(info, ps, patch);
11581167
if (ret < 0) {
11591168
fprintf_ln(stderr, _("Cannot save the current "
11601169
"worktree state"));
@@ -1225,7 +1234,8 @@ static int create_stash(int argc, const char **argv, const char *prefix)
12251234

12261235
memset(&ps, 0, sizeof(ps));
12271236
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);
12291239

12301240
if (!ret)
12311241
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)
12391249
return ret < 0;
12401250
}
12411251

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+
12421477
int cmd_stash__helper(int argc, const char **argv, const char *prefix)
12431478
{
12441479
pid_t pid = getpid();
@@ -1277,6 +1512,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
12771512
return !!store_stash(argc, argv, prefix);
12781513
else if (!strcmp(argv[0], "create"))
12791514
return !!create_stash(argc, argv, prefix);
1515+
else if (!strcmp(argv[0], "push"))
1516+
return !!push_stash(argc, argv, prefix);
12801517

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

git-stash.sh

Lines changed: 4 additions & 2 deletions
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)