3 #include "parse-options.h"
6 #include "cache-tree.h"
7 #include "unpack-trees.h"
8 #include "merge-recursive.h"
9 #include "argv-array.h"
10 #include "run-command.h"
18 #define INCLUDE_ALL_FILES 2
20 static const char * const git_stash_usage[] = {
21 N_("git stash list [<options>]"),
22 N_("git stash show [<options>] [<stash>]"),
23 N_("git stash drop [-q|--quiet] [<stash>]"),
24 N_("git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
25 N_("git stash branch <branchname> [<stash>]"),
26 N_("git stash clear"),
27 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
28 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
29 " [--] [<pathspec>...]]"),
30 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
31 " [-u|--include-untracked] [-a|--all] [<message>]"),
35 static const char * const git_stash_list_usage[] = {
36 N_("git stash list [<options>]"),
40 static const char * const git_stash_show_usage[] = {
41 N_("git stash show [<options>] [<stash>]"),
45 static const char * const git_stash_drop_usage[] = {
46 N_("git stash drop [-q|--quiet] [<stash>]"),
50 static const char * const git_stash_pop_usage[] = {
51 N_("git stash pop [--index] [-q|--quiet] [<stash>]"),
55 static const char * const git_stash_apply_usage[] = {
56 N_("git stash apply [--index] [-q|--quiet] [<stash>]"),
60 static const char * const git_stash_branch_usage[] = {
61 N_("git stash branch <branchname> [<stash>]"),
65 static const char * const git_stash_clear_usage[] = {
66 N_("git stash clear"),
70 static const char * const git_stash_store_usage[] = {
71 N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"),
75 static const char * const git_stash_push_usage[] = {
76 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
77 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
78 " [--] [<pathspec>...]]"),
82 static const char * const git_stash_save_usage[] = {
83 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
84 " [-u|--include-untracked] [-a|--all] [<message>]"),
88 static const char *ref_stash = "refs/stash";
89 static struct strbuf stash_index_path = STRBUF_INIT;
92 * w_commit is set to the commit containing the working tree
93 * b_commit is set to the base commit
94 * i_commit is set to the commit containing the index tree
95 * u_commit is set to the commit containing the untracked files tree
96 * w_tree is set to the working tree
97 * b_tree is set to the base tree
98 * i_tree is set to the index tree
99 * u_tree is set to the untracked files tree
102 struct object_id w_commit;
103 struct object_id b_commit;
104 struct object_id i_commit;
105 struct object_id u_commit;
106 struct object_id w_tree;
107 struct object_id b_tree;
108 struct object_id i_tree;
109 struct object_id u_tree;
110 struct strbuf revision;
115 static void free_stash_info(struct stash_info *info)
117 strbuf_release(&info->revision);
120 static void assert_stash_like(struct stash_info *info, const char *revision)
122 if (get_oidf(&info->b_commit, "%s^1", revision) ||
123 get_oidf(&info->w_tree, "%s:", revision) ||
124 get_oidf(&info->b_tree, "%s^1:", revision) ||
125 get_oidf(&info->i_tree, "%s^2:", revision))
126 die(_("'%s' is not a stash-like commit"), revision);
129 static int get_stash_info(struct stash_info *info, int argc, const char **argv)
134 const char *revision;
135 const char *commit = NULL;
136 struct object_id dummy;
137 struct strbuf symbolic = STRBUF_INIT;
141 struct strbuf refs_msg = STRBUF_INIT;
143 for (i = 0; i < argc; i++)
144 strbuf_addf(&refs_msg, " '%s'", argv[i]);
146 fprintf_ln(stderr, _("Too many revisions specified:%s"),
148 strbuf_release(&refs_msg);
156 strbuf_init(&info->revision, 0);
158 if (!ref_exists(ref_stash)) {
159 free_stash_info(info);
160 fprintf_ln(stderr, _("No stash entries found."));
164 strbuf_addf(&info->revision, "%s@{0}", ref_stash);
165 } else if (strspn(commit, "0123456789") == strlen(commit)) {
166 strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
168 strbuf_addstr(&info->revision, commit);
171 revision = info->revision.buf;
173 if (get_oid(revision, &info->w_commit)) {
174 error(_("%s is not a valid reference"), revision);
175 free_stash_info(info);
179 assert_stash_like(info, revision);
181 info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision);
183 end_of_rev = strchrnul(revision, '@');
184 strbuf_add(&symbolic, revision, end_of_rev - revision);
186 ret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref);
187 strbuf_release(&symbolic);
189 case 0: /* Not found, but valid ref */
190 info->is_stash_ref = 0;
193 info->is_stash_ref = !strcmp(expanded_ref, ref_stash);
195 default: /* Invalid or ambiguous */
196 free_stash_info(info);
200 return !(ret == 0 || ret == 1);
203 static int do_clear_stash(void)
205 struct object_id obj;
206 if (get_oid(ref_stash, &obj))
209 return delete_ref(NULL, ref_stash, &obj, 0);
212 static int clear_stash(int argc, const char **argv, const char *prefix)
214 struct option options[] = {
218 argc = parse_options(argc, argv, prefix, options,
219 git_stash_clear_usage,
220 PARSE_OPT_STOP_AT_NON_OPTION);
223 return error(_("git stash clear with parameters is "
226 return do_clear_stash();
229 static int reset_tree(struct object_id *i_tree, int update, int reset)
232 struct unpack_trees_options opts;
233 struct tree_desc t[MAX_UNPACK_TREES];
235 struct lock_file lock_file = LOCK_INIT;
237 read_cache_preload(NULL);
238 if (refresh_cache(REFRESH_QUIET))
241 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
243 memset(&opts, 0, sizeof(opts));
245 tree = parse_tree_indirect(i_tree);
246 if (parse_tree(tree))
249 init_tree_desc(t, tree->buffer, tree->size);
252 opts.src_index = &the_index;
253 opts.dst_index = &the_index;
256 opts.update = update;
257 opts.fn = oneway_merge;
259 if (unpack_trees(nr_trees, t, &opts))
262 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
263 return error(_("unable to write new index file"));
268 static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
270 struct child_process cp = CHILD_PROCESS_INIT;
271 const char *w_commit_hex = oid_to_hex(w_commit);
274 * Diff-tree would not be very hard to replace with a native function,
275 * however it should be done together with apply_cached.
278 argv_array_pushl(&cp.args, "diff-tree", "--binary", NULL);
279 argv_array_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
281 return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
284 static int apply_cached(struct strbuf *out)
286 struct child_process cp = CHILD_PROCESS_INIT;
289 * Apply currently only reads either from stdin or a file, thus
290 * apply_all_patches would have to be updated to optionally take a
294 argv_array_pushl(&cp.args, "apply", "--cached", NULL);
295 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
298 static int reset_head(void)
300 struct child_process cp = CHILD_PROCESS_INIT;
303 * Reset is overall quite simple, however there is no current public
307 argv_array_push(&cp.args, "reset");
309 return run_command(&cp);
312 static void add_diff_to_buf(struct diff_queue_struct *q,
313 struct diff_options *options,
318 for (i = 0; i < q->nr; i++) {
319 strbuf_addstr(data, q->queue[i]->one->path);
321 /* NUL-terminate: will be fed to update-index -z */
322 strbuf_addch(data, '\0');
326 static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)
328 struct child_process cp = CHILD_PROCESS_INIT;
329 const char *c_tree_hex = oid_to_hex(c_tree);
332 * diff-index is very similar to diff-tree above, and should be
333 * converted together with update_index.
336 argv_array_pushl(&cp.args, "diff-index", "--cached", "--name-only",
337 "--diff-filter=A", NULL);
338 argv_array_push(&cp.args, c_tree_hex);
339 return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
342 static int update_index(struct strbuf *out)
344 struct child_process cp = CHILD_PROCESS_INIT;
347 * Update-index is very complicated and may need to have a public
348 * function exposed in order to remove this forking.
351 argv_array_pushl(&cp.args, "update-index", "--add", "--stdin", NULL);
352 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
355 static int restore_untracked(struct object_id *u_tree)
358 struct child_process cp = CHILD_PROCESS_INIT;
361 * We need to run restore files from a given index, but without
362 * affecting the current index, so we use GIT_INDEX_FILE with
363 * run_command to fork processes that will not interfere.
366 argv_array_push(&cp.args, "read-tree");
367 argv_array_push(&cp.args, oid_to_hex(u_tree));
368 argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
369 stash_index_path.buf);
370 if (run_command(&cp)) {
371 remove_path(stash_index_path.buf);
375 child_process_init(&cp);
377 argv_array_pushl(&cp.args, "checkout-index", "--all", NULL);
378 argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
379 stash_index_path.buf);
381 res = run_command(&cp);
382 remove_path(stash_index_path.buf);
386 static int do_apply_stash(const char *prefix, struct stash_info *info,
387 int index, int quiet)
390 int has_index = index;
391 struct merge_options o;
392 struct object_id c_tree;
393 struct object_id index_tree;
394 struct commit *result;
395 const struct object_id *bases[1];
397 read_cache_preload(NULL);
398 if (refresh_cache(REFRESH_QUIET))
401 if (write_cache_as_tree(&c_tree, 0, NULL))
402 return error(_("cannot apply a stash in the middle of a merge"));
405 if (oideq(&info->b_tree, &info->i_tree) ||
406 oideq(&c_tree, &info->i_tree)) {
409 struct strbuf out = STRBUF_INIT;
411 if (diff_tree_binary(&out, &info->w_commit)) {
412 strbuf_release(&out);
413 return error(_("could not generate diff %s^!."),
414 oid_to_hex(&info->w_commit));
417 ret = apply_cached(&out);
418 strbuf_release(&out);
420 return error(_("conflicts in index."
421 "Try without --index."));
425 if (write_cache_as_tree(&index_tree, 0, NULL))
426 return error(_("could not save index tree"));
432 if (info->has_u && restore_untracked(&info->u_tree))
433 return error(_("could not restore untracked files from stash"));
435 init_merge_options(&o);
437 o.branch1 = "Updated upstream";
438 o.branch2 = "Stashed changes";
440 if (oideq(&info->b_tree, &c_tree))
441 o.branch1 = "Version stash was based on";
446 if (o.verbosity >= 3)
447 printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
449 bases[0] = &info->b_tree;
451 ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, 1, bases,
457 fprintf_ln(stderr, _("Index was not unstashed."));
463 if (reset_tree(&index_tree, 0, 0))
466 struct strbuf out = STRBUF_INIT;
468 if (get_newly_staged(&out, &c_tree)) {
469 strbuf_release(&out);
473 if (reset_tree(&c_tree, 0, 1)) {
474 strbuf_release(&out);
478 ret = update_index(&out);
479 strbuf_release(&out);
487 if (refresh_cache(REFRESH_QUIET))
488 warning("could not refresh index");
490 struct child_process cp = CHILD_PROCESS_INIT;
493 * Status is quite simple and could be replaced with calls to
494 * wt_status in the future, but it adds complexities which may
495 * require more tests.
499 argv_array_push(&cp.args, "status");
506 static int apply_stash(int argc, const char **argv, const char *prefix)
511 struct stash_info info;
512 struct option options[] = {
513 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
514 OPT_BOOL(0, "index", &index,
515 N_("attempt to recreate the index")),
519 argc = parse_options(argc, argv, prefix, options,
520 git_stash_apply_usage, 0);
522 if (get_stash_info(&info, argc, argv))
525 ret = do_apply_stash(prefix, &info, index, quiet);
526 free_stash_info(&info);
530 static int do_drop_stash(const char *prefix, struct stash_info *info, int quiet)
533 struct child_process cp_reflog = CHILD_PROCESS_INIT;
534 struct child_process cp = CHILD_PROCESS_INIT;
537 * reflog does not provide a simple function for deleting refs. One will
538 * need to be added to avoid implementing too much reflog code here
541 cp_reflog.git_cmd = 1;
542 argv_array_pushl(&cp_reflog.args, "reflog", "delete", "--updateref",
544 argv_array_push(&cp_reflog.args, info->revision.buf);
545 ret = run_command(&cp_reflog);
548 printf_ln(_("Dropped %s (%s)"), info->revision.buf,
549 oid_to_hex(&info->w_commit));
551 return error(_("%s: Could not drop stash entry"),
556 * This could easily be replaced by get_oid, but currently it will throw
557 * a fatal error when a reflog is empty, which we can not recover from.
560 /* Even though --quiet is specified, rev-parse still outputs the hash */
562 argv_array_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL);
563 argv_array_pushf(&cp.args, "%s@{0}", ref_stash);
564 ret = run_command(&cp);
566 /* do_clear_stash if we just dropped the last stash entry */
573 static void assert_stash_ref(struct stash_info *info)
575 if (!info->is_stash_ref) {
576 error(_("'%s' is not a stash reference"), info->revision.buf);
577 free_stash_info(info);
582 static int drop_stash(int argc, const char **argv, const char *prefix)
586 struct stash_info info;
587 struct option options[] = {
588 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
592 argc = parse_options(argc, argv, prefix, options,
593 git_stash_drop_usage, 0);
595 if (get_stash_info(&info, argc, argv))
598 assert_stash_ref(&info);
600 ret = do_drop_stash(prefix, &info, quiet);
601 free_stash_info(&info);
605 static int pop_stash(int argc, const char **argv, const char *prefix)
610 struct stash_info info;
611 struct option options[] = {
612 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
613 OPT_BOOL(0, "index", &index,
614 N_("attempt to recreate the index")),
618 argc = parse_options(argc, argv, prefix, options,
619 git_stash_pop_usage, 0);
621 if (get_stash_info(&info, argc, argv))
624 assert_stash_ref(&info);
625 if ((ret = do_apply_stash(prefix, &info, index, quiet)))
626 printf_ln(_("The stash entry is kept in case "
627 "you need it again."));
629 ret = do_drop_stash(prefix, &info, quiet);
631 free_stash_info(&info);
635 static int branch_stash(int argc, const char **argv, const char *prefix)
638 const char *branch = NULL;
639 struct stash_info info;
640 struct child_process cp = CHILD_PROCESS_INIT;
641 struct option options[] = {
645 argc = parse_options(argc, argv, prefix, options,
646 git_stash_branch_usage, 0);
649 fprintf_ln(stderr, _("No branch name specified"));
655 if (get_stash_info(&info, argc - 1, argv + 1))
659 argv_array_pushl(&cp.args, "checkout", "-b", NULL);
660 argv_array_push(&cp.args, branch);
661 argv_array_push(&cp.args, oid_to_hex(&info.b_commit));
662 ret = run_command(&cp);
664 ret = do_apply_stash(prefix, &info, 1, 0);
665 if (!ret && info.is_stash_ref)
666 ret = do_drop_stash(prefix, &info, 0);
668 free_stash_info(&info);
673 static int list_stash(int argc, const char **argv, const char *prefix)
675 struct child_process cp = CHILD_PROCESS_INIT;
676 struct option options[] = {
680 argc = parse_options(argc, argv, prefix, options,
681 git_stash_list_usage,
682 PARSE_OPT_KEEP_UNKNOWN);
684 if (!ref_exists(ref_stash))
688 argv_array_pushl(&cp.args, "log", "--format=%gd: %gs", "-g",
689 "--first-parent", "-m", NULL);
690 argv_array_pushv(&cp.args, argv);
691 argv_array_push(&cp.args, ref_stash);
692 argv_array_push(&cp.args, "--");
693 return run_command(&cp);
696 static int show_stat = 1;
697 static int show_patch;
699 static int git_stash_config(const char *var, const char *value, void *cb)
701 if (!strcmp(var, "stash.showstat")) {
702 show_stat = git_config_bool(var, value);
705 if (!strcmp(var, "stash.showpatch")) {
706 show_patch = git_config_bool(var, value);
709 return git_default_config(var, value, cb);
712 static int show_stash(int argc, const char **argv, const char *prefix)
717 struct stash_info info;
719 struct argv_array stash_args = ARGV_ARRAY_INIT;
720 struct option options[] = {
724 init_diff_ui_defaults();
725 git_config(git_diff_ui_config, NULL);
726 init_revisions(&rev, prefix);
728 for (i = 1; i < argc; i++) {
729 if (argv[i][0] != '-')
730 argv_array_push(&stash_args, argv[i]);
735 ret = get_stash_info(&info, stash_args.argc, stash_args.argv);
736 argv_array_clear(&stash_args);
741 * The config settings are applied only if there are not passed
745 git_config(git_stash_config, NULL);
747 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;
750 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
752 if (!show_stat && !show_patch) {
753 free_stash_info(&info);
758 argc = setup_revisions(argc, argv, &rev, NULL);
760 free_stash_info(&info);
761 usage_with_options(git_stash_show_usage, options);
763 if (!rev.diffopt.output_format) {
764 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
765 diff_setup_done(&rev.diffopt);
768 rev.diffopt.flags.recursive = 1;
769 setup_diff_pager(&rev.diffopt);
770 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
771 log_tree_diff_flush(&rev);
773 free_stash_info(&info);
774 return diff_result_code(&rev.diffopt, 0);
777 static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
781 stash_msg = "Created via \"git stash store\".";
783 if (update_ref(stash_msg, ref_stash, w_commit, NULL,
784 REF_FORCE_CREATE_REFLOG,
785 quiet ? UPDATE_REFS_QUIET_ON_ERR :
786 UPDATE_REFS_MSG_ON_ERR)) {
788 fprintf_ln(stderr, _("Cannot update %s with %s"),
789 ref_stash, oid_to_hex(w_commit));
797 static int store_stash(int argc, const char **argv, const char *prefix)
800 const char *stash_msg = NULL;
801 struct object_id obj;
802 struct object_context dummy;
803 struct option options[] = {
804 OPT__QUIET(&quiet, N_("be quiet")),
805 OPT_STRING('m', "message", &stash_msg, "message",
806 N_("stash message")),
810 argc = parse_options(argc, argv, prefix, options,
811 git_stash_store_usage,
812 PARSE_OPT_KEEP_UNKNOWN);
816 fprintf_ln(stderr, _("\"git stash store\" requires one "
817 "<commit> argument"));
821 if (get_oid_with_context(argv[0], quiet ? GET_OID_QUIETLY : 0, &obj,
824 fprintf_ln(stderr, _("Cannot update %s with %s"),
829 return do_store_stash(&obj, stash_msg, quiet);
832 static void add_pathspecs(struct argv_array *args,
833 struct pathspec ps) {
836 for (i = 0; i < ps.nr; i++)
837 argv_array_push(args, ps.items[i].match);
841 * `untracked_files` will be filled with the names of untracked files.
842 * The return value is:
844 * = 0 if there are not any untracked files
845 * > 0 if there are untracked files
847 static int get_untracked_files(struct pathspec ps, int include_untracked,
848 struct strbuf *untracked_files)
854 struct dir_struct dir;
856 memset(&dir, 0, sizeof(dir));
857 if (include_untracked != INCLUDE_ALL_FILES)
858 setup_standard_excludes(&dir);
860 seen = xcalloc(ps.nr, 1);
862 max_len = fill_directory(&dir, the_repository->index, &ps);
863 for (i = 0; i < dir.nr; i++) {
864 struct dir_entry *ent = dir.entries[i];
865 if (dir_path_match(&the_index, ent, &ps, max_len, seen)) {
867 strbuf_addstr(untracked_files, ent->name);
868 /* NUL-terminate: will be fed to update-index -z */
869 strbuf_addch(untracked_files, '\0');
877 clear_directory(&dir);
882 * The return value of `check_changes_tracked_files()` can be:
884 * < 0 if there was an error
885 * = 0 if there are no changes.
886 * > 0 if there are changes.
888 static int check_changes_tracked_files(struct pathspec ps)
892 struct object_id dummy;
894 /* No initial commit. */
895 if (get_oid("HEAD", &dummy))
898 if (read_cache() < 0)
901 init_revisions(&rev, NULL);
904 rev.diffopt.flags.quick = 1;
905 rev.diffopt.flags.ignore_submodules = 1;
908 add_head_to_pending(&rev);
909 diff_setup_done(&rev.diffopt);
911 result = run_diff_index(&rev, 1);
912 if (diff_result_code(&rev.diffopt, result))
915 object_array_clear(&rev.pending);
916 result = run_diff_files(&rev, 0);
917 if (diff_result_code(&rev.diffopt, result))
924 * The function will fill `untracked_files` with the names of untracked files
925 * It will return 1 if there were any changes and 0 if there were not.
927 static int check_changes(struct pathspec ps, int include_untracked,
928 struct strbuf *untracked_files)
931 if (check_changes_tracked_files(ps))
934 if (include_untracked && get_untracked_files(ps, include_untracked,
941 static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
945 struct strbuf untracked_msg = STRBUF_INIT;
946 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
947 struct index_state istate = { NULL };
949 cp_upd_index.git_cmd = 1;
950 argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
951 "--remove", "--stdin", NULL);
952 argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
953 stash_index_path.buf);
955 strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
956 if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,
962 if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
968 if (commit_tree(untracked_msg.buf, untracked_msg.len,
969 &info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
975 discard_index(&istate);
976 strbuf_release(&untracked_msg);
977 remove_path(stash_index_path.buf);
981 static int stash_patch(struct stash_info *info, struct pathspec ps,
982 struct strbuf *out_patch, int quiet)
985 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
986 struct child_process cp_add_i = CHILD_PROCESS_INIT;
987 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
988 struct index_state istate = { NULL };
990 remove_path(stash_index_path.buf);
992 cp_read_tree.git_cmd = 1;
993 argv_array_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL);
994 argv_array_pushf(&cp_read_tree.env_array, "GIT_INDEX_FILE=%s",
995 stash_index_path.buf);
996 if (run_command(&cp_read_tree)) {
1001 /* Find out what the user wants. */
1002 cp_add_i.git_cmd = 1;
1003 argv_array_pushl(&cp_add_i.args, "add--interactive", "--patch=stash",
1005 add_pathspecs(&cp_add_i.args, ps);
1006 argv_array_pushf(&cp_add_i.env_array, "GIT_INDEX_FILE=%s",
1007 stash_index_path.buf);
1008 if (run_command(&cp_add_i)) {
1013 /* State of the working tree. */
1014 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1020 cp_diff_tree.git_cmd = 1;
1021 argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD",
1022 oid_to_hex(&info->w_tree), "--", NULL);
1023 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
1028 if (!out_patch->len) {
1030 fprintf_ln(stderr, _("No changes selected"));
1035 discard_index(&istate);
1036 remove_path(stash_index_path.buf);
1040 static int stash_working_tree(struct stash_info *info, struct pathspec ps)
1043 struct rev_info rev;
1044 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1045 struct strbuf diff_output = STRBUF_INIT;
1046 struct index_state istate = { NULL };
1048 init_revisions(&rev, NULL);
1050 set_alternate_index_output(stash_index_path.buf);
1051 if (reset_tree(&info->i_tree, 0, 0)) {
1055 set_alternate_index_output(NULL);
1057 rev.prune_data = ps;
1058 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
1059 rev.diffopt.format_callback = add_diff_to_buf;
1060 rev.diffopt.format_callback_data = &diff_output;
1062 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1067 add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
1069 if (run_diff_index(&rev, 0)) {
1074 cp_upd_index.git_cmd = 1;
1075 argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
1076 "--remove", "--stdin", NULL);
1077 argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
1078 stash_index_path.buf);
1080 if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,
1081 NULL, 0, NULL, 0)) {
1086 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1093 discard_index(&istate);
1095 object_array_clear(&rev.pending);
1096 strbuf_release(&diff_output);
1097 remove_path(stash_index_path.buf);
1101 static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
1102 int include_untracked, int patch_mode,
1103 struct stash_info *info, struct strbuf *patch,
1108 int untracked_commit_option = 0;
1109 const char *head_short_sha1 = NULL;
1110 const char *branch_ref = NULL;
1111 const char *branch_name = "(no branch)";
1112 struct commit *head_commit = NULL;
1113 struct commit_list *parents = NULL;
1114 struct strbuf msg = STRBUF_INIT;
1115 struct strbuf commit_tree_label = STRBUF_INIT;
1116 struct strbuf untracked_files = STRBUF_INIT;
1118 prepare_fallback_ident("git stash", "git@stash");
1120 read_cache_preload(NULL);
1121 refresh_cache(REFRESH_QUIET);
1123 if (get_oid("HEAD", &info->b_commit)) {
1125 fprintf_ln(stderr, _("You do not have "
1126 "the initial commit yet"));
1130 head_commit = lookup_commit(the_repository, &info->b_commit);
1133 if (!check_changes(ps, include_untracked, &untracked_files)) {
1138 branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
1139 if (flags & REF_ISSYMREF)
1140 branch_name = strrchr(branch_ref, '/') + 1;
1141 head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
1143 strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);
1144 pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
1146 strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
1147 commit_list_insert(head_commit, &parents);
1148 if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
1149 commit_tree(commit_tree_label.buf, commit_tree_label.len,
1150 &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1152 fprintf_ln(stderr, _("Cannot save the current "
1158 if (include_untracked) {
1159 if (save_untracked_files(info, &msg, untracked_files)) {
1161 fprintf_ln(stderr, _("Cannot save "
1162 "the untracked files"));
1166 untracked_commit_option = 1;
1169 ret = stash_patch(info, ps, patch, quiet);
1172 fprintf_ln(stderr, _("Cannot save the current "
1175 } else if (ret > 0) {
1179 if (stash_working_tree(info, ps)) {
1181 fprintf_ln(stderr, _("Cannot save the current "
1188 if (!stash_msg_buf->len)
1189 strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf);
1191 strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name);
1194 * `parents` will be empty after calling `commit_tree()`, so there is
1195 * no need to call `free_commit_list()`
1198 if (untracked_commit_option)
1199 commit_list_insert(lookup_commit(the_repository,
1202 commit_list_insert(lookup_commit(the_repository, &info->i_commit),
1204 commit_list_insert(head_commit, &parents);
1206 if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
1207 parents, &info->w_commit, NULL, NULL)) {
1209 fprintf_ln(stderr, _("Cannot record "
1210 "working tree state"));
1216 strbuf_release(&commit_tree_label);
1217 strbuf_release(&msg);
1218 strbuf_release(&untracked_files);
1222 static int create_stash(int argc, const char **argv, const char *prefix)
1225 struct strbuf stash_msg_buf = STRBUF_INIT;
1226 struct stash_info info;
1229 /* Starting with argv[1], since argv[0] is "create" */
1230 strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
1232 memset(&ps, 0, sizeof(ps));
1233 if (!check_changes_tracked_files(ps))
1236 ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info,
1239 printf_ln("%s", oid_to_hex(&info.w_commit));
1241 strbuf_release(&stash_msg_buf);
1245 static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
1246 int keep_index, int patch_mode, int include_untracked)
1249 struct stash_info info;
1250 struct strbuf patch = STRBUF_INIT;
1251 struct strbuf stash_msg_buf = STRBUF_INIT;
1252 struct strbuf untracked_files = STRBUF_INIT;
1254 if (patch_mode && keep_index == -1)
1257 if (patch_mode && include_untracked) {
1258 fprintf_ln(stderr, _("Can't use --patch and --include-untracked"
1259 " or --all at the same time"));
1264 read_cache_preload(NULL);
1265 if (!include_untracked && ps.nr) {
1267 char *ps_matched = xcalloc(ps.nr, 1);
1269 for (i = 0; i < active_nr; i++)
1270 ce_path_match(&the_index, active_cache[i], &ps,
1273 if (report_path_error(ps_matched, &ps, NULL)) {
1274 fprintf_ln(stderr, _("Did you forget to 'git add'?"));
1282 if (refresh_cache(REFRESH_QUIET)) {
1287 if (!check_changes(ps, include_untracked, &untracked_files)) {
1289 printf_ln(_("No local changes to save"));
1293 if (!reflog_exists(ref_stash) && do_clear_stash()) {
1296 fprintf_ln(stderr, _("Cannot initialize stash"));
1301 strbuf_addstr(&stash_msg_buf, stash_msg);
1302 if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1303 &info, &patch, quiet)) {
1308 if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
1311 fprintf_ln(stderr, _("Cannot save the current status"));
1316 printf_ln(_("Saved working directory and index state %s"),
1320 if (include_untracked && !ps.nr) {
1321 struct child_process cp = CHILD_PROCESS_INIT;
1324 argv_array_pushl(&cp.args, "clean", "--force",
1325 "--quiet", "-d", NULL);
1326 if (include_untracked == INCLUDE_ALL_FILES)
1327 argv_array_push(&cp.args, "-x");
1328 if (run_command(&cp)) {
1335 struct child_process cp_add = CHILD_PROCESS_INIT;
1336 struct child_process cp_diff = CHILD_PROCESS_INIT;
1337 struct child_process cp_apply = CHILD_PROCESS_INIT;
1338 struct strbuf out = STRBUF_INIT;
1341 argv_array_push(&cp_add.args, "add");
1342 if (!include_untracked)
1343 argv_array_push(&cp_add.args, "-u");
1344 if (include_untracked == INCLUDE_ALL_FILES)
1345 argv_array_push(&cp_add.args, "--force");
1346 argv_array_push(&cp_add.args, "--");
1347 add_pathspecs(&cp_add.args, ps);
1348 if (run_command(&cp_add)) {
1353 cp_diff.git_cmd = 1;
1354 argv_array_pushl(&cp_diff.args, "diff-index", "-p",
1355 "--cached", "--binary", "HEAD", "--",
1357 add_pathspecs(&cp_diff.args, ps);
1358 if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {
1363 cp_apply.git_cmd = 1;
1364 argv_array_pushl(&cp_apply.args, "apply", "--index",
1366 if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,
1372 struct child_process cp = CHILD_PROCESS_INIT;
1374 argv_array_pushl(&cp.args, "reset", "--hard", "-q",
1376 if (run_command(&cp)) {
1382 if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1383 struct child_process cp_ls = CHILD_PROCESS_INIT;
1384 struct child_process cp_checkout = CHILD_PROCESS_INIT;
1385 struct strbuf out = STRBUF_INIT;
1387 if (reset_tree(&info.i_tree, 0, 1)) {
1393 argv_array_pushl(&cp_ls.args, "ls-files", "-z",
1394 "--modified", "--", NULL);
1396 add_pathspecs(&cp_ls.args, ps);
1397 if (pipe_command(&cp_ls, NULL, 0, &out, 0, NULL, 0)) {
1402 cp_checkout.git_cmd = 1;
1403 argv_array_pushl(&cp_checkout.args, "checkout-index",
1404 "-z", "--force", "--stdin", NULL);
1405 if (pipe_command(&cp_checkout, out.buf, out.len, NULL,
1413 struct child_process cp = CHILD_PROCESS_INIT;
1416 argv_array_pushl(&cp.args, "apply", "-R", NULL);
1418 if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1420 fprintf_ln(stderr, _("Cannot remove "
1421 "worktree changes"));
1426 if (keep_index < 1) {
1427 struct child_process cp = CHILD_PROCESS_INIT;
1430 argv_array_pushl(&cp.args, "reset", "-q", "--", NULL);
1431 add_pathspecs(&cp.args, ps);
1432 if (run_command(&cp)) {
1441 strbuf_release(&stash_msg_buf);
1445 static int push_stash(int argc, const char **argv, const char *prefix)
1447 int keep_index = -1;
1449 int include_untracked = 0;
1451 const char *stash_msg = NULL;
1453 struct option options[] = {
1454 OPT_BOOL('k', "keep-index", &keep_index,
1456 OPT_BOOL('p', "patch", &patch_mode,
1457 N_("stash in patch mode")),
1458 OPT__QUIET(&quiet, N_("quiet mode")),
1459 OPT_BOOL('u', "include-untracked", &include_untracked,
1460 N_("include untracked files in stash")),
1461 OPT_SET_INT('a', "all", &include_untracked,
1462 N_("include ignore files"), 2),
1463 OPT_STRING('m', "message", &stash_msg, N_("message"),
1464 N_("stash message")),
1469 argc = parse_options(argc, argv, prefix, options,
1470 git_stash_push_usage,
1473 parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);
1474 return do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,
1478 static int save_stash(int argc, const char **argv, const char *prefix)
1480 int keep_index = -1;
1482 int include_untracked = 0;
1485 const char *stash_msg = NULL;
1487 struct strbuf stash_msg_buf = STRBUF_INIT;
1488 struct option options[] = {
1489 OPT_BOOL('k', "keep-index", &keep_index,
1491 OPT_BOOL('p', "patch", &patch_mode,
1492 N_("stash in patch mode")),
1493 OPT__QUIET(&quiet, N_("quiet mode")),
1494 OPT_BOOL('u', "include-untracked", &include_untracked,
1495 N_("include untracked files in stash")),
1496 OPT_SET_INT('a', "all", &include_untracked,
1497 N_("include ignore files"), 2),
1498 OPT_STRING('m', "message", &stash_msg, "message",
1499 N_("stash message")),
1503 argc = parse_options(argc, argv, prefix, options,
1504 git_stash_save_usage,
1505 PARSE_OPT_KEEP_DASHDASH);
1508 stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
1510 memset(&ps, 0, sizeof(ps));
1511 ret = do_push_stash(ps, stash_msg, quiet, keep_index,
1512 patch_mode, include_untracked);
1514 strbuf_release(&stash_msg_buf);
1518 static int use_builtin_stash(void)
1520 struct child_process cp = CHILD_PROCESS_INIT;
1521 struct strbuf out = STRBUF_INIT;
1522 int ret, env = git_env_bool("GIT_TEST_STASH_USE_BUILTIN", -1);
1527 argv_array_pushl(&cp.args,
1528 "config", "--bool", "stash.usebuiltin", NULL);
1530 if (capture_command(&cp, &out, 6)) {
1531 strbuf_release(&out);
1536 ret = !strcmp("true", out.buf);
1537 strbuf_release(&out);
1541 int cmd_stash(int argc, const char **argv, const char *prefix)
1544 pid_t pid = getpid();
1545 const char *index_file;
1546 struct argv_array args = ARGV_ARRAY_INIT;
1548 struct option options[] = {
1552 if (!use_builtin_stash()) {
1553 const char *path = mkpath("%s/git-legacy-stash",
1556 if (sane_execvp(path, (char **)argv) < 0)
1557 die_errno(_("could not exec %s"), path);
1559 BUG("sane_execvp() returned???");
1562 prefix = setup_git_directory();
1563 trace_repo_setup(prefix);
1566 git_config(git_diff_basic_config, NULL);
1568 argc = parse_options(argc, argv, prefix, options, git_stash_usage,
1569 PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
1571 index_file = get_index_file();
1572 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1576 return !!push_stash(0, NULL, prefix);
1577 else if (!strcmp(argv[0], "apply"))
1578 return !!apply_stash(argc, argv, prefix);
1579 else if (!strcmp(argv[0], "clear"))
1580 return !!clear_stash(argc, argv, prefix);
1581 else if (!strcmp(argv[0], "drop"))
1582 return !!drop_stash(argc, argv, prefix);
1583 else if (!strcmp(argv[0], "pop"))
1584 return !!pop_stash(argc, argv, prefix);
1585 else if (!strcmp(argv[0], "branch"))
1586 return !!branch_stash(argc, argv, prefix);
1587 else if (!strcmp(argv[0], "list"))
1588 return !!list_stash(argc, argv, prefix);
1589 else if (!strcmp(argv[0], "show"))
1590 return !!show_stash(argc, argv, prefix);
1591 else if (!strcmp(argv[0], "store"))
1592 return !!store_stash(argc, argv, prefix);
1593 else if (!strcmp(argv[0], "create"))
1594 return !!create_stash(argc, argv, prefix);
1595 else if (!strcmp(argv[0], "push"))
1596 return !!push_stash(argc, argv, prefix);
1597 else if (!strcmp(argv[0], "save"))
1598 return !!save_stash(argc, argv, prefix);
1599 else if (*argv[0] != '-')
1600 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
1601 git_stash_usage, options);
1603 if (strcmp(argv[0], "-p")) {
1604 while (++i < argc && strcmp(argv[i], "--")) {
1606 * `akpqu` is a string which contains all short options,
1607 * except `-m` which is verified separately.
1609 if ((strlen(argv[i]) == 2) && *argv[i] == '-' &&
1610 strchr("akpqu", argv[i][1]))
1613 if (!strcmp(argv[i], "--all") ||
1614 !strcmp(argv[i], "--keep-index") ||
1615 !strcmp(argv[i], "--no-keep-index") ||
1616 !strcmp(argv[i], "--patch") ||
1617 !strcmp(argv[i], "--quiet") ||
1618 !strcmp(argv[i], "--include-untracked"))
1622 * `-m` and `--message=` are verified separately because
1623 * they need to be immediately followed by a string
1624 * (i.e.`-m"foobar"` or `--message="foobar"`).
1626 if (starts_with(argv[i], "-m") ||
1627 starts_with(argv[i], "--message="))
1630 usage_with_options(git_stash_usage, options);
1634 argv_array_push(&args, "push");
1635 argv_array_pushv(&args, argv);
1636 return !!push_stash(args.argc, args.argv, prefix);