unpack-trees: introduce preserve_ignored to unpack_trees_options
[git/debian.git] / builtin / stash.c
blob88287b890d5cd3ddd5d1d83bbe29ba1436238db5
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 if (update && !reset)
262 /* FIXME: Default should be to remove ignored files */
263 opts.preserve_ignored = 1;
264 opts.fn = oneway_merge;
266 if (unpack_trees(nr_trees, t, &opts))
267 return -1;
269 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
270 return error(_("unable to write new index file"));
272 return 0;
275 static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
277 struct child_process cp = CHILD_PROCESS_INIT;
278 const char *w_commit_hex = oid_to_hex(w_commit);
281 * Diff-tree would not be very hard to replace with a native function,
282 * however it should be done together with apply_cached.
284 cp.git_cmd = 1;
285 strvec_pushl(&cp.args, "diff-tree", "--binary", NULL);
286 strvec_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
288 return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
291 static int apply_cached(struct strbuf *out)
293 struct child_process cp = CHILD_PROCESS_INIT;
296 * Apply currently only reads either from stdin or a file, thus
297 * apply_all_patches would have to be updated to optionally take a
298 * buffer.
300 cp.git_cmd = 1;
301 strvec_pushl(&cp.args, "apply", "--cached", NULL);
302 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
305 static int reset_head(void)
307 struct child_process cp = CHILD_PROCESS_INIT;
310 * Reset is overall quite simple, however there is no current public
311 * API for resetting.
313 cp.git_cmd = 1;
314 strvec_push(&cp.args, "reset");
316 return run_command(&cp);
319 static void add_diff_to_buf(struct diff_queue_struct *q,
320 struct diff_options *options,
321 void *data)
323 int i;
325 for (i = 0; i < q->nr; i++) {
326 strbuf_addstr(data, q->queue[i]->one->path);
328 /* NUL-terminate: will be fed to update-index -z */
329 strbuf_addch(data, '\0');
333 static int restore_untracked(struct object_id *u_tree)
335 int res;
336 struct child_process cp = CHILD_PROCESS_INIT;
339 * We need to run restore files from a given index, but without
340 * affecting the current index, so we use GIT_INDEX_FILE with
341 * run_command to fork processes that will not interfere.
343 cp.git_cmd = 1;
344 strvec_push(&cp.args, "read-tree");
345 strvec_push(&cp.args, oid_to_hex(u_tree));
346 strvec_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
347 stash_index_path.buf);
348 if (run_command(&cp)) {
349 remove_path(stash_index_path.buf);
350 return -1;
353 child_process_init(&cp);
354 cp.git_cmd = 1;
355 strvec_pushl(&cp.args, "checkout-index", "--all", NULL);
356 strvec_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
357 stash_index_path.buf);
359 res = run_command(&cp);
360 remove_path(stash_index_path.buf);
361 return res;
364 static void unstage_changes_unless_new(struct object_id *orig_tree)
367 * When we enter this function, there has been a clean merge of
368 * relevant trees, and the merge logic always stages whatever merges
369 * cleanly. We want to unstage those changes, unless it corresponds
370 * to a file that didn't exist as of orig_tree.
372 * However, if any SKIP_WORKTREE path is modified relative to
373 * orig_tree, then we want to clear the SKIP_WORKTREE bit and write
374 * it to the worktree before unstaging.
377 struct checkout state = CHECKOUT_INIT;
378 struct diff_options diff_opts;
379 struct lock_file lock = LOCK_INIT;
380 int i;
382 /* If any entries have skip_worktree set, we'll have to check 'em out */
383 state.force = 1;
384 state.quiet = 1;
385 state.refresh_cache = 1;
386 state.istate = &the_index;
389 * Step 1: get a difference between orig_tree (which corresponding
390 * to the index before a merge was run) and the current index
391 * (reflecting the changes brought in by the merge).
393 diff_setup(&diff_opts);
394 diff_opts.flags.recursive = 1;
395 diff_opts.detect_rename = 0;
396 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
397 diff_setup_done(&diff_opts);
399 do_diff_cache(orig_tree, &diff_opts);
400 diffcore_std(&diff_opts);
402 /* Iterate over the paths that changed due to the merge... */
403 for (i = 0; i < diff_queued_diff.nr; i++) {
404 struct diff_filepair *p;
405 struct cache_entry *ce;
406 int pos;
408 /* Look up the path's position in the current index. */
409 p = diff_queued_diff.queue[i];
410 pos = index_name_pos(&the_index, p->two->path,
411 strlen(p->two->path));
414 * Step 2: Place changes in the working tree
416 * Stash is about restoring changes *to the working tree*.
417 * So if the merge successfully got a new version of some
418 * path, but left it out of the working tree, then clear the
419 * SKIP_WORKTREE bit and write it to the working tree.
421 if (pos >= 0 && ce_skip_worktree(active_cache[pos])) {
422 struct stat st;
424 ce = active_cache[pos];
425 if (!lstat(ce->name, &st)) {
426 /* Conflicting path present; relocate it */
427 struct strbuf new_path = STRBUF_INIT;
428 int fd;
430 strbuf_addf(&new_path,
431 "%s.stash.XXXXXX", ce->name);
432 fd = xmkstemp(new_path.buf);
433 close(fd);
434 printf(_("WARNING: Untracked file in way of "
435 "tracked file! Renaming\n "
436 " %s -> %s\n"
437 " to make room.\n"),
438 ce->name, new_path.buf);
439 if (rename(ce->name, new_path.buf))
440 die("Failed to move %s to %s\n",
441 ce->name, new_path.buf);
442 strbuf_release(&new_path);
444 checkout_entry(ce, &state, NULL, NULL);
445 ce->ce_flags &= ~CE_SKIP_WORKTREE;
449 * Step 3: "unstage" changes, as long as they are still tracked
451 if (p->one->oid_valid) {
453 * Path existed in orig_tree; restore index entry
454 * from that tree in order to "unstage" the changes.
456 int option = ADD_CACHE_OK_TO_REPLACE;
457 if (pos < 0)
458 option = ADD_CACHE_OK_TO_ADD;
460 ce = make_cache_entry(&the_index,
461 p->one->mode,
462 &p->one->oid,
463 p->one->path,
464 0, 0);
465 add_index_entry(&the_index, ce, option);
468 diff_flush(&diff_opts);
471 * Step 4: write the new index to disk
473 repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
474 if (write_locked_index(&the_index, &lock,
475 COMMIT_LOCK | SKIP_IF_UNCHANGED))
476 die(_("Unable to write index."));
479 static int do_apply_stash(const char *prefix, struct stash_info *info,
480 int index, int quiet)
482 int ret;
483 int has_index = index;
484 struct merge_options o;
485 struct object_id c_tree;
486 struct object_id index_tree;
487 struct commit *result;
488 const struct object_id *bases[1];
490 read_cache_preload(NULL);
491 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0))
492 return -1;
494 if (write_cache_as_tree(&c_tree, 0, NULL))
495 return error(_("cannot apply a stash in the middle of a merge"));
497 if (index) {
498 if (oideq(&info->b_tree, &info->i_tree) ||
499 oideq(&c_tree, &info->i_tree)) {
500 has_index = 0;
501 } else {
502 struct strbuf out = STRBUF_INIT;
504 if (diff_tree_binary(&out, &info->w_commit)) {
505 strbuf_release(&out);
506 return error(_("could not generate diff %s^!."),
507 oid_to_hex(&info->w_commit));
510 ret = apply_cached(&out);
511 strbuf_release(&out);
512 if (ret)
513 return error(_("conflicts in index. "
514 "Try without --index."));
516 discard_cache();
517 read_cache();
518 if (write_cache_as_tree(&index_tree, 0, NULL))
519 return error(_("could not save index tree"));
521 reset_head();
522 discard_cache();
523 read_cache();
527 if (info->has_u && restore_untracked(&info->u_tree))
528 return error(_("could not restore untracked files from stash"));
530 init_merge_options(&o, the_repository);
532 o.branch1 = "Updated upstream";
533 o.branch2 = "Stashed changes";
535 if (oideq(&info->b_tree, &c_tree))
536 o.branch1 = "Version stash was based on";
538 if (quiet)
539 o.verbosity = 0;
541 if (o.verbosity >= 3)
542 printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
544 bases[0] = &info->b_tree;
546 ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, 1, bases,
547 &result);
548 if (ret) {
549 rerere(0);
551 if (index)
552 fprintf_ln(stderr, _("Index was not unstashed."));
554 return ret;
557 if (has_index) {
558 if (reset_tree(&index_tree, 0, 0))
559 return -1;
560 } else {
561 unstage_changes_unless_new(&c_tree);
564 if (!quiet) {
565 struct child_process cp = CHILD_PROCESS_INIT;
568 * Status is quite simple and could be replaced with calls to
569 * wt_status in the future, but it adds complexities which may
570 * require more tests.
572 cp.git_cmd = 1;
573 cp.dir = prefix;
574 strvec_pushf(&cp.env_array, GIT_WORK_TREE_ENVIRONMENT"=%s",
575 absolute_path(get_git_work_tree()));
576 strvec_pushf(&cp.env_array, GIT_DIR_ENVIRONMENT"=%s",
577 absolute_path(get_git_dir()));
578 strvec_push(&cp.args, "status");
579 run_command(&cp);
582 return 0;
585 static int apply_stash(int argc, const char **argv, const char *prefix)
587 int ret;
588 int quiet = 0;
589 int index = 0;
590 struct stash_info info;
591 struct option options[] = {
592 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
593 OPT_BOOL(0, "index", &index,
594 N_("attempt to recreate the index")),
595 OPT_END()
598 argc = parse_options(argc, argv, prefix, options,
599 git_stash_apply_usage, 0);
601 if (get_stash_info(&info, argc, argv))
602 return -1;
604 ret = do_apply_stash(prefix, &info, index, quiet);
605 free_stash_info(&info);
606 return ret;
609 static int reject_reflog_ent(struct object_id *ooid, struct object_id *noid,
610 const char *email, timestamp_t timestamp, int tz,
611 const char *message, void *cb_data)
613 return 1;
616 static int reflog_is_empty(const char *refname)
618 return !for_each_reflog_ent(refname, reject_reflog_ent, NULL);
621 static int do_drop_stash(struct stash_info *info, int quiet)
623 int ret;
624 struct child_process cp_reflog = CHILD_PROCESS_INIT;
627 * reflog does not provide a simple function for deleting refs. One will
628 * need to be added to avoid implementing too much reflog code here
631 cp_reflog.git_cmd = 1;
632 strvec_pushl(&cp_reflog.args, "reflog", "delete", "--updateref",
633 "--rewrite", NULL);
634 strvec_push(&cp_reflog.args, info->revision.buf);
635 ret = run_command(&cp_reflog);
636 if (!ret) {
637 if (!quiet)
638 printf_ln(_("Dropped %s (%s)"), info->revision.buf,
639 oid_to_hex(&info->w_commit));
640 } else {
641 return error(_("%s: Could not drop stash entry"),
642 info->revision.buf);
645 if (reflog_is_empty(ref_stash))
646 do_clear_stash();
648 return 0;
651 static void assert_stash_ref(struct stash_info *info)
653 if (!info->is_stash_ref) {
654 error(_("'%s' is not a stash reference"), info->revision.buf);
655 free_stash_info(info);
656 exit(1);
660 static int drop_stash(int argc, const char **argv, const char *prefix)
662 int ret;
663 int quiet = 0;
664 struct stash_info info;
665 struct option options[] = {
666 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
667 OPT_END()
670 argc = parse_options(argc, argv, prefix, options,
671 git_stash_drop_usage, 0);
673 if (get_stash_info(&info, argc, argv))
674 return -1;
676 assert_stash_ref(&info);
678 ret = do_drop_stash(&info, quiet);
679 free_stash_info(&info);
680 return ret;
683 static int pop_stash(int argc, const char **argv, const char *prefix)
685 int ret;
686 int index = 0;
687 int quiet = 0;
688 struct stash_info info;
689 struct option options[] = {
690 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
691 OPT_BOOL(0, "index", &index,
692 N_("attempt to recreate the index")),
693 OPT_END()
696 argc = parse_options(argc, argv, prefix, options,
697 git_stash_pop_usage, 0);
699 if (get_stash_info(&info, argc, argv))
700 return -1;
702 assert_stash_ref(&info);
703 if ((ret = do_apply_stash(prefix, &info, index, quiet)))
704 printf_ln(_("The stash entry is kept in case "
705 "you need it again."));
706 else
707 ret = do_drop_stash(&info, quiet);
709 free_stash_info(&info);
710 return ret;
713 static int branch_stash(int argc, const char **argv, const char *prefix)
715 int ret;
716 const char *branch = NULL;
717 struct stash_info info;
718 struct child_process cp = CHILD_PROCESS_INIT;
719 struct option options[] = {
720 OPT_END()
723 argc = parse_options(argc, argv, prefix, options,
724 git_stash_branch_usage, 0);
726 if (!argc) {
727 fprintf_ln(stderr, _("No branch name specified"));
728 return -1;
731 branch = argv[0];
733 if (get_stash_info(&info, argc - 1, argv + 1))
734 return -1;
736 cp.git_cmd = 1;
737 strvec_pushl(&cp.args, "checkout", "-b", NULL);
738 strvec_push(&cp.args, branch);
739 strvec_push(&cp.args, oid_to_hex(&info.b_commit));
740 ret = run_command(&cp);
741 if (!ret)
742 ret = do_apply_stash(prefix, &info, 1, 0);
743 if (!ret && info.is_stash_ref)
744 ret = do_drop_stash(&info, 0);
746 free_stash_info(&info);
748 return ret;
751 static int list_stash(int argc, const char **argv, const char *prefix)
753 struct child_process cp = CHILD_PROCESS_INIT;
754 struct option options[] = {
755 OPT_END()
758 argc = parse_options(argc, argv, prefix, options,
759 git_stash_list_usage,
760 PARSE_OPT_KEEP_UNKNOWN);
762 if (!ref_exists(ref_stash))
763 return 0;
765 cp.git_cmd = 1;
766 strvec_pushl(&cp.args, "log", "--format=%gd: %gs", "-g",
767 "--first-parent", NULL);
768 strvec_pushv(&cp.args, argv);
769 strvec_push(&cp.args, ref_stash);
770 strvec_push(&cp.args, "--");
771 return run_command(&cp);
774 static int show_stat = 1;
775 static int show_patch;
776 static int show_include_untracked;
777 static int use_legacy_stash;
779 static int git_stash_config(const char *var, const char *value, void *cb)
781 if (!strcmp(var, "stash.showstat")) {
782 show_stat = git_config_bool(var, value);
783 return 0;
785 if (!strcmp(var, "stash.showpatch")) {
786 show_patch = git_config_bool(var, value);
787 return 0;
789 if (!strcmp(var, "stash.showincludeuntracked")) {
790 show_include_untracked = git_config_bool(var, value);
791 return 0;
793 if (!strcmp(var, "stash.usebuiltin")) {
794 use_legacy_stash = !git_config_bool(var, value);
795 return 0;
797 return git_diff_basic_config(var, value, cb);
800 static void diff_include_untracked(const struct stash_info *info, struct diff_options *diff_opt)
802 const struct object_id *oid[] = { &info->w_commit, &info->u_tree };
803 struct tree *tree[ARRAY_SIZE(oid)];
804 struct tree_desc tree_desc[ARRAY_SIZE(oid)];
805 struct unpack_trees_options unpack_tree_opt = { 0 };
806 int i;
808 for (i = 0; i < ARRAY_SIZE(oid); i++) {
809 tree[i] = parse_tree_indirect(oid[i]);
810 if (parse_tree(tree[i]) < 0)
811 die(_("failed to parse tree"));
812 init_tree_desc(&tree_desc[i], tree[i]->buffer, tree[i]->size);
815 unpack_tree_opt.head_idx = -1;
816 unpack_tree_opt.src_index = &the_index;
817 unpack_tree_opt.dst_index = &the_index;
818 unpack_tree_opt.merge = 1;
819 unpack_tree_opt.fn = stash_worktree_untracked_merge;
821 if (unpack_trees(ARRAY_SIZE(tree_desc), tree_desc, &unpack_tree_opt))
822 die(_("failed to unpack trees"));
824 do_diff_cache(&info->b_commit, diff_opt);
827 static int show_stash(int argc, const char **argv, const char *prefix)
829 int i;
830 int ret = 0;
831 struct stash_info info;
832 struct rev_info rev;
833 struct strvec stash_args = STRVEC_INIT;
834 struct strvec revision_args = STRVEC_INIT;
835 enum {
836 UNTRACKED_NONE,
837 UNTRACKED_INCLUDE,
838 UNTRACKED_ONLY
839 } show_untracked = show_include_untracked ? UNTRACKED_INCLUDE : UNTRACKED_NONE;
840 struct option options[] = {
841 OPT_SET_INT('u', "include-untracked", &show_untracked,
842 N_("include untracked files in the stash"),
843 UNTRACKED_INCLUDE),
844 OPT_SET_INT_F(0, "only-untracked", &show_untracked,
845 N_("only show untracked files in the stash"),
846 UNTRACKED_ONLY, PARSE_OPT_NONEG),
847 OPT_END()
850 init_diff_ui_defaults();
851 git_config(git_diff_ui_config, NULL);
852 init_revisions(&rev, prefix);
854 argc = parse_options(argc, argv, prefix, options, git_stash_show_usage,
855 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
856 PARSE_OPT_KEEP_DASHDASH);
858 strvec_push(&revision_args, argv[0]);
859 for (i = 1; i < argc; i++) {
860 if (argv[i][0] != '-')
861 strvec_push(&stash_args, argv[i]);
862 else
863 strvec_push(&revision_args, argv[i]);
866 ret = get_stash_info(&info, stash_args.nr, stash_args.v);
867 strvec_clear(&stash_args);
868 if (ret)
869 return -1;
872 * The config settings are applied only if there are not passed
873 * any options.
875 if (revision_args.nr == 1) {
876 if (show_stat)
877 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;
879 if (show_patch)
880 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
882 if (!show_stat && !show_patch) {
883 free_stash_info(&info);
884 return 0;
888 argc = setup_revisions(revision_args.nr, revision_args.v, &rev, NULL);
889 if (argc > 1) {
890 free_stash_info(&info);
891 usage_with_options(git_stash_show_usage, options);
893 if (!rev.diffopt.output_format) {
894 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
895 diff_setup_done(&rev.diffopt);
898 rev.diffopt.flags.recursive = 1;
899 setup_diff_pager(&rev.diffopt);
900 switch (show_untracked) {
901 case UNTRACKED_NONE:
902 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
903 break;
904 case UNTRACKED_ONLY:
905 if (info.has_u)
906 diff_root_tree_oid(&info.u_tree, "", &rev.diffopt);
907 break;
908 case UNTRACKED_INCLUDE:
909 if (info.has_u)
910 diff_include_untracked(&info, &rev.diffopt);
911 else
912 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
913 break;
915 log_tree_diff_flush(&rev);
917 free_stash_info(&info);
918 return diff_result_code(&rev.diffopt, 0);
921 static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
922 int quiet)
924 if (!stash_msg)
925 stash_msg = "Created via \"git stash store\".";
927 if (update_ref(stash_msg, ref_stash, w_commit, NULL,
928 REF_FORCE_CREATE_REFLOG,
929 quiet ? UPDATE_REFS_QUIET_ON_ERR :
930 UPDATE_REFS_MSG_ON_ERR)) {
931 if (!quiet) {
932 fprintf_ln(stderr, _("Cannot update %s with %s"),
933 ref_stash, oid_to_hex(w_commit));
935 return -1;
938 return 0;
941 static int store_stash(int argc, const char **argv, const char *prefix)
943 int quiet = 0;
944 const char *stash_msg = NULL;
945 struct object_id obj;
946 struct object_context dummy;
947 struct option options[] = {
948 OPT__QUIET(&quiet, N_("be quiet")),
949 OPT_STRING('m', "message", &stash_msg, "message",
950 N_("stash message")),
951 OPT_END()
954 argc = parse_options(argc, argv, prefix, options,
955 git_stash_store_usage,
956 PARSE_OPT_KEEP_UNKNOWN);
958 if (argc != 1) {
959 if (!quiet)
960 fprintf_ln(stderr, _("\"git stash store\" requires one "
961 "<commit> argument"));
962 return -1;
965 if (get_oid_with_context(the_repository,
966 argv[0], quiet ? GET_OID_QUIETLY : 0, &obj,
967 &dummy)) {
968 if (!quiet)
969 fprintf_ln(stderr, _("Cannot update %s with %s"),
970 ref_stash, argv[0]);
971 return -1;
974 return do_store_stash(&obj, stash_msg, quiet);
977 static void add_pathspecs(struct strvec *args,
978 const struct pathspec *ps) {
979 int i;
981 for (i = 0; i < ps->nr; i++)
982 strvec_push(args, ps->items[i].original);
986 * `untracked_files` will be filled with the names of untracked files.
987 * The return value is:
989 * = 0 if there are not any untracked files
990 * > 0 if there are untracked files
992 static int get_untracked_files(const struct pathspec *ps, int include_untracked,
993 struct strbuf *untracked_files)
995 int i;
996 int found = 0;
997 struct dir_struct dir = DIR_INIT;
999 if (include_untracked != INCLUDE_ALL_FILES)
1000 setup_standard_excludes(&dir);
1002 fill_directory(&dir, the_repository->index, ps);
1003 for (i = 0; i < dir.nr; i++) {
1004 struct dir_entry *ent = dir.entries[i];
1005 found++;
1006 strbuf_addstr(untracked_files, ent->name);
1007 /* NUL-terminate: will be fed to update-index -z */
1008 strbuf_addch(untracked_files, '\0');
1011 dir_clear(&dir);
1012 return found;
1016 * The return value of `check_changes_tracked_files()` can be:
1018 * < 0 if there was an error
1019 * = 0 if there are no changes.
1020 * > 0 if there are changes.
1022 static int check_changes_tracked_files(const struct pathspec *ps)
1024 int result;
1025 struct rev_info rev;
1026 struct object_id dummy;
1027 int ret = 0;
1029 /* No initial commit. */
1030 if (get_oid("HEAD", &dummy))
1031 return -1;
1033 if (read_cache() < 0)
1034 return -1;
1036 init_revisions(&rev, NULL);
1037 copy_pathspec(&rev.prune_data, ps);
1039 rev.diffopt.flags.quick = 1;
1040 rev.diffopt.flags.ignore_submodules = 1;
1041 rev.abbrev = 0;
1043 add_head_to_pending(&rev);
1044 diff_setup_done(&rev.diffopt);
1046 result = run_diff_index(&rev, 1);
1047 if (diff_result_code(&rev.diffopt, result)) {
1048 ret = 1;
1049 goto done;
1052 object_array_clear(&rev.pending);
1053 result = run_diff_files(&rev, 0);
1054 if (diff_result_code(&rev.diffopt, result)) {
1055 ret = 1;
1056 goto done;
1059 done:
1060 clear_pathspec(&rev.prune_data);
1061 return ret;
1065 * The function will fill `untracked_files` with the names of untracked files
1066 * It will return 1 if there were any changes and 0 if there were not.
1068 static int check_changes(const struct pathspec *ps, int include_untracked,
1069 struct strbuf *untracked_files)
1071 int ret = 0;
1072 if (check_changes_tracked_files(ps))
1073 ret = 1;
1075 if (include_untracked && get_untracked_files(ps, include_untracked,
1076 untracked_files))
1077 ret = 1;
1079 return ret;
1082 static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
1083 struct strbuf files)
1085 int ret = 0;
1086 struct strbuf untracked_msg = STRBUF_INIT;
1087 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1088 struct index_state istate = { NULL };
1090 cp_upd_index.git_cmd = 1;
1091 strvec_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
1092 "--remove", "--stdin", NULL);
1093 strvec_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
1094 stash_index_path.buf);
1096 strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
1097 if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,
1098 NULL, 0)) {
1099 ret = -1;
1100 goto done;
1103 if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
1104 NULL)) {
1105 ret = -1;
1106 goto done;
1109 if (commit_tree(untracked_msg.buf, untracked_msg.len,
1110 &info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
1111 ret = -1;
1112 goto done;
1115 done:
1116 discard_index(&istate);
1117 strbuf_release(&untracked_msg);
1118 remove_path(stash_index_path.buf);
1119 return ret;
1122 static int stash_patch(struct stash_info *info, const struct pathspec *ps,
1123 struct strbuf *out_patch, int quiet)
1125 int ret = 0;
1126 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
1127 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1128 struct index_state istate = { NULL };
1129 char *old_index_env = NULL, *old_repo_index_file;
1131 remove_path(stash_index_path.buf);
1133 cp_read_tree.git_cmd = 1;
1134 strvec_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL);
1135 strvec_pushf(&cp_read_tree.env_array, "GIT_INDEX_FILE=%s",
1136 stash_index_path.buf);
1137 if (run_command(&cp_read_tree)) {
1138 ret = -1;
1139 goto done;
1142 /* Find out what the user wants. */
1143 old_repo_index_file = the_repository->index_file;
1144 the_repository->index_file = stash_index_path.buf;
1145 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
1146 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
1148 ret = run_add_interactive(NULL, "--patch=stash", ps);
1150 the_repository->index_file = old_repo_index_file;
1151 if (old_index_env && *old_index_env)
1152 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
1153 else
1154 unsetenv(INDEX_ENVIRONMENT);
1155 FREE_AND_NULL(old_index_env);
1157 /* State of the working tree. */
1158 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1159 NULL)) {
1160 ret = -1;
1161 goto done;
1164 cp_diff_tree.git_cmd = 1;
1165 strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD",
1166 oid_to_hex(&info->w_tree), "--", NULL);
1167 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
1168 ret = -1;
1169 goto done;
1172 if (!out_patch->len) {
1173 if (!quiet)
1174 fprintf_ln(stderr, _("No changes selected"));
1175 ret = 1;
1178 done:
1179 discard_index(&istate);
1180 remove_path(stash_index_path.buf);
1181 return ret;
1184 static int stash_working_tree(struct stash_info *info, const struct pathspec *ps)
1186 int ret = 0;
1187 struct rev_info rev;
1188 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1189 struct strbuf diff_output = STRBUF_INIT;
1190 struct index_state istate = { NULL };
1192 init_revisions(&rev, NULL);
1193 copy_pathspec(&rev.prune_data, ps);
1195 set_alternate_index_output(stash_index_path.buf);
1196 if (reset_tree(&info->i_tree, 0, 0)) {
1197 ret = -1;
1198 goto done;
1200 set_alternate_index_output(NULL);
1202 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
1203 rev.diffopt.format_callback = add_diff_to_buf;
1204 rev.diffopt.format_callback_data = &diff_output;
1206 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1207 ret = -1;
1208 goto done;
1211 add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
1212 "");
1213 if (run_diff_index(&rev, 0)) {
1214 ret = -1;
1215 goto done;
1218 cp_upd_index.git_cmd = 1;
1219 strvec_pushl(&cp_upd_index.args, "update-index",
1220 "--ignore-skip-worktree-entries",
1221 "-z", "--add", "--remove", "--stdin", NULL);
1222 strvec_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
1223 stash_index_path.buf);
1225 if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,
1226 NULL, 0, NULL, 0)) {
1227 ret = -1;
1228 goto done;
1231 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1232 NULL)) {
1233 ret = -1;
1234 goto done;
1237 done:
1238 discard_index(&istate);
1239 UNLEAK(rev);
1240 object_array_clear(&rev.pending);
1241 clear_pathspec(&rev.prune_data);
1242 strbuf_release(&diff_output);
1243 remove_path(stash_index_path.buf);
1244 return ret;
1247 static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,
1248 int include_untracked, int patch_mode,
1249 struct stash_info *info, struct strbuf *patch,
1250 int quiet)
1252 int ret = 0;
1253 int flags = 0;
1254 int untracked_commit_option = 0;
1255 const char *head_short_sha1 = NULL;
1256 const char *branch_ref = NULL;
1257 const char *branch_name = "(no branch)";
1258 struct commit *head_commit = NULL;
1259 struct commit_list *parents = NULL;
1260 struct strbuf msg = STRBUF_INIT;
1261 struct strbuf commit_tree_label = STRBUF_INIT;
1262 struct strbuf untracked_files = STRBUF_INIT;
1264 prepare_fallback_ident("git stash", "git@stash");
1266 read_cache_preload(NULL);
1267 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0) < 0) {
1268 ret = -1;
1269 goto done;
1272 if (get_oid("HEAD", &info->b_commit)) {
1273 if (!quiet)
1274 fprintf_ln(stderr, _("You do not have "
1275 "the initial commit yet"));
1276 ret = -1;
1277 goto done;
1278 } else {
1279 head_commit = lookup_commit(the_repository, &info->b_commit);
1282 if (!check_changes(ps, include_untracked, &untracked_files)) {
1283 ret = 1;
1284 goto done;
1287 branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
1288 if (flags & REF_ISSYMREF)
1289 branch_name = strrchr(branch_ref, '/') + 1;
1290 head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
1291 DEFAULT_ABBREV);
1292 strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);
1293 pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
1295 strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
1296 commit_list_insert(head_commit, &parents);
1297 if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
1298 commit_tree(commit_tree_label.buf, commit_tree_label.len,
1299 &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1300 if (!quiet)
1301 fprintf_ln(stderr, _("Cannot save the current "
1302 "index state"));
1303 ret = -1;
1304 goto done;
1307 if (include_untracked) {
1308 if (save_untracked_files(info, &msg, untracked_files)) {
1309 if (!quiet)
1310 fprintf_ln(stderr, _("Cannot save "
1311 "the untracked files"));
1312 ret = -1;
1313 goto done;
1315 untracked_commit_option = 1;
1317 if (patch_mode) {
1318 ret = stash_patch(info, ps, patch, quiet);
1319 if (ret < 0) {
1320 if (!quiet)
1321 fprintf_ln(stderr, _("Cannot save the current "
1322 "worktree state"));
1323 goto done;
1324 } else if (ret > 0) {
1325 goto done;
1327 } else {
1328 if (stash_working_tree(info, ps)) {
1329 if (!quiet)
1330 fprintf_ln(stderr, _("Cannot save the current "
1331 "worktree state"));
1332 ret = -1;
1333 goto done;
1337 if (!stash_msg_buf->len)
1338 strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf);
1339 else
1340 strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name);
1343 * `parents` will be empty after calling `commit_tree()`, so there is
1344 * no need to call `free_commit_list()`
1346 parents = NULL;
1347 if (untracked_commit_option)
1348 commit_list_insert(lookup_commit(the_repository,
1349 &info->u_commit),
1350 &parents);
1351 commit_list_insert(lookup_commit(the_repository, &info->i_commit),
1352 &parents);
1353 commit_list_insert(head_commit, &parents);
1355 if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
1356 parents, &info->w_commit, NULL, NULL)) {
1357 if (!quiet)
1358 fprintf_ln(stderr, _("Cannot record "
1359 "working tree state"));
1360 ret = -1;
1361 goto done;
1364 done:
1365 strbuf_release(&commit_tree_label);
1366 strbuf_release(&msg);
1367 strbuf_release(&untracked_files);
1368 return ret;
1371 static int create_stash(int argc, const char **argv, const char *prefix)
1373 int ret = 0;
1374 struct strbuf stash_msg_buf = STRBUF_INIT;
1375 struct stash_info info;
1376 struct pathspec ps;
1378 /* Starting with argv[1], since argv[0] is "create" */
1379 strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
1381 memset(&ps, 0, sizeof(ps));
1382 if (!check_changes_tracked_files(&ps))
1383 return 0;
1385 ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, &info,
1386 NULL, 0);
1387 if (!ret)
1388 printf_ln("%s", oid_to_hex(&info.w_commit));
1390 strbuf_release(&stash_msg_buf);
1391 return ret;
1394 static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,
1395 int keep_index, int patch_mode, int include_untracked)
1397 int ret = 0;
1398 struct stash_info info;
1399 struct strbuf patch = STRBUF_INIT;
1400 struct strbuf stash_msg_buf = STRBUF_INIT;
1401 struct strbuf untracked_files = STRBUF_INIT;
1403 if (patch_mode && keep_index == -1)
1404 keep_index = 1;
1406 if (patch_mode && include_untracked) {
1407 fprintf_ln(stderr, _("Can't use --patch and --include-untracked"
1408 " or --all at the same time"));
1409 ret = -1;
1410 goto done;
1413 read_cache_preload(NULL);
1414 if (!include_untracked && ps->nr) {
1415 int i;
1416 char *ps_matched = xcalloc(ps->nr, 1);
1418 /* TODO: audit for interaction with sparse-index. */
1419 ensure_full_index(&the_index);
1420 for (i = 0; i < active_nr; i++)
1421 ce_path_match(&the_index, active_cache[i], ps,
1422 ps_matched);
1424 if (report_path_error(ps_matched, ps)) {
1425 fprintf_ln(stderr, _("Did you forget to 'git add'?"));
1426 ret = -1;
1427 free(ps_matched);
1428 goto done;
1430 free(ps_matched);
1433 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0)) {
1434 ret = -1;
1435 goto done;
1438 if (!check_changes(ps, include_untracked, &untracked_files)) {
1439 if (!quiet)
1440 printf_ln(_("No local changes to save"));
1441 goto done;
1444 if (!reflog_exists(ref_stash) && do_clear_stash()) {
1445 ret = -1;
1446 if (!quiet)
1447 fprintf_ln(stderr, _("Cannot initialize stash"));
1448 goto done;
1451 if (stash_msg)
1452 strbuf_addstr(&stash_msg_buf, stash_msg);
1453 if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1454 &info, &patch, quiet)) {
1455 ret = -1;
1456 goto done;
1459 if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
1460 ret = -1;
1461 if (!quiet)
1462 fprintf_ln(stderr, _("Cannot save the current status"));
1463 goto done;
1466 if (!quiet)
1467 printf_ln(_("Saved working directory and index state %s"),
1468 stash_msg_buf.buf);
1470 if (!patch_mode) {
1471 if (include_untracked && !ps->nr) {
1472 struct child_process cp = CHILD_PROCESS_INIT;
1474 cp.git_cmd = 1;
1475 strvec_pushl(&cp.args, "clean", "--force",
1476 "--quiet", "-d", NULL);
1477 if (include_untracked == INCLUDE_ALL_FILES)
1478 strvec_push(&cp.args, "-x");
1479 if (run_command(&cp)) {
1480 ret = -1;
1481 goto done;
1484 discard_cache();
1485 if (ps->nr) {
1486 struct child_process cp_add = CHILD_PROCESS_INIT;
1487 struct child_process cp_diff = CHILD_PROCESS_INIT;
1488 struct child_process cp_apply = CHILD_PROCESS_INIT;
1489 struct strbuf out = STRBUF_INIT;
1491 cp_add.git_cmd = 1;
1492 strvec_push(&cp_add.args, "add");
1493 if (!include_untracked)
1494 strvec_push(&cp_add.args, "-u");
1495 if (include_untracked == INCLUDE_ALL_FILES)
1496 strvec_push(&cp_add.args, "--force");
1497 strvec_push(&cp_add.args, "--");
1498 add_pathspecs(&cp_add.args, ps);
1499 if (run_command(&cp_add)) {
1500 ret = -1;
1501 goto done;
1504 cp_diff.git_cmd = 1;
1505 strvec_pushl(&cp_diff.args, "diff-index", "-p",
1506 "--cached", "--binary", "HEAD", "--",
1507 NULL);
1508 add_pathspecs(&cp_diff.args, ps);
1509 if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {
1510 ret = -1;
1511 goto done;
1514 cp_apply.git_cmd = 1;
1515 strvec_pushl(&cp_apply.args, "apply", "--index",
1516 "-R", NULL);
1517 if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,
1518 NULL, 0)) {
1519 ret = -1;
1520 goto done;
1522 } else {
1523 struct child_process cp = CHILD_PROCESS_INIT;
1524 cp.git_cmd = 1;
1525 strvec_pushl(&cp.args, "reset", "--hard", "-q",
1526 "--no-recurse-submodules", NULL);
1527 if (run_command(&cp)) {
1528 ret = -1;
1529 goto done;
1533 if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1534 struct child_process cp = CHILD_PROCESS_INIT;
1536 cp.git_cmd = 1;
1537 strvec_pushl(&cp.args, "checkout", "--no-overlay",
1538 oid_to_hex(&info.i_tree), "--", NULL);
1539 if (!ps->nr)
1540 strvec_push(&cp.args, ":/");
1541 else
1542 add_pathspecs(&cp.args, ps);
1543 if (run_command(&cp)) {
1544 ret = -1;
1545 goto done;
1548 goto done;
1549 } else {
1550 struct child_process cp = CHILD_PROCESS_INIT;
1552 cp.git_cmd = 1;
1553 strvec_pushl(&cp.args, "apply", "-R", NULL);
1555 if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1556 if (!quiet)
1557 fprintf_ln(stderr, _("Cannot remove "
1558 "worktree changes"));
1559 ret = -1;
1560 goto done;
1563 if (keep_index < 1) {
1564 struct child_process cp = CHILD_PROCESS_INIT;
1566 cp.git_cmd = 1;
1567 strvec_pushl(&cp.args, "reset", "-q", "--", NULL);
1568 add_pathspecs(&cp.args, ps);
1569 if (run_command(&cp)) {
1570 ret = -1;
1571 goto done;
1574 goto done;
1577 done:
1578 strbuf_release(&stash_msg_buf);
1579 return ret;
1582 static int push_stash(int argc, const char **argv, const char *prefix,
1583 int push_assumed)
1585 int force_assume = 0;
1586 int keep_index = -1;
1587 int patch_mode = 0;
1588 int include_untracked = 0;
1589 int quiet = 0;
1590 int pathspec_file_nul = 0;
1591 const char *stash_msg = NULL;
1592 const char *pathspec_from_file = NULL;
1593 struct pathspec ps;
1594 struct option options[] = {
1595 OPT_BOOL('k', "keep-index", &keep_index,
1596 N_("keep index")),
1597 OPT_BOOL('p', "patch", &patch_mode,
1598 N_("stash in patch mode")),
1599 OPT__QUIET(&quiet, N_("quiet mode")),
1600 OPT_BOOL('u', "include-untracked", &include_untracked,
1601 N_("include untracked files in stash")),
1602 OPT_SET_INT('a', "all", &include_untracked,
1603 N_("include ignore files"), 2),
1604 OPT_STRING('m', "message", &stash_msg, N_("message"),
1605 N_("stash message")),
1606 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1607 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
1608 OPT_END()
1611 if (argc) {
1612 force_assume = !strcmp(argv[0], "-p");
1613 argc = parse_options(argc, argv, prefix, options,
1614 git_stash_push_usage,
1615 PARSE_OPT_KEEP_DASHDASH);
1618 if (argc) {
1619 if (!strcmp(argv[0], "--")) {
1620 argc--;
1621 argv++;
1622 } else if (push_assumed && !force_assume) {
1623 die("subcommand wasn't specified; 'push' can't be assumed due to unexpected token '%s'",
1624 argv[0]);
1628 parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1629 prefix, argv);
1631 if (pathspec_from_file) {
1632 if (patch_mode)
1633 die(_("--pathspec-from-file is incompatible with --patch"));
1635 if (ps.nr)
1636 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
1638 parse_pathspec_file(&ps, 0,
1639 PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1640 prefix, pathspec_from_file, pathspec_file_nul);
1641 } else if (pathspec_file_nul) {
1642 die(_("--pathspec-file-nul requires --pathspec-from-file"));
1645 return do_push_stash(&ps, stash_msg, quiet, keep_index, patch_mode,
1646 include_untracked);
1649 static int save_stash(int argc, const char **argv, const char *prefix)
1651 int keep_index = -1;
1652 int patch_mode = 0;
1653 int include_untracked = 0;
1654 int quiet = 0;
1655 int ret = 0;
1656 const char *stash_msg = NULL;
1657 struct pathspec ps;
1658 struct strbuf stash_msg_buf = STRBUF_INIT;
1659 struct option options[] = {
1660 OPT_BOOL('k', "keep-index", &keep_index,
1661 N_("keep index")),
1662 OPT_BOOL('p', "patch", &patch_mode,
1663 N_("stash in patch mode")),
1664 OPT__QUIET(&quiet, N_("quiet mode")),
1665 OPT_BOOL('u', "include-untracked", &include_untracked,
1666 N_("include untracked files in stash")),
1667 OPT_SET_INT('a', "all", &include_untracked,
1668 N_("include ignore files"), 2),
1669 OPT_STRING('m', "message", &stash_msg, "message",
1670 N_("stash message")),
1671 OPT_END()
1674 argc = parse_options(argc, argv, prefix, options,
1675 git_stash_save_usage,
1676 PARSE_OPT_KEEP_DASHDASH);
1678 if (argc)
1679 stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
1681 memset(&ps, 0, sizeof(ps));
1682 ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
1683 patch_mode, include_untracked);
1685 strbuf_release(&stash_msg_buf);
1686 return ret;
1689 int cmd_stash(int argc, const char **argv, const char *prefix)
1691 pid_t pid = getpid();
1692 const char *index_file;
1693 struct strvec args = STRVEC_INIT;
1695 struct option options[] = {
1696 OPT_END()
1699 git_config(git_stash_config, NULL);
1701 if (use_legacy_stash ||
1702 !git_env_bool("GIT_TEST_STASH_USE_BUILTIN", -1))
1703 warning(_("the stash.useBuiltin support has been removed!\n"
1704 "See its entry in 'git help config' for details."));
1706 argc = parse_options(argc, argv, prefix, options, git_stash_usage,
1707 PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
1709 index_file = get_index_file();
1710 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1711 (uintmax_t)pid);
1713 if (!argc)
1714 return !!push_stash(0, NULL, prefix, 0);
1715 else if (!strcmp(argv[0], "apply"))
1716 return !!apply_stash(argc, argv, prefix);
1717 else if (!strcmp(argv[0], "clear"))
1718 return !!clear_stash(argc, argv, prefix);
1719 else if (!strcmp(argv[0], "drop"))
1720 return !!drop_stash(argc, argv, prefix);
1721 else if (!strcmp(argv[0], "pop"))
1722 return !!pop_stash(argc, argv, prefix);
1723 else if (!strcmp(argv[0], "branch"))
1724 return !!branch_stash(argc, argv, prefix);
1725 else if (!strcmp(argv[0], "list"))
1726 return !!list_stash(argc, argv, prefix);
1727 else if (!strcmp(argv[0], "show"))
1728 return !!show_stash(argc, argv, prefix);
1729 else if (!strcmp(argv[0], "store"))
1730 return !!store_stash(argc, argv, prefix);
1731 else if (!strcmp(argv[0], "create"))
1732 return !!create_stash(argc, argv, prefix);
1733 else if (!strcmp(argv[0], "push"))
1734 return !!push_stash(argc, argv, prefix, 0);
1735 else if (!strcmp(argv[0], "save"))
1736 return !!save_stash(argc, argv, prefix);
1737 else if (*argv[0] != '-')
1738 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
1739 git_stash_usage, options);
1741 /* Assume 'stash push' */
1742 strvec_push(&args, "push");
1743 strvec_pushv(&args, argv);
1744 return !!push_stash(args.nr, args.v, prefix, 1);