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);
764 rev.diffopt.flags.recursive = 1;
765 setup_diff_pager(&rev.diffopt);
766 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
767 log_tree_diff_flush(&rev);
769 free_stash_info(&info);
770 return diff_result_code(&rev.diffopt, 0);
773 static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
777 stash_msg = "Created via \"git stash store\".";
779 if (update_ref(stash_msg, ref_stash, w_commit, NULL,
780 REF_FORCE_CREATE_REFLOG,
781 quiet ? UPDATE_REFS_QUIET_ON_ERR :
782 UPDATE_REFS_MSG_ON_ERR)) {
784 fprintf_ln(stderr, _("Cannot update %s with %s"),
785 ref_stash, oid_to_hex(w_commit));
793 static int store_stash(int argc, const char **argv, const char *prefix)
796 const char *stash_msg = NULL;
797 struct object_id obj;
798 struct object_context dummy;
799 struct option options[] = {
800 OPT__QUIET(&quiet, N_("be quiet")),
801 OPT_STRING('m', "message", &stash_msg, "message",
802 N_("stash message")),
806 argc = parse_options(argc, argv, prefix, options,
807 git_stash_store_usage,
808 PARSE_OPT_KEEP_UNKNOWN);
812 fprintf_ln(stderr, _("\"git stash store\" requires one "
813 "<commit> argument"));
817 if (get_oid_with_context(argv[0], quiet ? GET_OID_QUIETLY : 0, &obj,
820 fprintf_ln(stderr, _("Cannot update %s with %s"),
825 return do_store_stash(&obj, stash_msg, quiet);
828 static void add_pathspecs(struct argv_array *args,
829 struct pathspec ps) {
832 for (i = 0; i < ps.nr; i++)
833 argv_array_push(args, ps.items[i].match);
837 * `untracked_files` will be filled with the names of untracked files.
838 * The return value is:
840 * = 0 if there are not any untracked files
841 * > 0 if there are untracked files
843 static int get_untracked_files(struct pathspec ps, int include_untracked,
844 struct strbuf *untracked_files)
850 struct dir_struct dir;
852 memset(&dir, 0, sizeof(dir));
853 if (include_untracked != INCLUDE_ALL_FILES)
854 setup_standard_excludes(&dir);
856 seen = xcalloc(ps.nr, 1);
858 max_len = fill_directory(&dir, the_repository->index, &ps);
859 for (i = 0; i < dir.nr; i++) {
860 struct dir_entry *ent = dir.entries[i];
861 if (dir_path_match(&the_index, ent, &ps, max_len, seen)) {
863 strbuf_addstr(untracked_files, ent->name);
864 /* NUL-terminate: will be fed to update-index -z */
865 strbuf_addch(untracked_files, '\0');
873 clear_directory(&dir);
878 * The return value of `check_changes_tracked_files()` can be:
880 * < 0 if there was an error
881 * = 0 if there are no changes.
882 * > 0 if there are changes.
884 static int check_changes_tracked_files(struct pathspec ps)
888 struct object_id dummy;
890 /* No initial commit. */
891 if (get_oid("HEAD", &dummy))
894 if (read_cache() < 0)
897 init_revisions(&rev, NULL);
900 rev.diffopt.flags.quick = 1;
901 rev.diffopt.flags.ignore_submodules = 1;
904 add_head_to_pending(&rev);
905 diff_setup_done(&rev.diffopt);
907 result = run_diff_index(&rev, 1);
908 if (diff_result_code(&rev.diffopt, result))
911 object_array_clear(&rev.pending);
912 result = run_diff_files(&rev, 0);
913 if (diff_result_code(&rev.diffopt, result))
920 * The function will fill `untracked_files` with the names of untracked files
921 * It will return 1 if there were any changes and 0 if there were not.
923 static int check_changes(struct pathspec ps, int include_untracked,
924 struct strbuf *untracked_files)
927 if (check_changes_tracked_files(ps))
930 if (include_untracked && get_untracked_files(ps, include_untracked,
937 static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
941 struct strbuf untracked_msg = STRBUF_INIT;
942 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
943 struct index_state istate = { NULL };
945 cp_upd_index.git_cmd = 1;
946 argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
947 "--remove", "--stdin", NULL);
948 argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
949 stash_index_path.buf);
951 strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
952 if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,
958 if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
964 if (commit_tree(untracked_msg.buf, untracked_msg.len,
965 &info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
971 discard_index(&istate);
972 strbuf_release(&untracked_msg);
973 remove_path(stash_index_path.buf);
977 static int stash_patch(struct stash_info *info, struct pathspec ps,
978 struct strbuf *out_patch, int quiet)
981 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
982 struct child_process cp_add_i = CHILD_PROCESS_INIT;
983 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
984 struct index_state istate = { NULL };
986 remove_path(stash_index_path.buf);
988 cp_read_tree.git_cmd = 1;
989 argv_array_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL);
990 argv_array_pushf(&cp_read_tree.env_array, "GIT_INDEX_FILE=%s",
991 stash_index_path.buf);
992 if (run_command(&cp_read_tree)) {
997 /* Find out what the user wants. */
998 cp_add_i.git_cmd = 1;
999 argv_array_pushl(&cp_add_i.args, "add--interactive", "--patch=stash",
1001 add_pathspecs(&cp_add_i.args, ps);
1002 argv_array_pushf(&cp_add_i.env_array, "GIT_INDEX_FILE=%s",
1003 stash_index_path.buf);
1004 if (run_command(&cp_add_i)) {
1009 /* State of the working tree. */
1010 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1016 cp_diff_tree.git_cmd = 1;
1017 argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD",
1018 oid_to_hex(&info->w_tree), "--", NULL);
1019 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
1024 if (!out_patch->len) {
1026 fprintf_ln(stderr, _("No changes selected"));
1031 discard_index(&istate);
1032 remove_path(stash_index_path.buf);
1036 static int stash_working_tree(struct stash_info *info, struct pathspec ps)
1039 struct rev_info rev;
1040 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1041 struct strbuf diff_output = STRBUF_INIT;
1042 struct index_state istate = { NULL };
1044 init_revisions(&rev, NULL);
1046 set_alternate_index_output(stash_index_path.buf);
1047 if (reset_tree(&info->i_tree, 0, 0)) {
1051 set_alternate_index_output(NULL);
1053 rev.prune_data = ps;
1054 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
1055 rev.diffopt.format_callback = add_diff_to_buf;
1056 rev.diffopt.format_callback_data = &diff_output;
1058 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1063 add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
1065 if (run_diff_index(&rev, 0)) {
1070 cp_upd_index.git_cmd = 1;
1071 argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
1072 "--remove", "--stdin", NULL);
1073 argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
1074 stash_index_path.buf);
1076 if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,
1077 NULL, 0, NULL, 0)) {
1082 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1089 discard_index(&istate);
1091 object_array_clear(&rev.pending);
1092 strbuf_release(&diff_output);
1093 remove_path(stash_index_path.buf);
1097 static int do_create_stash(struct pathspec ps, struct strbuf *stash_msg_buf,
1098 int include_untracked, int patch_mode,
1099 struct stash_info *info, struct strbuf *patch,
1104 int untracked_commit_option = 0;
1105 const char *head_short_sha1 = NULL;
1106 const char *branch_ref = NULL;
1107 const char *branch_name = "(no branch)";
1108 struct commit *head_commit = NULL;
1109 struct commit_list *parents = NULL;
1110 struct strbuf msg = STRBUF_INIT;
1111 struct strbuf commit_tree_label = STRBUF_INIT;
1112 struct strbuf untracked_files = STRBUF_INIT;
1114 prepare_fallback_ident("git stash", "git@stash");
1116 read_cache_preload(NULL);
1117 refresh_cache(REFRESH_QUIET);
1119 if (get_oid("HEAD", &info->b_commit)) {
1121 fprintf_ln(stderr, _("You do not have "
1122 "the initial commit yet"));
1126 head_commit = lookup_commit(the_repository, &info->b_commit);
1129 if (!check_changes(ps, include_untracked, &untracked_files)) {
1134 branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
1135 if (flags & REF_ISSYMREF)
1136 branch_name = strrchr(branch_ref, '/') + 1;
1137 head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
1139 strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);
1140 pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
1142 strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
1143 commit_list_insert(head_commit, &parents);
1144 if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
1145 commit_tree(commit_tree_label.buf, commit_tree_label.len,
1146 &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1148 fprintf_ln(stderr, _("Cannot save the current "
1154 if (include_untracked) {
1155 if (save_untracked_files(info, &msg, untracked_files)) {
1157 fprintf_ln(stderr, _("Cannot save "
1158 "the untracked files"));
1162 untracked_commit_option = 1;
1165 ret = stash_patch(info, ps, patch, quiet);
1168 fprintf_ln(stderr, _("Cannot save the current "
1171 } else if (ret > 0) {
1175 if (stash_working_tree(info, ps)) {
1177 fprintf_ln(stderr, _("Cannot save the current "
1184 if (!stash_msg_buf->len)
1185 strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf);
1187 strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name);
1190 * `parents` will be empty after calling `commit_tree()`, so there is
1191 * no need to call `free_commit_list()`
1194 if (untracked_commit_option)
1195 commit_list_insert(lookup_commit(the_repository,
1198 commit_list_insert(lookup_commit(the_repository, &info->i_commit),
1200 commit_list_insert(head_commit, &parents);
1202 if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
1203 parents, &info->w_commit, NULL, NULL)) {
1205 fprintf_ln(stderr, _("Cannot record "
1206 "working tree state"));
1212 strbuf_release(&commit_tree_label);
1213 strbuf_release(&msg);
1214 strbuf_release(&untracked_files);
1218 static int create_stash(int argc, const char **argv, const char *prefix)
1221 struct strbuf stash_msg_buf = STRBUF_INIT;
1222 struct stash_info info;
1225 /* Starting with argv[1], since argv[0] is "create" */
1226 strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
1228 memset(&ps, 0, sizeof(ps));
1229 if (!check_changes_tracked_files(ps))
1232 ret = do_create_stash(ps, &stash_msg_buf, 0, 0, &info,
1235 printf_ln("%s", oid_to_hex(&info.w_commit));
1237 strbuf_release(&stash_msg_buf);
1241 static int do_push_stash(struct pathspec ps, const char *stash_msg, int quiet,
1242 int keep_index, int patch_mode, int include_untracked)
1245 struct stash_info info;
1246 struct strbuf patch = STRBUF_INIT;
1247 struct strbuf stash_msg_buf = STRBUF_INIT;
1248 struct strbuf untracked_files = STRBUF_INIT;
1250 if (patch_mode && keep_index == -1)
1253 if (patch_mode && include_untracked) {
1254 fprintf_ln(stderr, _("Can't use --patch and --include-untracked"
1255 " or --all at the same time"));
1260 read_cache_preload(NULL);
1261 if (!include_untracked && ps.nr) {
1263 char *ps_matched = xcalloc(ps.nr, 1);
1265 for (i = 0; i < active_nr; i++)
1266 ce_path_match(&the_index, active_cache[i], &ps,
1269 if (report_path_error(ps_matched, &ps, NULL)) {
1270 fprintf_ln(stderr, _("Did you forget to 'git add'?"));
1278 if (refresh_cache(REFRESH_QUIET)) {
1283 if (!check_changes(ps, include_untracked, &untracked_files)) {
1285 printf_ln(_("No local changes to save"));
1289 if (!reflog_exists(ref_stash) && do_clear_stash()) {
1292 fprintf_ln(stderr, _("Cannot initialize stash"));
1297 strbuf_addstr(&stash_msg_buf, stash_msg);
1298 if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1299 &info, &patch, quiet)) {
1304 if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
1307 fprintf_ln(stderr, _("Cannot save the current status"));
1312 printf_ln(_("Saved working directory and index state %s"),
1316 if (include_untracked && !ps.nr) {
1317 struct child_process cp = CHILD_PROCESS_INIT;
1320 argv_array_pushl(&cp.args, "clean", "--force",
1321 "--quiet", "-d", NULL);
1322 if (include_untracked == INCLUDE_ALL_FILES)
1323 argv_array_push(&cp.args, "-x");
1324 if (run_command(&cp)) {
1331 struct child_process cp_add = CHILD_PROCESS_INIT;
1332 struct child_process cp_diff = CHILD_PROCESS_INIT;
1333 struct child_process cp_apply = CHILD_PROCESS_INIT;
1334 struct strbuf out = STRBUF_INIT;
1337 argv_array_push(&cp_add.args, "add");
1338 if (!include_untracked)
1339 argv_array_push(&cp_add.args, "-u");
1340 if (include_untracked == INCLUDE_ALL_FILES)
1341 argv_array_push(&cp_add.args, "--force");
1342 argv_array_push(&cp_add.args, "--");
1343 add_pathspecs(&cp_add.args, ps);
1344 if (run_command(&cp_add)) {
1349 cp_diff.git_cmd = 1;
1350 argv_array_pushl(&cp_diff.args, "diff-index", "-p",
1351 "--cached", "--binary", "HEAD", "--",
1353 add_pathspecs(&cp_diff.args, ps);
1354 if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {
1359 cp_apply.git_cmd = 1;
1360 argv_array_pushl(&cp_apply.args, "apply", "--index",
1362 if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,
1368 struct child_process cp = CHILD_PROCESS_INIT;
1370 argv_array_pushl(&cp.args, "reset", "--hard", "-q",
1372 if (run_command(&cp)) {
1378 if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1379 struct child_process cp_ls = CHILD_PROCESS_INIT;
1380 struct child_process cp_checkout = CHILD_PROCESS_INIT;
1381 struct strbuf out = STRBUF_INIT;
1383 if (reset_tree(&info.i_tree, 0, 1)) {
1389 argv_array_pushl(&cp_ls.args, "ls-files", "-z",
1390 "--modified", "--", NULL);
1392 add_pathspecs(&cp_ls.args, ps);
1393 if (pipe_command(&cp_ls, NULL, 0, &out, 0, NULL, 0)) {
1398 cp_checkout.git_cmd = 1;
1399 argv_array_pushl(&cp_checkout.args, "checkout-index",
1400 "-z", "--force", "--stdin", NULL);
1401 if (pipe_command(&cp_checkout, out.buf, out.len, NULL,
1409 struct child_process cp = CHILD_PROCESS_INIT;
1412 argv_array_pushl(&cp.args, "apply", "-R", NULL);
1414 if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1416 fprintf_ln(stderr, _("Cannot remove "
1417 "worktree changes"));
1422 if (keep_index < 1) {
1423 struct child_process cp = CHILD_PROCESS_INIT;
1426 argv_array_pushl(&cp.args, "reset", "-q", "--", NULL);
1427 add_pathspecs(&cp.args, ps);
1428 if (run_command(&cp)) {
1437 strbuf_release(&stash_msg_buf);
1441 static int push_stash(int argc, const char **argv, const char *prefix)
1443 int keep_index = -1;
1445 int include_untracked = 0;
1447 const char *stash_msg = NULL;
1449 struct option options[] = {
1450 OPT_BOOL('k', "keep-index", &keep_index,
1452 OPT_BOOL('p', "patch", &patch_mode,
1453 N_("stash in patch mode")),
1454 OPT__QUIET(&quiet, N_("quiet mode")),
1455 OPT_BOOL('u', "include-untracked", &include_untracked,
1456 N_("include untracked files in stash")),
1457 OPT_SET_INT('a', "all", &include_untracked,
1458 N_("include ignore files"), 2),
1459 OPT_STRING('m', "message", &stash_msg, N_("message"),
1460 N_("stash message")),
1465 argc = parse_options(argc, argv, prefix, options,
1466 git_stash_push_usage,
1469 parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL, prefix, argv);
1470 return do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,
1474 static int save_stash(int argc, const char **argv, const char *prefix)
1476 int keep_index = -1;
1478 int include_untracked = 0;
1481 const char *stash_msg = NULL;
1483 struct strbuf stash_msg_buf = STRBUF_INIT;
1484 struct option options[] = {
1485 OPT_BOOL('k', "keep-index", &keep_index,
1487 OPT_BOOL('p', "patch", &patch_mode,
1488 N_("stash in patch mode")),
1489 OPT__QUIET(&quiet, N_("quiet mode")),
1490 OPT_BOOL('u', "include-untracked", &include_untracked,
1491 N_("include untracked files in stash")),
1492 OPT_SET_INT('a', "all", &include_untracked,
1493 N_("include ignore files"), 2),
1494 OPT_STRING('m', "message", &stash_msg, "message",
1495 N_("stash message")),
1499 argc = parse_options(argc, argv, prefix, options,
1500 git_stash_save_usage,
1501 PARSE_OPT_KEEP_DASHDASH);
1504 stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
1506 memset(&ps, 0, sizeof(ps));
1507 ret = do_push_stash(ps, stash_msg, quiet, keep_index,
1508 patch_mode, include_untracked);
1510 strbuf_release(&stash_msg_buf);
1514 static int use_builtin_stash(void)
1516 struct child_process cp = CHILD_PROCESS_INIT;
1517 struct strbuf out = STRBUF_INIT;
1518 int ret, env = git_env_bool("GIT_TEST_STASH_USE_BUILTIN", -1);
1523 argv_array_pushl(&cp.args,
1524 "config", "--bool", "stash.usebuiltin", NULL);
1526 if (capture_command(&cp, &out, 6)) {
1527 strbuf_release(&out);
1532 ret = !strcmp("true", out.buf);
1533 strbuf_release(&out);
1537 int cmd_stash(int argc, const char **argv, const char *prefix)
1540 pid_t pid = getpid();
1541 const char *index_file;
1542 struct argv_array args = ARGV_ARRAY_INIT;
1544 struct option options[] = {
1548 if (!use_builtin_stash()) {
1549 const char *path = mkpath("%s/git-legacy-stash",
1552 if (sane_execvp(path, (char **)argv) < 0)
1553 die_errno(_("could not exec %s"), path);
1555 BUG("sane_execvp() returned???");
1558 prefix = setup_git_directory();
1559 trace_repo_setup(prefix);
1562 git_config(git_diff_basic_config, NULL);
1564 argc = parse_options(argc, argv, prefix, options, git_stash_usage,
1565 PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
1567 index_file = get_index_file();
1568 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1572 return !!push_stash(0, NULL, prefix);
1573 else if (!strcmp(argv[0], "apply"))
1574 return !!apply_stash(argc, argv, prefix);
1575 else if (!strcmp(argv[0], "clear"))
1576 return !!clear_stash(argc, argv, prefix);
1577 else if (!strcmp(argv[0], "drop"))
1578 return !!drop_stash(argc, argv, prefix);
1579 else if (!strcmp(argv[0], "pop"))
1580 return !!pop_stash(argc, argv, prefix);
1581 else if (!strcmp(argv[0], "branch"))
1582 return !!branch_stash(argc, argv, prefix);
1583 else if (!strcmp(argv[0], "list"))
1584 return !!list_stash(argc, argv, prefix);
1585 else if (!strcmp(argv[0], "show"))
1586 return !!show_stash(argc, argv, prefix);
1587 else if (!strcmp(argv[0], "store"))
1588 return !!store_stash(argc, argv, prefix);
1589 else if (!strcmp(argv[0], "create"))
1590 return !!create_stash(argc, argv, prefix);
1591 else if (!strcmp(argv[0], "push"))
1592 return !!push_stash(argc, argv, prefix);
1593 else if (!strcmp(argv[0], "save"))
1594 return !!save_stash(argc, argv, prefix);
1595 else if (*argv[0] != '-')
1596 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
1597 git_stash_usage, options);
1599 if (strcmp(argv[0], "-p")) {
1600 while (++i < argc && strcmp(argv[i], "--")) {
1602 * `akpqu` is a string which contains all short options,
1603 * except `-m` which is verified separately.
1605 if ((strlen(argv[i]) == 2) && *argv[i] == '-' &&
1606 strchr("akpqu", argv[i][1]))
1609 if (!strcmp(argv[i], "--all") ||
1610 !strcmp(argv[i], "--keep-index") ||
1611 !strcmp(argv[i], "--no-keep-index") ||
1612 !strcmp(argv[i], "--patch") ||
1613 !strcmp(argv[i], "--quiet") ||
1614 !strcmp(argv[i], "--include-untracked"))
1618 * `-m` and `--message=` are verified separately because
1619 * they need to be immediately followed by a string
1620 * (i.e.`-m"foobar"` or `--message="foobar"`).
1622 if (starts_with(argv[i], "-m") ||
1623 starts_with(argv[i], "--message="))
1626 usage_with_options(git_stash_usage, options);
1630 argv_array_push(&args, "push");
1631 argv_array_pushv(&args, argv);
1632 return !!push_stash(args.argc, args.argv, prefix);