t0301: fixes for windows compatibility
[git/debian.git] / builtin / stash.c
blob8f42360ca91385387a3da84c948023e0a3b06574
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 "strvec.h"
11 #include "run-command.h"
12 #include "dir.h"
13 #include "entry.h"
14 #include "rerere.h"
15 #include "revision.h"
16 #include "log-tree.h"
17 #include "diffcore.h"
18 #include "exec-cmd.h"
19 #include "entry.h"
21 #define INCLUDE_ALL_FILES 2
23 static const char * const git_stash_usage[] = {
24 N_("git stash list [<options>]"),
25 N_("git stash show [<options>] [<stash>]"),
26 N_("git stash drop [-q|--quiet] [<stash>]"),
27 N_("git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
28 N_("git stash branch <branchname> [<stash>]"),
29 "git stash clear",
30 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
31 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
32 " [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
33 " [--] [<pathspec>...]]"),
34 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
35 " [-u|--include-untracked] [-a|--all] [<message>]"),
36 NULL
39 static const char * const git_stash_list_usage[] = {
40 N_("git stash list [<options>]"),
41 NULL
44 static const char * const git_stash_show_usage[] = {
45 N_("git stash show [<options>] [<stash>]"),
46 NULL
49 static const char * const git_stash_drop_usage[] = {
50 N_("git stash drop [-q|--quiet] [<stash>]"),
51 NULL
54 static const char * const git_stash_pop_usage[] = {
55 N_("git stash pop [--index] [-q|--quiet] [<stash>]"),
56 NULL
59 static const char * const git_stash_apply_usage[] = {
60 N_("git stash apply [--index] [-q|--quiet] [<stash>]"),
61 NULL
64 static const char * const git_stash_branch_usage[] = {
65 N_("git stash branch <branchname> [<stash>]"),
66 NULL
69 static const char * const git_stash_clear_usage[] = {
70 "git stash clear",
71 NULL
74 static const char * const git_stash_store_usage[] = {
75 N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"),
76 NULL
79 static const char * const git_stash_push_usage[] = {
80 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
81 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
82 " [--] [<pathspec>...]]"),
83 NULL
86 static const char * const git_stash_save_usage[] = {
87 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
88 " [-u|--include-untracked] [-a|--all] [<message>]"),
89 NULL
92 static const char ref_stash[] = "refs/stash";
93 static struct strbuf stash_index_path = STRBUF_INIT;
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
105 struct 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;
119 static void free_stash_info(struct stash_info *info)
121 strbuf_release(&info->revision);
124 static void assert_stash_like(struct stash_info *info, const char *revision)
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);
133 static int get_stash_info(struct stash_info *info, int argc, const char **argv)
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;
143 if (argc > 1) {
144 int i;
145 struct strbuf refs_msg = STRBUF_INIT;
147 for (i = 0; i < argc; i++)
148 strbuf_addf(&refs_msg, " '%s'", argv[i]);
150 fprintf_ln(stderr, _("Too many revisions specified:%s"),
151 refs_msg.buf);
152 strbuf_release(&refs_msg);
154 return -1;
157 if (argc == 1)
158 commit = argv[0];
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;
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);
175 revision = info->revision.buf;
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;
183 assert_stash_like(info, revision);
185 info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision);
187 end_of_rev = strchrnul(revision, '@');
188 strbuf_add(&symbolic, revision, end_of_rev - revision);
190 ret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref, 0);
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);
203 free(expanded_ref);
204 return !(ret == 0 || ret == 1);
207 static int do_clear_stash(void)
209 struct object_id obj;
210 if (get_oid(ref_stash, &obj))
211 return 0;
213 return delete_ref(NULL, ref_stash, &obj, 0);
216 static int clear_stash(int argc, const char **argv, const char *prefix)
218 struct option options[] = {
219 OPT_END()
222 argc = parse_options(argc, argv, prefix, options,
223 git_stash_clear_usage,
224 PARSE_OPT_STOP_AT_NON_OPTION);
226 if (argc)
227 return error(_("git stash clear with arguments is "
228 "unimplemented"));
230 return do_clear_stash();
233 static int reset_tree(struct object_id *i_tree, int update, int reset)
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;
241 read_cache_preload(NULL);
242 if (refresh_cache(REFRESH_QUIET))
243 return -1;
245 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
247 memset(&opts, 0, sizeof(opts));
249 tree = parse_tree_indirect(i_tree);
250 if (parse_tree(tree))
251 return -1;
253 init_tree_desc(t, tree->buffer, tree->size);
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;
263 if (unpack_trees(nr_trees, t, &opts))
264 return -1;
266 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
267 return error(_("unable to write new index file"));
269 return 0;
272 static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
274 struct child_process cp = CHILD_PROCESS_INIT;
275 const char *w_commit_hex = oid_to_hex(w_commit);
278 * Diff-tree would not be very hard to replace with a native function,
279 * however it should be done together with apply_cached.
281 cp.git_cmd = 1;
282 strvec_pushl(&cp.args, "diff-tree", "--binary", NULL);
283 strvec_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
285 return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
288 static int apply_cached(struct strbuf *out)
290 struct child_process cp = CHILD_PROCESS_INIT;
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.
297 cp.git_cmd = 1;
298 strvec_pushl(&cp.args, "apply", "--cached", NULL);
299 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
302 static int reset_head(void)
304 struct child_process cp = CHILD_PROCESS_INIT;
307 * Reset is overall quite simple, however there is no current public
308 * API for resetting.
310 cp.git_cmd = 1;
311 strvec_push(&cp.args, "reset");
313 return run_command(&cp);
316 static void add_diff_to_buf(struct diff_queue_struct *q,
317 struct diff_options *options,
318 void *data)
320 int i;
322 for (i = 0; i < q->nr; i++) {
323 strbuf_addstr(data, q->queue[i]->one->path);
325 /* NUL-terminate: will be fed to update-index -z */
326 strbuf_addch(data, '\0');
330 static int restore_untracked(struct object_id *u_tree)
332 int res;
333 struct child_process cp = CHILD_PROCESS_INIT;
336 * We need to run restore files from a given index, but without
337 * affecting the current index, so we use GIT_INDEX_FILE with
338 * run_command to fork processes that will not interfere.
340 cp.git_cmd = 1;
341 strvec_push(&cp.args, "read-tree");
342 strvec_push(&cp.args, oid_to_hex(u_tree));
343 strvec_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
344 stash_index_path.buf);
345 if (run_command(&cp)) {
346 remove_path(stash_index_path.buf);
347 return -1;
350 child_process_init(&cp);
351 cp.git_cmd = 1;
352 strvec_pushl(&cp.args, "checkout-index", "--all", NULL);
353 strvec_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
354 stash_index_path.buf);
356 res = run_command(&cp);
357 remove_path(stash_index_path.buf);
358 return res;
361 static void unstage_changes_unless_new(struct object_id *orig_tree)
364 * When we enter this function, there has been a clean merge of
365 * relevant trees, and the merge logic always stages whatever merges
366 * cleanly. We want to unstage those changes, unless it corresponds
367 * to a file that didn't exist as of orig_tree.
369 * However, if any SKIP_WORKTREE path is modified relative to
370 * orig_tree, then we want to clear the SKIP_WORKTREE bit and write
371 * it to the worktree before unstaging.
374 struct checkout state = CHECKOUT_INIT;
375 struct diff_options diff_opts;
376 struct lock_file lock = LOCK_INIT;
377 int i;
379 /* If any entries have skip_worktree set, we'll have to check 'em out */
380 state.force = 1;
381 state.quiet = 1;
382 state.refresh_cache = 1;
383 state.istate = &the_index;
386 * Step 1: get a difference between orig_tree (which corresponding
387 * to the index before a merge was run) and the current index
388 * (reflecting the changes brought in by the merge).
390 diff_setup(&diff_opts);
391 diff_opts.flags.recursive = 1;
392 diff_opts.detect_rename = 0;
393 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
394 diff_setup_done(&diff_opts);
396 do_diff_cache(orig_tree, &diff_opts);
397 diffcore_std(&diff_opts);
399 /* Iterate over the paths that changed due to the merge... */
400 for (i = 0; i < diff_queued_diff.nr; i++) {
401 struct diff_filepair *p;
402 struct cache_entry *ce;
403 int pos;
405 /* Look up the path's position in the current index. */
406 p = diff_queued_diff.queue[i];
407 pos = index_name_pos(&the_index, p->two->path,
408 strlen(p->two->path));
411 * Step 2: Place changes in the working tree
413 * Stash is about restoring changes *to the working tree*.
414 * So if the merge successfully got a new version of some
415 * path, but left it out of the working tree, then clear the
416 * SKIP_WORKTREE bit and write it to the working tree.
418 if (pos >= 0 && ce_skip_worktree(active_cache[pos])) {
419 struct stat st;
421 ce = active_cache[pos];
422 if (!lstat(ce->name, &st)) {
423 /* Conflicting path present; relocate it */
424 struct strbuf new_path = STRBUF_INIT;
425 int fd;
427 strbuf_addf(&new_path,
428 "%s.stash.XXXXXX", ce->name);
429 fd = xmkstemp(new_path.buf);
430 close(fd);
431 printf(_("WARNING: Untracked file in way of "
432 "tracked file! Renaming\n "
433 " %s -> %s\n"
434 " to make room.\n"),
435 ce->name, new_path.buf);
436 if (rename(ce->name, new_path.buf))
437 die("Failed to move %s to %s\n",
438 ce->name, new_path.buf);
439 strbuf_release(&new_path);
441 checkout_entry(ce, &state, NULL, NULL);
442 ce->ce_flags &= ~CE_SKIP_WORKTREE;
446 * Step 3: "unstage" changes, as long as they are still tracked
448 if (p->one->oid_valid) {
450 * Path existed in orig_tree; restore index entry
451 * from that tree in order to "unstage" the changes.
453 int option = ADD_CACHE_OK_TO_REPLACE;
454 if (pos < 0)
455 option = ADD_CACHE_OK_TO_ADD;
457 ce = make_cache_entry(&the_index,
458 p->one->mode,
459 &p->one->oid,
460 p->one->path,
461 0, 0);
462 add_index_entry(&the_index, ce, option);
465 diff_flush(&diff_opts);
468 * Step 4: write the new index to disk
470 repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
471 if (write_locked_index(&the_index, &lock,
472 COMMIT_LOCK | SKIP_IF_UNCHANGED))
473 die(_("Unable to write index."));
476 static int do_apply_stash(const char *prefix, struct stash_info *info,
477 int index, int quiet)
479 int ret;
480 int has_index = index;
481 struct merge_options o;
482 struct object_id c_tree;
483 struct object_id index_tree;
484 struct commit *result;
485 const struct object_id *bases[1];
487 read_cache_preload(NULL);
488 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0))
489 return -1;
491 if (write_cache_as_tree(&c_tree, 0, NULL))
492 return error(_("cannot apply a stash in the middle of a merge"));
494 if (index) {
495 if (oideq(&info->b_tree, &info->i_tree) ||
496 oideq(&c_tree, &info->i_tree)) {
497 has_index = 0;
498 } else {
499 struct strbuf out = STRBUF_INIT;
501 if (diff_tree_binary(&out, &info->w_commit)) {
502 strbuf_release(&out);
503 return error(_("could not generate diff %s^!."),
504 oid_to_hex(&info->w_commit));
507 ret = apply_cached(&out);
508 strbuf_release(&out);
509 if (ret)
510 return error(_("conflicts in index. "
511 "Try without --index."));
513 discard_cache();
514 read_cache();
515 if (write_cache_as_tree(&index_tree, 0, NULL))
516 return error(_("could not save index tree"));
518 reset_head();
519 discard_cache();
520 read_cache();
524 if (info->has_u && restore_untracked(&info->u_tree))
525 return error(_("could not restore untracked files from stash"));
527 init_merge_options(&o, the_repository);
529 o.branch1 = "Updated upstream";
530 o.branch2 = "Stashed changes";
532 if (oideq(&info->b_tree, &c_tree))
533 o.branch1 = "Version stash was based on";
535 if (quiet)
536 o.verbosity = 0;
538 if (o.verbosity >= 3)
539 printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
541 bases[0] = &info->b_tree;
543 ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, 1, bases,
544 &result);
545 if (ret) {
546 rerere(0);
548 if (index)
549 fprintf_ln(stderr, _("Index was not unstashed."));
551 return ret;
554 if (has_index) {
555 if (reset_tree(&index_tree, 0, 0))
556 return -1;
557 } else {
558 unstage_changes_unless_new(&c_tree);
561 if (!quiet) {
562 struct child_process cp = CHILD_PROCESS_INIT;
565 * Status is quite simple and could be replaced with calls to
566 * wt_status in the future, but it adds complexities which may
567 * require more tests.
569 cp.git_cmd = 1;
570 cp.dir = prefix;
571 strvec_pushf(&cp.env_array, GIT_WORK_TREE_ENVIRONMENT"=%s",
572 absolute_path(get_git_work_tree()));
573 strvec_pushf(&cp.env_array, GIT_DIR_ENVIRONMENT"=%s",
574 absolute_path(get_git_dir()));
575 strvec_push(&cp.args, "status");
576 run_command(&cp);
579 return 0;
582 static int apply_stash(int argc, const char **argv, const char *prefix)
584 int ret;
585 int quiet = 0;
586 int index = 0;
587 struct stash_info info;
588 struct option options[] = {
589 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
590 OPT_BOOL(0, "index", &index,
591 N_("attempt to recreate the index")),
592 OPT_END()
595 argc = parse_options(argc, argv, prefix, options,
596 git_stash_apply_usage, 0);
598 if (get_stash_info(&info, argc, argv))
599 return -1;
601 ret = do_apply_stash(prefix, &info, index, quiet);
602 free_stash_info(&info);
603 return ret;
606 static int reject_reflog_ent(struct object_id *ooid, struct object_id *noid,
607 const char *email, timestamp_t timestamp, int tz,
608 const char *message, void *cb_data)
610 return 1;
613 static int reflog_is_empty(const char *refname)
615 return !for_each_reflog_ent(refname, reject_reflog_ent, NULL);
618 static int do_drop_stash(struct stash_info *info, int quiet)
620 int ret;
621 struct child_process cp_reflog = CHILD_PROCESS_INIT;
624 * reflog does not provide a simple function for deleting refs. One will
625 * need to be added to avoid implementing too much reflog code here
628 cp_reflog.git_cmd = 1;
629 strvec_pushl(&cp_reflog.args, "reflog", "delete", "--updateref",
630 "--rewrite", NULL);
631 strvec_push(&cp_reflog.args, info->revision.buf);
632 ret = run_command(&cp_reflog);
633 if (!ret) {
634 if (!quiet)
635 printf_ln(_("Dropped %s (%s)"), info->revision.buf,
636 oid_to_hex(&info->w_commit));
637 } else {
638 return error(_("%s: Could not drop stash entry"),
639 info->revision.buf);
642 if (reflog_is_empty(ref_stash))
643 do_clear_stash();
645 return 0;
648 static void assert_stash_ref(struct stash_info *info)
650 if (!info->is_stash_ref) {
651 error(_("'%s' is not a stash reference"), info->revision.buf);
652 free_stash_info(info);
653 exit(1);
657 static int drop_stash(int argc, const char **argv, const char *prefix)
659 int ret;
660 int quiet = 0;
661 struct stash_info info;
662 struct option options[] = {
663 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
664 OPT_END()
667 argc = parse_options(argc, argv, prefix, options,
668 git_stash_drop_usage, 0);
670 if (get_stash_info(&info, argc, argv))
671 return -1;
673 assert_stash_ref(&info);
675 ret = do_drop_stash(&info, quiet);
676 free_stash_info(&info);
677 return ret;
680 static int pop_stash(int argc, const char **argv, const char *prefix)
682 int ret;
683 int index = 0;
684 int quiet = 0;
685 struct stash_info info;
686 struct option options[] = {
687 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
688 OPT_BOOL(0, "index", &index,
689 N_("attempt to recreate the index")),
690 OPT_END()
693 argc = parse_options(argc, argv, prefix, options,
694 git_stash_pop_usage, 0);
696 if (get_stash_info(&info, argc, argv))
697 return -1;
699 assert_stash_ref(&info);
700 if ((ret = do_apply_stash(prefix, &info, index, quiet)))
701 printf_ln(_("The stash entry is kept in case "
702 "you need it again."));
703 else
704 ret = do_drop_stash(&info, quiet);
706 free_stash_info(&info);
707 return ret;
710 static int branch_stash(int argc, const char **argv, const char *prefix)
712 int ret;
713 const char *branch = NULL;
714 struct stash_info info;
715 struct child_process cp = CHILD_PROCESS_INIT;
716 struct option options[] = {
717 OPT_END()
720 argc = parse_options(argc, argv, prefix, options,
721 git_stash_branch_usage, 0);
723 if (!argc) {
724 fprintf_ln(stderr, _("No branch name specified"));
725 return -1;
728 branch = argv[0];
730 if (get_stash_info(&info, argc - 1, argv + 1))
731 return -1;
733 cp.git_cmd = 1;
734 strvec_pushl(&cp.args, "checkout", "-b", NULL);
735 strvec_push(&cp.args, branch);
736 strvec_push(&cp.args, oid_to_hex(&info.b_commit));
737 ret = run_command(&cp);
738 if (!ret)
739 ret = do_apply_stash(prefix, &info, 1, 0);
740 if (!ret && info.is_stash_ref)
741 ret = do_drop_stash(&info, 0);
743 free_stash_info(&info);
745 return ret;
748 static int list_stash(int argc, const char **argv, const char *prefix)
750 struct child_process cp = CHILD_PROCESS_INIT;
751 struct option options[] = {
752 OPT_END()
755 argc = parse_options(argc, argv, prefix, options,
756 git_stash_list_usage,
757 PARSE_OPT_KEEP_UNKNOWN);
759 if (!ref_exists(ref_stash))
760 return 0;
762 cp.git_cmd = 1;
763 strvec_pushl(&cp.args, "log", "--format=%gd: %gs", "-g",
764 "--first-parent", NULL);
765 strvec_pushv(&cp.args, argv);
766 strvec_push(&cp.args, ref_stash);
767 strvec_push(&cp.args, "--");
768 return run_command(&cp);
771 static int show_stat = 1;
772 static int show_patch;
773 static int show_include_untracked;
774 static int use_legacy_stash;
776 static int git_stash_config(const char *var, const char *value, void *cb)
778 if (!strcmp(var, "stash.showstat")) {
779 show_stat = git_config_bool(var, value);
780 return 0;
782 if (!strcmp(var, "stash.showpatch")) {
783 show_patch = git_config_bool(var, value);
784 return 0;
786 if (!strcmp(var, "stash.showincludeuntracked")) {
787 show_include_untracked = git_config_bool(var, value);
788 return 0;
790 if (!strcmp(var, "stash.usebuiltin")) {
791 use_legacy_stash = !git_config_bool(var, value);
792 return 0;
794 return git_diff_basic_config(var, value, cb);
797 static void diff_include_untracked(const struct stash_info *info, struct diff_options *diff_opt)
799 const struct object_id *oid[] = { &info->w_commit, &info->u_tree };
800 struct tree *tree[ARRAY_SIZE(oid)];
801 struct tree_desc tree_desc[ARRAY_SIZE(oid)];
802 struct unpack_trees_options unpack_tree_opt = { 0 };
803 int i;
805 for (i = 0; i < ARRAY_SIZE(oid); i++) {
806 tree[i] = parse_tree_indirect(oid[i]);
807 if (parse_tree(tree[i]) < 0)
808 die(_("failed to parse tree"));
809 init_tree_desc(&tree_desc[i], tree[i]->buffer, tree[i]->size);
812 unpack_tree_opt.head_idx = -1;
813 unpack_tree_opt.src_index = &the_index;
814 unpack_tree_opt.dst_index = &the_index;
815 unpack_tree_opt.merge = 1;
816 unpack_tree_opt.fn = stash_worktree_untracked_merge;
818 if (unpack_trees(ARRAY_SIZE(tree_desc), tree_desc, &unpack_tree_opt))
819 die(_("failed to unpack trees"));
821 do_diff_cache(&info->b_commit, diff_opt);
824 static int show_stash(int argc, const char **argv, const char *prefix)
826 int i;
827 int ret = 0;
828 struct stash_info info;
829 struct rev_info rev;
830 struct strvec stash_args = STRVEC_INIT;
831 struct strvec revision_args = STRVEC_INIT;
832 enum {
833 UNTRACKED_NONE,
834 UNTRACKED_INCLUDE,
835 UNTRACKED_ONLY
836 } show_untracked = show_include_untracked ? UNTRACKED_INCLUDE : UNTRACKED_NONE;
837 struct option options[] = {
838 OPT_SET_INT('u', "include-untracked", &show_untracked,
839 N_("include untracked files in the stash"),
840 UNTRACKED_INCLUDE),
841 OPT_SET_INT_F(0, "only-untracked", &show_untracked,
842 N_("only show untracked files in the stash"),
843 UNTRACKED_ONLY, PARSE_OPT_NONEG),
844 OPT_END()
847 init_diff_ui_defaults();
848 git_config(git_diff_ui_config, NULL);
849 init_revisions(&rev, prefix);
851 argc = parse_options(argc, argv, prefix, options, git_stash_show_usage,
852 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
853 PARSE_OPT_KEEP_DASHDASH);
855 strvec_push(&revision_args, argv[0]);
856 for (i = 1; i < argc; i++) {
857 if (argv[i][0] != '-')
858 strvec_push(&stash_args, argv[i]);
859 else
860 strvec_push(&revision_args, argv[i]);
863 ret = get_stash_info(&info, stash_args.nr, stash_args.v);
864 strvec_clear(&stash_args);
865 if (ret)
866 return -1;
869 * The config settings are applied only if there are not passed
870 * any options.
872 if (revision_args.nr == 1) {
873 if (show_stat)
874 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;
876 if (show_patch)
877 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
879 if (!show_stat && !show_patch) {
880 free_stash_info(&info);
881 return 0;
885 argc = setup_revisions(revision_args.nr, revision_args.v, &rev, NULL);
886 if (argc > 1) {
887 free_stash_info(&info);
888 usage_with_options(git_stash_show_usage, options);
890 if (!rev.diffopt.output_format) {
891 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
892 diff_setup_done(&rev.diffopt);
895 rev.diffopt.flags.recursive = 1;
896 setup_diff_pager(&rev.diffopt);
897 switch (show_untracked) {
898 case UNTRACKED_NONE:
899 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
900 break;
901 case UNTRACKED_ONLY:
902 if (info.has_u)
903 diff_root_tree_oid(&info.u_tree, "", &rev.diffopt);
904 break;
905 case UNTRACKED_INCLUDE:
906 if (info.has_u)
907 diff_include_untracked(&info, &rev.diffopt);
908 else
909 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
910 break;
912 log_tree_diff_flush(&rev);
914 free_stash_info(&info);
915 return diff_result_code(&rev.diffopt, 0);
918 static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
919 int quiet)
921 if (!stash_msg)
922 stash_msg = "Created via \"git stash store\".";
924 if (update_ref(stash_msg, ref_stash, w_commit, NULL,
925 REF_FORCE_CREATE_REFLOG,
926 quiet ? UPDATE_REFS_QUIET_ON_ERR :
927 UPDATE_REFS_MSG_ON_ERR)) {
928 if (!quiet) {
929 fprintf_ln(stderr, _("Cannot update %s with %s"),
930 ref_stash, oid_to_hex(w_commit));
932 return -1;
935 return 0;
938 static int store_stash(int argc, const char **argv, const char *prefix)
940 int quiet = 0;
941 const char *stash_msg = NULL;
942 struct object_id obj;
943 struct object_context dummy;
944 struct option options[] = {
945 OPT__QUIET(&quiet, N_("be quiet")),
946 OPT_STRING('m', "message", &stash_msg, "message",
947 N_("stash message")),
948 OPT_END()
951 argc = parse_options(argc, argv, prefix, options,
952 git_stash_store_usage,
953 PARSE_OPT_KEEP_UNKNOWN);
955 if (argc != 1) {
956 if (!quiet)
957 fprintf_ln(stderr, _("\"git stash store\" requires one "
958 "<commit> argument"));
959 return -1;
962 if (get_oid_with_context(the_repository,
963 argv[0], quiet ? GET_OID_QUIETLY : 0, &obj,
964 &dummy)) {
965 if (!quiet)
966 fprintf_ln(stderr, _("Cannot update %s with %s"),
967 ref_stash, argv[0]);
968 return -1;
971 return do_store_stash(&obj, stash_msg, quiet);
974 static void add_pathspecs(struct strvec *args,
975 const struct pathspec *ps) {
976 int i;
978 for (i = 0; i < ps->nr; i++)
979 strvec_push(args, ps->items[i].original);
983 * `untracked_files` will be filled with the names of untracked files.
984 * The return value is:
986 * = 0 if there are not any untracked files
987 * > 0 if there are untracked files
989 static int get_untracked_files(const struct pathspec *ps, int include_untracked,
990 struct strbuf *untracked_files)
992 int i;
993 int found = 0;
994 struct dir_struct dir = DIR_INIT;
996 if (include_untracked != INCLUDE_ALL_FILES)
997 setup_standard_excludes(&dir);
999 fill_directory(&dir, the_repository->index, ps);
1000 for (i = 0; i < dir.nr; i++) {
1001 struct dir_entry *ent = dir.entries[i];
1002 found++;
1003 strbuf_addstr(untracked_files, ent->name);
1004 /* NUL-terminate: will be fed to update-index -z */
1005 strbuf_addch(untracked_files, '\0');
1008 dir_clear(&dir);
1009 return found;
1013 * The return value of `check_changes_tracked_files()` can be:
1015 * < 0 if there was an error
1016 * = 0 if there are no changes.
1017 * > 0 if there are changes.
1019 static int check_changes_tracked_files(const struct pathspec *ps)
1021 int result;
1022 struct rev_info rev;
1023 struct object_id dummy;
1024 int ret = 0;
1026 /* No initial commit. */
1027 if (get_oid("HEAD", &dummy))
1028 return -1;
1030 if (read_cache() < 0)
1031 return -1;
1033 init_revisions(&rev, NULL);
1034 copy_pathspec(&rev.prune_data, ps);
1036 rev.diffopt.flags.quick = 1;
1037 rev.diffopt.flags.ignore_submodules = 1;
1038 rev.abbrev = 0;
1040 add_head_to_pending(&rev);
1041 diff_setup_done(&rev.diffopt);
1043 result = run_diff_index(&rev, 1);
1044 if (diff_result_code(&rev.diffopt, result)) {
1045 ret = 1;
1046 goto done;
1049 object_array_clear(&rev.pending);
1050 result = run_diff_files(&rev, 0);
1051 if (diff_result_code(&rev.diffopt, result)) {
1052 ret = 1;
1053 goto done;
1056 done:
1057 clear_pathspec(&rev.prune_data);
1058 return ret;
1062 * The function will fill `untracked_files` with the names of untracked files
1063 * It will return 1 if there were any changes and 0 if there were not.
1065 static int check_changes(const struct pathspec *ps, int include_untracked,
1066 struct strbuf *untracked_files)
1068 int ret = 0;
1069 if (check_changes_tracked_files(ps))
1070 ret = 1;
1072 if (include_untracked && get_untracked_files(ps, include_untracked,
1073 untracked_files))
1074 ret = 1;
1076 return ret;
1079 static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
1080 struct strbuf files)
1082 int ret = 0;
1083 struct strbuf untracked_msg = STRBUF_INIT;
1084 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1085 struct index_state istate = { NULL };
1087 cp_upd_index.git_cmd = 1;
1088 strvec_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
1089 "--remove", "--stdin", NULL);
1090 strvec_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
1091 stash_index_path.buf);
1093 strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
1094 if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,
1095 NULL, 0)) {
1096 ret = -1;
1097 goto done;
1100 if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
1101 NULL)) {
1102 ret = -1;
1103 goto done;
1106 if (commit_tree(untracked_msg.buf, untracked_msg.len,
1107 &info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
1108 ret = -1;
1109 goto done;
1112 done:
1113 discard_index(&istate);
1114 strbuf_release(&untracked_msg);
1115 remove_path(stash_index_path.buf);
1116 return ret;
1119 static int stash_patch(struct stash_info *info, const struct pathspec *ps,
1120 struct strbuf *out_patch, int quiet)
1122 int ret = 0;
1123 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
1124 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1125 struct index_state istate = { NULL };
1126 char *old_index_env = NULL, *old_repo_index_file;
1128 remove_path(stash_index_path.buf);
1130 cp_read_tree.git_cmd = 1;
1131 strvec_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL);
1132 strvec_pushf(&cp_read_tree.env_array, "GIT_INDEX_FILE=%s",
1133 stash_index_path.buf);
1134 if (run_command(&cp_read_tree)) {
1135 ret = -1;
1136 goto done;
1139 /* Find out what the user wants. */
1140 old_repo_index_file = the_repository->index_file;
1141 the_repository->index_file = stash_index_path.buf;
1142 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
1143 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
1145 ret = run_add_interactive(NULL, "--patch=stash", ps);
1147 the_repository->index_file = old_repo_index_file;
1148 if (old_index_env && *old_index_env)
1149 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
1150 else
1151 unsetenv(INDEX_ENVIRONMENT);
1152 FREE_AND_NULL(old_index_env);
1154 /* State of the working tree. */
1155 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1156 NULL)) {
1157 ret = -1;
1158 goto done;
1161 cp_diff_tree.git_cmd = 1;
1162 strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD",
1163 oid_to_hex(&info->w_tree), "--", NULL);
1164 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
1165 ret = -1;
1166 goto done;
1169 if (!out_patch->len) {
1170 if (!quiet)
1171 fprintf_ln(stderr, _("No changes selected"));
1172 ret = 1;
1175 done:
1176 discard_index(&istate);
1177 remove_path(stash_index_path.buf);
1178 return ret;
1181 static int stash_working_tree(struct stash_info *info, const struct pathspec *ps)
1183 int ret = 0;
1184 struct rev_info rev;
1185 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1186 struct strbuf diff_output = STRBUF_INIT;
1187 struct index_state istate = { NULL };
1189 init_revisions(&rev, NULL);
1190 copy_pathspec(&rev.prune_data, ps);
1192 set_alternate_index_output(stash_index_path.buf);
1193 if (reset_tree(&info->i_tree, 0, 0)) {
1194 ret = -1;
1195 goto done;
1197 set_alternate_index_output(NULL);
1199 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
1200 rev.diffopt.format_callback = add_diff_to_buf;
1201 rev.diffopt.format_callback_data = &diff_output;
1203 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1204 ret = -1;
1205 goto done;
1208 add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
1209 "");
1210 if (run_diff_index(&rev, 0)) {
1211 ret = -1;
1212 goto done;
1215 cp_upd_index.git_cmd = 1;
1216 strvec_pushl(&cp_upd_index.args, "update-index",
1217 "--ignore-skip-worktree-entries",
1218 "-z", "--add", "--remove", "--stdin", NULL);
1219 strvec_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
1220 stash_index_path.buf);
1222 if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,
1223 NULL, 0, NULL, 0)) {
1224 ret = -1;
1225 goto done;
1228 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1229 NULL)) {
1230 ret = -1;
1231 goto done;
1234 done:
1235 discard_index(&istate);
1236 UNLEAK(rev);
1237 object_array_clear(&rev.pending);
1238 clear_pathspec(&rev.prune_data);
1239 strbuf_release(&diff_output);
1240 remove_path(stash_index_path.buf);
1241 return ret;
1244 static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,
1245 int include_untracked, int patch_mode,
1246 struct stash_info *info, struct strbuf *patch,
1247 int quiet)
1249 int ret = 0;
1250 int flags = 0;
1251 int untracked_commit_option = 0;
1252 const char *head_short_sha1 = NULL;
1253 const char *branch_ref = NULL;
1254 const char *branch_name = "(no branch)";
1255 struct commit *head_commit = NULL;
1256 struct commit_list *parents = NULL;
1257 struct strbuf msg = STRBUF_INIT;
1258 struct strbuf commit_tree_label = STRBUF_INIT;
1259 struct strbuf untracked_files = STRBUF_INIT;
1261 prepare_fallback_ident("git stash", "git@stash");
1263 read_cache_preload(NULL);
1264 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0) < 0) {
1265 ret = -1;
1266 goto done;
1269 if (get_oid("HEAD", &info->b_commit)) {
1270 if (!quiet)
1271 fprintf_ln(stderr, _("You do not have "
1272 "the initial commit yet"));
1273 ret = -1;
1274 goto done;
1275 } else {
1276 head_commit = lookup_commit(the_repository, &info->b_commit);
1279 if (!check_changes(ps, include_untracked, &untracked_files)) {
1280 ret = 1;
1281 goto done;
1284 branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
1285 if (flags & REF_ISSYMREF)
1286 branch_name = strrchr(branch_ref, '/') + 1;
1287 head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
1288 DEFAULT_ABBREV);
1289 strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);
1290 pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
1292 strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
1293 commit_list_insert(head_commit, &parents);
1294 if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
1295 commit_tree(commit_tree_label.buf, commit_tree_label.len,
1296 &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1297 if (!quiet)
1298 fprintf_ln(stderr, _("Cannot save the current "
1299 "index state"));
1300 ret = -1;
1301 goto done;
1304 if (include_untracked) {
1305 if (save_untracked_files(info, &msg, untracked_files)) {
1306 if (!quiet)
1307 fprintf_ln(stderr, _("Cannot save "
1308 "the untracked files"));
1309 ret = -1;
1310 goto done;
1312 untracked_commit_option = 1;
1314 if (patch_mode) {
1315 ret = stash_patch(info, ps, patch, quiet);
1316 if (ret < 0) {
1317 if (!quiet)
1318 fprintf_ln(stderr, _("Cannot save the current "
1319 "worktree state"));
1320 goto done;
1321 } else if (ret > 0) {
1322 goto done;
1324 } else {
1325 if (stash_working_tree(info, ps)) {
1326 if (!quiet)
1327 fprintf_ln(stderr, _("Cannot save the current "
1328 "worktree state"));
1329 ret = -1;
1330 goto done;
1334 if (!stash_msg_buf->len)
1335 strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf);
1336 else
1337 strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name);
1340 * `parents` will be empty after calling `commit_tree()`, so there is
1341 * no need to call `free_commit_list()`
1343 parents = NULL;
1344 if (untracked_commit_option)
1345 commit_list_insert(lookup_commit(the_repository,
1346 &info->u_commit),
1347 &parents);
1348 commit_list_insert(lookup_commit(the_repository, &info->i_commit),
1349 &parents);
1350 commit_list_insert(head_commit, &parents);
1352 if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
1353 parents, &info->w_commit, NULL, NULL)) {
1354 if (!quiet)
1355 fprintf_ln(stderr, _("Cannot record "
1356 "working tree state"));
1357 ret = -1;
1358 goto done;
1361 done:
1362 strbuf_release(&commit_tree_label);
1363 strbuf_release(&msg);
1364 strbuf_release(&untracked_files);
1365 return ret;
1368 static int create_stash(int argc, const char **argv, const char *prefix)
1370 int ret = 0;
1371 struct strbuf stash_msg_buf = STRBUF_INIT;
1372 struct stash_info info;
1373 struct pathspec ps;
1375 /* Starting with argv[1], since argv[0] is "create" */
1376 strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
1378 memset(&ps, 0, sizeof(ps));
1379 if (!check_changes_tracked_files(&ps))
1380 return 0;
1382 ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, &info,
1383 NULL, 0);
1384 if (!ret)
1385 printf_ln("%s", oid_to_hex(&info.w_commit));
1387 strbuf_release(&stash_msg_buf);
1388 return ret;
1391 static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,
1392 int keep_index, int patch_mode, int include_untracked)
1394 int ret = 0;
1395 struct stash_info info;
1396 struct strbuf patch = STRBUF_INIT;
1397 struct strbuf stash_msg_buf = STRBUF_INIT;
1398 struct strbuf untracked_files = STRBUF_INIT;
1400 if (patch_mode && keep_index == -1)
1401 keep_index = 1;
1403 if (patch_mode && include_untracked) {
1404 fprintf_ln(stderr, _("Can't use --patch and --include-untracked"
1405 " or --all at the same time"));
1406 ret = -1;
1407 goto done;
1410 read_cache_preload(NULL);
1411 if (!include_untracked && ps->nr) {
1412 int i;
1413 char *ps_matched = xcalloc(ps->nr, 1);
1415 /* TODO: audit for interaction with sparse-index. */
1416 ensure_full_index(&the_index);
1417 for (i = 0; i < active_nr; i++)
1418 ce_path_match(&the_index, active_cache[i], ps,
1419 ps_matched);
1421 if (report_path_error(ps_matched, ps)) {
1422 fprintf_ln(stderr, _("Did you forget to 'git add'?"));
1423 ret = -1;
1424 free(ps_matched);
1425 goto done;
1427 free(ps_matched);
1430 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0)) {
1431 ret = -1;
1432 goto done;
1435 if (!check_changes(ps, include_untracked, &untracked_files)) {
1436 if (!quiet)
1437 printf_ln(_("No local changes to save"));
1438 goto done;
1441 if (!reflog_exists(ref_stash) && do_clear_stash()) {
1442 ret = -1;
1443 if (!quiet)
1444 fprintf_ln(stderr, _("Cannot initialize stash"));
1445 goto done;
1448 if (stash_msg)
1449 strbuf_addstr(&stash_msg_buf, stash_msg);
1450 if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1451 &info, &patch, quiet)) {
1452 ret = -1;
1453 goto done;
1456 if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
1457 ret = -1;
1458 if (!quiet)
1459 fprintf_ln(stderr, _("Cannot save the current status"));
1460 goto done;
1463 if (!quiet)
1464 printf_ln(_("Saved working directory and index state %s"),
1465 stash_msg_buf.buf);
1467 if (!patch_mode) {
1468 if (include_untracked && !ps->nr) {
1469 struct child_process cp = CHILD_PROCESS_INIT;
1471 cp.git_cmd = 1;
1472 strvec_pushl(&cp.args, "clean", "--force",
1473 "--quiet", "-d", NULL);
1474 if (include_untracked == INCLUDE_ALL_FILES)
1475 strvec_push(&cp.args, "-x");
1476 if (run_command(&cp)) {
1477 ret = -1;
1478 goto done;
1481 discard_cache();
1482 if (ps->nr) {
1483 struct child_process cp_add = CHILD_PROCESS_INIT;
1484 struct child_process cp_diff = CHILD_PROCESS_INIT;
1485 struct child_process cp_apply = CHILD_PROCESS_INIT;
1486 struct strbuf out = STRBUF_INIT;
1488 cp_add.git_cmd = 1;
1489 strvec_push(&cp_add.args, "add");
1490 if (!include_untracked)
1491 strvec_push(&cp_add.args, "-u");
1492 if (include_untracked == INCLUDE_ALL_FILES)
1493 strvec_push(&cp_add.args, "--force");
1494 strvec_push(&cp_add.args, "--");
1495 add_pathspecs(&cp_add.args, ps);
1496 if (run_command(&cp_add)) {
1497 ret = -1;
1498 goto done;
1501 cp_diff.git_cmd = 1;
1502 strvec_pushl(&cp_diff.args, "diff-index", "-p",
1503 "--cached", "--binary", "HEAD", "--",
1504 NULL);
1505 add_pathspecs(&cp_diff.args, ps);
1506 if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {
1507 ret = -1;
1508 goto done;
1511 cp_apply.git_cmd = 1;
1512 strvec_pushl(&cp_apply.args, "apply", "--index",
1513 "-R", NULL);
1514 if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,
1515 NULL, 0)) {
1516 ret = -1;
1517 goto done;
1519 } else {
1520 struct child_process cp = CHILD_PROCESS_INIT;
1521 cp.git_cmd = 1;
1522 strvec_pushl(&cp.args, "reset", "--hard", "-q",
1523 "--no-recurse-submodules", NULL);
1524 if (run_command(&cp)) {
1525 ret = -1;
1526 goto done;
1530 if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1531 struct child_process cp = CHILD_PROCESS_INIT;
1533 cp.git_cmd = 1;
1534 strvec_pushl(&cp.args, "checkout", "--no-overlay",
1535 oid_to_hex(&info.i_tree), "--", NULL);
1536 if (!ps->nr)
1537 strvec_push(&cp.args, ":/");
1538 else
1539 add_pathspecs(&cp.args, ps);
1540 if (run_command(&cp)) {
1541 ret = -1;
1542 goto done;
1545 goto done;
1546 } else {
1547 struct child_process cp = CHILD_PROCESS_INIT;
1549 cp.git_cmd = 1;
1550 strvec_pushl(&cp.args, "apply", "-R", NULL);
1552 if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1553 if (!quiet)
1554 fprintf_ln(stderr, _("Cannot remove "
1555 "worktree changes"));
1556 ret = -1;
1557 goto done;
1560 if (keep_index < 1) {
1561 struct child_process cp = CHILD_PROCESS_INIT;
1563 cp.git_cmd = 1;
1564 strvec_pushl(&cp.args, "reset", "-q", "--", NULL);
1565 add_pathspecs(&cp.args, ps);
1566 if (run_command(&cp)) {
1567 ret = -1;
1568 goto done;
1571 goto done;
1574 done:
1575 strbuf_release(&stash_msg_buf);
1576 return ret;
1579 static int push_stash(int argc, const char **argv, const char *prefix,
1580 int push_assumed)
1582 int force_assume = 0;
1583 int keep_index = -1;
1584 int patch_mode = 0;
1585 int include_untracked = 0;
1586 int quiet = 0;
1587 int pathspec_file_nul = 0;
1588 const char *stash_msg = NULL;
1589 const char *pathspec_from_file = NULL;
1590 struct pathspec ps;
1591 struct option options[] = {
1592 OPT_BOOL('k', "keep-index", &keep_index,
1593 N_("keep index")),
1594 OPT_BOOL('p', "patch", &patch_mode,
1595 N_("stash in patch mode")),
1596 OPT__QUIET(&quiet, N_("quiet mode")),
1597 OPT_BOOL('u', "include-untracked", &include_untracked,
1598 N_("include untracked files in stash")),
1599 OPT_SET_INT('a', "all", &include_untracked,
1600 N_("include ignore files"), 2),
1601 OPT_STRING('m', "message", &stash_msg, N_("message"),
1602 N_("stash message")),
1603 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1604 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
1605 OPT_END()
1608 if (argc) {
1609 force_assume = !strcmp(argv[0], "-p");
1610 argc = parse_options(argc, argv, prefix, options,
1611 git_stash_push_usage,
1612 PARSE_OPT_KEEP_DASHDASH);
1615 if (argc) {
1616 if (!strcmp(argv[0], "--")) {
1617 argc--;
1618 argv++;
1619 } else if (push_assumed && !force_assume) {
1620 die("subcommand wasn't specified; 'push' can't be assumed due to unexpected token '%s'",
1621 argv[0]);
1625 parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1626 prefix, argv);
1628 if (pathspec_from_file) {
1629 if (patch_mode)
1630 die(_("--pathspec-from-file is incompatible with --patch"));
1632 if (ps.nr)
1633 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
1635 parse_pathspec_file(&ps, 0,
1636 PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1637 prefix, pathspec_from_file, pathspec_file_nul);
1638 } else if (pathspec_file_nul) {
1639 die(_("--pathspec-file-nul requires --pathspec-from-file"));
1642 return do_push_stash(&ps, stash_msg, quiet, keep_index, patch_mode,
1643 include_untracked);
1646 static int save_stash(int argc, const char **argv, const char *prefix)
1648 int keep_index = -1;
1649 int patch_mode = 0;
1650 int include_untracked = 0;
1651 int quiet = 0;
1652 int ret = 0;
1653 const char *stash_msg = NULL;
1654 struct pathspec ps;
1655 struct strbuf stash_msg_buf = STRBUF_INIT;
1656 struct option options[] = {
1657 OPT_BOOL('k', "keep-index", &keep_index,
1658 N_("keep index")),
1659 OPT_BOOL('p', "patch", &patch_mode,
1660 N_("stash in patch mode")),
1661 OPT__QUIET(&quiet, N_("quiet mode")),
1662 OPT_BOOL('u', "include-untracked", &include_untracked,
1663 N_("include untracked files in stash")),
1664 OPT_SET_INT('a', "all", &include_untracked,
1665 N_("include ignore files"), 2),
1666 OPT_STRING('m', "message", &stash_msg, "message",
1667 N_("stash message")),
1668 OPT_END()
1671 argc = parse_options(argc, argv, prefix, options,
1672 git_stash_save_usage,
1673 PARSE_OPT_KEEP_DASHDASH);
1675 if (argc)
1676 stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
1678 memset(&ps, 0, sizeof(ps));
1679 ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
1680 patch_mode, include_untracked);
1682 strbuf_release(&stash_msg_buf);
1683 return ret;
1686 int cmd_stash(int argc, const char **argv, const char *prefix)
1688 pid_t pid = getpid();
1689 const char *index_file;
1690 struct strvec args = STRVEC_INIT;
1692 struct option options[] = {
1693 OPT_END()
1696 git_config(git_stash_config, NULL);
1698 if (use_legacy_stash ||
1699 !git_env_bool("GIT_TEST_STASH_USE_BUILTIN", -1))
1700 warning(_("the stash.useBuiltin support has been removed!\n"
1701 "See its entry in 'git help config' for details."));
1703 argc = parse_options(argc, argv, prefix, options, git_stash_usage,
1704 PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
1706 index_file = get_index_file();
1707 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1708 (uintmax_t)pid);
1710 if (!argc)
1711 return !!push_stash(0, NULL, prefix, 0);
1712 else if (!strcmp(argv[0], "apply"))
1713 return !!apply_stash(argc, argv, prefix);
1714 else if (!strcmp(argv[0], "clear"))
1715 return !!clear_stash(argc, argv, prefix);
1716 else if (!strcmp(argv[0], "drop"))
1717 return !!drop_stash(argc, argv, prefix);
1718 else if (!strcmp(argv[0], "pop"))
1719 return !!pop_stash(argc, argv, prefix);
1720 else if (!strcmp(argv[0], "branch"))
1721 return !!branch_stash(argc, argv, prefix);
1722 else if (!strcmp(argv[0], "list"))
1723 return !!list_stash(argc, argv, prefix);
1724 else if (!strcmp(argv[0], "show"))
1725 return !!show_stash(argc, argv, prefix);
1726 else if (!strcmp(argv[0], "store"))
1727 return !!store_stash(argc, argv, prefix);
1728 else if (!strcmp(argv[0], "create"))
1729 return !!create_stash(argc, argv, prefix);
1730 else if (!strcmp(argv[0], "push"))
1731 return !!push_stash(argc, argv, prefix, 0);
1732 else if (!strcmp(argv[0], "save"))
1733 return !!save_stash(argc, argv, prefix);
1734 else if (*argv[0] != '-')
1735 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
1736 git_stash_usage, options);
1738 /* Assume 'stash push' */
1739 strvec_push(&args, "push");
1740 strvec_pushv(&args, argv);
1741 return !!push_stash(args.nr, args.v, prefix, 1);