Merge branch 'js/stash-apply-in-secondary-worktree'
[alt-git.git] / builtin / stash.c
blob4fc44b35e472b8dfe4cb6a09043dfcc4031d7614
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 "argv-array.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"
19 #define INCLUDE_ALL_FILES 2
21 static const char * const git_stash_usage[] = {
22 N_("git stash list [<options>]"),
23 N_("git stash show [<options>] [<stash>]"),
24 N_("git stash drop [-q|--quiet] [<stash>]"),
25 N_("git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
26 N_("git stash branch <branchname> [<stash>]"),
27 N_("git stash clear"),
28 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
29 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
30 " [--] [<pathspec>...]]"),
31 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
32 " [-u|--include-untracked] [-a|--all] [<message>]"),
33 NULL
36 static const char * const git_stash_list_usage[] = {
37 N_("git stash list [<options>]"),
38 NULL
41 static const char * const git_stash_show_usage[] = {
42 N_("git stash show [<options>] [<stash>]"),
43 NULL
46 static const char * const git_stash_drop_usage[] = {
47 N_("git stash drop [-q|--quiet] [<stash>]"),
48 NULL
51 static const char * const git_stash_pop_usage[] = {
52 N_("git stash pop [--index] [-q|--quiet] [<stash>]"),
53 NULL
56 static const char * const git_stash_apply_usage[] = {
57 N_("git stash apply [--index] [-q|--quiet] [<stash>]"),
58 NULL
61 static const char * const git_stash_branch_usage[] = {
62 N_("git stash branch <branchname> [<stash>]"),
63 NULL
66 static const char * const git_stash_clear_usage[] = {
67 N_("git stash clear"),
68 NULL
71 static const char * const git_stash_store_usage[] = {
72 N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"),
73 NULL
76 static const char * const git_stash_push_usage[] = {
77 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
78 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
79 " [--] [<pathspec>...]]"),
80 NULL
83 static const char * const git_stash_save_usage[] = {
84 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
85 " [-u|--include-untracked] [-a|--all] [<message>]"),
86 NULL
89 static const char *ref_stash = "refs/stash";
90 static struct strbuf stash_index_path = STRBUF_INIT;
93 * w_commit is set to the commit containing the working tree
94 * b_commit is set to the base commit
95 * i_commit is set to the commit containing the index tree
96 * u_commit is set to the commit containing the untracked files tree
97 * w_tree is set to the working tree
98 * b_tree is set to the base tree
99 * i_tree is set to the index tree
100 * u_tree is set to the untracked files tree
102 struct stash_info {
103 struct object_id w_commit;
104 struct object_id b_commit;
105 struct object_id i_commit;
106 struct object_id u_commit;
107 struct object_id w_tree;
108 struct object_id b_tree;
109 struct object_id i_tree;
110 struct object_id u_tree;
111 struct strbuf revision;
112 int is_stash_ref;
113 int has_u;
116 static void free_stash_info(struct stash_info *info)
118 strbuf_release(&info->revision);
121 static void assert_stash_like(struct stash_info *info, const char *revision)
123 if (get_oidf(&info->b_commit, "%s^1", revision) ||
124 get_oidf(&info->w_tree, "%s:", revision) ||
125 get_oidf(&info->b_tree, "%s^1:", revision) ||
126 get_oidf(&info->i_tree, "%s^2:", revision))
127 die(_("'%s' is not a stash-like commit"), revision);
130 static int get_stash_info(struct stash_info *info, int argc, const char **argv)
132 int ret;
133 char *end_of_rev;
134 char *expanded_ref;
135 const char *revision;
136 const char *commit = NULL;
137 struct object_id dummy;
138 struct strbuf symbolic = STRBUF_INIT;
140 if (argc > 1) {
141 int i;
142 struct strbuf refs_msg = STRBUF_INIT;
144 for (i = 0; i < argc; i++)
145 strbuf_addf(&refs_msg, " '%s'", argv[i]);
147 fprintf_ln(stderr, _("Too many revisions specified:%s"),
148 refs_msg.buf);
149 strbuf_release(&refs_msg);
151 return -1;
154 if (argc == 1)
155 commit = argv[0];
157 strbuf_init(&info->revision, 0);
158 if (!commit) {
159 if (!ref_exists(ref_stash)) {
160 free_stash_info(info);
161 fprintf_ln(stderr, _("No stash entries found."));
162 return -1;
165 strbuf_addf(&info->revision, "%s@{0}", ref_stash);
166 } else if (strspn(commit, "0123456789") == strlen(commit)) {
167 strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
168 } else {
169 strbuf_addstr(&info->revision, commit);
172 revision = info->revision.buf;
174 if (get_oid(revision, &info->w_commit)) {
175 error(_("%s is not a valid reference"), revision);
176 free_stash_info(info);
177 return -1;
180 assert_stash_like(info, revision);
182 info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision);
184 end_of_rev = strchrnul(revision, '@');
185 strbuf_add(&symbolic, revision, end_of_rev - revision);
187 ret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref);
188 strbuf_release(&symbolic);
189 switch (ret) {
190 case 0: /* Not found, but valid ref */
191 info->is_stash_ref = 0;
192 break;
193 case 1:
194 info->is_stash_ref = !strcmp(expanded_ref, ref_stash);
195 break;
196 default: /* Invalid or ambiguous */
197 free_stash_info(info);
200 free(expanded_ref);
201 return !(ret == 0 || ret == 1);
204 static int do_clear_stash(void)
206 struct object_id obj;
207 if (get_oid(ref_stash, &obj))
208 return 0;
210 return delete_ref(NULL, ref_stash, &obj, 0);
213 static int clear_stash(int argc, const char **argv, const char *prefix)
215 struct option options[] = {
216 OPT_END()
219 argc = parse_options(argc, argv, prefix, options,
220 git_stash_clear_usage,
221 PARSE_OPT_STOP_AT_NON_OPTION);
223 if (argc)
224 return error(_("git stash clear with parameters is "
225 "unimplemented"));
227 return do_clear_stash();
230 static int reset_tree(struct object_id *i_tree, int update, int reset)
232 int nr_trees = 1;
233 struct unpack_trees_options opts;
234 struct tree_desc t[MAX_UNPACK_TREES];
235 struct tree *tree;
236 struct lock_file lock_file = LOCK_INIT;
238 read_cache_preload(NULL);
239 if (refresh_cache(REFRESH_QUIET))
240 return -1;
242 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
244 memset(&opts, 0, sizeof(opts));
246 tree = parse_tree_indirect(i_tree);
247 if (parse_tree(tree))
248 return -1;
250 init_tree_desc(t, tree->buffer, tree->size);
252 opts.head_idx = 1;
253 opts.src_index = &the_index;
254 opts.dst_index = &the_index;
255 opts.merge = 1;
256 opts.reset = reset;
257 opts.update = update;
258 opts.fn = oneway_merge;
260 if (unpack_trees(nr_trees, t, &opts))
261 return -1;
263 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
264 return error(_("unable to write new index file"));
266 return 0;
269 static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
271 struct child_process cp = CHILD_PROCESS_INIT;
272 const char *w_commit_hex = oid_to_hex(w_commit);
275 * Diff-tree would not be very hard to replace with a native function,
276 * however it should be done together with apply_cached.
278 cp.git_cmd = 1;
279 argv_array_pushl(&cp.args, "diff-tree", "--binary", NULL);
280 argv_array_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
282 return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
285 static int apply_cached(struct strbuf *out)
287 struct child_process cp = CHILD_PROCESS_INIT;
290 * Apply currently only reads either from stdin or a file, thus
291 * apply_all_patches would have to be updated to optionally take a
292 * buffer.
294 cp.git_cmd = 1;
295 argv_array_pushl(&cp.args, "apply", "--cached", NULL);
296 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
299 static int reset_head(void)
301 struct child_process cp = CHILD_PROCESS_INIT;
304 * Reset is overall quite simple, however there is no current public
305 * API for resetting.
307 cp.git_cmd = 1;
308 argv_array_push(&cp.args, "reset");
310 return run_command(&cp);
313 static void add_diff_to_buf(struct diff_queue_struct *q,
314 struct diff_options *options,
315 void *data)
317 int i;
319 for (i = 0; i < q->nr; i++) {
320 strbuf_addstr(data, q->queue[i]->one->path);
322 /* NUL-terminate: will be fed to update-index -z */
323 strbuf_addch(data, '\0');
327 static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)
329 struct child_process cp = CHILD_PROCESS_INIT;
330 const char *c_tree_hex = oid_to_hex(c_tree);
333 * diff-index is very similar to diff-tree above, and should be
334 * converted together with update_index.
336 cp.git_cmd = 1;
337 argv_array_pushl(&cp.args, "diff-index", "--cached", "--name-only",
338 "--diff-filter=A", NULL);
339 argv_array_push(&cp.args, c_tree_hex);
340 return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
343 static int update_index(struct strbuf *out)
345 struct child_process cp = CHILD_PROCESS_INIT;
348 * Update-index is very complicated and may need to have a public
349 * function exposed in order to remove this forking.
351 cp.git_cmd = 1;
352 argv_array_pushl(&cp.args, "update-index", "--add", "--stdin", NULL);
353 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
356 static int restore_untracked(struct object_id *u_tree)
358 int res;
359 struct child_process cp = CHILD_PROCESS_INIT;
362 * We need to run restore files from a given index, but without
363 * affecting the current index, so we use GIT_INDEX_FILE with
364 * run_command to fork processes that will not interfere.
366 cp.git_cmd = 1;
367 argv_array_push(&cp.args, "read-tree");
368 argv_array_push(&cp.args, oid_to_hex(u_tree));
369 argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
370 stash_index_path.buf);
371 if (run_command(&cp)) {
372 remove_path(stash_index_path.buf);
373 return -1;
376 child_process_init(&cp);
377 cp.git_cmd = 1;
378 argv_array_pushl(&cp.args, "checkout-index", "--all", NULL);
379 argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s",
380 stash_index_path.buf);
382 res = run_command(&cp);
383 remove_path(stash_index_path.buf);
384 return res;
387 static int do_apply_stash(const char *prefix, struct stash_info *info,
388 int index, int quiet)
390 int ret;
391 int has_index = index;
392 struct merge_options o;
393 struct object_id c_tree;
394 struct object_id index_tree;
395 struct commit *result;
396 const struct object_id *bases[1];
398 read_cache_preload(NULL);
399 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0))
400 return -1;
402 if (write_cache_as_tree(&c_tree, 0, NULL))
403 return error(_("cannot apply a stash in the middle of a merge"));
405 if (index) {
406 if (oideq(&info->b_tree, &info->i_tree) ||
407 oideq(&c_tree, &info->i_tree)) {
408 has_index = 0;
409 } else {
410 struct strbuf out = STRBUF_INIT;
412 if (diff_tree_binary(&out, &info->w_commit)) {
413 strbuf_release(&out);
414 return error(_("could not generate diff %s^!."),
415 oid_to_hex(&info->w_commit));
418 ret = apply_cached(&out);
419 strbuf_release(&out);
420 if (ret)
421 return error(_("conflicts in index."
422 "Try without --index."));
424 discard_cache();
425 read_cache();
426 if (write_cache_as_tree(&index_tree, 0, NULL))
427 return error(_("could not save index tree"));
429 reset_head();
433 if (info->has_u && restore_untracked(&info->u_tree))
434 return error(_("could not restore untracked files from stash"));
436 init_merge_options(&o, the_repository);
438 o.branch1 = "Updated upstream";
439 o.branch2 = "Stashed changes";
441 if (oideq(&info->b_tree, &c_tree))
442 o.branch1 = "Version stash was based on";
444 if (quiet)
445 o.verbosity = 0;
447 if (o.verbosity >= 3)
448 printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
450 bases[0] = &info->b_tree;
452 ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, 1, bases,
453 &result);
454 if (ret) {
455 rerere(0);
457 if (index)
458 fprintf_ln(stderr, _("Index was not unstashed."));
460 return ret;
463 if (has_index) {
464 if (reset_tree(&index_tree, 0, 0))
465 return -1;
466 } else {
467 struct strbuf out = STRBUF_INIT;
469 if (get_newly_staged(&out, &c_tree)) {
470 strbuf_release(&out);
471 return -1;
474 if (reset_tree(&c_tree, 0, 1)) {
475 strbuf_release(&out);
476 return -1;
479 ret = update_index(&out);
480 strbuf_release(&out);
481 if (ret)
482 return -1;
484 discard_cache();
487 if (quiet) {
488 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0))
489 warning("could not refresh index");
490 } else {
491 struct child_process cp = CHILD_PROCESS_INIT;
494 * Status is quite simple and could be replaced with calls to
495 * wt_status in the future, but it adds complexities which may
496 * require more tests.
498 cp.git_cmd = 1;
499 cp.dir = prefix;
500 argv_array_pushf(&cp.env_array, GIT_WORK_TREE_ENVIRONMENT"=%s",
501 absolute_path(get_git_work_tree()));
502 argv_array_pushf(&cp.env_array, GIT_DIR_ENVIRONMENT"=%s",
503 absolute_path(get_git_dir()));
504 argv_array_push(&cp.args, "status");
505 run_command(&cp);
508 return 0;
511 static int apply_stash(int argc, const char **argv, const char *prefix)
513 int ret;
514 int quiet = 0;
515 int index = 0;
516 struct stash_info info;
517 struct option options[] = {
518 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
519 OPT_BOOL(0, "index", &index,
520 N_("attempt to recreate the index")),
521 OPT_END()
524 argc = parse_options(argc, argv, prefix, options,
525 git_stash_apply_usage, 0);
527 if (get_stash_info(&info, argc, argv))
528 return -1;
530 ret = do_apply_stash(prefix, &info, index, quiet);
531 free_stash_info(&info);
532 return ret;
535 static int do_drop_stash(struct stash_info *info, int quiet)
537 int ret;
538 struct child_process cp_reflog = CHILD_PROCESS_INIT;
539 struct child_process cp = CHILD_PROCESS_INIT;
542 * reflog does not provide a simple function for deleting refs. One will
543 * need to be added to avoid implementing too much reflog code here
546 cp_reflog.git_cmd = 1;
547 argv_array_pushl(&cp_reflog.args, "reflog", "delete", "--updateref",
548 "--rewrite", NULL);
549 argv_array_push(&cp_reflog.args, info->revision.buf);
550 ret = run_command(&cp_reflog);
551 if (!ret) {
552 if (!quiet)
553 printf_ln(_("Dropped %s (%s)"), info->revision.buf,
554 oid_to_hex(&info->w_commit));
555 } else {
556 return error(_("%s: Could not drop stash entry"),
557 info->revision.buf);
561 * This could easily be replaced by get_oid, but currently it will throw
562 * a fatal error when a reflog is empty, which we can not recover from.
564 cp.git_cmd = 1;
565 /* Even though --quiet is specified, rev-parse still outputs the hash */
566 cp.no_stdout = 1;
567 argv_array_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL);
568 argv_array_pushf(&cp.args, "%s@{0}", ref_stash);
569 ret = run_command(&cp);
571 /* do_clear_stash if we just dropped the last stash entry */
572 if (ret)
573 do_clear_stash();
575 return 0;
578 static void assert_stash_ref(struct stash_info *info)
580 if (!info->is_stash_ref) {
581 error(_("'%s' is not a stash reference"), info->revision.buf);
582 free_stash_info(info);
583 exit(1);
587 static int drop_stash(int argc, const char **argv, const char *prefix)
589 int ret;
590 int quiet = 0;
591 struct stash_info info;
592 struct option options[] = {
593 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
594 OPT_END()
597 argc = parse_options(argc, argv, prefix, options,
598 git_stash_drop_usage, 0);
600 if (get_stash_info(&info, argc, argv))
601 return -1;
603 assert_stash_ref(&info);
605 ret = do_drop_stash(&info, quiet);
606 free_stash_info(&info);
607 return ret;
610 static int pop_stash(int argc, const char **argv, const char *prefix)
612 int ret;
613 int index = 0;
614 int quiet = 0;
615 struct stash_info info;
616 struct option options[] = {
617 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
618 OPT_BOOL(0, "index", &index,
619 N_("attempt to recreate the index")),
620 OPT_END()
623 argc = parse_options(argc, argv, prefix, options,
624 git_stash_pop_usage, 0);
626 if (get_stash_info(&info, argc, argv))
627 return -1;
629 assert_stash_ref(&info);
630 if ((ret = do_apply_stash(prefix, &info, index, quiet)))
631 printf_ln(_("The stash entry is kept in case "
632 "you need it again."));
633 else
634 ret = do_drop_stash(&info, quiet);
636 free_stash_info(&info);
637 return ret;
640 static int branch_stash(int argc, const char **argv, const char *prefix)
642 int ret;
643 const char *branch = NULL;
644 struct stash_info info;
645 struct child_process cp = CHILD_PROCESS_INIT;
646 struct option options[] = {
647 OPT_END()
650 argc = parse_options(argc, argv, prefix, options,
651 git_stash_branch_usage, 0);
653 if (!argc) {
654 fprintf_ln(stderr, _("No branch name specified"));
655 return -1;
658 branch = argv[0];
660 if (get_stash_info(&info, argc - 1, argv + 1))
661 return -1;
663 cp.git_cmd = 1;
664 argv_array_pushl(&cp.args, "checkout", "-b", NULL);
665 argv_array_push(&cp.args, branch);
666 argv_array_push(&cp.args, oid_to_hex(&info.b_commit));
667 ret = run_command(&cp);
668 if (!ret)
669 ret = do_apply_stash(prefix, &info, 1, 0);
670 if (!ret && info.is_stash_ref)
671 ret = do_drop_stash(&info, 0);
673 free_stash_info(&info);
675 return ret;
678 static int list_stash(int argc, const char **argv, const char *prefix)
680 struct child_process cp = CHILD_PROCESS_INIT;
681 struct option options[] = {
682 OPT_END()
685 argc = parse_options(argc, argv, prefix, options,
686 git_stash_list_usage,
687 PARSE_OPT_KEEP_UNKNOWN);
689 if (!ref_exists(ref_stash))
690 return 0;
692 cp.git_cmd = 1;
693 argv_array_pushl(&cp.args, "log", "--format=%gd: %gs", "-g",
694 "--first-parent", "-m", NULL);
695 argv_array_pushv(&cp.args, argv);
696 argv_array_push(&cp.args, ref_stash);
697 argv_array_push(&cp.args, "--");
698 return run_command(&cp);
701 static int show_stat = 1;
702 static int show_patch;
704 static int git_stash_config(const char *var, const char *value, void *cb)
706 if (!strcmp(var, "stash.showstat")) {
707 show_stat = git_config_bool(var, value);
708 return 0;
710 if (!strcmp(var, "stash.showpatch")) {
711 show_patch = git_config_bool(var, value);
712 return 0;
714 return git_default_config(var, value, cb);
717 static int show_stash(int argc, const char **argv, const char *prefix)
719 int i;
720 int ret = 0;
721 struct stash_info info;
722 struct rev_info rev;
723 struct argv_array stash_args = ARGV_ARRAY_INIT;
724 struct argv_array revision_args = ARGV_ARRAY_INIT;
725 struct option options[] = {
726 OPT_END()
729 init_diff_ui_defaults();
730 git_config(git_diff_ui_config, NULL);
731 init_revisions(&rev, prefix);
733 argv_array_push(&revision_args, argv[0]);
734 for (i = 1; i < argc; i++) {
735 if (argv[i][0] != '-')
736 argv_array_push(&stash_args, argv[i]);
737 else
738 argv_array_push(&revision_args, argv[i]);
741 ret = get_stash_info(&info, stash_args.argc, stash_args.argv);
742 argv_array_clear(&stash_args);
743 if (ret)
744 return -1;
747 * The config settings are applied only if there are not passed
748 * any options.
750 if (revision_args.argc == 1) {
751 git_config(git_stash_config, NULL);
752 if (show_stat)
753 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;
755 if (show_patch)
756 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
758 if (!show_stat && !show_patch) {
759 free_stash_info(&info);
760 return 0;
764 argc = setup_revisions(revision_args.argc, revision_args.argv, &rev, NULL);
765 if (argc > 1) {
766 free_stash_info(&info);
767 usage_with_options(git_stash_show_usage, options);
769 if (!rev.diffopt.output_format) {
770 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
771 diff_setup_done(&rev.diffopt);
774 rev.diffopt.flags.recursive = 1;
775 setup_diff_pager(&rev.diffopt);
776 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
777 log_tree_diff_flush(&rev);
779 free_stash_info(&info);
780 return diff_result_code(&rev.diffopt, 0);
783 static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
784 int quiet)
786 if (!stash_msg)
787 stash_msg = "Created via \"git stash store\".";
789 if (update_ref(stash_msg, ref_stash, w_commit, NULL,
790 REF_FORCE_CREATE_REFLOG,
791 quiet ? UPDATE_REFS_QUIET_ON_ERR :
792 UPDATE_REFS_MSG_ON_ERR)) {
793 if (!quiet) {
794 fprintf_ln(stderr, _("Cannot update %s with %s"),
795 ref_stash, oid_to_hex(w_commit));
797 return -1;
800 return 0;
803 static int store_stash(int argc, const char **argv, const char *prefix)
805 int quiet = 0;
806 const char *stash_msg = NULL;
807 struct object_id obj;
808 struct object_context dummy;
809 struct option options[] = {
810 OPT__QUIET(&quiet, N_("be quiet")),
811 OPT_STRING('m', "message", &stash_msg, "message",
812 N_("stash message")),
813 OPT_END()
816 argc = parse_options(argc, argv, prefix, options,
817 git_stash_store_usage,
818 PARSE_OPT_KEEP_UNKNOWN);
820 if (argc != 1) {
821 if (!quiet)
822 fprintf_ln(stderr, _("\"git stash store\" requires one "
823 "<commit> argument"));
824 return -1;
827 if (get_oid_with_context(the_repository,
828 argv[0], quiet ? GET_OID_QUIETLY : 0, &obj,
829 &dummy)) {
830 if (!quiet)
831 fprintf_ln(stderr, _("Cannot update %s with %s"),
832 ref_stash, argv[0]);
833 return -1;
836 return do_store_stash(&obj, stash_msg, quiet);
839 static void add_pathspecs(struct argv_array *args,
840 const struct pathspec *ps) {
841 int i;
843 for (i = 0; i < ps->nr; i++)
844 argv_array_push(args, ps->items[i].original);
848 * `untracked_files` will be filled with the names of untracked files.
849 * The return value is:
851 * = 0 if there are not any untracked files
852 * > 0 if there are untracked files
854 static int get_untracked_files(const struct pathspec *ps, int include_untracked,
855 struct strbuf *untracked_files)
857 int i;
858 int max_len;
859 int found = 0;
860 char *seen;
861 struct dir_struct dir;
863 memset(&dir, 0, sizeof(dir));
864 if (include_untracked != INCLUDE_ALL_FILES)
865 setup_standard_excludes(&dir);
867 seen = xcalloc(ps->nr, 1);
869 max_len = fill_directory(&dir, the_repository->index, ps);
870 for (i = 0; i < dir.nr; i++) {
871 struct dir_entry *ent = dir.entries[i];
872 if (dir_path_match(&the_index, ent, ps, max_len, seen)) {
873 found++;
874 strbuf_addstr(untracked_files, ent->name);
875 /* NUL-terminate: will be fed to update-index -z */
876 strbuf_addch(untracked_files, '\0');
878 free(ent);
881 free(seen);
882 free(dir.entries);
883 free(dir.ignored);
884 clear_directory(&dir);
885 return found;
889 * The return value of `check_changes_tracked_files()` can be:
891 * < 0 if there was an error
892 * = 0 if there are no changes.
893 * > 0 if there are changes.
895 static int check_changes_tracked_files(const struct pathspec *ps)
897 int result;
898 struct rev_info rev;
899 struct object_id dummy;
900 int ret = 0;
902 /* No initial commit. */
903 if (get_oid("HEAD", &dummy))
904 return -1;
906 if (read_cache() < 0)
907 return -1;
909 init_revisions(&rev, NULL);
910 copy_pathspec(&rev.prune_data, ps);
912 rev.diffopt.flags.quick = 1;
913 rev.diffopt.flags.ignore_submodules = 1;
914 rev.abbrev = 0;
916 add_head_to_pending(&rev);
917 diff_setup_done(&rev.diffopt);
919 result = run_diff_index(&rev, 1);
920 if (diff_result_code(&rev.diffopt, result)) {
921 ret = 1;
922 goto done;
925 object_array_clear(&rev.pending);
926 result = run_diff_files(&rev, 0);
927 if (diff_result_code(&rev.diffopt, result)) {
928 ret = 1;
929 goto done;
932 done:
933 clear_pathspec(&rev.prune_data);
934 return ret;
938 * The function will fill `untracked_files` with the names of untracked files
939 * It will return 1 if there were any changes and 0 if there were not.
941 static int check_changes(const struct pathspec *ps, int include_untracked,
942 struct strbuf *untracked_files)
944 int ret = 0;
945 if (check_changes_tracked_files(ps))
946 ret = 1;
948 if (include_untracked && get_untracked_files(ps, include_untracked,
949 untracked_files))
950 ret = 1;
952 return ret;
955 static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
956 struct strbuf files)
958 int ret = 0;
959 struct strbuf untracked_msg = STRBUF_INIT;
960 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
961 struct index_state istate = { NULL };
963 cp_upd_index.git_cmd = 1;
964 argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
965 "--remove", "--stdin", NULL);
966 argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
967 stash_index_path.buf);
969 strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
970 if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,
971 NULL, 0)) {
972 ret = -1;
973 goto done;
976 if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
977 NULL)) {
978 ret = -1;
979 goto done;
982 if (commit_tree(untracked_msg.buf, untracked_msg.len,
983 &info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
984 ret = -1;
985 goto done;
988 done:
989 discard_index(&istate);
990 strbuf_release(&untracked_msg);
991 remove_path(stash_index_path.buf);
992 return ret;
995 static int stash_patch(struct stash_info *info, const struct pathspec *ps,
996 struct strbuf *out_patch, int quiet)
998 int ret = 0;
999 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
1000 struct child_process cp_add_i = CHILD_PROCESS_INIT;
1001 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1002 struct index_state istate = { NULL };
1004 remove_path(stash_index_path.buf);
1006 cp_read_tree.git_cmd = 1;
1007 argv_array_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL);
1008 argv_array_pushf(&cp_read_tree.env_array, "GIT_INDEX_FILE=%s",
1009 stash_index_path.buf);
1010 if (run_command(&cp_read_tree)) {
1011 ret = -1;
1012 goto done;
1015 /* Find out what the user wants. */
1016 cp_add_i.git_cmd = 1;
1017 argv_array_pushl(&cp_add_i.args, "add--interactive", "--patch=stash",
1018 "--", NULL);
1019 add_pathspecs(&cp_add_i.args, ps);
1020 argv_array_pushf(&cp_add_i.env_array, "GIT_INDEX_FILE=%s",
1021 stash_index_path.buf);
1022 if (run_command(&cp_add_i)) {
1023 ret = -1;
1024 goto done;
1027 /* State of the working tree. */
1028 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1029 NULL)) {
1030 ret = -1;
1031 goto done;
1034 cp_diff_tree.git_cmd = 1;
1035 argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD",
1036 oid_to_hex(&info->w_tree), "--", NULL);
1037 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
1038 ret = -1;
1039 goto done;
1042 if (!out_patch->len) {
1043 if (!quiet)
1044 fprintf_ln(stderr, _("No changes selected"));
1045 ret = 1;
1048 done:
1049 discard_index(&istate);
1050 remove_path(stash_index_path.buf);
1051 return ret;
1054 static int stash_working_tree(struct stash_info *info, const struct pathspec *ps)
1056 int ret = 0;
1057 struct rev_info rev;
1058 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1059 struct strbuf diff_output = STRBUF_INIT;
1060 struct index_state istate = { NULL };
1062 init_revisions(&rev, NULL);
1063 copy_pathspec(&rev.prune_data, ps);
1065 set_alternate_index_output(stash_index_path.buf);
1066 if (reset_tree(&info->i_tree, 0, 0)) {
1067 ret = -1;
1068 goto done;
1070 set_alternate_index_output(NULL);
1072 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
1073 rev.diffopt.format_callback = add_diff_to_buf;
1074 rev.diffopt.format_callback_data = &diff_output;
1076 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1077 ret = -1;
1078 goto done;
1081 add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
1082 "");
1083 if (run_diff_index(&rev, 0)) {
1084 ret = -1;
1085 goto done;
1088 cp_upd_index.git_cmd = 1;
1089 argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
1090 "--remove", "--stdin", NULL);
1091 argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
1092 stash_index_path.buf);
1094 if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,
1095 NULL, 0, NULL, 0)) {
1096 ret = -1;
1097 goto done;
1100 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1101 NULL)) {
1102 ret = -1;
1103 goto done;
1106 done:
1107 discard_index(&istate);
1108 UNLEAK(rev);
1109 object_array_clear(&rev.pending);
1110 clear_pathspec(&rev.prune_data);
1111 strbuf_release(&diff_output);
1112 remove_path(stash_index_path.buf);
1113 return ret;
1116 static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,
1117 int include_untracked, int patch_mode,
1118 struct stash_info *info, struct strbuf *patch,
1119 int quiet)
1121 int ret = 0;
1122 int flags = 0;
1123 int untracked_commit_option = 0;
1124 const char *head_short_sha1 = NULL;
1125 const char *branch_ref = NULL;
1126 const char *branch_name = "(no branch)";
1127 struct commit *head_commit = NULL;
1128 struct commit_list *parents = NULL;
1129 struct strbuf msg = STRBUF_INIT;
1130 struct strbuf commit_tree_label = STRBUF_INIT;
1131 struct strbuf untracked_files = STRBUF_INIT;
1133 prepare_fallback_ident("git stash", "git@stash");
1135 read_cache_preload(NULL);
1136 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0) < 0) {
1137 ret = -1;
1138 goto done;
1141 if (get_oid("HEAD", &info->b_commit)) {
1142 if (!quiet)
1143 fprintf_ln(stderr, _("You do not have "
1144 "the initial commit yet"));
1145 ret = -1;
1146 goto done;
1147 } else {
1148 head_commit = lookup_commit(the_repository, &info->b_commit);
1151 if (!check_changes(ps, include_untracked, &untracked_files)) {
1152 ret = 1;
1153 goto done;
1156 branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
1157 if (flags & REF_ISSYMREF)
1158 branch_name = strrchr(branch_ref, '/') + 1;
1159 head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
1160 DEFAULT_ABBREV);
1161 strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);
1162 pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
1164 strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
1165 commit_list_insert(head_commit, &parents);
1166 if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
1167 commit_tree(commit_tree_label.buf, commit_tree_label.len,
1168 &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1169 if (!quiet)
1170 fprintf_ln(stderr, _("Cannot save the current "
1171 "index state"));
1172 ret = -1;
1173 goto done;
1176 if (include_untracked) {
1177 if (save_untracked_files(info, &msg, untracked_files)) {
1178 if (!quiet)
1179 fprintf_ln(stderr, _("Cannot save "
1180 "the untracked files"));
1181 ret = -1;
1182 goto done;
1184 untracked_commit_option = 1;
1186 if (patch_mode) {
1187 ret = stash_patch(info, ps, patch, quiet);
1188 if (ret < 0) {
1189 if (!quiet)
1190 fprintf_ln(stderr, _("Cannot save the current "
1191 "worktree state"));
1192 goto done;
1193 } else if (ret > 0) {
1194 goto done;
1196 } else {
1197 if (stash_working_tree(info, ps)) {
1198 if (!quiet)
1199 fprintf_ln(stderr, _("Cannot save the current "
1200 "worktree state"));
1201 ret = -1;
1202 goto done;
1206 if (!stash_msg_buf->len)
1207 strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf);
1208 else
1209 strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name);
1212 * `parents` will be empty after calling `commit_tree()`, so there is
1213 * no need to call `free_commit_list()`
1215 parents = NULL;
1216 if (untracked_commit_option)
1217 commit_list_insert(lookup_commit(the_repository,
1218 &info->u_commit),
1219 &parents);
1220 commit_list_insert(lookup_commit(the_repository, &info->i_commit),
1221 &parents);
1222 commit_list_insert(head_commit, &parents);
1224 if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
1225 parents, &info->w_commit, NULL, NULL)) {
1226 if (!quiet)
1227 fprintf_ln(stderr, _("Cannot record "
1228 "working tree state"));
1229 ret = -1;
1230 goto done;
1233 done:
1234 strbuf_release(&commit_tree_label);
1235 strbuf_release(&msg);
1236 strbuf_release(&untracked_files);
1237 return ret;
1240 static int create_stash(int argc, const char **argv, const char *prefix)
1242 int ret = 0;
1243 struct strbuf stash_msg_buf = STRBUF_INIT;
1244 struct stash_info info;
1245 struct pathspec ps;
1247 /* Starting with argv[1], since argv[0] is "create" */
1248 strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
1250 memset(&ps, 0, sizeof(ps));
1251 if (!check_changes_tracked_files(&ps))
1252 return 0;
1254 ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, &info,
1255 NULL, 0);
1256 if (!ret)
1257 printf_ln("%s", oid_to_hex(&info.w_commit));
1259 strbuf_release(&stash_msg_buf);
1260 return ret;
1263 static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,
1264 int keep_index, int patch_mode, int include_untracked)
1266 int ret = 0;
1267 struct stash_info info;
1268 struct strbuf patch = STRBUF_INIT;
1269 struct strbuf stash_msg_buf = STRBUF_INIT;
1270 struct strbuf untracked_files = STRBUF_INIT;
1272 if (patch_mode && keep_index == -1)
1273 keep_index = 1;
1275 if (patch_mode && include_untracked) {
1276 fprintf_ln(stderr, _("Can't use --patch and --include-untracked"
1277 " or --all at the same time"));
1278 ret = -1;
1279 goto done;
1282 read_cache_preload(NULL);
1283 if (!include_untracked && ps->nr) {
1284 int i;
1285 char *ps_matched = xcalloc(ps->nr, 1);
1287 for (i = 0; i < active_nr; i++)
1288 ce_path_match(&the_index, active_cache[i], ps,
1289 ps_matched);
1291 if (report_path_error(ps_matched, ps)) {
1292 fprintf_ln(stderr, _("Did you forget to 'git add'?"));
1293 ret = -1;
1294 free(ps_matched);
1295 goto done;
1297 free(ps_matched);
1300 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0)) {
1301 ret = -1;
1302 goto done;
1305 if (!check_changes(ps, include_untracked, &untracked_files)) {
1306 if (!quiet)
1307 printf_ln(_("No local changes to save"));
1308 goto done;
1311 if (!reflog_exists(ref_stash) && do_clear_stash()) {
1312 ret = -1;
1313 if (!quiet)
1314 fprintf_ln(stderr, _("Cannot initialize stash"));
1315 goto done;
1318 if (stash_msg)
1319 strbuf_addstr(&stash_msg_buf, stash_msg);
1320 if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1321 &info, &patch, quiet)) {
1322 ret = -1;
1323 goto done;
1326 if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
1327 ret = -1;
1328 if (!quiet)
1329 fprintf_ln(stderr, _("Cannot save the current status"));
1330 goto done;
1333 if (!quiet)
1334 printf_ln(_("Saved working directory and index state %s"),
1335 stash_msg_buf.buf);
1337 if (!patch_mode) {
1338 if (include_untracked && !ps->nr) {
1339 struct child_process cp = CHILD_PROCESS_INIT;
1341 cp.git_cmd = 1;
1342 argv_array_pushl(&cp.args, "clean", "--force",
1343 "--quiet", "-d", NULL);
1344 if (include_untracked == INCLUDE_ALL_FILES)
1345 argv_array_push(&cp.args, "-x");
1346 if (run_command(&cp)) {
1347 ret = -1;
1348 goto done;
1351 discard_cache();
1352 if (ps->nr) {
1353 struct child_process cp_add = CHILD_PROCESS_INIT;
1354 struct child_process cp_diff = CHILD_PROCESS_INIT;
1355 struct child_process cp_apply = CHILD_PROCESS_INIT;
1356 struct strbuf out = STRBUF_INIT;
1358 cp_add.git_cmd = 1;
1359 argv_array_push(&cp_add.args, "add");
1360 if (!include_untracked)
1361 argv_array_push(&cp_add.args, "-u");
1362 if (include_untracked == INCLUDE_ALL_FILES)
1363 argv_array_push(&cp_add.args, "--force");
1364 argv_array_push(&cp_add.args, "--");
1365 add_pathspecs(&cp_add.args, ps);
1366 if (run_command(&cp_add)) {
1367 ret = -1;
1368 goto done;
1371 cp_diff.git_cmd = 1;
1372 argv_array_pushl(&cp_diff.args, "diff-index", "-p",
1373 "--cached", "--binary", "HEAD", "--",
1374 NULL);
1375 add_pathspecs(&cp_diff.args, ps);
1376 if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {
1377 ret = -1;
1378 goto done;
1381 cp_apply.git_cmd = 1;
1382 argv_array_pushl(&cp_apply.args, "apply", "--index",
1383 "-R", NULL);
1384 if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,
1385 NULL, 0)) {
1386 ret = -1;
1387 goto done;
1389 } else {
1390 struct child_process cp = CHILD_PROCESS_INIT;
1391 cp.git_cmd = 1;
1392 argv_array_pushl(&cp.args, "reset", "--hard", "-q",
1393 NULL);
1394 if (run_command(&cp)) {
1395 ret = -1;
1396 goto done;
1400 if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1401 struct child_process cp = CHILD_PROCESS_INIT;
1403 cp.git_cmd = 1;
1404 argv_array_pushl(&cp.args, "checkout", "--no-overlay",
1405 oid_to_hex(&info.i_tree), "--", NULL);
1406 if (!ps->nr)
1407 argv_array_push(&cp.args, ":/");
1408 else
1409 add_pathspecs(&cp.args, ps);
1410 if (run_command(&cp)) {
1411 ret = -1;
1412 goto done;
1415 goto done;
1416 } else {
1417 struct child_process cp = CHILD_PROCESS_INIT;
1419 cp.git_cmd = 1;
1420 argv_array_pushl(&cp.args, "apply", "-R", NULL);
1422 if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1423 if (!quiet)
1424 fprintf_ln(stderr, _("Cannot remove "
1425 "worktree changes"));
1426 ret = -1;
1427 goto done;
1430 if (keep_index < 1) {
1431 struct child_process cp = CHILD_PROCESS_INIT;
1433 cp.git_cmd = 1;
1434 argv_array_pushl(&cp.args, "reset", "-q", "--", NULL);
1435 add_pathspecs(&cp.args, ps);
1436 if (run_command(&cp)) {
1437 ret = -1;
1438 goto done;
1441 goto done;
1444 done:
1445 strbuf_release(&stash_msg_buf);
1446 return ret;
1449 static int push_stash(int argc, const char **argv, const char *prefix)
1451 int keep_index = -1;
1452 int patch_mode = 0;
1453 int include_untracked = 0;
1454 int quiet = 0;
1455 const char *stash_msg = NULL;
1456 struct pathspec ps;
1457 struct option options[] = {
1458 OPT_BOOL('k', "keep-index", &keep_index,
1459 N_("keep index")),
1460 OPT_BOOL('p', "patch", &patch_mode,
1461 N_("stash in patch mode")),
1462 OPT__QUIET(&quiet, N_("quiet mode")),
1463 OPT_BOOL('u', "include-untracked", &include_untracked,
1464 N_("include untracked files in stash")),
1465 OPT_SET_INT('a', "all", &include_untracked,
1466 N_("include ignore files"), 2),
1467 OPT_STRING('m', "message", &stash_msg, N_("message"),
1468 N_("stash message")),
1469 OPT_END()
1472 if (argc)
1473 argc = parse_options(argc, argv, prefix, options,
1474 git_stash_push_usage,
1477 parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1478 prefix, argv);
1479 return do_push_stash(&ps, stash_msg, quiet, keep_index, patch_mode,
1480 include_untracked);
1483 static int save_stash(int argc, const char **argv, const char *prefix)
1485 int keep_index = -1;
1486 int patch_mode = 0;
1487 int include_untracked = 0;
1488 int quiet = 0;
1489 int ret = 0;
1490 const char *stash_msg = NULL;
1491 struct pathspec ps;
1492 struct strbuf stash_msg_buf = STRBUF_INIT;
1493 struct option options[] = {
1494 OPT_BOOL('k', "keep-index", &keep_index,
1495 N_("keep index")),
1496 OPT_BOOL('p', "patch", &patch_mode,
1497 N_("stash in patch mode")),
1498 OPT__QUIET(&quiet, N_("quiet mode")),
1499 OPT_BOOL('u', "include-untracked", &include_untracked,
1500 N_("include untracked files in stash")),
1501 OPT_SET_INT('a', "all", &include_untracked,
1502 N_("include ignore files"), 2),
1503 OPT_STRING('m', "message", &stash_msg, "message",
1504 N_("stash message")),
1505 OPT_END()
1508 argc = parse_options(argc, argv, prefix, options,
1509 git_stash_save_usage,
1510 PARSE_OPT_KEEP_DASHDASH);
1512 if (argc)
1513 stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
1515 memset(&ps, 0, sizeof(ps));
1516 ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
1517 patch_mode, include_untracked);
1519 strbuf_release(&stash_msg_buf);
1520 return ret;
1523 static int use_builtin_stash(void)
1525 struct child_process cp = CHILD_PROCESS_INIT;
1526 struct strbuf out = STRBUF_INIT;
1527 int ret, env = git_env_bool("GIT_TEST_STASH_USE_BUILTIN", -1);
1529 if (env != -1)
1530 return env;
1532 argv_array_pushl(&cp.args,
1533 "config", "--bool", "stash.usebuiltin", NULL);
1534 cp.git_cmd = 1;
1535 if (capture_command(&cp, &out, 6)) {
1536 strbuf_release(&out);
1537 return 1;
1540 strbuf_trim(&out);
1541 ret = !strcmp("true", out.buf);
1542 strbuf_release(&out);
1543 return ret;
1546 int cmd_stash(int argc, const char **argv, const char *prefix)
1548 int i = -1;
1549 pid_t pid = getpid();
1550 const char *index_file;
1551 struct argv_array args = ARGV_ARRAY_INIT;
1553 struct option options[] = {
1554 OPT_END()
1557 if (!use_builtin_stash()) {
1558 const char *path = mkpath("%s/git-legacy-stash",
1559 git_exec_path());
1561 if (sane_execvp(path, (char **)argv) < 0)
1562 die_errno(_("could not exec %s"), path);
1563 else
1564 BUG("sane_execvp() returned???");
1567 prefix = setup_git_directory();
1568 trace_repo_setup(prefix);
1569 setup_work_tree();
1571 git_config(git_diff_basic_config, NULL);
1573 argc = parse_options(argc, argv, prefix, options, git_stash_usage,
1574 PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
1576 index_file = get_index_file();
1577 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1578 (uintmax_t)pid);
1580 if (!argc)
1581 return !!push_stash(0, NULL, prefix);
1582 else if (!strcmp(argv[0], "apply"))
1583 return !!apply_stash(argc, argv, prefix);
1584 else if (!strcmp(argv[0], "clear"))
1585 return !!clear_stash(argc, argv, prefix);
1586 else if (!strcmp(argv[0], "drop"))
1587 return !!drop_stash(argc, argv, prefix);
1588 else if (!strcmp(argv[0], "pop"))
1589 return !!pop_stash(argc, argv, prefix);
1590 else if (!strcmp(argv[0], "branch"))
1591 return !!branch_stash(argc, argv, prefix);
1592 else if (!strcmp(argv[0], "list"))
1593 return !!list_stash(argc, argv, prefix);
1594 else if (!strcmp(argv[0], "show"))
1595 return !!show_stash(argc, argv, prefix);
1596 else if (!strcmp(argv[0], "store"))
1597 return !!store_stash(argc, argv, prefix);
1598 else if (!strcmp(argv[0], "create"))
1599 return !!create_stash(argc, argv, prefix);
1600 else if (!strcmp(argv[0], "push"))
1601 return !!push_stash(argc, argv, prefix);
1602 else if (!strcmp(argv[0], "save"))
1603 return !!save_stash(argc, argv, prefix);
1604 else if (*argv[0] != '-')
1605 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
1606 git_stash_usage, options);
1608 if (strcmp(argv[0], "-p")) {
1609 while (++i < argc && strcmp(argv[i], "--")) {
1611 * `akpqu` is a string which contains all short options,
1612 * except `-m` which is verified separately.
1614 if ((strlen(argv[i]) == 2) && *argv[i] == '-' &&
1615 strchr("akpqu", argv[i][1]))
1616 continue;
1618 if (!strcmp(argv[i], "--all") ||
1619 !strcmp(argv[i], "--keep-index") ||
1620 !strcmp(argv[i], "--no-keep-index") ||
1621 !strcmp(argv[i], "--patch") ||
1622 !strcmp(argv[i], "--quiet") ||
1623 !strcmp(argv[i], "--include-untracked"))
1624 continue;
1627 * `-m` and `--message=` are verified separately because
1628 * they need to be immediately followed by a string
1629 * (i.e.`-m"foobar"` or `--message="foobar"`).
1631 if (starts_with(argv[i], "-m") ||
1632 starts_with(argv[i], "--message="))
1633 continue;
1635 usage_with_options(git_stash_usage, options);
1639 argv_array_push(&args, "push");
1640 argv_array_pushv(&args, argv);
1641 return !!push_stash(args.argc, args.argv, prefix);