doc: fix wrong 4-byte length of pkt-line message
[git.git] / builtin / stash.c
blob78af6ce56431a5b1a8eac311d8a33d2c702ee8ba
1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
2 #include "builtin.h"
3 #include "config.h"
4 #include "parse-options.h"
5 #include "refs.h"
6 #include "lockfile.h"
7 #include "cache-tree.h"
8 #include "unpack-trees.h"
9 #include "merge-recursive.h"
10 #include "argv-array.h"
11 #include "run-command.h"
12 #include "dir.h"
13 #include "rerere.h"
14 #include "revision.h"
15 #include "log-tree.h"
16 #include "diffcore.h"
17 #include "exec-cmd.h"
19 #define INCLUDE_ALL_FILES 2
21 static const char * const git_stash_usage[] = {
22 N_("git stash list [<options>]"),
23 N_("git stash show [<options>] [<stash>]"),
24 N_("git stash drop [-q|--quiet] [<stash>]"),
25 N_("git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
26 N_("git stash branch <branchname> [<stash>]"),
27 N_("git stash clear"),
28 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
29 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
30 " [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
31 " [--] [<pathspec>...]]"),
32 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
33 " [-u|--include-untracked] [-a|--all] [<message>]"),
34 NULL
37 static const char * const git_stash_list_usage[] = {
38 N_("git stash list [<options>]"),
39 NULL
42 static const char * const git_stash_show_usage[] = {
43 N_("git stash show [<options>] [<stash>]"),
44 NULL
47 static const char * const git_stash_drop_usage[] = {
48 N_("git stash drop [-q|--quiet] [<stash>]"),
49 NULL
52 static const char * const git_stash_pop_usage[] = {
53 N_("git stash pop [--index] [-q|--quiet] [<stash>]"),
54 NULL
57 static const char * const git_stash_apply_usage[] = {
58 N_("git stash apply [--index] [-q|--quiet] [<stash>]"),
59 NULL
62 static const char * const git_stash_branch_usage[] = {
63 N_("git stash branch <branchname> [<stash>]"),
64 NULL
67 static const char * const git_stash_clear_usage[] = {
68 N_("git stash clear"),
69 NULL
72 static const char * const git_stash_store_usage[] = {
73 N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"),
74 NULL
77 static const char * const git_stash_push_usage[] = {
78 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
79 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
80 " [--] [<pathspec>...]]"),
81 NULL
84 static const char * const git_stash_save_usage[] = {
85 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
86 " [-u|--include-untracked] [-a|--all] [<message>]"),
87 NULL
90 static const char *ref_stash = "refs/stash";
91 static struct strbuf stash_index_path = STRBUF_INIT;
94 * w_commit is set to the commit containing the working tree
95 * b_commit is set to the base commit
96 * i_commit is set to the commit containing the index tree
97 * u_commit is set to the commit containing the untracked files tree
98 * w_tree is set to the working tree
99 * b_tree is set to the base tree
100 * i_tree is set to the index tree
101 * u_tree is set to the untracked files tree
103 struct stash_info {
104 struct object_id w_commit;
105 struct object_id b_commit;
106 struct object_id i_commit;
107 struct object_id u_commit;
108 struct object_id w_tree;
109 struct object_id b_tree;
110 struct object_id i_tree;
111 struct object_id u_tree;
112 struct strbuf revision;
113 int is_stash_ref;
114 int has_u;
117 static void free_stash_info(struct stash_info *info)
119 strbuf_release(&info->revision);
122 static void assert_stash_like(struct stash_info *info, const char *revision)
124 if (get_oidf(&info->b_commit, "%s^1", revision) ||
125 get_oidf(&info->w_tree, "%s:", revision) ||
126 get_oidf(&info->b_tree, "%s^1:", revision) ||
127 get_oidf(&info->i_tree, "%s^2:", revision))
128 die(_("'%s' is not a stash-like commit"), revision);
131 static int get_stash_info(struct stash_info *info, int argc, const char **argv)
133 int ret;
134 char *end_of_rev;
135 char *expanded_ref;
136 const char *revision;
137 const char *commit = NULL;
138 struct object_id dummy;
139 struct strbuf symbolic = STRBUF_INIT;
141 if (argc > 1) {
142 int i;
143 struct strbuf refs_msg = STRBUF_INIT;
145 for (i = 0; i < argc; i++)
146 strbuf_addf(&refs_msg, " '%s'", argv[i]);
148 fprintf_ln(stderr, _("Too many revisions specified:%s"),
149 refs_msg.buf);
150 strbuf_release(&refs_msg);
152 return -1;
155 if (argc == 1)
156 commit = argv[0];
158 strbuf_init(&info->revision, 0);
159 if (!commit) {
160 if (!ref_exists(ref_stash)) {
161 free_stash_info(info);
162 fprintf_ln(stderr, _("No stash entries found."));
163 return -1;
166 strbuf_addf(&info->revision, "%s@{0}", ref_stash);
167 } else if (strspn(commit, "0123456789") == strlen(commit)) {
168 strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
169 } else {
170 strbuf_addstr(&info->revision, commit);
173 revision = info->revision.buf;
175 if (get_oid(revision, &info->w_commit)) {
176 error(_("%s is not a valid reference"), revision);
177 free_stash_info(info);
178 return -1;
181 assert_stash_like(info, revision);
183 info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision);
185 end_of_rev = strchrnul(revision, '@');
186 strbuf_add(&symbolic, revision, end_of_rev - revision);
188 ret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref);
189 strbuf_release(&symbolic);
190 switch (ret) {
191 case 0: /* Not found, but valid ref */
192 info->is_stash_ref = 0;
193 break;
194 case 1:
195 info->is_stash_ref = !strcmp(expanded_ref, ref_stash);
196 break;
197 default: /* Invalid or ambiguous */
198 free_stash_info(info);
201 free(expanded_ref);
202 return !(ret == 0 || ret == 1);
205 static int do_clear_stash(void)
207 struct object_id obj;
208 if (get_oid(ref_stash, &obj))
209 return 0;
211 return delete_ref(NULL, ref_stash, &obj, 0);
214 static int clear_stash(int argc, const char **argv, const char *prefix)
216 struct option options[] = {
217 OPT_END()
220 argc = parse_options(argc, argv, prefix, options,
221 git_stash_clear_usage,
222 PARSE_OPT_STOP_AT_NON_OPTION);
224 if (argc)
225 return error(_("git stash clear with parameters is "
226 "unimplemented"));
228 return do_clear_stash();
231 static int reset_tree(struct object_id *i_tree, int update, int reset)
233 int nr_trees = 1;
234 struct unpack_trees_options opts;
235 struct tree_desc t[MAX_UNPACK_TREES];
236 struct tree *tree;
237 struct lock_file lock_file = LOCK_INIT;
239 read_cache_preload(NULL);
240 if (refresh_cache(REFRESH_QUIET))
241 return -1;
243 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
245 memset(&opts, 0, sizeof(opts));
247 tree = parse_tree_indirect(i_tree);
248 if (parse_tree(tree))
249 return -1;
251 init_tree_desc(t, tree->buffer, tree->size);
253 opts.head_idx = 1;
254 opts.src_index = &the_index;
255 opts.dst_index = &the_index;
256 opts.merge = 1;
257 opts.reset = reset;
258 opts.update = update;
259 opts.fn = oneway_merge;
261 if (unpack_trees(nr_trees, t, &opts))
262 return -1;
264 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
265 return error(_("unable to write new index file"));
267 return 0;
270 static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
272 struct child_process cp = CHILD_PROCESS_INIT;
273 const char *w_commit_hex = oid_to_hex(w_commit);
276 * Diff-tree would not be very hard to replace with a native function,
277 * however it should be done together with apply_cached.
279 cp.git_cmd = 1;
280 argv_array_pushl(&cp.args, "diff-tree", "--binary", NULL);
281 argv_array_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
283 return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
286 static int apply_cached(struct strbuf *out)
288 struct child_process cp = CHILD_PROCESS_INIT;
291 * Apply currently only reads either from stdin or a file, thus
292 * apply_all_patches would have to be updated to optionally take a
293 * buffer.
295 cp.git_cmd = 1;
296 argv_array_pushl(&cp.args, "apply", "--cached", NULL);
297 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
300 static int reset_head(void)
302 struct child_process cp = CHILD_PROCESS_INIT;
305 * Reset is overall quite simple, however there is no current public
306 * API for resetting.
308 cp.git_cmd = 1;
309 argv_array_push(&cp.args, "reset");
311 return run_command(&cp);
314 static void add_diff_to_buf(struct diff_queue_struct *q,
315 struct diff_options *options,
316 void *data)
318 int i;
320 for (i = 0; i < q->nr; i++) {
321 strbuf_addstr(data, q->queue[i]->one->path);
323 /* NUL-terminate: will be fed to update-index -z */
324 strbuf_addch(data, '\0');
328 static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)
330 struct child_process cp = CHILD_PROCESS_INIT;
331 const char *c_tree_hex = oid_to_hex(c_tree);
334 * diff-index is very similar to diff-tree above, and should be
335 * converted together with update_index.
337 cp.git_cmd = 1;
338 argv_array_pushl(&cp.args, "diff-index", "--cached", "--name-only",
339 "--diff-filter=A", NULL);
340 argv_array_push(&cp.args, c_tree_hex);
341 return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
344 static int update_index(struct strbuf *out)
346 struct child_process cp = CHILD_PROCESS_INIT;
349 * Update-index is very complicated and may need to have a public
350 * function exposed in order to remove this forking.
352 cp.git_cmd = 1;
353 argv_array_pushl(&cp.args, "update-index", "--add", "--stdin", NULL);
354 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
357 static int restore_untracked(struct object_id *u_tree)
359 int res;
360 struct child_process cp = CHILD_PROCESS_INIT;
363 * We need to run restore files from a given index, but without
364 * affecting the current index, so we use GIT_INDEX_FILE with
365 * run_command to fork processes that will not interfere.
367 cp.git_cmd = 1;
368 argv_array_push(&cp.args, "read-tree");
369 argv_array_push(&cp.args, oid_to_hex(u_tree));
370 argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
371 stash_index_path.buf);
372 if (run_command(&cp)) {
373 remove_path(stash_index_path.buf);
374 return -1;
377 child_process_init(&cp);
378 cp.git_cmd = 1;
379 argv_array_pushl(&cp.args, "checkout-index", "--all", NULL);
380 argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
381 stash_index_path.buf);
383 res = run_command(&cp);
384 remove_path(stash_index_path.buf);
385 return res;
388 static int do_apply_stash(const char *prefix, struct stash_info *info,
389 int index, int quiet)
391 int ret;
392 int has_index = index;
393 struct merge_options o;
394 struct object_id c_tree;
395 struct object_id index_tree;
396 struct commit *result;
397 const struct object_id *bases[1];
399 read_cache_preload(NULL);
400 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0))
401 return -1;
403 if (write_cache_as_tree(&c_tree, 0, NULL))
404 return error(_("cannot apply a stash in the middle of a merge"));
406 if (index) {
407 if (oideq(&info->b_tree, &info->i_tree) ||
408 oideq(&c_tree, &info->i_tree)) {
409 has_index = 0;
410 } else {
411 struct strbuf out = STRBUF_INIT;
413 if (diff_tree_binary(&out, &info->w_commit)) {
414 strbuf_release(&out);
415 return error(_("could not generate diff %s^!."),
416 oid_to_hex(&info->w_commit));
419 ret = apply_cached(&out);
420 strbuf_release(&out);
421 if (ret)
422 return error(_("conflicts in index."
423 "Try without --index."));
425 discard_cache();
426 read_cache();
427 if (write_cache_as_tree(&index_tree, 0, NULL))
428 return error(_("could not save index tree"));
430 reset_head();
431 discard_cache();
432 read_cache();
436 if (info->has_u && restore_untracked(&info->u_tree))
437 return error(_("could not restore untracked files from stash"));
439 init_merge_options(&o, the_repository);
441 o.branch1 = "Updated upstream";
442 o.branch2 = "Stashed changes";
444 if (oideq(&info->b_tree, &c_tree))
445 o.branch1 = "Version stash was based on";
447 if (quiet)
448 o.verbosity = 0;
450 if (o.verbosity >= 3)
451 printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
453 bases[0] = &info->b_tree;
455 ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, 1, bases,
456 &result);
457 if (ret) {
458 rerere(0);
460 if (index)
461 fprintf_ln(stderr, _("Index was not unstashed."));
463 return ret;
466 if (has_index) {
467 if (reset_tree(&index_tree, 0, 0))
468 return -1;
469 } else {
470 struct strbuf out = STRBUF_INIT;
472 if (get_newly_staged(&out, &c_tree)) {
473 strbuf_release(&out);
474 return -1;
477 if (reset_tree(&c_tree, 0, 1)) {
478 strbuf_release(&out);
479 return -1;
482 ret = update_index(&out);
483 strbuf_release(&out);
484 if (ret)
485 return -1;
487 /* read back the result of update_index() back from the disk */
488 discard_cache();
489 read_cache();
492 if (!quiet) {
493 struct child_process cp = CHILD_PROCESS_INIT;
496 * Status is quite simple and could be replaced with calls to
497 * wt_status in the future, but it adds complexities which may
498 * require more tests.
500 cp.git_cmd = 1;
501 cp.dir = prefix;
502 argv_array_pushf(&cp.env_array, GIT_WORK_TREE_ENVIRONMENT"=%s",
503 absolute_path(get_git_work_tree()));
504 argv_array_pushf(&cp.env_array, GIT_DIR_ENVIRONMENT"=%s",
505 absolute_path(get_git_dir()));
506 argv_array_push(&cp.args, "status");
507 run_command(&cp);
510 return 0;
513 static int apply_stash(int argc, const char **argv, const char *prefix)
515 int ret;
516 int quiet = 0;
517 int index = 0;
518 struct stash_info info;
519 struct option options[] = {
520 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
521 OPT_BOOL(0, "index", &index,
522 N_("attempt to recreate the index")),
523 OPT_END()
526 argc = parse_options(argc, argv, prefix, options,
527 git_stash_apply_usage, 0);
529 if (get_stash_info(&info, argc, argv))
530 return -1;
532 ret = do_apply_stash(prefix, &info, index, quiet);
533 free_stash_info(&info);
534 return ret;
537 static int do_drop_stash(struct stash_info *info, int quiet)
539 int ret;
540 struct child_process cp_reflog = CHILD_PROCESS_INIT;
541 struct child_process cp = CHILD_PROCESS_INIT;
544 * reflog does not provide a simple function for deleting refs. One will
545 * need to be added to avoid implementing too much reflog code here
548 cp_reflog.git_cmd = 1;
549 argv_array_pushl(&cp_reflog.args, "reflog", "delete", "--updateref",
550 "--rewrite", NULL);
551 argv_array_push(&cp_reflog.args, info->revision.buf);
552 ret = run_command(&cp_reflog);
553 if (!ret) {
554 if (!quiet)
555 printf_ln(_("Dropped %s (%s)"), info->revision.buf,
556 oid_to_hex(&info->w_commit));
557 } else {
558 return error(_("%s: Could not drop stash entry"),
559 info->revision.buf);
563 * This could easily be replaced by get_oid, but currently it will throw
564 * a fatal error when a reflog is empty, which we can not recover from.
566 cp.git_cmd = 1;
567 /* Even though --quiet is specified, rev-parse still outputs the hash */
568 cp.no_stdout = 1;
569 argv_array_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL);
570 argv_array_pushf(&cp.args, "%s@{0}", ref_stash);
571 ret = run_command(&cp);
573 /* do_clear_stash if we just dropped the last stash entry */
574 if (ret)
575 do_clear_stash();
577 return 0;
580 static void assert_stash_ref(struct stash_info *info)
582 if (!info->is_stash_ref) {
583 error(_("'%s' is not a stash reference"), info->revision.buf);
584 free_stash_info(info);
585 exit(1);
589 static int drop_stash(int argc, const char **argv, const char *prefix)
591 int ret;
592 int quiet = 0;
593 struct stash_info info;
594 struct option options[] = {
595 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
596 OPT_END()
599 argc = parse_options(argc, argv, prefix, options,
600 git_stash_drop_usage, 0);
602 if (get_stash_info(&info, argc, argv))
603 return -1;
605 assert_stash_ref(&info);
607 ret = do_drop_stash(&info, quiet);
608 free_stash_info(&info);
609 return ret;
612 static int pop_stash(int argc, const char **argv, const char *prefix)
614 int ret;
615 int index = 0;
616 int quiet = 0;
617 struct stash_info info;
618 struct option options[] = {
619 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
620 OPT_BOOL(0, "index", &index,
621 N_("attempt to recreate the index")),
622 OPT_END()
625 argc = parse_options(argc, argv, prefix, options,
626 git_stash_pop_usage, 0);
628 if (get_stash_info(&info, argc, argv))
629 return -1;
631 assert_stash_ref(&info);
632 if ((ret = do_apply_stash(prefix, &info, index, quiet)))
633 printf_ln(_("The stash entry is kept in case "
634 "you need it again."));
635 else
636 ret = do_drop_stash(&info, quiet);
638 free_stash_info(&info);
639 return ret;
642 static int branch_stash(int argc, const char **argv, const char *prefix)
644 int ret;
645 const char *branch = NULL;
646 struct stash_info info;
647 struct child_process cp = CHILD_PROCESS_INIT;
648 struct option options[] = {
649 OPT_END()
652 argc = parse_options(argc, argv, prefix, options,
653 git_stash_branch_usage, 0);
655 if (!argc) {
656 fprintf_ln(stderr, _("No branch name specified"));
657 return -1;
660 branch = argv[0];
662 if (get_stash_info(&info, argc - 1, argv + 1))
663 return -1;
665 cp.git_cmd = 1;
666 argv_array_pushl(&cp.args, "checkout", "-b", NULL);
667 argv_array_push(&cp.args, branch);
668 argv_array_push(&cp.args, oid_to_hex(&info.b_commit));
669 ret = run_command(&cp);
670 if (!ret)
671 ret = do_apply_stash(prefix, &info, 1, 0);
672 if (!ret && info.is_stash_ref)
673 ret = do_drop_stash(&info, 0);
675 free_stash_info(&info);
677 return ret;
680 static int list_stash(int argc, const char **argv, const char *prefix)
682 struct child_process cp = CHILD_PROCESS_INIT;
683 struct option options[] = {
684 OPT_END()
687 argc = parse_options(argc, argv, prefix, options,
688 git_stash_list_usage,
689 PARSE_OPT_KEEP_UNKNOWN);
691 if (!ref_exists(ref_stash))
692 return 0;
694 cp.git_cmd = 1;
695 argv_array_pushl(&cp.args, "log", "--format=%gd: %gs", "-g",
696 "--first-parent", "-m", NULL);
697 argv_array_pushv(&cp.args, argv);
698 argv_array_push(&cp.args, ref_stash);
699 argv_array_push(&cp.args, "--");
700 return run_command(&cp);
703 static int show_stat = 1;
704 static int show_patch;
706 static int git_stash_config(const char *var, const char *value, void *cb)
708 if (!strcmp(var, "stash.showstat")) {
709 show_stat = git_config_bool(var, value);
710 return 0;
712 if (!strcmp(var, "stash.showpatch")) {
713 show_patch = git_config_bool(var, value);
714 return 0;
716 return git_default_config(var, value, cb);
719 static int show_stash(int argc, const char **argv, const char *prefix)
721 int i;
722 int ret = 0;
723 struct stash_info info;
724 struct rev_info rev;
725 struct argv_array stash_args = ARGV_ARRAY_INIT;
726 struct argv_array revision_args = ARGV_ARRAY_INIT;
727 struct option options[] = {
728 OPT_END()
731 init_diff_ui_defaults();
732 git_config(git_diff_ui_config, NULL);
733 init_revisions(&rev, prefix);
735 argv_array_push(&revision_args, argv[0]);
736 for (i = 1; i < argc; i++) {
737 if (argv[i][0] != '-')
738 argv_array_push(&stash_args, argv[i]);
739 else
740 argv_array_push(&revision_args, argv[i]);
743 ret = get_stash_info(&info, stash_args.argc, stash_args.argv);
744 argv_array_clear(&stash_args);
745 if (ret)
746 return -1;
749 * The config settings are applied only if there are not passed
750 * any options.
752 if (revision_args.argc == 1) {
753 git_config(git_stash_config, NULL);
754 if (show_stat)
755 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;
757 if (show_patch)
758 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
760 if (!show_stat && !show_patch) {
761 free_stash_info(&info);
762 return 0;
766 argc = setup_revisions(revision_args.argc, revision_args.argv, &rev, NULL);
767 if (argc > 1) {
768 free_stash_info(&info);
769 usage_with_options(git_stash_show_usage, options);
771 if (!rev.diffopt.output_format) {
772 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
773 diff_setup_done(&rev.diffopt);
776 rev.diffopt.flags.recursive = 1;
777 setup_diff_pager(&rev.diffopt);
778 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
779 log_tree_diff_flush(&rev);
781 free_stash_info(&info);
782 return diff_result_code(&rev.diffopt, 0);
785 static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
786 int quiet)
788 if (!stash_msg)
789 stash_msg = "Created via \"git stash store\".";
791 if (update_ref(stash_msg, ref_stash, w_commit, NULL,
792 REF_FORCE_CREATE_REFLOG,
793 quiet ? UPDATE_REFS_QUIET_ON_ERR :
794 UPDATE_REFS_MSG_ON_ERR)) {
795 if (!quiet) {
796 fprintf_ln(stderr, _("Cannot update %s with %s"),
797 ref_stash, oid_to_hex(w_commit));
799 return -1;
802 return 0;
805 static int store_stash(int argc, const char **argv, const char *prefix)
807 int quiet = 0;
808 const char *stash_msg = NULL;
809 struct object_id obj;
810 struct object_context dummy;
811 struct option options[] = {
812 OPT__QUIET(&quiet, N_("be quiet")),
813 OPT_STRING('m', "message", &stash_msg, "message",
814 N_("stash message")),
815 OPT_END()
818 argc = parse_options(argc, argv, prefix, options,
819 git_stash_store_usage,
820 PARSE_OPT_KEEP_UNKNOWN);
822 if (argc != 1) {
823 if (!quiet)
824 fprintf_ln(stderr, _("\"git stash store\" requires one "
825 "<commit> argument"));
826 return -1;
829 if (get_oid_with_context(the_repository,
830 argv[0], quiet ? GET_OID_QUIETLY : 0, &obj,
831 &dummy)) {
832 if (!quiet)
833 fprintf_ln(stderr, _("Cannot update %s with %s"),
834 ref_stash, argv[0]);
835 return -1;
838 return do_store_stash(&obj, stash_msg, quiet);
841 static void add_pathspecs(struct argv_array *args,
842 const struct pathspec *ps) {
843 int i;
845 for (i = 0; i < ps->nr; i++)
846 argv_array_push(args, ps->items[i].original);
850 * `untracked_files` will be filled with the names of untracked files.
851 * The return value is:
853 * = 0 if there are not any untracked files
854 * > 0 if there are untracked files
856 static int get_untracked_files(const struct pathspec *ps, int include_untracked,
857 struct strbuf *untracked_files)
859 int i;
860 int max_len;
861 int found = 0;
862 char *seen;
863 struct dir_struct dir;
865 memset(&dir, 0, sizeof(dir));
866 if (include_untracked != INCLUDE_ALL_FILES)
867 setup_standard_excludes(&dir);
869 seen = xcalloc(ps->nr, 1);
871 max_len = fill_directory(&dir, the_repository->index, ps);
872 for (i = 0; i < dir.nr; i++) {
873 struct dir_entry *ent = dir.entries[i];
874 if (dir_path_match(&the_index, ent, ps, max_len, seen)) {
875 found++;
876 strbuf_addstr(untracked_files, ent->name);
877 /* NUL-terminate: will be fed to update-index -z */
878 strbuf_addch(untracked_files, '\0');
880 free(ent);
883 free(seen);
884 free(dir.entries);
885 free(dir.ignored);
886 clear_directory(&dir);
887 return found;
891 * The return value of `check_changes_tracked_files()` can be:
893 * < 0 if there was an error
894 * = 0 if there are no changes.
895 * > 0 if there are changes.
897 static int check_changes_tracked_files(const struct pathspec *ps)
899 int result;
900 struct rev_info rev;
901 struct object_id dummy;
902 int ret = 0;
904 /* No initial commit. */
905 if (get_oid("HEAD", &dummy))
906 return -1;
908 if (read_cache() < 0)
909 return -1;
911 init_revisions(&rev, NULL);
912 copy_pathspec(&rev.prune_data, ps);
914 rev.diffopt.flags.quick = 1;
915 rev.diffopt.flags.ignore_submodules = 1;
916 rev.abbrev = 0;
918 add_head_to_pending(&rev);
919 diff_setup_done(&rev.diffopt);
921 result = run_diff_index(&rev, 1);
922 if (diff_result_code(&rev.diffopt, result)) {
923 ret = 1;
924 goto done;
927 object_array_clear(&rev.pending);
928 result = run_diff_files(&rev, 0);
929 if (diff_result_code(&rev.diffopt, result)) {
930 ret = 1;
931 goto done;
934 done:
935 clear_pathspec(&rev.prune_data);
936 return ret;
940 * The function will fill `untracked_files` with the names of untracked files
941 * It will return 1 if there were any changes and 0 if there were not.
943 static int check_changes(const struct pathspec *ps, int include_untracked,
944 struct strbuf *untracked_files)
946 int ret = 0;
947 if (check_changes_tracked_files(ps))
948 ret = 1;
950 if (include_untracked && get_untracked_files(ps, include_untracked,
951 untracked_files))
952 ret = 1;
954 return ret;
957 static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
958 struct strbuf files)
960 int ret = 0;
961 struct strbuf untracked_msg = STRBUF_INIT;
962 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
963 struct index_state istate = { NULL };
965 cp_upd_index.git_cmd = 1;
966 argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
967 "--remove", "--stdin", NULL);
968 argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
969 stash_index_path.buf);
971 strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
972 if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,
973 NULL, 0)) {
974 ret = -1;
975 goto done;
978 if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
979 NULL)) {
980 ret = -1;
981 goto done;
984 if (commit_tree(untracked_msg.buf, untracked_msg.len,
985 &info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
986 ret = -1;
987 goto done;
990 done:
991 discard_index(&istate);
992 strbuf_release(&untracked_msg);
993 remove_path(stash_index_path.buf);
994 return ret;
997 static int stash_patch(struct stash_info *info, const struct pathspec *ps,
998 struct strbuf *out_patch, int quiet)
1000 int ret = 0;
1001 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
1002 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1003 struct index_state istate = { NULL };
1004 char *old_index_env = NULL, *old_repo_index_file;
1006 remove_path(stash_index_path.buf);
1008 cp_read_tree.git_cmd = 1;
1009 argv_array_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL);
1010 argv_array_pushf(&cp_read_tree.env_array, "GIT_INDEX_FILE=%s",
1011 stash_index_path.buf);
1012 if (run_command(&cp_read_tree)) {
1013 ret = -1;
1014 goto done;
1017 /* Find out what the user wants. */
1018 old_repo_index_file = the_repository->index_file;
1019 the_repository->index_file = stash_index_path.buf;
1020 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
1021 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
1023 ret = run_add_interactive(NULL, "--patch=stash", ps);
1025 the_repository->index_file = old_repo_index_file;
1026 if (old_index_env && *old_index_env)
1027 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
1028 else
1029 unsetenv(INDEX_ENVIRONMENT);
1030 FREE_AND_NULL(old_index_env);
1032 /* State of the working tree. */
1033 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1034 NULL)) {
1035 ret = -1;
1036 goto done;
1039 cp_diff_tree.git_cmd = 1;
1040 argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD",
1041 oid_to_hex(&info->w_tree), "--", NULL);
1042 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
1043 ret = -1;
1044 goto done;
1047 if (!out_patch->len) {
1048 if (!quiet)
1049 fprintf_ln(stderr, _("No changes selected"));
1050 ret = 1;
1053 done:
1054 discard_index(&istate);
1055 remove_path(stash_index_path.buf);
1056 return ret;
1059 static int stash_working_tree(struct stash_info *info, const struct pathspec *ps)
1061 int ret = 0;
1062 struct rev_info rev;
1063 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1064 struct strbuf diff_output = STRBUF_INIT;
1065 struct index_state istate = { NULL };
1067 init_revisions(&rev, NULL);
1068 copy_pathspec(&rev.prune_data, ps);
1070 set_alternate_index_output(stash_index_path.buf);
1071 if (reset_tree(&info->i_tree, 0, 0)) {
1072 ret = -1;
1073 goto done;
1075 set_alternate_index_output(NULL);
1077 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
1078 rev.diffopt.format_callback = add_diff_to_buf;
1079 rev.diffopt.format_callback_data = &diff_output;
1081 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1082 ret = -1;
1083 goto done;
1086 add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
1087 "");
1088 if (run_diff_index(&rev, 0)) {
1089 ret = -1;
1090 goto done;
1093 cp_upd_index.git_cmd = 1;
1094 argv_array_pushl(&cp_upd_index.args, "update-index",
1095 "--ignore-skip-worktree-entries",
1096 "-z", "--add", "--remove", "--stdin", NULL);
1097 argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
1098 stash_index_path.buf);
1100 if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,
1101 NULL, 0, NULL, 0)) {
1102 ret = -1;
1103 goto done;
1106 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1107 NULL)) {
1108 ret = -1;
1109 goto done;
1112 done:
1113 discard_index(&istate);
1114 UNLEAK(rev);
1115 object_array_clear(&rev.pending);
1116 clear_pathspec(&rev.prune_data);
1117 strbuf_release(&diff_output);
1118 remove_path(stash_index_path.buf);
1119 return ret;
1122 static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,
1123 int include_untracked, int patch_mode,
1124 struct stash_info *info, struct strbuf *patch,
1125 int quiet)
1127 int ret = 0;
1128 int flags = 0;
1129 int untracked_commit_option = 0;
1130 const char *head_short_sha1 = NULL;
1131 const char *branch_ref = NULL;
1132 const char *branch_name = "(no branch)";
1133 struct commit *head_commit = NULL;
1134 struct commit_list *parents = NULL;
1135 struct strbuf msg = STRBUF_INIT;
1136 struct strbuf commit_tree_label = STRBUF_INIT;
1137 struct strbuf untracked_files = STRBUF_INIT;
1139 prepare_fallback_ident("git stash", "git@stash");
1141 read_cache_preload(NULL);
1142 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0) < 0) {
1143 ret = -1;
1144 goto done;
1147 if (get_oid("HEAD", &info->b_commit)) {
1148 if (!quiet)
1149 fprintf_ln(stderr, _("You do not have "
1150 "the initial commit yet"));
1151 ret = -1;
1152 goto done;
1153 } else {
1154 head_commit = lookup_commit(the_repository, &info->b_commit);
1157 if (!check_changes(ps, include_untracked, &untracked_files)) {
1158 ret = 1;
1159 goto done;
1162 branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
1163 if (flags & REF_ISSYMREF)
1164 branch_name = strrchr(branch_ref, '/') + 1;
1165 head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
1166 DEFAULT_ABBREV);
1167 strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);
1168 pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
1170 strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
1171 commit_list_insert(head_commit, &parents);
1172 if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
1173 commit_tree(commit_tree_label.buf, commit_tree_label.len,
1174 &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1175 if (!quiet)
1176 fprintf_ln(stderr, _("Cannot save the current "
1177 "index state"));
1178 ret = -1;
1179 goto done;
1182 if (include_untracked) {
1183 if (save_untracked_files(info, &msg, untracked_files)) {
1184 if (!quiet)
1185 fprintf_ln(stderr, _("Cannot save "
1186 "the untracked files"));
1187 ret = -1;
1188 goto done;
1190 untracked_commit_option = 1;
1192 if (patch_mode) {
1193 ret = stash_patch(info, ps, patch, quiet);
1194 if (ret < 0) {
1195 if (!quiet)
1196 fprintf_ln(stderr, _("Cannot save the current "
1197 "worktree state"));
1198 goto done;
1199 } else if (ret > 0) {
1200 goto done;
1202 } else {
1203 if (stash_working_tree(info, ps)) {
1204 if (!quiet)
1205 fprintf_ln(stderr, _("Cannot save the current "
1206 "worktree state"));
1207 ret = -1;
1208 goto done;
1212 if (!stash_msg_buf->len)
1213 strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf);
1214 else
1215 strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name);
1218 * `parents` will be empty after calling `commit_tree()`, so there is
1219 * no need to call `free_commit_list()`
1221 parents = NULL;
1222 if (untracked_commit_option)
1223 commit_list_insert(lookup_commit(the_repository,
1224 &info->u_commit),
1225 &parents);
1226 commit_list_insert(lookup_commit(the_repository, &info->i_commit),
1227 &parents);
1228 commit_list_insert(head_commit, &parents);
1230 if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
1231 parents, &info->w_commit, NULL, NULL)) {
1232 if (!quiet)
1233 fprintf_ln(stderr, _("Cannot record "
1234 "working tree state"));
1235 ret = -1;
1236 goto done;
1239 done:
1240 strbuf_release(&commit_tree_label);
1241 strbuf_release(&msg);
1242 strbuf_release(&untracked_files);
1243 return ret;
1246 static int create_stash(int argc, const char **argv, const char *prefix)
1248 int ret = 0;
1249 struct strbuf stash_msg_buf = STRBUF_INIT;
1250 struct stash_info info;
1251 struct pathspec ps;
1253 /* Starting with argv[1], since argv[0] is "create" */
1254 strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
1256 memset(&ps, 0, sizeof(ps));
1257 if (!check_changes_tracked_files(&ps))
1258 return 0;
1260 ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, &info,
1261 NULL, 0);
1262 if (!ret)
1263 printf_ln("%s", oid_to_hex(&info.w_commit));
1265 strbuf_release(&stash_msg_buf);
1266 return ret;
1269 static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,
1270 int keep_index, int patch_mode, int include_untracked)
1272 int ret = 0;
1273 struct stash_info info;
1274 struct strbuf patch = STRBUF_INIT;
1275 struct strbuf stash_msg_buf = STRBUF_INIT;
1276 struct strbuf untracked_files = STRBUF_INIT;
1278 if (patch_mode && keep_index == -1)
1279 keep_index = 1;
1281 if (patch_mode && include_untracked) {
1282 fprintf_ln(stderr, _("Can't use --patch and --include-untracked"
1283 " or --all at the same time"));
1284 ret = -1;
1285 goto done;
1288 read_cache_preload(NULL);
1289 if (!include_untracked && ps->nr) {
1290 int i;
1291 char *ps_matched = xcalloc(ps->nr, 1);
1293 for (i = 0; i < active_nr; i++)
1294 ce_path_match(&the_index, active_cache[i], ps,
1295 ps_matched);
1297 if (report_path_error(ps_matched, ps)) {
1298 fprintf_ln(stderr, _("Did you forget to 'git add'?"));
1299 ret = -1;
1300 free(ps_matched);
1301 goto done;
1303 free(ps_matched);
1306 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0)) {
1307 ret = -1;
1308 goto done;
1311 if (!check_changes(ps, include_untracked, &untracked_files)) {
1312 if (!quiet)
1313 printf_ln(_("No local changes to save"));
1314 goto done;
1317 if (!reflog_exists(ref_stash) && do_clear_stash()) {
1318 ret = -1;
1319 if (!quiet)
1320 fprintf_ln(stderr, _("Cannot initialize stash"));
1321 goto done;
1324 if (stash_msg)
1325 strbuf_addstr(&stash_msg_buf, stash_msg);
1326 if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1327 &info, &patch, quiet)) {
1328 ret = -1;
1329 goto done;
1332 if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
1333 ret = -1;
1334 if (!quiet)
1335 fprintf_ln(stderr, _("Cannot save the current status"));
1336 goto done;
1339 if (!quiet)
1340 printf_ln(_("Saved working directory and index state %s"),
1341 stash_msg_buf.buf);
1343 if (!patch_mode) {
1344 if (include_untracked && !ps->nr) {
1345 struct child_process cp = CHILD_PROCESS_INIT;
1347 cp.git_cmd = 1;
1348 argv_array_pushl(&cp.args, "clean", "--force",
1349 "--quiet", "-d", NULL);
1350 if (include_untracked == INCLUDE_ALL_FILES)
1351 argv_array_push(&cp.args, "-x");
1352 if (run_command(&cp)) {
1353 ret = -1;
1354 goto done;
1357 discard_cache();
1358 if (ps->nr) {
1359 struct child_process cp_add = CHILD_PROCESS_INIT;
1360 struct child_process cp_diff = CHILD_PROCESS_INIT;
1361 struct child_process cp_apply = CHILD_PROCESS_INIT;
1362 struct strbuf out = STRBUF_INIT;
1364 cp_add.git_cmd = 1;
1365 argv_array_push(&cp_add.args, "add");
1366 if (!include_untracked)
1367 argv_array_push(&cp_add.args, "-u");
1368 if (include_untracked == INCLUDE_ALL_FILES)
1369 argv_array_push(&cp_add.args, "--force");
1370 argv_array_push(&cp_add.args, "--");
1371 add_pathspecs(&cp_add.args, ps);
1372 if (run_command(&cp_add)) {
1373 ret = -1;
1374 goto done;
1377 cp_diff.git_cmd = 1;
1378 argv_array_pushl(&cp_diff.args, "diff-index", "-p",
1379 "--cached", "--binary", "HEAD", "--",
1380 NULL);
1381 add_pathspecs(&cp_diff.args, ps);
1382 if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {
1383 ret = -1;
1384 goto done;
1387 cp_apply.git_cmd = 1;
1388 argv_array_pushl(&cp_apply.args, "apply", "--index",
1389 "-R", NULL);
1390 if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,
1391 NULL, 0)) {
1392 ret = -1;
1393 goto done;
1395 } else {
1396 struct child_process cp = CHILD_PROCESS_INIT;
1397 cp.git_cmd = 1;
1398 argv_array_pushl(&cp.args, "reset", "--hard", "-q",
1399 "--no-recurse-submodules", NULL);
1400 if (run_command(&cp)) {
1401 ret = -1;
1402 goto done;
1406 if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1407 struct child_process cp = CHILD_PROCESS_INIT;
1409 cp.git_cmd = 1;
1410 argv_array_pushl(&cp.args, "checkout", "--no-overlay",
1411 oid_to_hex(&info.i_tree), "--", NULL);
1412 if (!ps->nr)
1413 argv_array_push(&cp.args, ":/");
1414 else
1415 add_pathspecs(&cp.args, ps);
1416 if (run_command(&cp)) {
1417 ret = -1;
1418 goto done;
1421 goto done;
1422 } else {
1423 struct child_process cp = CHILD_PROCESS_INIT;
1425 cp.git_cmd = 1;
1426 argv_array_pushl(&cp.args, "apply", "-R", NULL);
1428 if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1429 if (!quiet)
1430 fprintf_ln(stderr, _("Cannot remove "
1431 "worktree changes"));
1432 ret = -1;
1433 goto done;
1436 if (keep_index < 1) {
1437 struct child_process cp = CHILD_PROCESS_INIT;
1439 cp.git_cmd = 1;
1440 argv_array_pushl(&cp.args, "reset", "-q", "--", NULL);
1441 add_pathspecs(&cp.args, ps);
1442 if (run_command(&cp)) {
1443 ret = -1;
1444 goto done;
1447 goto done;
1450 done:
1451 strbuf_release(&stash_msg_buf);
1452 return ret;
1455 static int push_stash(int argc, const char **argv, const char *prefix,
1456 int push_assumed)
1458 int force_assume = 0;
1459 int keep_index = -1;
1460 int patch_mode = 0;
1461 int include_untracked = 0;
1462 int quiet = 0;
1463 int pathspec_file_nul = 0;
1464 const char *stash_msg = NULL;
1465 const char *pathspec_from_file = NULL;
1466 struct pathspec ps;
1467 struct option options[] = {
1468 OPT_BOOL('k', "keep-index", &keep_index,
1469 N_("keep index")),
1470 OPT_BOOL('p', "patch", &patch_mode,
1471 N_("stash in patch mode")),
1472 OPT__QUIET(&quiet, N_("quiet mode")),
1473 OPT_BOOL('u', "include-untracked", &include_untracked,
1474 N_("include untracked files in stash")),
1475 OPT_SET_INT('a', "all", &include_untracked,
1476 N_("include ignore files"), 2),
1477 OPT_STRING('m', "message", &stash_msg, N_("message"),
1478 N_("stash message")),
1479 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1480 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
1481 OPT_END()
1484 if (argc) {
1485 force_assume = !strcmp(argv[0], "-p");
1486 argc = parse_options(argc, argv, prefix, options,
1487 git_stash_push_usage,
1488 PARSE_OPT_KEEP_DASHDASH);
1491 if (argc) {
1492 if (!strcmp(argv[0], "--")) {
1493 argc--;
1494 argv++;
1495 } else if (push_assumed && !force_assume) {
1496 die("subcommand wasn't specified; 'push' can't be assumed due to unexpected token '%s'",
1497 argv[0]);
1501 parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1502 prefix, argv);
1504 if (pathspec_from_file) {
1505 if (patch_mode)
1506 die(_("--pathspec-from-file is incompatible with --patch"));
1508 if (ps.nr)
1509 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
1511 parse_pathspec_file(&ps, 0,
1512 PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1513 prefix, pathspec_from_file, pathspec_file_nul);
1514 } else if (pathspec_file_nul) {
1515 die(_("--pathspec-file-nul requires --pathspec-from-file"));
1518 return do_push_stash(&ps, stash_msg, quiet, keep_index, patch_mode,
1519 include_untracked);
1522 static int save_stash(int argc, const char **argv, const char *prefix)
1524 int keep_index = -1;
1525 int patch_mode = 0;
1526 int include_untracked = 0;
1527 int quiet = 0;
1528 int ret = 0;
1529 const char *stash_msg = NULL;
1530 struct pathspec ps;
1531 struct strbuf stash_msg_buf = STRBUF_INIT;
1532 struct option options[] = {
1533 OPT_BOOL('k', "keep-index", &keep_index,
1534 N_("keep index")),
1535 OPT_BOOL('p', "patch", &patch_mode,
1536 N_("stash in patch mode")),
1537 OPT__QUIET(&quiet, N_("quiet mode")),
1538 OPT_BOOL('u', "include-untracked", &include_untracked,
1539 N_("include untracked files in stash")),
1540 OPT_SET_INT('a', "all", &include_untracked,
1541 N_("include ignore files"), 2),
1542 OPT_STRING('m', "message", &stash_msg, "message",
1543 N_("stash message")),
1544 OPT_END()
1547 argc = parse_options(argc, argv, prefix, options,
1548 git_stash_save_usage,
1549 PARSE_OPT_KEEP_DASHDASH);
1551 if (argc)
1552 stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
1554 memset(&ps, 0, sizeof(ps));
1555 ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
1556 patch_mode, include_untracked);
1558 strbuf_release(&stash_msg_buf);
1559 return ret;
1562 static int use_builtin_stash(void)
1564 struct child_process cp = CHILD_PROCESS_INIT;
1565 struct strbuf out = STRBUF_INIT;
1566 int ret, env = git_env_bool("GIT_TEST_STASH_USE_BUILTIN", -1);
1568 if (env != -1)
1569 return env;
1571 argv_array_pushl(&cp.args,
1572 "config", "--bool", "stash.usebuiltin", NULL);
1573 cp.git_cmd = 1;
1574 if (capture_command(&cp, &out, 6)) {
1575 strbuf_release(&out);
1576 return 1;
1579 strbuf_trim(&out);
1580 ret = !strcmp("true", out.buf);
1581 strbuf_release(&out);
1582 return ret;
1585 int cmd_stash(int argc, const char **argv, const char *prefix)
1587 pid_t pid = getpid();
1588 const char *index_file;
1589 struct argv_array args = ARGV_ARRAY_INIT;
1591 struct option options[] = {
1592 OPT_END()
1595 if (!use_builtin_stash()) {
1596 const char *path = mkpath("%s/git-legacy-stash",
1597 git_exec_path());
1599 if (sane_execvp(path, (char **)argv) < 0)
1600 die_errno(_("could not exec %s"), path);
1601 else
1602 BUG("sane_execvp() returned???");
1605 prefix = setup_git_directory();
1606 trace_repo_setup(prefix);
1607 setup_work_tree();
1609 git_config(git_diff_basic_config, NULL);
1611 argc = parse_options(argc, argv, prefix, options, git_stash_usage,
1612 PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
1614 index_file = get_index_file();
1615 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1616 (uintmax_t)pid);
1618 if (!argc)
1619 return !!push_stash(0, NULL, prefix, 0);
1620 else if (!strcmp(argv[0], "apply"))
1621 return !!apply_stash(argc, argv, prefix);
1622 else if (!strcmp(argv[0], "clear"))
1623 return !!clear_stash(argc, argv, prefix);
1624 else if (!strcmp(argv[0], "drop"))
1625 return !!drop_stash(argc, argv, prefix);
1626 else if (!strcmp(argv[0], "pop"))
1627 return !!pop_stash(argc, argv, prefix);
1628 else if (!strcmp(argv[0], "branch"))
1629 return !!branch_stash(argc, argv, prefix);
1630 else if (!strcmp(argv[0], "list"))
1631 return !!list_stash(argc, argv, prefix);
1632 else if (!strcmp(argv[0], "show"))
1633 return !!show_stash(argc, argv, prefix);
1634 else if (!strcmp(argv[0], "store"))
1635 return !!store_stash(argc, argv, prefix);
1636 else if (!strcmp(argv[0], "create"))
1637 return !!create_stash(argc, argv, prefix);
1638 else if (!strcmp(argv[0], "push"))
1639 return !!push_stash(argc, argv, prefix, 0);
1640 else if (!strcmp(argv[0], "save"))
1641 return !!save_stash(argc, argv, prefix);
1642 else if (*argv[0] != '-')
1643 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
1644 git_stash_usage, options);
1646 /* Assume 'stash push' */
1647 argv_array_push(&args, "push");
1648 argv_array_pushv(&args, argv);
1649 return !!push_stash(args.argc, args.argv, prefix, 1);