]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/stash--helper.c
stash: replace all `write-tree` child processes with API calls
[thirdparty/git.git] / builtin / stash--helper.c
CommitLineData
8a0fc8d1
JT
1#include "builtin.h"
2#include "config.h"
3#include "parse-options.h"
4#include "refs.h"
5#include "lockfile.h"
6#include "cache-tree.h"
7#include "unpack-trees.h"
8#include "merge-recursive.h"
9#include "argv-array.h"
10#include "run-command.h"
11#include "dir.h"
12#include "rerere.h"
dc7bd382
PSU
13#include "revision.h"
14#include "log-tree.h"
d4788af8
PSU
15#include "diffcore.h"
16
17#define INCLUDE_ALL_FILES 2
8a0fc8d1
JT
18
19static const char * const git_stash_helper_usage[] = {
130f2697 20 N_("git stash--helper list [<options>]"),
dc7bd382 21 N_("git stash--helper show [<options>] [<stash>]"),
4e2dd393 22 N_("git stash--helper drop [-q|--quiet] [<stash>]"),
c4de61d7 23 N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
577c1995 24 N_("git stash--helper branch <branchname> [<stash>]"),
4e2dd393 25 N_("git stash--helper clear"),
d553f538
PSU
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>...]]"),
64fe9c26
PSU
29 N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
30 " [-u|--include-untracked] [-a|--all] [<message>]"),
4e2dd393
JT
31 NULL
32};
33
130f2697
PSU
34static const char * const git_stash_helper_list_usage[] = {
35 N_("git stash--helper list [<options>]"),
36 NULL
37};
38
dc7bd382
PSU
39static const char * const git_stash_helper_show_usage[] = {
40 N_("git stash--helper show [<options>] [<stash>]"),
41 NULL
42};
43
4e2dd393
JT
44static const char * const git_stash_helper_drop_usage[] = {
45 N_("git stash--helper drop [-q|--quiet] [<stash>]"),
8a0fc8d1
JT
46 NULL
47};
48
c4de61d7
JT
49static const char * const git_stash_helper_pop_usage[] = {
50 N_("git stash--helper pop [--index] [-q|--quiet] [<stash>]"),
51 NULL
52};
53
8a0fc8d1
JT
54static const char * const git_stash_helper_apply_usage[] = {
55 N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
56 NULL
57};
58
577c1995
JT
59static const char * const git_stash_helper_branch_usage[] = {
60 N_("git stash--helper branch <branchname> [<stash>]"),
61 NULL
62};
63
4e2dd393
JT
64static const char * const git_stash_helper_clear_usage[] = {
65 N_("git stash--helper clear"),
66 NULL
67};
68
41e0dd55
PSU
69static const char * const git_stash_helper_store_usage[] = {
70 N_("git stash--helper store [-m|--message <message>] [-q|--quiet] <commit>"),
71 NULL
72};
73
d4788af8
PSU
74static const char * const git_stash_helper_create_usage[] = {
75 N_("git stash--helper create [<message>]"),
76 NULL
77};
78
d553f538
PSU
79static const char * const git_stash_helper_push_usage[] = {
80 N_("git stash--helper [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
81 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
82 " [--] [<pathspec>...]]"),
83 NULL
84};
85
64fe9c26
PSU
86static const char * const git_stash_helper_save_usage[] = {
87 N_("git stash--helper save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
88 " [-u|--include-untracked] [-a|--all] [<message>]"),
89 NULL
90};
91
8a0fc8d1
JT
92static const char *ref_stash = "refs/stash";
93static struct strbuf stash_index_path = STRBUF_INIT;
94
95/*
96 * w_commit is set to the commit containing the working tree
97 * b_commit is set to the base commit
98 * i_commit is set to the commit containing the index tree
99 * u_commit is set to the commit containing the untracked files tree
100 * w_tree is set to the working tree
101 * b_tree is set to the base tree
102 * i_tree is set to the index tree
103 * u_tree is set to the untracked files tree
104 */
105struct stash_info {
106 struct object_id w_commit;
107 struct object_id b_commit;
108 struct object_id i_commit;
109 struct object_id u_commit;
110 struct object_id w_tree;
111 struct object_id b_tree;
112 struct object_id i_tree;
113 struct object_id u_tree;
114 struct strbuf revision;
115 int is_stash_ref;
116 int has_u;
117};
118
119static void free_stash_info(struct stash_info *info)
120{
121 strbuf_release(&info->revision);
122}
123
124static void assert_stash_like(struct stash_info *info, const char *revision)
125{
126 if (get_oidf(&info->b_commit, "%s^1", revision) ||
127 get_oidf(&info->w_tree, "%s:", revision) ||
128 get_oidf(&info->b_tree, "%s^1:", revision) ||
129 get_oidf(&info->i_tree, "%s^2:", revision))
130 die(_("'%s' is not a stash-like commit"), revision);
131}
132
133static int get_stash_info(struct stash_info *info, int argc, const char **argv)
134{
135 int ret;
136 char *end_of_rev;
137 char *expanded_ref;
138 const char *revision;
139 const char *commit = NULL;
140 struct object_id dummy;
141 struct strbuf symbolic = STRBUF_INIT;
142
143 if (argc > 1) {
144 int i;
145 struct strbuf refs_msg = STRBUF_INIT;
146
147 for (i = 0; i < argc; i++)
148 strbuf_addf(&refs_msg, " '%s'", argv[i]);
149
150 fprintf_ln(stderr, _("Too many revisions specified:%s"),
151 refs_msg.buf);
152 strbuf_release(&refs_msg);
153
154 return -1;
155 }
156
157 if (argc == 1)
158 commit = argv[0];
159
160 strbuf_init(&info->revision, 0);
161 if (!commit) {
162 if (!ref_exists(ref_stash)) {
163 free_stash_info(info);
164 fprintf_ln(stderr, _("No stash entries found."));
165 return -1;
166 }
167
168 strbuf_addf(&info->revision, "%s@{0}", ref_stash);
169 } else if (strspn(commit, "0123456789") == strlen(commit)) {
170 strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
171 } else {
172 strbuf_addstr(&info->revision, commit);
173 }
174
175 revision = info->revision.buf;
176
177 if (get_oid(revision, &info->w_commit)) {
178 error(_("%s is not a valid reference"), revision);
179 free_stash_info(info);
180 return -1;
181 }
182
183 assert_stash_like(info, revision);
184
185 info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision);
186
187 end_of_rev = strchrnul(revision, '@');
188 strbuf_add(&symbolic, revision, end_of_rev - revision);
189
190 ret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref);
191 strbuf_release(&symbolic);
192 switch (ret) {
193 case 0: /* Not found, but valid ref */
194 info->is_stash_ref = 0;
195 break;
196 case 1:
197 info->is_stash_ref = !strcmp(expanded_ref, ref_stash);
198 break;
199 default: /* Invalid or ambiguous */
200 free_stash_info(info);
201 }
202
203 free(expanded_ref);
204 return !(ret == 0 || ret == 1);
205}
206
4e2dd393
JT
207static int do_clear_stash(void)
208{
209 struct object_id obj;
210 if (get_oid(ref_stash, &obj))
211 return 0;
212
213 return delete_ref(NULL, ref_stash, &obj, 0);
214}
215
216static int clear_stash(int argc, const char **argv, const char *prefix)
217{
218 struct option options[] = {
219 OPT_END()
220 };
221
222 argc = parse_options(argc, argv, prefix, options,
223 git_stash_helper_clear_usage,
224 PARSE_OPT_STOP_AT_NON_OPTION);
225
226 if (argc)
227 return error(_("git stash clear with parameters is "
228 "unimplemented"));
229
230 return do_clear_stash();
231}
232
8a0fc8d1
JT
233static int reset_tree(struct object_id *i_tree, int update, int reset)
234{
235 int nr_trees = 1;
236 struct unpack_trees_options opts;
237 struct tree_desc t[MAX_UNPACK_TREES];
238 struct tree *tree;
239 struct lock_file lock_file = LOCK_INIT;
240
241 read_cache_preload(NULL);
242 if (refresh_cache(REFRESH_QUIET))
243 return -1;
244
245 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
246
247 memset(&opts, 0, sizeof(opts));
248
249 tree = parse_tree_indirect(i_tree);
250 if (parse_tree(tree))
251 return -1;
252
253 init_tree_desc(t, tree->buffer, tree->size);
254
255 opts.head_idx = 1;
256 opts.src_index = &the_index;
257 opts.dst_index = &the_index;
258 opts.merge = 1;
259 opts.reset = reset;
260 opts.update = update;
261 opts.fn = oneway_merge;
262
263 if (unpack_trees(nr_trees, t, &opts))
264 return -1;
265
266 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
267 return error(_("unable to write new index file"));
268
269 return 0;
270}
271
272static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
273{
274 struct child_process cp = CHILD_PROCESS_INIT;
275 const char *w_commit_hex = oid_to_hex(w_commit);
276
277 /*
278 * Diff-tree would not be very hard to replace with a native function,
279 * however it should be done together with apply_cached.
280 */
281 cp.git_cmd = 1;
282 argv_array_pushl(&cp.args, "diff-tree", "--binary", NULL);
283 argv_array_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
284
285 return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
286}
287
288static int apply_cached(struct strbuf *out)
289{
290 struct child_process cp = CHILD_PROCESS_INIT;
291
292 /*
293 * Apply currently only reads either from stdin or a file, thus
294 * apply_all_patches would have to be updated to optionally take a
295 * buffer.
296 */
297 cp.git_cmd = 1;
298 argv_array_pushl(&cp.args, "apply", "--cached", NULL);
299 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
300}
301
302static int reset_head(void)
303{
304 struct child_process cp = CHILD_PROCESS_INIT;
305
306 /*
307 * Reset is overall quite simple, however there is no current public
308 * API for resetting.
309 */
310 cp.git_cmd = 1;
311 argv_array_push(&cp.args, "reset");
312
313 return run_command(&cp);
314}
315
d4788af8
PSU
316static void add_diff_to_buf(struct diff_queue_struct *q,
317 struct diff_options *options,
318 void *data)
319{
320 int i;
321
322 for (i = 0; i < q->nr; i++) {
323 strbuf_addstr(data, q->queue[i]->one->path);
324
325 /* NUL-terminate: will be fed to update-index -z */
326 strbuf_addch(data, '\0');
327 }
328}
329
8a0fc8d1
JT
330static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)
331{
332 struct child_process cp = CHILD_PROCESS_INIT;
333 const char *c_tree_hex = oid_to_hex(c_tree);
334
335 /*
336 * diff-index is very similar to diff-tree above, and should be
337 * converted together with update_index.
338 */
339 cp.git_cmd = 1;
340 argv_array_pushl(&cp.args, "diff-index", "--cached", "--name-only",
341 "--diff-filter=A", NULL);
342 argv_array_push(&cp.args, c_tree_hex);
343 return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
344}
345
346static int update_index(struct strbuf *out)
347{
348 struct child_process cp = CHILD_PROCESS_INIT;
349
350 /*
351 * Update-index is very complicated and may need to have a public
352 * function exposed in order to remove this forking.
353 */
354 cp.git_cmd = 1;
355 argv_array_pushl(&cp.args, "update-index", "--add", "--stdin", NULL);
356 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
357}
358
359static int restore_untracked(struct object_id *u_tree)
360{
361 int res;
362 struct child_process cp = CHILD_PROCESS_INIT;
363
364 /*
365 * We need to run restore files from a given index, but without
366 * affecting the current index, so we use GIT_INDEX_FILE with
367 * run_command to fork processes that will not interfere.
368 */
369 cp.git_cmd = 1;
370 argv_array_push(&cp.args, "read-tree");
371 argv_array_push(&cp.args, oid_to_hex(u_tree));
372 argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
373 stash_index_path.buf);
374 if (run_command(&cp)) {
375 remove_path(stash_index_path.buf);
376 return -1;
377 }
378
379 child_process_init(&cp);
380 cp.git_cmd = 1;
381 argv_array_pushl(&cp.args, "checkout-index", "--all", NULL);
382 argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
383 stash_index_path.buf);
384
385 res = run_command(&cp);
386 remove_path(stash_index_path.buf);
387 return res;
388}
389
390static int do_apply_stash(const char *prefix, struct stash_info *info,
391 int index, int quiet)
392{
393 int ret;
394 int has_index = index;
395 struct merge_options o;
396 struct object_id c_tree;
397 struct object_id index_tree;
398 struct commit *result;
399 const struct object_id *bases[1];
400
401 read_cache_preload(NULL);
402 if (refresh_cache(REFRESH_QUIET))
403 return -1;
404
405 if (write_cache_as_tree(&c_tree, 0, NULL))
406 return error(_("cannot apply a stash in the middle of a merge"));
407
408 if (index) {
409 if (oideq(&info->b_tree, &info->i_tree) ||
410 oideq(&c_tree, &info->i_tree)) {
411 has_index = 0;
412 } else {
413 struct strbuf out = STRBUF_INIT;
414
415 if (diff_tree_binary(&out, &info->w_commit)) {
416 strbuf_release(&out);
417 return error(_("could not generate diff %s^!."),
418 oid_to_hex(&info->w_commit));
419 }
420
421 ret = apply_cached(&out);
422 strbuf_release(&out);
423 if (ret)
424 return error(_("conflicts in index."
425 "Try without --index."));
426
427 discard_cache();
428 read_cache();
429 if (write_cache_as_tree(&index_tree, 0, NULL))
430 return error(_("could not save index tree"));
431
432 reset_head();
433 }
434 }
435
436 if (info->has_u && restore_untracked(&info->u_tree))
437 return error(_("could not restore untracked files from stash"));
438
439 init_merge_options(&o);
440
441 o.branch1 = "Updated upstream";
442 o.branch2 = "Stashed changes";
443
444 if (oideq(&info->b_tree, &c_tree))
445 o.branch1 = "Version stash was based on";
446
447 if (quiet)
448 o.verbosity = 0;
449
450 if (o.verbosity >= 3)
451 printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
452
453 bases[0] = &info->b_tree;
454
455 ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, 1, bases,
456 &result);
457 if (ret) {
458 rerere(0);
459
460 if (index)
461 fprintf_ln(stderr, _("Index was not unstashed."));
462
463 return ret;
464 }
465
466 if (has_index) {
467 if (reset_tree(&index_tree, 0, 0))
468 return -1;
469 } else {
470 struct strbuf out = STRBUF_INIT;
471
472 if (get_newly_staged(&out, &c_tree)) {
473 strbuf_release(&out);
474 return -1;
475 }
476
477 if (reset_tree(&c_tree, 0, 1)) {
478 strbuf_release(&out);
479 return -1;
480 }
481
482 ret = update_index(&out);
483 strbuf_release(&out);
484 if (ret)
485 return -1;
486
487 discard_cache();
488 }
489
490 if (quiet) {
491 if (refresh_cache(REFRESH_QUIET))
492 warning("could not refresh index");
493 } else {
494 struct child_process cp = CHILD_PROCESS_INIT;
495
496 /*
497 * Status is quite simple and could be replaced with calls to
498 * wt_status in the future, but it adds complexities which may
499 * require more tests.
500 */
501 cp.git_cmd = 1;
502 cp.dir = prefix;
503 argv_array_push(&cp.args, "status");
504 run_command(&cp);
505 }
506
507 return 0;
508}
509
510static int apply_stash(int argc, const char **argv, const char *prefix)
511{
512 int ret;
513 int quiet = 0;
514 int index = 0;
515 struct stash_info info;
516 struct option options[] = {
517 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
518 OPT_BOOL(0, "index", &index,
519 N_("attempt to recreate the index")),
520 OPT_END()
521 };
522
523 argc = parse_options(argc, argv, prefix, options,
524 git_stash_helper_apply_usage, 0);
525
526 if (get_stash_info(&info, argc, argv))
527 return -1;
528
529 ret = do_apply_stash(prefix, &info, index, quiet);
530 free_stash_info(&info);
531 return ret;
532}
533
4e2dd393
JT
534static int do_drop_stash(const char *prefix, struct stash_info *info, int quiet)
535{
536 int ret;
537 struct child_process cp_reflog = CHILD_PROCESS_INIT;
538 struct child_process cp = CHILD_PROCESS_INIT;
539
540 /*
541 * reflog does not provide a simple function for deleting refs. One will
542 * need to be added to avoid implementing too much reflog code here
543 */
544
545 cp_reflog.git_cmd = 1;
546 argv_array_pushl(&cp_reflog.args, "reflog", "delete", "--updateref",
547 "--rewrite", NULL);
548 argv_array_push(&cp_reflog.args, info->revision.buf);
549 ret = run_command(&cp_reflog);
550 if (!ret) {
551 if (!quiet)
552 printf_ln(_("Dropped %s (%s)"), info->revision.buf,
553 oid_to_hex(&info->w_commit));
554 } else {
555 return error(_("%s: Could not drop stash entry"),
556 info->revision.buf);
557 }
558
559 /*
560 * This could easily be replaced by get_oid, but currently it will throw
561 * a fatal error when a reflog is empty, which we can not recover from.
562 */
563 cp.git_cmd = 1;
564 /* Even though --quiet is specified, rev-parse still outputs the hash */
565 cp.no_stdout = 1;
566 argv_array_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL);
567 argv_array_pushf(&cp.args, "%s@{0}", ref_stash);
568 ret = run_command(&cp);
569
570 /* do_clear_stash if we just dropped the last stash entry */
571 if (ret)
572 do_clear_stash();
573
574 return 0;
575}
576
577static void assert_stash_ref(struct stash_info *info)
578{
579 if (!info->is_stash_ref) {
580 error(_("'%s' is not a stash reference"), info->revision.buf);
581 free_stash_info(info);
582 exit(1);
583 }
584}
585
586static int drop_stash(int argc, const char **argv, const char *prefix)
587{
588 int ret;
589 int quiet = 0;
590 struct stash_info info;
591 struct option options[] = {
592 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
593 OPT_END()
594 };
595
596 argc = parse_options(argc, argv, prefix, options,
597 git_stash_helper_drop_usage, 0);
598
599 if (get_stash_info(&info, argc, argv))
600 return -1;
601
602 assert_stash_ref(&info);
603
604 ret = do_drop_stash(prefix, &info, quiet);
605 free_stash_info(&info);
606 return ret;
607}
608
c4de61d7
JT
609static int pop_stash(int argc, const char **argv, const char *prefix)
610{
611 int ret;
612 int index = 0;
613 int quiet = 0;
614 struct stash_info info;
615 struct option options[] = {
616 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
617 OPT_BOOL(0, "index", &index,
618 N_("attempt to recreate the index")),
619 OPT_END()
620 };
621
622 argc = parse_options(argc, argv, prefix, options,
623 git_stash_helper_pop_usage, 0);
624
625 if (get_stash_info(&info, argc, argv))
626 return -1;
627
628 assert_stash_ref(&info);
629 if ((ret = do_apply_stash(prefix, &info, index, quiet)))
630 printf_ln(_("The stash entry is kept in case "
631 "you need it again."));
632 else
633 ret = do_drop_stash(prefix, &info, quiet);
634
635 free_stash_info(&info);
636 return ret;
637}
638
577c1995
JT
639static int branch_stash(int argc, const char **argv, const char *prefix)
640{
641 int ret;
642 const char *branch = NULL;
643 struct stash_info info;
644 struct child_process cp = CHILD_PROCESS_INIT;
645 struct option options[] = {
646 OPT_END()
647 };
648
649 argc = parse_options(argc, argv, prefix, options,
650 git_stash_helper_branch_usage, 0);
651
652 if (!argc) {
653 fprintf_ln(stderr, _("No branch name specified"));
654 return -1;
655 }
656
657 branch = argv[0];
658
659 if (get_stash_info(&info, argc - 1, argv + 1))
660 return -1;
661
662 cp.git_cmd = 1;
663 argv_array_pushl(&cp.args, "checkout", "-b", NULL);
664 argv_array_push(&cp.args, branch);
665 argv_array_push(&cp.args, oid_to_hex(&info.b_commit));
666 ret = run_command(&cp);
667 if (!ret)
668 ret = do_apply_stash(prefix, &info, 1, 0);
669 if (!ret && info.is_stash_ref)
670 ret = do_drop_stash(prefix, &info, 0);
671
672 free_stash_info(&info);
673
674 return ret;
675}
676
130f2697
PSU
677static int list_stash(int argc, const char **argv, const char *prefix)
678{
679 struct child_process cp = CHILD_PROCESS_INIT;
680 struct option options[] = {
681 OPT_END()
682 };
683
684 argc = parse_options(argc, argv, prefix, options,
685 git_stash_helper_list_usage,
686 PARSE_OPT_KEEP_UNKNOWN);
687
688 if (!ref_exists(ref_stash))
689 return 0;
690
691 cp.git_cmd = 1;
692 argv_array_pushl(&cp.args, "log", "--format=%gd: %gs", "-g",
693 "--first-parent", "-m", NULL);
694 argv_array_pushv(&cp.args, argv);
695 argv_array_push(&cp.args, ref_stash);
696 argv_array_push(&cp.args, "--");
697 return run_command(&cp);
698}
699
dc7bd382
PSU
700static int show_stat = 1;
701static int show_patch;
702
703static int git_stash_config(const char *var, const char *value, void *cb)
704{
705 if (!strcmp(var, "stash.showstat")) {
706 show_stat = git_config_bool(var, value);
707 return 0;
708 }
709 if (!strcmp(var, "stash.showpatch")) {
710 show_patch = git_config_bool(var, value);
711 return 0;
712 }
713 return git_default_config(var, value, cb);
714}
715
716static int show_stash(int argc, const char **argv, const char *prefix)
717{
718 int i;
719 int opts = 0;
720 int ret = 0;
721 struct stash_info info;
722 struct rev_info rev;
723 struct argv_array stash_args = ARGV_ARRAY_INIT;
724 struct option options[] = {
725 OPT_END()
726 };
727
728 init_diff_ui_defaults();
729 git_config(git_diff_ui_config, NULL);
730 init_revisions(&rev, prefix);
731
732 for (i = 1; i < argc; i++) {
733 if (argv[i][0] != '-')
734 argv_array_push(&stash_args, argv[i]);
735 else
736 opts++;
737 }
738
739 ret = get_stash_info(&info, stash_args.argc, stash_args.argv);
740 argv_array_clear(&stash_args);
741 if (ret)
742 return -1;
743
744 /*
745 * The config settings are applied only if there are not passed
746 * any options.
747 */
748 if (!opts) {
749 git_config(git_stash_config, NULL);
750 if (show_stat)
751 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;
752
753 if (show_patch)
754 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
755
756 if (!show_stat && !show_patch) {
757 free_stash_info(&info);
758 return 0;
759 }
760 }
761
762 argc = setup_revisions(argc, argv, &rev, NULL);
763 if (argc > 1) {
764 free_stash_info(&info);
765 usage_with_options(git_stash_helper_show_usage, options);
766 }
767
768 rev.diffopt.flags.recursive = 1;
769 setup_diff_pager(&rev.diffopt);
770 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
771 log_tree_diff_flush(&rev);
772
773 free_stash_info(&info);
774 return diff_result_code(&rev.diffopt, 0);
775}
776
41e0dd55
PSU
777static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
778 int quiet)
779{
780 if (!stash_msg)
781 stash_msg = "Created via \"git stash store\".";
782
783 if (update_ref(stash_msg, ref_stash, w_commit, NULL,
784 REF_FORCE_CREATE_REFLOG,
785 quiet ? UPDATE_REFS_QUIET_ON_ERR :
786 UPDATE_REFS_MSG_ON_ERR)) {
787 if (!quiet) {
788 fprintf_ln(stderr, _("Cannot update %s with %s"),
789 ref_stash, oid_to_hex(w_commit));
790 }
791 return -1;
792 }
793
794 return 0;
795}
796
797static int store_stash(int argc, const char **argv, const char *prefix)
798{
799 int quiet = 0;
800 const char *stash_msg = NULL;
801 struct object_id obj;
802 struct object_context dummy;
803 struct option options[] = {
804 OPT__QUIET(&quiet, N_("be quiet")),
805 OPT_STRING('m', "message", &stash_msg, "message",
806 N_("stash message")),
807 OPT_END()
808 };
809
810 argc = parse_options(argc, argv, prefix, options,
811 git_stash_helper_store_usage,
812 PARSE_OPT_KEEP_UNKNOWN);
813
814 if (argc != 1) {
815 if (!quiet)
816 fprintf_ln(stderr, _("\"git stash store\" requires one "
817 "<commit> argument"));
818 return -1;
819 }
820
821 if (get_oid_with_context(argv[0], quiet ? GET_OID_QUIETLY : 0, &obj,
822 &dummy)) {
823 if (!quiet)
824 fprintf_ln(stderr, _("Cannot update %s with %s"),
825 ref_stash, argv[0]);
826 return -1;
827 }
828
829 return do_store_stash(&obj, stash_msg, quiet);
830}
831
d4788af8
PSU
832static void add_pathspecs(struct argv_array *args,
833 struct pathspec ps) {
834 int i;
835
836 for (i = 0; i < ps.nr; i++)
837 argv_array_push(args, ps.items[i].match);
838}
839
840/*
841 * `untracked_files` will be filled with the names of untracked files.
842 * The return value is:
843 *
844 * = 0 if there are not any untracked files
845 * > 0 if there are untracked files
846 */
847static int get_untracked_files(struct pathspec ps, int include_untracked,
848 struct strbuf *untracked_files)
849{
850 int i;
851 int max_len;
852 int found = 0;
853 char *seen;
854 struct dir_struct dir;
855
856 memset(&dir, 0, sizeof(dir));
857 if (include_untracked != INCLUDE_ALL_FILES)
858 setup_standard_excludes(&dir);
859
860 seen = xcalloc(ps.nr, 1);
861
862 max_len = fill_directory(&dir, the_repository->index, &ps);
863 for (i = 0; i < dir.nr; i++) {
864 struct dir_entry *ent = dir.entries[i];
865 if (dir_path_match(&the_index, ent, &ps, max_len, seen)) {
866 found++;
867 strbuf_addstr(untracked_files, ent->name);
868 /* NUL-terminate: will be fed to update-index -z */
869 strbuf_addch(untracked_files, '\0');
870 }
871 free(ent);
872 }
873
874 free(seen);
875 free(dir.entries);
876 free(dir.ignored);
877 clear_directory(&dir);
878 return found;
879}
880
881/*
ef0f0b45 882 * The return value of `check_changes_tracked_files()` can be:
d4788af8
PSU
883 *
884 * < 0 if there was an error
885 * = 0 if there are no changes.
886 * > 0 if there are changes.
887 */
ef0f0b45 888static int check_changes_tracked_files(struct pathspec ps)
d4788af8
PSU
889{
890 int result;
891 struct rev_info rev;
892 struct object_id dummy;
d4788af8
PSU
893
894 /* No initial commit. */
895 if (get_oid("HEAD", &dummy))
896 return -1;
897
898 if (read_cache() < 0)
899 return -1;
900
901 init_revisions(&rev, NULL);
902 rev.prune_data = ps;
903
904 rev.diffopt.flags.quick = 1;
905 rev.diffopt.flags.ignore_submodules = 1;
906 rev.abbrev = 0;
907
908 add_head_to_pending(&rev);
909 diff_setup_done(&rev.diffopt);
910
911 result = run_diff_index(&rev, 1);
912 if (diff_result_code(&rev.diffopt, result))
913 return 1;
914
915 object_array_clear(&rev.pending);
916 result = run_diff_files(&rev, 0);
917 if (diff_result_code(&rev.diffopt, result))
918 return 1;
919
ef0f0b45
PSU
920 return 0;
921}
922
923/*
924 * The function will fill `untracked_files` with the names of untracked files
925 * It will return 1 if there were any changes and 0 if there were not.
926 */
927static int check_changes(struct pathspec ps, int include_untracked,
928 struct strbuf *untracked_files)
929{
930 int ret = 0;
931 if (check_changes_tracked_files(ps))
932 ret = 1;
933
d4788af8 934 if (include_untracked && get_untracked_files(ps, include_untracked,
ef0f0b45
PSU
935 untracked_files))
936 ret = 1;
d4788af8 937
ef0f0b45 938 return ret;
d4788af8
PSU
939}
940
941static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
942 struct strbuf files)
943{
944 int ret = 0;
945 struct strbuf untracked_msg = STRBUF_INIT;
d4788af8 946 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
48ee24ab 947 struct index_state istate = { NULL };
d4788af8
PSU
948
949 cp_upd_index.git_cmd = 1;
950 argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
951 "--remove", "--stdin", NULL);
952 argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
953 stash_index_path.buf);
954
955 strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
956 if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,
957 NULL, 0)) {
958 ret = -1;
959 goto done;
960 }
961
48ee24ab
PSU
962 if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
963 NULL)) {
d4788af8
PSU
964 ret = -1;
965 goto done;
966 }
d4788af8
PSU
967
968 if (commit_tree(untracked_msg.buf, untracked_msg.len,
969 &info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
970 ret = -1;
971 goto done;
972 }
973
974done:
48ee24ab 975 discard_index(&istate);
d4788af8 976 strbuf_release(&untracked_msg);
d4788af8
PSU
977 remove_path(stash_index_path.buf);
978 return ret;
979}
980
981static int stash_patch(struct stash_info *info, struct pathspec ps,
1ac528c0 982 struct strbuf *out_patch, int quiet)
d4788af8
PSU
983{
984 int ret = 0;
d4788af8
PSU
985 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
986 struct child_process cp_add_i = CHILD_PROCESS_INIT;
d4788af8 987 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
48ee24ab 988 struct index_state istate = { NULL };
d4788af8
PSU
989
990 remove_path(stash_index_path.buf);
991
992 cp_read_tree.git_cmd = 1;
993 argv_array_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL);
994 argv_array_pushf(&cp_read_tree.env_array, "GIT_INDEX_FILE=%s",
995 stash_index_path.buf);
996 if (run_command(&cp_read_tree)) {
997 ret = -1;
998 goto done;
999 }
1000
1001 /* Find out what the user wants. */
1002 cp_add_i.git_cmd = 1;
1003 argv_array_pushl(&cp_add_i.args, "add--interactive", "--patch=stash",
1004 "--", NULL);
1005 add_pathspecs(&cp_add_i.args, ps);
1006 argv_array_pushf(&cp_add_i.env_array, "GIT_INDEX_FILE=%s",
1007 stash_index_path.buf);
1008 if (run_command(&cp_add_i)) {
1009 ret = -1;
1010 goto done;
1011 }
1012
1013 /* State of the working tree. */
48ee24ab
PSU
1014 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1015 NULL)) {
d4788af8
PSU
1016 ret = -1;
1017 goto done;
1018 }
1019
d4788af8
PSU
1020 cp_diff_tree.git_cmd = 1;
1021 argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD",
1022 oid_to_hex(&info->w_tree), "--", NULL);
1023 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
1024 ret = -1;
1025 goto done;
1026 }
1027
1028 if (!out_patch->len) {
1ac528c0
PSU
1029 if (!quiet)
1030 fprintf_ln(stderr, _("No changes selected"));
d4788af8
PSU
1031 ret = 1;
1032 }
1033
1034done:
48ee24ab 1035 discard_index(&istate);
d4788af8
PSU
1036 remove_path(stash_index_path.buf);
1037 return ret;
1038}
1039
1040static int stash_working_tree(struct stash_info *info, struct pathspec ps)
1041{
1042 int ret = 0;
1043 struct rev_info rev;
1044 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
d4788af8 1045 struct strbuf diff_output = STRBUF_INIT;
48ee24ab 1046 struct index_state istate = { NULL };
d4788af8
PSU
1047
1048 init_revisions(&rev, NULL);
1049
1050 set_alternate_index_output(stash_index_path.buf);
1051 if (reset_tree(&info->i_tree, 0, 0)) {
1052 ret = -1;
1053 goto done;
1054 }
1055 set_alternate_index_output(NULL);
1056
1057 rev.prune_data = ps;
1058 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
1059 rev.diffopt.format_callback = add_diff_to_buf;
1060 rev.diffopt.format_callback_data = &diff_output;
1061
1062 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1063 ret = -1;
1064 goto done;
1065 }
1066
1067 add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
1068 "");
1069 if (run_diff_index(&rev, 0)) {
1070 ret = -1;
1071 goto done;
1072 }
1073
1074 cp_upd_index.git_cmd = 1;
1075 argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
1076 "--remove", "--stdin", NULL);
1077 argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
1078 stash_index_path.buf);
1079
1080 if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,
1081 NULL, 0, NULL, 0)) {
1082 ret = -1;
1083 goto done;
1084 }
1085
48ee24ab
PSU
1086 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1087 NULL)) {
d4788af8
PSU
1088 ret = -1;
1089 goto done;
1090 }
1091
d4788af8 1092done:
48ee24ab 1093 discard_index(&istate);
d4788af8 1094 UNLEAK(rev);
d4788af8
PSU
1095 object_array_clear(&rev.pending);
1096 strbuf_release(&diff_output);
1097 remove_path(stash_index_path.buf);
1098 return ret;
1099}
1100
1101static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
1102 int include_untracked, int patch_mode,
1ac528c0
PSU
1103 struct stash_info *info, struct strbuf *patch,
1104 int quiet)
d4788af8
PSU
1105{
1106 int ret = 0;
1107 int flags = 0;
1108 int untracked_commit_option = 0;
1109 const char *head_short_sha1 = NULL;
1110 const char *branch_ref = NULL;
1111 const char *branch_name = "(no branch)";
1112 struct commit *head_commit = NULL;
1113 struct commit_list *parents = NULL;
1114 struct strbuf msg = STRBUF_INIT;
1115 struct strbuf commit_tree_label = STRBUF_INIT;
1116 struct strbuf untracked_files = STRBUF_INIT;
d4788af8
PSU
1117
1118 prepare_fallback_ident("git stash", "git@stash");
1119
1120 read_cache_preload(NULL);
1121 refresh_cache(REFRESH_QUIET);
1122
1123 if (get_oid("HEAD", &info->b_commit)) {
1ac528c0
PSU
1124 if (!quiet)
1125 fprintf_ln(stderr, _("You do not have "
1126 "the initial commit yet"));
d4788af8
PSU
1127 ret = -1;
1128 goto done;
1129 } else {
1130 head_commit = lookup_commit(the_repository, &info->b_commit);
1131 }
1132
ef0f0b45 1133 if (!check_changes(ps, include_untracked, &untracked_files)) {
d4788af8
PSU
1134 ret = 1;
1135 goto done;
1136 }
1137
1138 branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
1139 if (flags & REF_ISSYMREF)
1140 branch_name = strrchr(branch_ref, '/') + 1;
1141 head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
1142 DEFAULT_ABBREV);
1143 strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);
1144 pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
1145
1146 strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
1147 commit_list_insert(head_commit, &parents);
1148 if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
1149 commit_tree(commit_tree_label.buf, commit_tree_label.len,
1150 &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1ac528c0
PSU
1151 if (!quiet)
1152 fprintf_ln(stderr, _("Cannot save the current "
1153 "index state"));
d4788af8
PSU
1154 ret = -1;
1155 goto done;
1156 }
1157
ef0f0b45 1158 if (include_untracked) {
d4788af8 1159 if (save_untracked_files(info, &msg, untracked_files)) {
1ac528c0
PSU
1160 if (!quiet)
1161 fprintf_ln(stderr, _("Cannot save "
1162 "the untracked files"));
d4788af8
PSU
1163 ret = -1;
1164 goto done;
1165 }
1166 untracked_commit_option = 1;
1167 }
1168 if (patch_mode) {
1ac528c0 1169 ret = stash_patch(info, ps, patch, quiet);
d4788af8 1170 if (ret < 0) {
1ac528c0
PSU
1171 if (!quiet)
1172 fprintf_ln(stderr, _("Cannot save the current "
1173 "worktree state"));
d4788af8
PSU
1174 goto done;
1175 } else if (ret > 0) {
1176 goto done;
1177 }
1178 } else {
1179 if (stash_working_tree(info, ps)) {
1ac528c0
PSU
1180 if (!quiet)
1181 fprintf_ln(stderr, _("Cannot save the current "
1182 "worktree state"));
d4788af8
PSU
1183 ret = -1;
1184 goto done;
1185 }
1186 }
1187
1188 if (!stash_msg_buf->len)
1189 strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf);
1190 else
1191 strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name);
1192
1193 /*
1194 * `parents` will be empty after calling `commit_tree()`, so there is
1195 * no need to call `free_commit_list()`
1196 */
1197 parents = NULL;
1198 if (untracked_commit_option)
1199 commit_list_insert(lookup_commit(the_repository,
1200 &info->u_commit),
1201 &parents);
1202 commit_list_insert(lookup_commit(the_repository, &info->i_commit),
1203 &parents);
1204 commit_list_insert(head_commit, &parents);
1205
1206 if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
1207 parents, &info->w_commit, NULL, NULL)) {
1ac528c0
PSU
1208 if (!quiet)
1209 fprintf_ln(stderr, _("Cannot record "
1210 "working tree state"));
d4788af8
PSU
1211 ret = -1;
1212 goto done;
1213 }
1214
1215done:
1216 strbuf_release(&commit_tree_label);
1217 strbuf_release(&msg);
1218 strbuf_release(&untracked_files);
1219 return ret;
1220}
1221
1222static int create_stash(int argc, const char **argv, const char *prefix)
1223{
1224 int include_untracked = 0;
1225 int ret = 0;
1226 const char *stash_msg = NULL;
1227 struct strbuf stash_msg_buf = STRBUF_INIT;
1228 struct stash_info info;
1229 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 };
1237
1238 argc = parse_options(argc, argv, prefix, options,
1239 git_stash_helper_create_usage,
1240 0);
1241
1242 memset(&ps, 0, sizeof(ps));
ef0f0b45
PSU
1243 if (!check_changes_tracked_files(ps))
1244 return 0;
1245
d4788af8 1246 strbuf_addstr(&stash_msg_buf, stash_msg);
d553f538 1247 ret = do_create_stash(ps, &stash_msg_buf, include_untracked, 0, &info,
1ac528c0 1248 NULL, 0);
d4788af8
PSU
1249 if (!ret)
1250 printf_ln("%s", oid_to_hex(&info.w_commit));
1251
1252 strbuf_release(&stash_msg_buf);
ef0f0b45 1253 return ret;
d4788af8
PSU
1254}
1255
d553f538
PSU
1256static 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;
ef0f0b45 1263 struct strbuf untracked_files = STRBUF_INIT;
d553f538
PSU
1264
1265 if (patch_mode && keep_index == -1)
1266 keep_index = 1;
1267
1268 if (patch_mode && include_untracked) {
1269 fprintf_ln(stderr, _("Can't use --patch and --include-untracked"
1270 " or --all at the same time"));
1271 ret = -1;
1272 goto done;
1273 }
1274
1275 read_cache_preload(NULL);
1276 if (!include_untracked && ps.nr) {
1277 int i;
1278 char *ps_matched = xcalloc(ps.nr, 1);
1279
1280 for (i = 0; i < active_nr; i++)
1281 ce_path_match(&the_index, active_cache[i], &ps,
1282 ps_matched);
1283
1284 if (report_path_error(ps_matched, &ps, NULL)) {
1285 fprintf_ln(stderr, _("Did you forget to 'git add'?"));
1286 ret = -1;
1287 free(ps_matched);
1288 goto done;
1289 }
1290 free(ps_matched);
1291 }
1292
1293 if (refresh_cache(REFRESH_QUIET)) {
1294 ret = -1;
1295 goto done;
1296 }
1297
ef0f0b45 1298 if (!check_changes(ps, include_untracked, &untracked_files)) {
d553f538
PSU
1299 if (!quiet)
1300 printf_ln(_("No local changes to save"));
1301 goto done;
1302 }
1303
1304 if (!reflog_exists(ref_stash) && do_clear_stash()) {
1305 ret = -1;
1ac528c0
PSU
1306 if (!quiet)
1307 fprintf_ln(stderr, _("Cannot initialize stash"));
d553f538
PSU
1308 goto done;
1309 }
1310
1311 if (stash_msg)
1312 strbuf_addstr(&stash_msg_buf, stash_msg);
1313 if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1ac528c0 1314 &info, &patch, quiet)) {
d553f538
PSU
1315 ret = -1;
1316 goto done;
1317 }
1318
1319 if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
1320 ret = -1;
1ac528c0
PSU
1321 if (!quiet)
1322 fprintf_ln(stderr, _("Cannot save the current status"));
d553f538
PSU
1323 goto done;
1324 }
1325
1ac528c0
PSU
1326 if (!quiet)
1327 printf_ln(_("Saved working directory and index state %s"),
1328 stash_msg_buf.buf);
d553f538
PSU
1329
1330 if (!patch_mode) {
1331 if (include_untracked && !ps.nr) {
1332 struct child_process cp = CHILD_PROCESS_INIT;
1333
1334 cp.git_cmd = 1;
1335 argv_array_pushl(&cp.args, "clean", "--force",
1336 "--quiet", "-d", NULL);
1337 if (include_untracked == INCLUDE_ALL_FILES)
1338 argv_array_push(&cp.args, "-x");
1339 if (run_command(&cp)) {
1340 ret = -1;
1341 goto done;
1342 }
1343 }
1344 discard_cache();
1345 if (ps.nr) {
1346 struct child_process cp_add = CHILD_PROCESS_INIT;
1347 struct child_process cp_diff = CHILD_PROCESS_INIT;
1348 struct child_process cp_apply = CHILD_PROCESS_INIT;
1349 struct strbuf out = STRBUF_INIT;
1350
1351 cp_add.git_cmd = 1;
1352 argv_array_push(&cp_add.args, "add");
1353 if (!include_untracked)
1354 argv_array_push(&cp_add.args, "-u");
1355 if (include_untracked == INCLUDE_ALL_FILES)
1356 argv_array_push(&cp_add.args, "--force");
1357 argv_array_push(&cp_add.args, "--");
1358 add_pathspecs(&cp_add.args, ps);
1359 if (run_command(&cp_add)) {
1360 ret = -1;
1361 goto done;
1362 }
1363
1364 cp_diff.git_cmd = 1;
1365 argv_array_pushl(&cp_diff.args, "diff-index", "-p",
1366 "--cached", "--binary", "HEAD", "--",
1367 NULL);
1368 add_pathspecs(&cp_diff.args, ps);
1369 if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {
1370 ret = -1;
1371 goto done;
1372 }
1373
1374 cp_apply.git_cmd = 1;
1375 argv_array_pushl(&cp_apply.args, "apply", "--index",
1376 "-R", NULL);
1377 if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,
1378 NULL, 0)) {
1379 ret = -1;
1380 goto done;
1381 }
1382 } else {
1383 struct child_process cp = CHILD_PROCESS_INIT;
1384 cp.git_cmd = 1;
1385 argv_array_pushl(&cp.args, "reset", "--hard", "-q",
1386 NULL);
1387 if (run_command(&cp)) {
1388 ret = -1;
1389 goto done;
1390 }
1391 }
1392
1393 if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1394 struct child_process cp_ls = CHILD_PROCESS_INIT;
1395 struct child_process cp_checkout = CHILD_PROCESS_INIT;
1396 struct strbuf out = STRBUF_INIT;
1397
1398 if (reset_tree(&info.i_tree, 0, 1)) {
1399 ret = -1;
1400 goto done;
1401 }
1402
1403 cp_ls.git_cmd = 1;
1404 argv_array_pushl(&cp_ls.args, "ls-files", "-z",
1405 "--modified", "--", NULL);
1406
1407 add_pathspecs(&cp_ls.args, ps);
1408 if (pipe_command(&cp_ls, NULL, 0, &out, 0, NULL, 0)) {
1409 ret = -1;
1410 goto done;
1411 }
1412
1413 cp_checkout.git_cmd = 1;
1414 argv_array_pushl(&cp_checkout.args, "checkout-index",
1415 "-z", "--force", "--stdin", NULL);
1416 if (pipe_command(&cp_checkout, out.buf, out.len, NULL,
1417 0, NULL, 0)) {
1418 ret = -1;
1419 goto done;
1420 }
1421 }
1422 goto done;
1423 } else {
1424 struct child_process cp = CHILD_PROCESS_INIT;
1425
1426 cp.git_cmd = 1;
1427 argv_array_pushl(&cp.args, "apply", "-R", NULL);
1428
1429 if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1ac528c0
PSU
1430 if (!quiet)
1431 fprintf_ln(stderr, _("Cannot remove "
1432 "worktree changes"));
d553f538
PSU
1433 ret = -1;
1434 goto done;
1435 }
1436
1437 if (keep_index < 1) {
1438 struct child_process cp = CHILD_PROCESS_INIT;
1439
1440 cp.git_cmd = 1;
1441 argv_array_pushl(&cp.args, "reset", "-q", "--", NULL);
1442 add_pathspecs(&cp.args, ps);
1443 if (run_command(&cp)) {
1444 ret = -1;
1445 goto done;
1446 }
1447 }
1448 goto done;
1449 }
1450
1451done:
1452 strbuf_release(&stash_msg_buf);
1453 return ret;
1454}
1455
1456static int push_stash(int argc, const char **argv, const char *prefix)
1457{
1458 int keep_index = -1;
1459 int patch_mode = 0;
1460 int include_untracked = 0;
1461 int quiet = 0;
1462 const char *stash_msg = NULL;
1463 struct pathspec ps;
1464 struct option options[] = {
1465 OPT_BOOL('k', "keep-index", &keep_index,
1466 N_("keep index")),
1467 OPT_BOOL('p', "patch", &patch_mode,
1468 N_("stash in patch mode")),
1469 OPT__QUIET(&quiet, N_("quiet mode")),
1470 OPT_BOOL('u', "include-untracked", &include_untracked,
1471 N_("include untracked files in stash")),
1472 OPT_SET_INT('a', "all", &include_untracked,
1473 N_("include ignore files"), 2),
1474 OPT_STRING('m', "message", &stash_msg, N_("message"),
1475 N_("stash message")),
1476 OPT_END()
1477 };
1478
1479 argc = parse_options(argc, argv, prefix, options,
1480 git_stash_helper_push_usage,
1481 0);
1482
1483 parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);
1484 return do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,
1485 include_untracked);
1486}
1487
64fe9c26
PSU
1488static int save_stash(int argc, const char **argv, const char *prefix)
1489{
1490 int keep_index = -1;
1491 int patch_mode = 0;
1492 int include_untracked = 0;
1493 int quiet = 0;
1494 int ret = 0;
1495 const char *stash_msg = NULL;
1496 struct pathspec ps;
1497 struct strbuf stash_msg_buf = STRBUF_INIT;
1498 struct option options[] = {
1499 OPT_BOOL('k', "keep-index", &keep_index,
1500 N_("keep index")),
1501 OPT_BOOL('p', "patch", &patch_mode,
1502 N_("stash in patch mode")),
1503 OPT__QUIET(&quiet, N_("quiet mode")),
1504 OPT_BOOL('u', "include-untracked", &include_untracked,
1505 N_("include untracked files in stash")),
1506 OPT_SET_INT('a', "all", &include_untracked,
1507 N_("include ignore files"), 2),
1508 OPT_STRING('m', "message", &stash_msg, "message",
1509 N_("stash message")),
1510 OPT_END()
1511 };
1512
1513 argc = parse_options(argc, argv, prefix, options,
1514 git_stash_helper_save_usage,
1515 PARSE_OPT_KEEP_DASHDASH);
1516
1517 if (argc)
1518 stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
1519
1520 memset(&ps, 0, sizeof(ps));
1521 ret = do_push_stash(ps, stash_msg, quiet, keep_index,
1522 patch_mode, include_untracked);
1523
1524 strbuf_release(&stash_msg_buf);
1525 return ret;
1526}
1527
8a0fc8d1
JT
1528int cmd_stash__helper(int argc, const char **argv, const char *prefix)
1529{
1530 pid_t pid = getpid();
1531 const char *index_file;
1532
1533 struct option options[] = {
1534 OPT_END()
1535 };
1536
d4788af8 1537 git_config(git_diff_basic_config, NULL);
8a0fc8d1
JT
1538
1539 argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,
1540 PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
1541
1542 index_file = get_index_file();
1543 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1544 (uintmax_t)pid);
1545
1546 if (argc < 1)
1547 usage_with_options(git_stash_helper_usage, options);
1548 if (!strcmp(argv[0], "apply"))
1549 return !!apply_stash(argc, argv, prefix);
4e2dd393
JT
1550 else if (!strcmp(argv[0], "clear"))
1551 return !!clear_stash(argc, argv, prefix);
1552 else if (!strcmp(argv[0], "drop"))
1553 return !!drop_stash(argc, argv, prefix);
c4de61d7
JT
1554 else if (!strcmp(argv[0], "pop"))
1555 return !!pop_stash(argc, argv, prefix);
577c1995
JT
1556 else if (!strcmp(argv[0], "branch"))
1557 return !!branch_stash(argc, argv, prefix);
130f2697
PSU
1558 else if (!strcmp(argv[0], "list"))
1559 return !!list_stash(argc, argv, prefix);
dc7bd382
PSU
1560 else if (!strcmp(argv[0], "show"))
1561 return !!show_stash(argc, argv, prefix);
41e0dd55
PSU
1562 else if (!strcmp(argv[0], "store"))
1563 return !!store_stash(argc, argv, prefix);
d4788af8
PSU
1564 else if (!strcmp(argv[0], "create"))
1565 return !!create_stash(argc, argv, prefix);
d553f538
PSU
1566 else if (!strcmp(argv[0], "push"))
1567 return !!push_stash(argc, argv, prefix);
64fe9c26
PSU
1568 else if (!strcmp(argv[0], "save"))
1569 return !!save_stash(argc, argv, prefix);
8a0fc8d1
JT
1570
1571 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
1572 git_stash_helper_usage, options);
1573}