entry: extract a header file for entry.c functions
[git/debian.git] / builtin / stash.c
blob11f3ae303933a4c956f120fadac60bf267c78a13
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 "rerere.h"
14 #include "revision.h"
15 #include "log-tree.h"
16 #include "diffcore.h"
17 #include "exec-cmd.h"
18 #include "entry.h"
20 #define INCLUDE_ALL_FILES 2
22 static const char * const git_stash_usage[] = {
23 N_("git stash list [<options>]"),
24 N_("git stash show [<options>] [<stash>]"),
25 N_("git stash drop [-q|--quiet] [<stash>]"),
26 N_("git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
27 N_("git stash branch <branchname> [<stash>]"),
28 N_("git stash clear"),
29 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
30 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
31 " [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
32 " [--] [<pathspec>...]]"),
33 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
34 " [-u|--include-untracked] [-a|--all] [<message>]"),
35 NULL
38 static const char * const git_stash_list_usage[] = {
39 N_("git stash list [<options>]"),
40 NULL
43 static const char * const git_stash_show_usage[] = {
44 N_("git stash show [<options>] [<stash>]"),
45 NULL
48 static const char * const git_stash_drop_usage[] = {
49 N_("git stash drop [-q|--quiet] [<stash>]"),
50 NULL
53 static const char * const git_stash_pop_usage[] = {
54 N_("git stash pop [--index] [-q|--quiet] [<stash>]"),
55 NULL
58 static const char * const git_stash_apply_usage[] = {
59 N_("git stash apply [--index] [-q|--quiet] [<stash>]"),
60 NULL
63 static const char * const git_stash_branch_usage[] = {
64 N_("git stash branch <branchname> [<stash>]"),
65 NULL
68 static const char * const git_stash_clear_usage[] = {
69 N_("git stash clear"),
70 NULL
73 static const char * const git_stash_store_usage[] = {
74 N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"),
75 NULL
78 static const char * const git_stash_push_usage[] = {
79 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
80 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
81 " [--] [<pathspec>...]]"),
82 NULL
85 static const char * const git_stash_save_usage[] = {
86 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
87 " [-u|--include-untracked] [-a|--all] [<message>]"),
88 NULL
91 static const char ref_stash[] = "refs/stash";
92 static struct strbuf stash_index_path = STRBUF_INIT;
95 * w_commit is set to the commit containing the working tree
96 * b_commit is set to the base commit
97 * i_commit is set to the commit containing the index tree
98 * u_commit is set to the commit containing the untracked files tree
99 * w_tree is set to the working tree
100 * b_tree is set to the base tree
101 * i_tree is set to the index tree
102 * u_tree is set to the untracked files tree
104 struct stash_info {
105 struct object_id w_commit;
106 struct object_id b_commit;
107 struct object_id i_commit;
108 struct object_id u_commit;
109 struct object_id w_tree;
110 struct object_id b_tree;
111 struct object_id i_tree;
112 struct object_id u_tree;
113 struct strbuf revision;
114 int is_stash_ref;
115 int has_u;
118 static void free_stash_info(struct stash_info *info)
120 strbuf_release(&info->revision);
123 static void assert_stash_like(struct stash_info *info, const char *revision)
125 if (get_oidf(&info->b_commit, "%s^1", revision) ||
126 get_oidf(&info->w_tree, "%s:", revision) ||
127 get_oidf(&info->b_tree, "%s^1:", revision) ||
128 get_oidf(&info->i_tree, "%s^2:", revision))
129 die(_("'%s' is not a stash-like commit"), revision);
132 static int get_stash_info(struct stash_info *info, int argc, const char **argv)
134 int ret;
135 char *end_of_rev;
136 char *expanded_ref;
137 const char *revision;
138 const char *commit = NULL;
139 struct object_id dummy;
140 struct strbuf symbolic = STRBUF_INIT;
142 if (argc > 1) {
143 int i;
144 struct strbuf refs_msg = STRBUF_INIT;
146 for (i = 0; i < argc; i++)
147 strbuf_addf(&refs_msg, " '%s'", argv[i]);
149 fprintf_ln(stderr, _("Too many revisions specified:%s"),
150 refs_msg.buf);
151 strbuf_release(&refs_msg);
153 return -1;
156 if (argc == 1)
157 commit = argv[0];
159 strbuf_init(&info->revision, 0);
160 if (!commit) {
161 if (!ref_exists(ref_stash)) {
162 free_stash_info(info);
163 fprintf_ln(stderr, _("No stash entries found."));
164 return -1;
167 strbuf_addf(&info->revision, "%s@{0}", ref_stash);
168 } else if (strspn(commit, "0123456789") == strlen(commit)) {
169 strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
170 } else {
171 strbuf_addstr(&info->revision, commit);
174 revision = info->revision.buf;
176 if (get_oid(revision, &info->w_commit)) {
177 error(_("%s is not a valid reference"), revision);
178 free_stash_info(info);
179 return -1;
182 assert_stash_like(info, revision);
184 info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision);
186 end_of_rev = strchrnul(revision, '@');
187 strbuf_add(&symbolic, revision, end_of_rev - revision);
189 ret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref, 0);
190 strbuf_release(&symbolic);
191 switch (ret) {
192 case 0: /* Not found, but valid ref */
193 info->is_stash_ref = 0;
194 break;
195 case 1:
196 info->is_stash_ref = !strcmp(expanded_ref, ref_stash);
197 break;
198 default: /* Invalid or ambiguous */
199 free_stash_info(info);
202 free(expanded_ref);
203 return !(ret == 0 || ret == 1);
206 static int do_clear_stash(void)
208 struct object_id obj;
209 if (get_oid(ref_stash, &obj))
210 return 0;
212 return delete_ref(NULL, ref_stash, &obj, 0);
215 static int clear_stash(int argc, const char **argv, const char *prefix)
217 struct option options[] = {
218 OPT_END()
221 argc = parse_options(argc, argv, prefix, options,
222 git_stash_clear_usage,
223 PARSE_OPT_STOP_AT_NON_OPTION);
225 if (argc)
226 return error(_("git stash clear with arguments is "
227 "unimplemented"));
229 return do_clear_stash();
232 static int reset_tree(struct object_id *i_tree, int update, int reset)
234 int nr_trees = 1;
235 struct unpack_trees_options opts;
236 struct tree_desc t[MAX_UNPACK_TREES];
237 struct tree *tree;
238 struct lock_file lock_file = LOCK_INIT;
240 read_cache_preload(NULL);
241 if (refresh_cache(REFRESH_QUIET))
242 return -1;
244 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
246 memset(&opts, 0, sizeof(opts));
248 tree = parse_tree_indirect(i_tree);
249 if (parse_tree(tree))
250 return -1;
252 init_tree_desc(t, tree->buffer, tree->size);
254 opts.head_idx = 1;
255 opts.src_index = &the_index;
256 opts.dst_index = &the_index;
257 opts.merge = 1;
258 opts.reset = reset;
259 opts.update = update;
260 opts.fn = oneway_merge;
262 if (unpack_trees(nr_trees, t, &opts))
263 return -1;
265 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
266 return error(_("unable to write new index file"));
268 return 0;
271 static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
273 struct child_process cp = CHILD_PROCESS_INIT;
274 const char *w_commit_hex = oid_to_hex(w_commit);
277 * Diff-tree would not be very hard to replace with a native function,
278 * however it should be done together with apply_cached.
280 cp.git_cmd = 1;
281 strvec_pushl(&cp.args, "diff-tree", "--binary", NULL);
282 strvec_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
284 return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
287 static int apply_cached(struct strbuf *out)
289 struct child_process cp = CHILD_PROCESS_INIT;
292 * Apply currently only reads either from stdin or a file, thus
293 * apply_all_patches would have to be updated to optionally take a
294 * buffer.
296 cp.git_cmd = 1;
297 strvec_pushl(&cp.args, "apply", "--cached", NULL);
298 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
301 static int reset_head(void)
303 struct child_process cp = CHILD_PROCESS_INIT;
306 * Reset is overall quite simple, however there is no current public
307 * API for resetting.
309 cp.git_cmd = 1;
310 strvec_push(&cp.args, "reset");
312 return run_command(&cp);
315 static void add_diff_to_buf(struct diff_queue_struct *q,
316 struct diff_options *options,
317 void *data)
319 int i;
321 for (i = 0; i < q->nr; i++) {
322 strbuf_addstr(data, q->queue[i]->one->path);
324 /* NUL-terminate: will be fed to update-index -z */
325 strbuf_addch(data, '\0');
329 static int restore_untracked(struct object_id *u_tree)
331 int res;
332 struct child_process cp = CHILD_PROCESS_INIT;
335 * We need to run restore files from a given index, but without
336 * affecting the current index, so we use GIT_INDEX_FILE with
337 * run_command to fork processes that will not interfere.
339 cp.git_cmd = 1;
340 strvec_push(&cp.args, "read-tree");
341 strvec_push(&cp.args, oid_to_hex(u_tree));
342 strvec_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
343 stash_index_path.buf);
344 if (run_command(&cp)) {
345 remove_path(stash_index_path.buf);
346 return -1;
349 child_process_init(&cp);
350 cp.git_cmd = 1;
351 strvec_pushl(&cp.args, "checkout-index", "--all", NULL);
352 strvec_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
353 stash_index_path.buf);
355 res = run_command(&cp);
356 remove_path(stash_index_path.buf);
357 return res;
360 static void unstage_changes_unless_new(struct object_id *orig_tree)
363 * When we enter this function, there has been a clean merge of
364 * relevant trees, and the merge logic always stages whatever merges
365 * cleanly. We want to unstage those changes, unless it corresponds
366 * to a file that didn't exist as of orig_tree.
368 * However, if any SKIP_WORKTREE path is modified relative to
369 * orig_tree, then we want to clear the SKIP_WORKTREE bit and write
370 * it to the worktree before unstaging.
373 struct checkout state = CHECKOUT_INIT;
374 struct diff_options diff_opts;
375 struct lock_file lock = LOCK_INIT;
376 int i;
378 /* If any entries have skip_worktree set, we'll have to check 'em out */
379 state.force = 1;
380 state.quiet = 1;
381 state.refresh_cache = 1;
382 state.istate = &the_index;
385 * Step 1: get a difference between orig_tree (which corresponding
386 * to the index before a merge was run) and the current index
387 * (reflecting the changes brought in by the merge).
389 diff_setup(&diff_opts);
390 diff_opts.flags.recursive = 1;
391 diff_opts.detect_rename = 0;
392 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
393 diff_setup_done(&diff_opts);
395 do_diff_cache(orig_tree, &diff_opts);
396 diffcore_std(&diff_opts);
398 /* Iterate over the paths that changed due to the merge... */
399 for (i = 0; i < diff_queued_diff.nr; i++) {
400 struct diff_filepair *p;
401 struct cache_entry *ce;
402 int pos;
404 /* Look up the path's position in the current index. */
405 p = diff_queued_diff.queue[i];
406 pos = index_name_pos(&the_index, p->two->path,
407 strlen(p->two->path));
410 * Step 2: Place changes in the working tree
412 * Stash is about restoring changes *to the working tree*.
413 * So if the merge successfully got a new version of some
414 * path, but left it out of the working tree, then clear the
415 * SKIP_WORKTREE bit and write it to the working tree.
417 if (pos >= 0 && ce_skip_worktree(active_cache[pos])) {
418 struct stat st;
420 ce = active_cache[pos];
421 if (!lstat(ce->name, &st)) {
422 /* Conflicting path present; relocate it */
423 struct strbuf new_path = STRBUF_INIT;
424 int fd;
426 strbuf_addf(&new_path,
427 "%s.stash.XXXXXX", ce->name);
428 fd = xmkstemp(new_path.buf);
429 close(fd);
430 printf(_("WARNING: Untracked file in way of "
431 "tracked file! Renaming\n "
432 " %s -> %s\n"
433 " to make room.\n"),
434 ce->name, new_path.buf);
435 if (rename(ce->name, new_path.buf))
436 die("Failed to move %s to %s\n",
437 ce->name, new_path.buf);
438 strbuf_release(&new_path);
440 checkout_entry(ce, &state, NULL, NULL);
441 ce->ce_flags &= ~CE_SKIP_WORKTREE;
445 * Step 3: "unstage" changes, as long as they are still tracked
447 if (p->one->oid_valid) {
449 * Path existed in orig_tree; restore index entry
450 * from that tree in order to "unstage" the changes.
452 int option = ADD_CACHE_OK_TO_REPLACE;
453 if (pos < 0)
454 option = ADD_CACHE_OK_TO_ADD;
456 ce = make_cache_entry(&the_index,
457 p->one->mode,
458 &p->one->oid,
459 p->one->path,
460 0, 0);
461 add_index_entry(&the_index, ce, option);
464 diff_flush(&diff_opts);
467 * Step 4: write the new index to disk
469 repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
470 if (write_locked_index(&the_index, &lock,
471 COMMIT_LOCK | SKIP_IF_UNCHANGED))
472 die(_("Unable to write index."));
475 static int do_apply_stash(const char *prefix, struct stash_info *info,
476 int index, int quiet)
478 int ret;
479 int has_index = index;
480 struct merge_options o;
481 struct object_id c_tree;
482 struct object_id index_tree;
483 struct commit *result;
484 const struct object_id *bases[1];
486 read_cache_preload(NULL);
487 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0))
488 return -1;
490 if (write_cache_as_tree(&c_tree, 0, NULL))
491 return error(_("cannot apply a stash in the middle of a merge"));
493 if (index) {
494 if (oideq(&info->b_tree, &info->i_tree) ||
495 oideq(&c_tree, &info->i_tree)) {
496 has_index = 0;
497 } else {
498 struct strbuf out = STRBUF_INIT;
500 if (diff_tree_binary(&out, &info->w_commit)) {
501 strbuf_release(&out);
502 return error(_("could not generate diff %s^!."),
503 oid_to_hex(&info->w_commit));
506 ret = apply_cached(&out);
507 strbuf_release(&out);
508 if (ret)
509 return error(_("conflicts in index. "
510 "Try without --index."));
512 discard_cache();
513 read_cache();
514 if (write_cache_as_tree(&index_tree, 0, NULL))
515 return error(_("could not save index tree"));
517 reset_head();
518 discard_cache();
519 read_cache();
523 if (info->has_u && restore_untracked(&info->u_tree))
524 return error(_("could not restore untracked files from stash"));
526 init_merge_options(&o, the_repository);
528 o.branch1 = "Updated upstream";
529 o.branch2 = "Stashed changes";
531 if (oideq(&info->b_tree, &c_tree))
532 o.branch1 = "Version stash was based on";
534 if (quiet)
535 o.verbosity = 0;
537 if (o.verbosity >= 3)
538 printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
540 bases[0] = &info->b_tree;
542 ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, 1, bases,
543 &result);
544 if (ret) {
545 rerere(0);
547 if (index)
548 fprintf_ln(stderr, _("Index was not unstashed."));
550 return ret;
553 if (has_index) {
554 if (reset_tree(&index_tree, 0, 0))
555 return -1;
556 } else {
557 unstage_changes_unless_new(&c_tree);
560 if (!quiet) {
561 struct child_process cp = CHILD_PROCESS_INIT;
564 * Status is quite simple and could be replaced with calls to
565 * wt_status in the future, but it adds complexities which may
566 * require more tests.
568 cp.git_cmd = 1;
569 cp.dir = prefix;
570 strvec_pushf(&cp.env_array, GIT_WORK_TREE_ENVIRONMENT"=%s",
571 absolute_path(get_git_work_tree()));
572 strvec_pushf(&cp.env_array, GIT_DIR_ENVIRONMENT"=%s",
573 absolute_path(get_git_dir()));
574 strvec_push(&cp.args, "status");
575 run_command(&cp);
578 return 0;
581 static int apply_stash(int argc, const char **argv, const char *prefix)
583 int ret;
584 int quiet = 0;
585 int index = 0;
586 struct stash_info info;
587 struct option options[] = {
588 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
589 OPT_BOOL(0, "index", &index,
590 N_("attempt to recreate the index")),
591 OPT_END()
594 argc = parse_options(argc, argv, prefix, options,
595 git_stash_apply_usage, 0);
597 if (get_stash_info(&info, argc, argv))
598 return -1;
600 ret = do_apply_stash(prefix, &info, index, quiet);
601 free_stash_info(&info);
602 return ret;
605 static int reject_reflog_ent(struct object_id *ooid, struct object_id *noid,
606 const char *email, timestamp_t timestamp, int tz,
607 const char *message, void *cb_data)
609 return 1;
612 static int reflog_is_empty(const char *refname)
614 return !for_each_reflog_ent(refname, reject_reflog_ent, NULL);
617 static int do_drop_stash(struct stash_info *info, int quiet)
619 int ret;
620 struct child_process cp_reflog = CHILD_PROCESS_INIT;
623 * reflog does not provide a simple function for deleting refs. One will
624 * need to be added to avoid implementing too much reflog code here
627 cp_reflog.git_cmd = 1;
628 strvec_pushl(&cp_reflog.args, "reflog", "delete", "--updateref",
629 "--rewrite", NULL);
630 strvec_push(&cp_reflog.args, info->revision.buf);
631 ret = run_command(&cp_reflog);
632 if (!ret) {
633 if (!quiet)
634 printf_ln(_("Dropped %s (%s)"), info->revision.buf,
635 oid_to_hex(&info->w_commit));
636 } else {
637 return error(_("%s: Could not drop stash entry"),
638 info->revision.buf);
641 if (reflog_is_empty(ref_stash))
642 do_clear_stash();
644 return 0;
647 static void assert_stash_ref(struct stash_info *info)
649 if (!info->is_stash_ref) {
650 error(_("'%s' is not a stash reference"), info->revision.buf);
651 free_stash_info(info);
652 exit(1);
656 static int drop_stash(int argc, const char **argv, const char *prefix)
658 int ret;
659 int quiet = 0;
660 struct stash_info info;
661 struct option options[] = {
662 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
663 OPT_END()
666 argc = parse_options(argc, argv, prefix, options,
667 git_stash_drop_usage, 0);
669 if (get_stash_info(&info, argc, argv))
670 return -1;
672 assert_stash_ref(&info);
674 ret = do_drop_stash(&info, quiet);
675 free_stash_info(&info);
676 return ret;
679 static int pop_stash(int argc, const char **argv, const char *prefix)
681 int ret;
682 int index = 0;
683 int quiet = 0;
684 struct stash_info info;
685 struct option options[] = {
686 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
687 OPT_BOOL(0, "index", &index,
688 N_("attempt to recreate the index")),
689 OPT_END()
692 argc = parse_options(argc, argv, prefix, options,
693 git_stash_pop_usage, 0);
695 if (get_stash_info(&info, argc, argv))
696 return -1;
698 assert_stash_ref(&info);
699 if ((ret = do_apply_stash(prefix, &info, index, quiet)))
700 printf_ln(_("The stash entry is kept in case "
701 "you need it again."));
702 else
703 ret = do_drop_stash(&info, quiet);
705 free_stash_info(&info);
706 return ret;
709 static int branch_stash(int argc, const char **argv, const char *prefix)
711 int ret;
712 const char *branch = NULL;
713 struct stash_info info;
714 struct child_process cp = CHILD_PROCESS_INIT;
715 struct option options[] = {
716 OPT_END()
719 argc = parse_options(argc, argv, prefix, options,
720 git_stash_branch_usage, 0);
722 if (!argc) {
723 fprintf_ln(stderr, _("No branch name specified"));
724 return -1;
727 branch = argv[0];
729 if (get_stash_info(&info, argc - 1, argv + 1))
730 return -1;
732 cp.git_cmd = 1;
733 strvec_pushl(&cp.args, "checkout", "-b", NULL);
734 strvec_push(&cp.args, branch);
735 strvec_push(&cp.args, oid_to_hex(&info.b_commit));
736 ret = run_command(&cp);
737 if (!ret)
738 ret = do_apply_stash(prefix, &info, 1, 0);
739 if (!ret && info.is_stash_ref)
740 ret = do_drop_stash(&info, 0);
742 free_stash_info(&info);
744 return ret;
747 static int list_stash(int argc, const char **argv, const char *prefix)
749 struct child_process cp = CHILD_PROCESS_INIT;
750 struct option options[] = {
751 OPT_END()
754 argc = parse_options(argc, argv, prefix, options,
755 git_stash_list_usage,
756 PARSE_OPT_KEEP_UNKNOWN);
758 if (!ref_exists(ref_stash))
759 return 0;
761 cp.git_cmd = 1;
762 strvec_pushl(&cp.args, "log", "--format=%gd: %gs", "-g",
763 "--first-parent", "-m", NULL);
764 strvec_pushv(&cp.args, argv);
765 strvec_push(&cp.args, ref_stash);
766 strvec_push(&cp.args, "--");
767 return run_command(&cp);
770 static int show_stat = 1;
771 static int show_patch;
772 static int use_legacy_stash;
774 static int git_stash_config(const char *var, const char *value, void *cb)
776 if (!strcmp(var, "stash.showstat")) {
777 show_stat = git_config_bool(var, value);
778 return 0;
780 if (!strcmp(var, "stash.showpatch")) {
781 show_patch = git_config_bool(var, value);
782 return 0;
784 if (!strcmp(var, "stash.usebuiltin")) {
785 use_legacy_stash = !git_config_bool(var, value);
786 return 0;
788 return git_diff_basic_config(var, value, cb);
791 static int show_stash(int argc, const char **argv, const char *prefix)
793 int i;
794 int ret = 0;
795 struct stash_info info;
796 struct rev_info rev;
797 struct strvec stash_args = STRVEC_INIT;
798 struct strvec revision_args = STRVEC_INIT;
799 struct option options[] = {
800 OPT_END()
803 init_diff_ui_defaults();
804 git_config(git_diff_ui_config, NULL);
805 init_revisions(&rev, prefix);
807 strvec_push(&revision_args, argv[0]);
808 for (i = 1; i < argc; i++) {
809 if (argv[i][0] != '-')
810 strvec_push(&stash_args, argv[i]);
811 else
812 strvec_push(&revision_args, argv[i]);
815 ret = get_stash_info(&info, stash_args.nr, stash_args.v);
816 strvec_clear(&stash_args);
817 if (ret)
818 return -1;
821 * The config settings are applied only if there are not passed
822 * any options.
824 if (revision_args.nr == 1) {
825 if (show_stat)
826 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;
828 if (show_patch)
829 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
831 if (!show_stat && !show_patch) {
832 free_stash_info(&info);
833 return 0;
837 argc = setup_revisions(revision_args.nr, revision_args.v, &rev, NULL);
838 if (argc > 1) {
839 free_stash_info(&info);
840 usage_with_options(git_stash_show_usage, options);
842 if (!rev.diffopt.output_format) {
843 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
844 diff_setup_done(&rev.diffopt);
847 rev.diffopt.flags.recursive = 1;
848 setup_diff_pager(&rev.diffopt);
849 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
850 log_tree_diff_flush(&rev);
852 free_stash_info(&info);
853 return diff_result_code(&rev.diffopt, 0);
856 static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
857 int quiet)
859 if (!stash_msg)
860 stash_msg = "Created via \"git stash store\".";
862 if (update_ref(stash_msg, ref_stash, w_commit, NULL,
863 REF_FORCE_CREATE_REFLOG,
864 quiet ? UPDATE_REFS_QUIET_ON_ERR :
865 UPDATE_REFS_MSG_ON_ERR)) {
866 if (!quiet) {
867 fprintf_ln(stderr, _("Cannot update %s with %s"),
868 ref_stash, oid_to_hex(w_commit));
870 return -1;
873 return 0;
876 static int store_stash(int argc, const char **argv, const char *prefix)
878 int quiet = 0;
879 const char *stash_msg = NULL;
880 struct object_id obj;
881 struct object_context dummy;
882 struct option options[] = {
883 OPT__QUIET(&quiet, N_("be quiet")),
884 OPT_STRING('m', "message", &stash_msg, "message",
885 N_("stash message")),
886 OPT_END()
889 argc = parse_options(argc, argv, prefix, options,
890 git_stash_store_usage,
891 PARSE_OPT_KEEP_UNKNOWN);
893 if (argc != 1) {
894 if (!quiet)
895 fprintf_ln(stderr, _("\"git stash store\" requires one "
896 "<commit> argument"));
897 return -1;
900 if (get_oid_with_context(the_repository,
901 argv[0], quiet ? GET_OID_QUIETLY : 0, &obj,
902 &dummy)) {
903 if (!quiet)
904 fprintf_ln(stderr, _("Cannot update %s with %s"),
905 ref_stash, argv[0]);
906 return -1;
909 return do_store_stash(&obj, stash_msg, quiet);
912 static void add_pathspecs(struct strvec *args,
913 const struct pathspec *ps) {
914 int i;
916 for (i = 0; i < ps->nr; i++)
917 strvec_push(args, ps->items[i].original);
921 * `untracked_files` will be filled with the names of untracked files.
922 * The return value is:
924 * = 0 if there are not any untracked files
925 * > 0 if there are untracked files
927 static int get_untracked_files(const struct pathspec *ps, int include_untracked,
928 struct strbuf *untracked_files)
930 int i;
931 int found = 0;
932 struct dir_struct dir;
934 dir_init(&dir);
935 if (include_untracked != INCLUDE_ALL_FILES)
936 setup_standard_excludes(&dir);
938 fill_directory(&dir, the_repository->index, ps);
939 for (i = 0; i < dir.nr; i++) {
940 struct dir_entry *ent = dir.entries[i];
941 found++;
942 strbuf_addstr(untracked_files, ent->name);
943 /* NUL-terminate: will be fed to update-index -z */
944 strbuf_addch(untracked_files, '\0');
947 dir_clear(&dir);
948 return found;
952 * The return value of `check_changes_tracked_files()` can be:
954 * < 0 if there was an error
955 * = 0 if there are no changes.
956 * > 0 if there are changes.
958 static int check_changes_tracked_files(const struct pathspec *ps)
960 int result;
961 struct rev_info rev;
962 struct object_id dummy;
963 int ret = 0;
965 /* No initial commit. */
966 if (get_oid("HEAD", &dummy))
967 return -1;
969 if (read_cache() < 0)
970 return -1;
972 init_revisions(&rev, NULL);
973 copy_pathspec(&rev.prune_data, ps);
975 rev.diffopt.flags.quick = 1;
976 rev.diffopt.flags.ignore_submodules = 1;
977 rev.abbrev = 0;
979 add_head_to_pending(&rev);
980 diff_setup_done(&rev.diffopt);
982 result = run_diff_index(&rev, 1);
983 if (diff_result_code(&rev.diffopt, result)) {
984 ret = 1;
985 goto done;
988 object_array_clear(&rev.pending);
989 result = run_diff_files(&rev, 0);
990 if (diff_result_code(&rev.diffopt, result)) {
991 ret = 1;
992 goto done;
995 done:
996 clear_pathspec(&rev.prune_data);
997 return ret;
1001 * The function will fill `untracked_files` with the names of untracked files
1002 * It will return 1 if there were any changes and 0 if there were not.
1004 static int check_changes(const struct pathspec *ps, int include_untracked,
1005 struct strbuf *untracked_files)
1007 int ret = 0;
1008 if (check_changes_tracked_files(ps))
1009 ret = 1;
1011 if (include_untracked && get_untracked_files(ps, include_untracked,
1012 untracked_files))
1013 ret = 1;
1015 return ret;
1018 static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
1019 struct strbuf files)
1021 int ret = 0;
1022 struct strbuf untracked_msg = STRBUF_INIT;
1023 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1024 struct index_state istate = { NULL };
1026 cp_upd_index.git_cmd = 1;
1027 strvec_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
1028 "--remove", "--stdin", NULL);
1029 strvec_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
1030 stash_index_path.buf);
1032 strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
1033 if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,
1034 NULL, 0)) {
1035 ret = -1;
1036 goto done;
1039 if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
1040 NULL)) {
1041 ret = -1;
1042 goto done;
1045 if (commit_tree(untracked_msg.buf, untracked_msg.len,
1046 &info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
1047 ret = -1;
1048 goto done;
1051 done:
1052 discard_index(&istate);
1053 strbuf_release(&untracked_msg);
1054 remove_path(stash_index_path.buf);
1055 return ret;
1058 static int stash_patch(struct stash_info *info, const struct pathspec *ps,
1059 struct strbuf *out_patch, int quiet)
1061 int ret = 0;
1062 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
1063 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1064 struct index_state istate = { NULL };
1065 char *old_index_env = NULL, *old_repo_index_file;
1067 remove_path(stash_index_path.buf);
1069 cp_read_tree.git_cmd = 1;
1070 strvec_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL);
1071 strvec_pushf(&cp_read_tree.env_array, "GIT_INDEX_FILE=%s",
1072 stash_index_path.buf);
1073 if (run_command(&cp_read_tree)) {
1074 ret = -1;
1075 goto done;
1078 /* Find out what the user wants. */
1079 old_repo_index_file = the_repository->index_file;
1080 the_repository->index_file = stash_index_path.buf;
1081 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
1082 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
1084 ret = run_add_interactive(NULL, "--patch=stash", ps);
1086 the_repository->index_file = old_repo_index_file;
1087 if (old_index_env && *old_index_env)
1088 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
1089 else
1090 unsetenv(INDEX_ENVIRONMENT);
1091 FREE_AND_NULL(old_index_env);
1093 /* State of the working tree. */
1094 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1095 NULL)) {
1096 ret = -1;
1097 goto done;
1100 cp_diff_tree.git_cmd = 1;
1101 strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD",
1102 oid_to_hex(&info->w_tree), "--", NULL);
1103 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
1104 ret = -1;
1105 goto done;
1108 if (!out_patch->len) {
1109 if (!quiet)
1110 fprintf_ln(stderr, _("No changes selected"));
1111 ret = 1;
1114 done:
1115 discard_index(&istate);
1116 remove_path(stash_index_path.buf);
1117 return ret;
1120 static int stash_working_tree(struct stash_info *info, const struct pathspec *ps)
1122 int ret = 0;
1123 struct rev_info rev;
1124 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1125 struct strbuf diff_output = STRBUF_INIT;
1126 struct index_state istate = { NULL };
1128 init_revisions(&rev, NULL);
1129 copy_pathspec(&rev.prune_data, ps);
1131 set_alternate_index_output(stash_index_path.buf);
1132 if (reset_tree(&info->i_tree, 0, 0)) {
1133 ret = -1;
1134 goto done;
1136 set_alternate_index_output(NULL);
1138 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
1139 rev.diffopt.format_callback = add_diff_to_buf;
1140 rev.diffopt.format_callback_data = &diff_output;
1142 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1143 ret = -1;
1144 goto done;
1147 add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
1148 "");
1149 if (run_diff_index(&rev, 0)) {
1150 ret = -1;
1151 goto done;
1154 cp_upd_index.git_cmd = 1;
1155 strvec_pushl(&cp_upd_index.args, "update-index",
1156 "--ignore-skip-worktree-entries",
1157 "-z", "--add", "--remove", "--stdin", NULL);
1158 strvec_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
1159 stash_index_path.buf);
1161 if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,
1162 NULL, 0, NULL, 0)) {
1163 ret = -1;
1164 goto done;
1167 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1168 NULL)) {
1169 ret = -1;
1170 goto done;
1173 done:
1174 discard_index(&istate);
1175 UNLEAK(rev);
1176 object_array_clear(&rev.pending);
1177 clear_pathspec(&rev.prune_data);
1178 strbuf_release(&diff_output);
1179 remove_path(stash_index_path.buf);
1180 return ret;
1183 static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,
1184 int include_untracked, int patch_mode,
1185 struct stash_info *info, struct strbuf *patch,
1186 int quiet)
1188 int ret = 0;
1189 int flags = 0;
1190 int untracked_commit_option = 0;
1191 const char *head_short_sha1 = NULL;
1192 const char *branch_ref = NULL;
1193 const char *branch_name = "(no branch)";
1194 struct commit *head_commit = NULL;
1195 struct commit_list *parents = NULL;
1196 struct strbuf msg = STRBUF_INIT;
1197 struct strbuf commit_tree_label = STRBUF_INIT;
1198 struct strbuf untracked_files = STRBUF_INIT;
1200 prepare_fallback_ident("git stash", "git@stash");
1202 read_cache_preload(NULL);
1203 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0) < 0) {
1204 ret = -1;
1205 goto done;
1208 if (get_oid("HEAD", &info->b_commit)) {
1209 if (!quiet)
1210 fprintf_ln(stderr, _("You do not have "
1211 "the initial commit yet"));
1212 ret = -1;
1213 goto done;
1214 } else {
1215 head_commit = lookup_commit(the_repository, &info->b_commit);
1218 if (!check_changes(ps, include_untracked, &untracked_files)) {
1219 ret = 1;
1220 goto done;
1223 branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
1224 if (flags & REF_ISSYMREF)
1225 branch_name = strrchr(branch_ref, '/') + 1;
1226 head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
1227 DEFAULT_ABBREV);
1228 strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);
1229 pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
1231 strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
1232 commit_list_insert(head_commit, &parents);
1233 if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
1234 commit_tree(commit_tree_label.buf, commit_tree_label.len,
1235 &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1236 if (!quiet)
1237 fprintf_ln(stderr, _("Cannot save the current "
1238 "index state"));
1239 ret = -1;
1240 goto done;
1243 if (include_untracked) {
1244 if (save_untracked_files(info, &msg, untracked_files)) {
1245 if (!quiet)
1246 fprintf_ln(stderr, _("Cannot save "
1247 "the untracked files"));
1248 ret = -1;
1249 goto done;
1251 untracked_commit_option = 1;
1253 if (patch_mode) {
1254 ret = stash_patch(info, ps, patch, quiet);
1255 if (ret < 0) {
1256 if (!quiet)
1257 fprintf_ln(stderr, _("Cannot save the current "
1258 "worktree state"));
1259 goto done;
1260 } else if (ret > 0) {
1261 goto done;
1263 } else {
1264 if (stash_working_tree(info, ps)) {
1265 if (!quiet)
1266 fprintf_ln(stderr, _("Cannot save the current "
1267 "worktree state"));
1268 ret = -1;
1269 goto done;
1273 if (!stash_msg_buf->len)
1274 strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf);
1275 else
1276 strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name);
1279 * `parents` will be empty after calling `commit_tree()`, so there is
1280 * no need to call `free_commit_list()`
1282 parents = NULL;
1283 if (untracked_commit_option)
1284 commit_list_insert(lookup_commit(the_repository,
1285 &info->u_commit),
1286 &parents);
1287 commit_list_insert(lookup_commit(the_repository, &info->i_commit),
1288 &parents);
1289 commit_list_insert(head_commit, &parents);
1291 if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
1292 parents, &info->w_commit, NULL, NULL)) {
1293 if (!quiet)
1294 fprintf_ln(stderr, _("Cannot record "
1295 "working tree state"));
1296 ret = -1;
1297 goto done;
1300 done:
1301 strbuf_release(&commit_tree_label);
1302 strbuf_release(&msg);
1303 strbuf_release(&untracked_files);
1304 return ret;
1307 static int create_stash(int argc, const char **argv, const char *prefix)
1309 int ret = 0;
1310 struct strbuf stash_msg_buf = STRBUF_INIT;
1311 struct stash_info info;
1312 struct pathspec ps;
1314 /* Starting with argv[1], since argv[0] is "create" */
1315 strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
1317 memset(&ps, 0, sizeof(ps));
1318 if (!check_changes_tracked_files(&ps))
1319 return 0;
1321 ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, &info,
1322 NULL, 0);
1323 if (!ret)
1324 printf_ln("%s", oid_to_hex(&info.w_commit));
1326 strbuf_release(&stash_msg_buf);
1327 return ret;
1330 static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,
1331 int keep_index, int patch_mode, int include_untracked)
1333 int ret = 0;
1334 struct stash_info info;
1335 struct strbuf patch = STRBUF_INIT;
1336 struct strbuf stash_msg_buf = STRBUF_INIT;
1337 struct strbuf untracked_files = STRBUF_INIT;
1339 if (patch_mode && keep_index == -1)
1340 keep_index = 1;
1342 if (patch_mode && include_untracked) {
1343 fprintf_ln(stderr, _("Can't use --patch and --include-untracked"
1344 " or --all at the same time"));
1345 ret = -1;
1346 goto done;
1349 read_cache_preload(NULL);
1350 if (!include_untracked && ps->nr) {
1351 int i;
1352 char *ps_matched = xcalloc(ps->nr, 1);
1354 for (i = 0; i < active_nr; i++)
1355 ce_path_match(&the_index, active_cache[i], ps,
1356 ps_matched);
1358 if (report_path_error(ps_matched, ps)) {
1359 fprintf_ln(stderr, _("Did you forget to 'git add'?"));
1360 ret = -1;
1361 free(ps_matched);
1362 goto done;
1364 free(ps_matched);
1367 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0)) {
1368 ret = -1;
1369 goto done;
1372 if (!check_changes(ps, include_untracked, &untracked_files)) {
1373 if (!quiet)
1374 printf_ln(_("No local changes to save"));
1375 goto done;
1378 if (!reflog_exists(ref_stash) && do_clear_stash()) {
1379 ret = -1;
1380 if (!quiet)
1381 fprintf_ln(stderr, _("Cannot initialize stash"));
1382 goto done;
1385 if (stash_msg)
1386 strbuf_addstr(&stash_msg_buf, stash_msg);
1387 if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1388 &info, &patch, quiet)) {
1389 ret = -1;
1390 goto done;
1393 if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
1394 ret = -1;
1395 if (!quiet)
1396 fprintf_ln(stderr, _("Cannot save the current status"));
1397 goto done;
1400 if (!quiet)
1401 printf_ln(_("Saved working directory and index state %s"),
1402 stash_msg_buf.buf);
1404 if (!patch_mode) {
1405 if (include_untracked && !ps->nr) {
1406 struct child_process cp = CHILD_PROCESS_INIT;
1408 cp.git_cmd = 1;
1409 strvec_pushl(&cp.args, "clean", "--force",
1410 "--quiet", "-d", NULL);
1411 if (include_untracked == INCLUDE_ALL_FILES)
1412 strvec_push(&cp.args, "-x");
1413 if (run_command(&cp)) {
1414 ret = -1;
1415 goto done;
1418 discard_cache();
1419 if (ps->nr) {
1420 struct child_process cp_add = CHILD_PROCESS_INIT;
1421 struct child_process cp_diff = CHILD_PROCESS_INIT;
1422 struct child_process cp_apply = CHILD_PROCESS_INIT;
1423 struct strbuf out = STRBUF_INIT;
1425 cp_add.git_cmd = 1;
1426 strvec_push(&cp_add.args, "add");
1427 if (!include_untracked)
1428 strvec_push(&cp_add.args, "-u");
1429 if (include_untracked == INCLUDE_ALL_FILES)
1430 strvec_push(&cp_add.args, "--force");
1431 strvec_push(&cp_add.args, "--");
1432 add_pathspecs(&cp_add.args, ps);
1433 if (run_command(&cp_add)) {
1434 ret = -1;
1435 goto done;
1438 cp_diff.git_cmd = 1;
1439 strvec_pushl(&cp_diff.args, "diff-index", "-p",
1440 "--cached", "--binary", "HEAD", "--",
1441 NULL);
1442 add_pathspecs(&cp_diff.args, ps);
1443 if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {
1444 ret = -1;
1445 goto done;
1448 cp_apply.git_cmd = 1;
1449 strvec_pushl(&cp_apply.args, "apply", "--index",
1450 "-R", NULL);
1451 if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,
1452 NULL, 0)) {
1453 ret = -1;
1454 goto done;
1456 } else {
1457 struct child_process cp = CHILD_PROCESS_INIT;
1458 cp.git_cmd = 1;
1459 strvec_pushl(&cp.args, "reset", "--hard", "-q",
1460 "--no-recurse-submodules", NULL);
1461 if (run_command(&cp)) {
1462 ret = -1;
1463 goto done;
1467 if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1468 struct child_process cp = CHILD_PROCESS_INIT;
1470 cp.git_cmd = 1;
1471 strvec_pushl(&cp.args, "checkout", "--no-overlay",
1472 oid_to_hex(&info.i_tree), "--", NULL);
1473 if (!ps->nr)
1474 strvec_push(&cp.args, ":/");
1475 else
1476 add_pathspecs(&cp.args, ps);
1477 if (run_command(&cp)) {
1478 ret = -1;
1479 goto done;
1482 goto done;
1483 } else {
1484 struct child_process cp = CHILD_PROCESS_INIT;
1486 cp.git_cmd = 1;
1487 strvec_pushl(&cp.args, "apply", "-R", NULL);
1489 if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1490 if (!quiet)
1491 fprintf_ln(stderr, _("Cannot remove "
1492 "worktree changes"));
1493 ret = -1;
1494 goto done;
1497 if (keep_index < 1) {
1498 struct child_process cp = CHILD_PROCESS_INIT;
1500 cp.git_cmd = 1;
1501 strvec_pushl(&cp.args, "reset", "-q", "--", NULL);
1502 add_pathspecs(&cp.args, ps);
1503 if (run_command(&cp)) {
1504 ret = -1;
1505 goto done;
1508 goto done;
1511 done:
1512 strbuf_release(&stash_msg_buf);
1513 return ret;
1516 static int push_stash(int argc, const char **argv, const char *prefix,
1517 int push_assumed)
1519 int force_assume = 0;
1520 int keep_index = -1;
1521 int patch_mode = 0;
1522 int include_untracked = 0;
1523 int quiet = 0;
1524 int pathspec_file_nul = 0;
1525 const char *stash_msg = NULL;
1526 const char *pathspec_from_file = NULL;
1527 struct pathspec ps;
1528 struct option options[] = {
1529 OPT_BOOL('k', "keep-index", &keep_index,
1530 N_("keep index")),
1531 OPT_BOOL('p', "patch", &patch_mode,
1532 N_("stash in patch mode")),
1533 OPT__QUIET(&quiet, N_("quiet mode")),
1534 OPT_BOOL('u', "include-untracked", &include_untracked,
1535 N_("include untracked files in stash")),
1536 OPT_SET_INT('a', "all", &include_untracked,
1537 N_("include ignore files"), 2),
1538 OPT_STRING('m', "message", &stash_msg, N_("message"),
1539 N_("stash message")),
1540 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1541 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
1542 OPT_END()
1545 if (argc) {
1546 force_assume = !strcmp(argv[0], "-p");
1547 argc = parse_options(argc, argv, prefix, options,
1548 git_stash_push_usage,
1549 PARSE_OPT_KEEP_DASHDASH);
1552 if (argc) {
1553 if (!strcmp(argv[0], "--")) {
1554 argc--;
1555 argv++;
1556 } else if (push_assumed && !force_assume) {
1557 die("subcommand wasn't specified; 'push' can't be assumed due to unexpected token '%s'",
1558 argv[0]);
1562 parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1563 prefix, argv);
1565 if (pathspec_from_file) {
1566 if (patch_mode)
1567 die(_("--pathspec-from-file is incompatible with --patch"));
1569 if (ps.nr)
1570 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
1572 parse_pathspec_file(&ps, 0,
1573 PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1574 prefix, pathspec_from_file, pathspec_file_nul);
1575 } else if (pathspec_file_nul) {
1576 die(_("--pathspec-file-nul requires --pathspec-from-file"));
1579 return do_push_stash(&ps, stash_msg, quiet, keep_index, patch_mode,
1580 include_untracked);
1583 static int save_stash(int argc, const char **argv, const char *prefix)
1585 int keep_index = -1;
1586 int patch_mode = 0;
1587 int include_untracked = 0;
1588 int quiet = 0;
1589 int ret = 0;
1590 const char *stash_msg = NULL;
1591 struct pathspec ps;
1592 struct strbuf stash_msg_buf = STRBUF_INIT;
1593 struct option options[] = {
1594 OPT_BOOL('k', "keep-index", &keep_index,
1595 N_("keep index")),
1596 OPT_BOOL('p', "patch", &patch_mode,
1597 N_("stash in patch mode")),
1598 OPT__QUIET(&quiet, N_("quiet mode")),
1599 OPT_BOOL('u', "include-untracked", &include_untracked,
1600 N_("include untracked files in stash")),
1601 OPT_SET_INT('a', "all", &include_untracked,
1602 N_("include ignore files"), 2),
1603 OPT_STRING('m', "message", &stash_msg, "message",
1604 N_("stash message")),
1605 OPT_END()
1608 argc = parse_options(argc, argv, prefix, options,
1609 git_stash_save_usage,
1610 PARSE_OPT_KEEP_DASHDASH);
1612 if (argc)
1613 stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
1615 memset(&ps, 0, sizeof(ps));
1616 ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
1617 patch_mode, include_untracked);
1619 strbuf_release(&stash_msg_buf);
1620 return ret;
1623 int cmd_stash(int argc, const char **argv, const char *prefix)
1625 pid_t pid = getpid();
1626 const char *index_file;
1627 struct strvec args = STRVEC_INIT;
1629 struct option options[] = {
1630 OPT_END()
1633 git_config(git_stash_config, NULL);
1635 if (use_legacy_stash ||
1636 !git_env_bool("GIT_TEST_STASH_USE_BUILTIN", -1))
1637 warning(_("the stash.useBuiltin support has been removed!\n"
1638 "See its entry in 'git help config' for details."));
1640 argc = parse_options(argc, argv, prefix, options, git_stash_usage,
1641 PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
1643 index_file = get_index_file();
1644 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1645 (uintmax_t)pid);
1647 if (!argc)
1648 return !!push_stash(0, NULL, prefix, 0);
1649 else if (!strcmp(argv[0], "apply"))
1650 return !!apply_stash(argc, argv, prefix);
1651 else if (!strcmp(argv[0], "clear"))
1652 return !!clear_stash(argc, argv, prefix);
1653 else if (!strcmp(argv[0], "drop"))
1654 return !!drop_stash(argc, argv, prefix);
1655 else if (!strcmp(argv[0], "pop"))
1656 return !!pop_stash(argc, argv, prefix);
1657 else if (!strcmp(argv[0], "branch"))
1658 return !!branch_stash(argc, argv, prefix);
1659 else if (!strcmp(argv[0], "list"))
1660 return !!list_stash(argc, argv, prefix);
1661 else if (!strcmp(argv[0], "show"))
1662 return !!show_stash(argc, argv, prefix);
1663 else if (!strcmp(argv[0], "store"))
1664 return !!store_stash(argc, argv, prefix);
1665 else if (!strcmp(argv[0], "create"))
1666 return !!create_stash(argc, argv, prefix);
1667 else if (!strcmp(argv[0], "push"))
1668 return !!push_stash(argc, argv, prefix, 0);
1669 else if (!strcmp(argv[0], "save"))
1670 return !!save_stash(argc, argv, prefix);
1671 else if (*argv[0] != '-')
1672 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
1673 git_stash_usage, options);
1675 /* Assume 'stash push' */
1676 strvec_push(&args, "push");
1677 strvec_pushv(&args, argv);
1678 return !!push_stash(args.nr, args.v, prefix, 1);