status: ignore status.aheadbehind in porcelain formats
[git/debian.git] / builtin / stash.c
blob2a8e6d09b406345519201ef64f572197a409e7a4
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_cache(REFRESH_QUIET))
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_cache(REFRESH_QUIET))
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_push(&cp.args, "status");
501 run_command(&cp);
504 return 0;
507 static int apply_stash(int argc, const char **argv, const char *prefix)
509 int ret;
510 int quiet = 0;
511 int index = 0;
512 struct stash_info info;
513 struct option options[] = {
514 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
515 OPT_BOOL(0, "index", &index,
516 N_("attempt to recreate the index")),
517 OPT_END()
520 argc = parse_options(argc, argv, prefix, options,
521 git_stash_apply_usage, 0);
523 if (get_stash_info(&info, argc, argv))
524 return -1;
526 ret = do_apply_stash(prefix, &info, index, quiet);
527 free_stash_info(&info);
528 return ret;
531 static int do_drop_stash(struct stash_info *info, int quiet)
533 int ret;
534 struct child_process cp_reflog = CHILD_PROCESS_INIT;
535 struct child_process cp = CHILD_PROCESS_INIT;
538 * reflog does not provide a simple function for deleting refs. One will
539 * need to be added to avoid implementing too much reflog code here
542 cp_reflog.git_cmd = 1;
543 argv_array_pushl(&cp_reflog.args, "reflog", "delete", "--updateref",
544 "--rewrite", NULL);
545 argv_array_push(&cp_reflog.args, info->revision.buf);
546 ret = run_command(&cp_reflog);
547 if (!ret) {
548 if (!quiet)
549 printf_ln(_("Dropped %s (%s)"), info->revision.buf,
550 oid_to_hex(&info->w_commit));
551 } else {
552 return error(_("%s: Could not drop stash entry"),
553 info->revision.buf);
557 * This could easily be replaced by get_oid, but currently it will throw
558 * a fatal error when a reflog is empty, which we can not recover from.
560 cp.git_cmd = 1;
561 /* Even though --quiet is specified, rev-parse still outputs the hash */
562 cp.no_stdout = 1;
563 argv_array_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL);
564 argv_array_pushf(&cp.args, "%s@{0}", ref_stash);
565 ret = run_command(&cp);
567 /* do_clear_stash if we just dropped the last stash entry */
568 if (ret)
569 do_clear_stash();
571 return 0;
574 static void assert_stash_ref(struct stash_info *info)
576 if (!info->is_stash_ref) {
577 error(_("'%s' is not a stash reference"), info->revision.buf);
578 free_stash_info(info);
579 exit(1);
583 static int drop_stash(int argc, const char **argv, const char *prefix)
585 int ret;
586 int quiet = 0;
587 struct stash_info info;
588 struct option options[] = {
589 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
590 OPT_END()
593 argc = parse_options(argc, argv, prefix, options,
594 git_stash_drop_usage, 0);
596 if (get_stash_info(&info, argc, argv))
597 return -1;
599 assert_stash_ref(&info);
601 ret = do_drop_stash(&info, quiet);
602 free_stash_info(&info);
603 return ret;
606 static int pop_stash(int argc, const char **argv, const char *prefix)
608 int ret;
609 int index = 0;
610 int quiet = 0;
611 struct stash_info info;
612 struct option options[] = {
613 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
614 OPT_BOOL(0, "index", &index,
615 N_("attempt to recreate the index")),
616 OPT_END()
619 argc = parse_options(argc, argv, prefix, options,
620 git_stash_pop_usage, 0);
622 if (get_stash_info(&info, argc, argv))
623 return -1;
625 assert_stash_ref(&info);
626 if ((ret = do_apply_stash(prefix, &info, index, quiet)))
627 printf_ln(_("The stash entry is kept in case "
628 "you need it again."));
629 else
630 ret = do_drop_stash(&info, quiet);
632 free_stash_info(&info);
633 return ret;
636 static int branch_stash(int argc, const char **argv, const char *prefix)
638 int ret;
639 const char *branch = NULL;
640 struct stash_info info;
641 struct child_process cp = CHILD_PROCESS_INIT;
642 struct option options[] = {
643 OPT_END()
646 argc = parse_options(argc, argv, prefix, options,
647 git_stash_branch_usage, 0);
649 if (!argc) {
650 fprintf_ln(stderr, _("No branch name specified"));
651 return -1;
654 branch = argv[0];
656 if (get_stash_info(&info, argc - 1, argv + 1))
657 return -1;
659 cp.git_cmd = 1;
660 argv_array_pushl(&cp.args, "checkout", "-b", NULL);
661 argv_array_push(&cp.args, branch);
662 argv_array_push(&cp.args, oid_to_hex(&info.b_commit));
663 ret = run_command(&cp);
664 if (!ret)
665 ret = do_apply_stash(prefix, &info, 1, 0);
666 if (!ret && info.is_stash_ref)
667 ret = do_drop_stash(&info, 0);
669 free_stash_info(&info);
671 return ret;
674 static int list_stash(int argc, const char **argv, const char *prefix)
676 struct child_process cp = CHILD_PROCESS_INIT;
677 struct option options[] = {
678 OPT_END()
681 argc = parse_options(argc, argv, prefix, options,
682 git_stash_list_usage,
683 PARSE_OPT_KEEP_UNKNOWN);
685 if (!ref_exists(ref_stash))
686 return 0;
688 cp.git_cmd = 1;
689 argv_array_pushl(&cp.args, "log", "--format=%gd: %gs", "-g",
690 "--first-parent", "-m", NULL);
691 argv_array_pushv(&cp.args, argv);
692 argv_array_push(&cp.args, ref_stash);
693 argv_array_push(&cp.args, "--");
694 return run_command(&cp);
697 static int show_stat = 1;
698 static int show_patch;
700 static int git_stash_config(const char *var, const char *value, void *cb)
702 if (!strcmp(var, "stash.showstat")) {
703 show_stat = git_config_bool(var, value);
704 return 0;
706 if (!strcmp(var, "stash.showpatch")) {
707 show_patch = git_config_bool(var, value);
708 return 0;
710 return git_default_config(var, value, cb);
713 static int show_stash(int argc, const char **argv, const char *prefix)
715 int i;
716 int opts = 0;
717 int ret = 0;
718 struct stash_info info;
719 struct rev_info rev;
720 struct argv_array stash_args = ARGV_ARRAY_INIT;
721 struct option options[] = {
722 OPT_END()
725 init_diff_ui_defaults();
726 git_config(git_diff_ui_config, NULL);
727 init_revisions(&rev, prefix);
729 for (i = 1; i < argc; i++) {
730 if (argv[i][0] != '-')
731 argv_array_push(&stash_args, argv[i]);
732 else
733 opts++;
736 ret = get_stash_info(&info, stash_args.argc, stash_args.argv);
737 argv_array_clear(&stash_args);
738 if (ret)
739 return -1;
742 * The config settings are applied only if there are not passed
743 * any options.
745 if (!opts) {
746 git_config(git_stash_config, NULL);
747 if (show_stat)
748 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;
750 if (show_patch)
751 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
753 if (!show_stat && !show_patch) {
754 free_stash_info(&info);
755 return 0;
759 argc = setup_revisions(argc, argv, &rev, NULL);
760 if (argc > 1) {
761 free_stash_info(&info);
762 usage_with_options(git_stash_show_usage, options);
764 if (!rev.diffopt.output_format) {
765 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
766 diff_setup_done(&rev.diffopt);
769 rev.diffopt.flags.recursive = 1;
770 setup_diff_pager(&rev.diffopt);
771 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
772 log_tree_diff_flush(&rev);
774 free_stash_info(&info);
775 return diff_result_code(&rev.diffopt, 0);
778 static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
779 int quiet)
781 if (!stash_msg)
782 stash_msg = "Created via \"git stash store\".";
784 if (update_ref(stash_msg, ref_stash, w_commit, NULL,
785 REF_FORCE_CREATE_REFLOG,
786 quiet ? UPDATE_REFS_QUIET_ON_ERR :
787 UPDATE_REFS_MSG_ON_ERR)) {
788 if (!quiet) {
789 fprintf_ln(stderr, _("Cannot update %s with %s"),
790 ref_stash, oid_to_hex(w_commit));
792 return -1;
795 return 0;
798 static int store_stash(int argc, const char **argv, const char *prefix)
800 int quiet = 0;
801 const char *stash_msg = NULL;
802 struct object_id obj;
803 struct object_context dummy;
804 struct option options[] = {
805 OPT__QUIET(&quiet, N_("be quiet")),
806 OPT_STRING('m', "message", &stash_msg, "message",
807 N_("stash message")),
808 OPT_END()
811 argc = parse_options(argc, argv, prefix, options,
812 git_stash_store_usage,
813 PARSE_OPT_KEEP_UNKNOWN);
815 if (argc != 1) {
816 if (!quiet)
817 fprintf_ln(stderr, _("\"git stash store\" requires one "
818 "<commit> argument"));
819 return -1;
822 if (get_oid_with_context(the_repository,
823 argv[0], quiet ? GET_OID_QUIETLY : 0, &obj,
824 &dummy)) {
825 if (!quiet)
826 fprintf_ln(stderr, _("Cannot update %s with %s"),
827 ref_stash, argv[0]);
828 return -1;
831 return do_store_stash(&obj, stash_msg, quiet);
834 static void add_pathspecs(struct argv_array *args,
835 const struct pathspec *ps) {
836 int i;
838 for (i = 0; i < ps->nr; i++)
839 argv_array_push(args, ps->items[i].original);
843 * `untracked_files` will be filled with the names of untracked files.
844 * The return value is:
846 * = 0 if there are not any untracked files
847 * > 0 if there are untracked files
849 static int get_untracked_files(const struct pathspec *ps, int include_untracked,
850 struct strbuf *untracked_files)
852 int i;
853 int max_len;
854 int found = 0;
855 char *seen;
856 struct dir_struct dir;
858 memset(&dir, 0, sizeof(dir));
859 if (include_untracked != INCLUDE_ALL_FILES)
860 setup_standard_excludes(&dir);
862 seen = xcalloc(ps->nr, 1);
864 max_len = fill_directory(&dir, the_repository->index, ps);
865 for (i = 0; i < dir.nr; i++) {
866 struct dir_entry *ent = dir.entries[i];
867 if (dir_path_match(&the_index, ent, ps, max_len, seen)) {
868 found++;
869 strbuf_addstr(untracked_files, ent->name);
870 /* NUL-terminate: will be fed to update-index -z */
871 strbuf_addch(untracked_files, '\0');
873 free(ent);
876 free(seen);
877 free(dir.entries);
878 free(dir.ignored);
879 clear_directory(&dir);
880 return found;
884 * The return value of `check_changes_tracked_files()` can be:
886 * < 0 if there was an error
887 * = 0 if there are no changes.
888 * > 0 if there are changes.
890 static int check_changes_tracked_files(const struct pathspec *ps)
892 int result;
893 struct rev_info rev;
894 struct object_id dummy;
895 int ret = 0;
897 /* No initial commit. */
898 if (get_oid("HEAD", &dummy))
899 return -1;
901 if (read_cache() < 0)
902 return -1;
904 init_revisions(&rev, NULL);
905 copy_pathspec(&rev.prune_data, ps);
907 rev.diffopt.flags.quick = 1;
908 rev.diffopt.flags.ignore_submodules = 1;
909 rev.abbrev = 0;
911 add_head_to_pending(&rev);
912 diff_setup_done(&rev.diffopt);
914 result = run_diff_index(&rev, 1);
915 if (diff_result_code(&rev.diffopt, result)) {
916 ret = 1;
917 goto done;
920 object_array_clear(&rev.pending);
921 result = run_diff_files(&rev, 0);
922 if (diff_result_code(&rev.diffopt, result)) {
923 ret = 1;
924 goto done;
927 done:
928 clear_pathspec(&rev.prune_data);
929 return ret;
933 * The function will fill `untracked_files` with the names of untracked files
934 * It will return 1 if there were any changes and 0 if there were not.
936 static int check_changes(const struct pathspec *ps, int include_untracked,
937 struct strbuf *untracked_files)
939 int ret = 0;
940 if (check_changes_tracked_files(ps))
941 ret = 1;
943 if (include_untracked && get_untracked_files(ps, include_untracked,
944 untracked_files))
945 ret = 1;
947 return ret;
950 static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
951 struct strbuf files)
953 int ret = 0;
954 struct strbuf untracked_msg = STRBUF_INIT;
955 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
956 struct index_state istate = { NULL };
958 cp_upd_index.git_cmd = 1;
959 argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
960 "--remove", "--stdin", NULL);
961 argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
962 stash_index_path.buf);
964 strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
965 if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,
966 NULL, 0)) {
967 ret = -1;
968 goto done;
971 if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
972 NULL)) {
973 ret = -1;
974 goto done;
977 if (commit_tree(untracked_msg.buf, untracked_msg.len,
978 &info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
979 ret = -1;
980 goto done;
983 done:
984 discard_index(&istate);
985 strbuf_release(&untracked_msg);
986 remove_path(stash_index_path.buf);
987 return ret;
990 static int stash_patch(struct stash_info *info, const struct pathspec *ps,
991 struct strbuf *out_patch, int quiet)
993 int ret = 0;
994 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
995 struct child_process cp_add_i = CHILD_PROCESS_INIT;
996 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
997 struct index_state istate = { NULL };
999 remove_path(stash_index_path.buf);
1001 cp_read_tree.git_cmd = 1;
1002 argv_array_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL);
1003 argv_array_pushf(&cp_read_tree.env_array, "GIT_INDEX_FILE=%s",
1004 stash_index_path.buf);
1005 if (run_command(&cp_read_tree)) {
1006 ret = -1;
1007 goto done;
1010 /* Find out what the user wants. */
1011 cp_add_i.git_cmd = 1;
1012 argv_array_pushl(&cp_add_i.args, "add--interactive", "--patch=stash",
1013 "--", NULL);
1014 add_pathspecs(&cp_add_i.args, ps);
1015 argv_array_pushf(&cp_add_i.env_array, "GIT_INDEX_FILE=%s",
1016 stash_index_path.buf);
1017 if (run_command(&cp_add_i)) {
1018 ret = -1;
1019 goto done;
1022 /* State of the working tree. */
1023 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1024 NULL)) {
1025 ret = -1;
1026 goto done;
1029 cp_diff_tree.git_cmd = 1;
1030 argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD",
1031 oid_to_hex(&info->w_tree), "--", NULL);
1032 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
1033 ret = -1;
1034 goto done;
1037 if (!out_patch->len) {
1038 if (!quiet)
1039 fprintf_ln(stderr, _("No changes selected"));
1040 ret = 1;
1043 done:
1044 discard_index(&istate);
1045 remove_path(stash_index_path.buf);
1046 return ret;
1049 static int stash_working_tree(struct stash_info *info, const struct pathspec *ps)
1051 int ret = 0;
1052 struct rev_info rev;
1053 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1054 struct strbuf diff_output = STRBUF_INIT;
1055 struct index_state istate = { NULL };
1057 init_revisions(&rev, NULL);
1058 copy_pathspec(&rev.prune_data, ps);
1060 set_alternate_index_output(stash_index_path.buf);
1061 if (reset_tree(&info->i_tree, 0, 0)) {
1062 ret = -1;
1063 goto done;
1065 set_alternate_index_output(NULL);
1067 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
1068 rev.diffopt.format_callback = add_diff_to_buf;
1069 rev.diffopt.format_callback_data = &diff_output;
1071 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1072 ret = -1;
1073 goto done;
1076 add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
1077 "");
1078 if (run_diff_index(&rev, 0)) {
1079 ret = -1;
1080 goto done;
1083 cp_upd_index.git_cmd = 1;
1084 argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
1085 "--remove", "--stdin", NULL);
1086 argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
1087 stash_index_path.buf);
1089 if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,
1090 NULL, 0, NULL, 0)) {
1091 ret = -1;
1092 goto done;
1095 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1096 NULL)) {
1097 ret = -1;
1098 goto done;
1101 done:
1102 discard_index(&istate);
1103 UNLEAK(rev);
1104 object_array_clear(&rev.pending);
1105 clear_pathspec(&rev.prune_data);
1106 strbuf_release(&diff_output);
1107 remove_path(stash_index_path.buf);
1108 return ret;
1111 static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,
1112 int include_untracked, int patch_mode,
1113 struct stash_info *info, struct strbuf *patch,
1114 int quiet)
1116 int ret = 0;
1117 int flags = 0;
1118 int untracked_commit_option = 0;
1119 const char *head_short_sha1 = NULL;
1120 const char *branch_ref = NULL;
1121 const char *branch_name = "(no branch)";
1122 struct commit *head_commit = NULL;
1123 struct commit_list *parents = NULL;
1124 struct strbuf msg = STRBUF_INIT;
1125 struct strbuf commit_tree_label = STRBUF_INIT;
1126 struct strbuf untracked_files = STRBUF_INIT;
1128 prepare_fallback_ident("git stash", "git@stash");
1130 read_cache_preload(NULL);
1131 refresh_cache(REFRESH_QUIET);
1133 if (get_oid("HEAD", &info->b_commit)) {
1134 if (!quiet)
1135 fprintf_ln(stderr, _("You do not have "
1136 "the initial commit yet"));
1137 ret = -1;
1138 goto done;
1139 } else {
1140 head_commit = lookup_commit(the_repository, &info->b_commit);
1143 if (!check_changes(ps, include_untracked, &untracked_files)) {
1144 ret = 1;
1145 goto done;
1148 branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
1149 if (flags & REF_ISSYMREF)
1150 branch_name = strrchr(branch_ref, '/') + 1;
1151 head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
1152 DEFAULT_ABBREV);
1153 strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);
1154 pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
1156 strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
1157 commit_list_insert(head_commit, &parents);
1158 if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
1159 commit_tree(commit_tree_label.buf, commit_tree_label.len,
1160 &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1161 if (!quiet)
1162 fprintf_ln(stderr, _("Cannot save the current "
1163 "index state"));
1164 ret = -1;
1165 goto done;
1168 if (include_untracked) {
1169 if (save_untracked_files(info, &msg, untracked_files)) {
1170 if (!quiet)
1171 fprintf_ln(stderr, _("Cannot save "
1172 "the untracked files"));
1173 ret = -1;
1174 goto done;
1176 untracked_commit_option = 1;
1178 if (patch_mode) {
1179 ret = stash_patch(info, ps, patch, quiet);
1180 if (ret < 0) {
1181 if (!quiet)
1182 fprintf_ln(stderr, _("Cannot save the current "
1183 "worktree state"));
1184 goto done;
1185 } else if (ret > 0) {
1186 goto done;
1188 } else {
1189 if (stash_working_tree(info, ps)) {
1190 if (!quiet)
1191 fprintf_ln(stderr, _("Cannot save the current "
1192 "worktree state"));
1193 ret = -1;
1194 goto done;
1198 if (!stash_msg_buf->len)
1199 strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf);
1200 else
1201 strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name);
1204 * `parents` will be empty after calling `commit_tree()`, so there is
1205 * no need to call `free_commit_list()`
1207 parents = NULL;
1208 if (untracked_commit_option)
1209 commit_list_insert(lookup_commit(the_repository,
1210 &info->u_commit),
1211 &parents);
1212 commit_list_insert(lookup_commit(the_repository, &info->i_commit),
1213 &parents);
1214 commit_list_insert(head_commit, &parents);
1216 if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
1217 parents, &info->w_commit, NULL, NULL)) {
1218 if (!quiet)
1219 fprintf_ln(stderr, _("Cannot record "
1220 "working tree state"));
1221 ret = -1;
1222 goto done;
1225 done:
1226 strbuf_release(&commit_tree_label);
1227 strbuf_release(&msg);
1228 strbuf_release(&untracked_files);
1229 return ret;
1232 static int create_stash(int argc, const char **argv, const char *prefix)
1234 int ret = 0;
1235 struct strbuf stash_msg_buf = STRBUF_INIT;
1236 struct stash_info info;
1237 struct pathspec ps;
1239 /* Starting with argv[1], since argv[0] is "create" */
1240 strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
1242 memset(&ps, 0, sizeof(ps));
1243 if (!check_changes_tracked_files(&ps))
1244 return 0;
1246 ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, &info,
1247 NULL, 0);
1248 if (!ret)
1249 printf_ln("%s", oid_to_hex(&info.w_commit));
1251 strbuf_release(&stash_msg_buf);
1252 return ret;
1255 static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,
1256 int keep_index, int patch_mode, int include_untracked)
1258 int ret = 0;
1259 struct stash_info info;
1260 struct strbuf patch = STRBUF_INIT;
1261 struct strbuf stash_msg_buf = STRBUF_INIT;
1262 struct strbuf untracked_files = STRBUF_INIT;
1264 if (patch_mode && keep_index == -1)
1265 keep_index = 1;
1267 if (patch_mode && include_untracked) {
1268 fprintf_ln(stderr, _("Can't use --patch and --include-untracked"
1269 " or --all at the same time"));
1270 ret = -1;
1271 goto done;
1274 read_cache_preload(NULL);
1275 if (!include_untracked && ps->nr) {
1276 int i;
1277 char *ps_matched = xcalloc(ps->nr, 1);
1279 for (i = 0; i < active_nr; i++)
1280 ce_path_match(&the_index, active_cache[i], ps,
1281 ps_matched);
1283 if (report_path_error(ps_matched, ps)) {
1284 fprintf_ln(stderr, _("Did you forget to 'git add'?"));
1285 ret = -1;
1286 free(ps_matched);
1287 goto done;
1289 free(ps_matched);
1292 if (refresh_cache(REFRESH_QUIET)) {
1293 ret = -1;
1294 goto done;
1297 if (!check_changes(ps, include_untracked, &untracked_files)) {
1298 if (!quiet)
1299 printf_ln(_("No local changes to save"));
1300 goto done;
1303 if (!reflog_exists(ref_stash) && do_clear_stash()) {
1304 ret = -1;
1305 if (!quiet)
1306 fprintf_ln(stderr, _("Cannot initialize stash"));
1307 goto done;
1310 if (stash_msg)
1311 strbuf_addstr(&stash_msg_buf, stash_msg);
1312 if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1313 &info, &patch, quiet)) {
1314 ret = -1;
1315 goto done;
1318 if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
1319 ret = -1;
1320 if (!quiet)
1321 fprintf_ln(stderr, _("Cannot save the current status"));
1322 goto done;
1325 if (!quiet)
1326 printf_ln(_("Saved working directory and index state %s"),
1327 stash_msg_buf.buf);
1329 if (!patch_mode) {
1330 if (include_untracked && !ps->nr) {
1331 struct child_process cp = CHILD_PROCESS_INIT;
1333 cp.git_cmd = 1;
1334 argv_array_pushl(&cp.args, "clean", "--force",
1335 "--quiet", "-d", NULL);
1336 if (include_untracked == INCLUDE_ALL_FILES)
1337 argv_array_push(&cp.args, "-x");
1338 if (run_command(&cp)) {
1339 ret = -1;
1340 goto done;
1343 discard_cache();
1344 if (ps->nr) {
1345 struct child_process cp_add = CHILD_PROCESS_INIT;
1346 struct child_process cp_diff = CHILD_PROCESS_INIT;
1347 struct child_process cp_apply = CHILD_PROCESS_INIT;
1348 struct strbuf out = STRBUF_INIT;
1350 cp_add.git_cmd = 1;
1351 argv_array_push(&cp_add.args, "add");
1352 if (!include_untracked)
1353 argv_array_push(&cp_add.args, "-u");
1354 if (include_untracked == INCLUDE_ALL_FILES)
1355 argv_array_push(&cp_add.args, "--force");
1356 argv_array_push(&cp_add.args, "--");
1357 add_pathspecs(&cp_add.args, ps);
1358 if (run_command(&cp_add)) {
1359 ret = -1;
1360 goto done;
1363 cp_diff.git_cmd = 1;
1364 argv_array_pushl(&cp_diff.args, "diff-index", "-p",
1365 "--cached", "--binary", "HEAD", "--",
1366 NULL);
1367 add_pathspecs(&cp_diff.args, ps);
1368 if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {
1369 ret = -1;
1370 goto done;
1373 cp_apply.git_cmd = 1;
1374 argv_array_pushl(&cp_apply.args, "apply", "--index",
1375 "-R", NULL);
1376 if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,
1377 NULL, 0)) {
1378 ret = -1;
1379 goto done;
1381 } else {
1382 struct child_process cp = CHILD_PROCESS_INIT;
1383 cp.git_cmd = 1;
1384 argv_array_pushl(&cp.args, "reset", "--hard", "-q",
1385 NULL);
1386 if (run_command(&cp)) {
1387 ret = -1;
1388 goto done;
1392 if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1393 struct child_process cp_ls = CHILD_PROCESS_INIT;
1394 struct child_process cp_checkout = CHILD_PROCESS_INIT;
1395 struct strbuf out = STRBUF_INIT;
1397 if (reset_tree(&info.i_tree, 0, 1)) {
1398 ret = -1;
1399 goto done;
1402 cp_ls.git_cmd = 1;
1403 argv_array_pushl(&cp_ls.args, "ls-files", "-z",
1404 "--modified", "--", NULL);
1406 add_pathspecs(&cp_ls.args, ps);
1407 if (pipe_command(&cp_ls, NULL, 0, &out, 0, NULL, 0)) {
1408 ret = -1;
1409 goto done;
1412 cp_checkout.git_cmd = 1;
1413 argv_array_pushl(&cp_checkout.args, "checkout-index",
1414 "-z", "--force", "--stdin", NULL);
1415 if (pipe_command(&cp_checkout, out.buf, out.len, NULL,
1416 0, NULL, 0)) {
1417 ret = -1;
1418 goto done;
1421 goto done;
1422 } else {
1423 struct child_process cp = CHILD_PROCESS_INIT;
1425 cp.git_cmd = 1;
1426 argv_array_pushl(&cp.args, "apply", "-R", NULL);
1428 if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1429 if (!quiet)
1430 fprintf_ln(stderr, _("Cannot remove "
1431 "worktree changes"));
1432 ret = -1;
1433 goto done;
1436 if (keep_index < 1) {
1437 struct child_process cp = CHILD_PROCESS_INIT;
1439 cp.git_cmd = 1;
1440 argv_array_pushl(&cp.args, "reset", "-q", "--", NULL);
1441 add_pathspecs(&cp.args, ps);
1442 if (run_command(&cp)) {
1443 ret = -1;
1444 goto done;
1447 goto done;
1450 done:
1451 strbuf_release(&stash_msg_buf);
1452 return ret;
1455 static int push_stash(int argc, const char **argv, const char *prefix)
1457 int keep_index = -1;
1458 int patch_mode = 0;
1459 int include_untracked = 0;
1460 int quiet = 0;
1461 const char *stash_msg = NULL;
1462 struct pathspec ps;
1463 struct option options[] = {
1464 OPT_BOOL('k', "keep-index", &keep_index,
1465 N_("keep index")),
1466 OPT_BOOL('p', "patch", &patch_mode,
1467 N_("stash in patch mode")),
1468 OPT__QUIET(&quiet, N_("quiet mode")),
1469 OPT_BOOL('u', "include-untracked", &include_untracked,
1470 N_("include untracked files in stash")),
1471 OPT_SET_INT('a', "all", &include_untracked,
1472 N_("include ignore files"), 2),
1473 OPT_STRING('m', "message", &stash_msg, N_("message"),
1474 N_("stash message")),
1475 OPT_END()
1478 if (argc)
1479 argc = parse_options(argc, argv, prefix, options,
1480 git_stash_push_usage,
1483 parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1484 prefix, argv);
1485 return do_push_stash(&ps, stash_msg, quiet, keep_index, patch_mode,
1486 include_untracked);
1489 static int save_stash(int argc, const char **argv, const char *prefix)
1491 int keep_index = -1;
1492 int patch_mode = 0;
1493 int include_untracked = 0;
1494 int quiet = 0;
1495 int ret = 0;
1496 const char *stash_msg = NULL;
1497 struct pathspec ps;
1498 struct strbuf stash_msg_buf = STRBUF_INIT;
1499 struct option options[] = {
1500 OPT_BOOL('k', "keep-index", &keep_index,
1501 N_("keep index")),
1502 OPT_BOOL('p', "patch", &patch_mode,
1503 N_("stash in patch mode")),
1504 OPT__QUIET(&quiet, N_("quiet mode")),
1505 OPT_BOOL('u', "include-untracked", &include_untracked,
1506 N_("include untracked files in stash")),
1507 OPT_SET_INT('a', "all", &include_untracked,
1508 N_("include ignore files"), 2),
1509 OPT_STRING('m', "message", &stash_msg, "message",
1510 N_("stash message")),
1511 OPT_END()
1514 argc = parse_options(argc, argv, prefix, options,
1515 git_stash_save_usage,
1516 PARSE_OPT_KEEP_DASHDASH);
1518 if (argc)
1519 stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
1521 memset(&ps, 0, sizeof(ps));
1522 ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
1523 patch_mode, include_untracked);
1525 strbuf_release(&stash_msg_buf);
1526 return ret;
1529 static int use_builtin_stash(void)
1531 struct child_process cp = CHILD_PROCESS_INIT;
1532 struct strbuf out = STRBUF_INIT;
1533 int ret, env = git_env_bool("GIT_TEST_STASH_USE_BUILTIN", -1);
1535 if (env != -1)
1536 return env;
1538 argv_array_pushl(&cp.args,
1539 "config", "--bool", "stash.usebuiltin", NULL);
1540 cp.git_cmd = 1;
1541 if (capture_command(&cp, &out, 6)) {
1542 strbuf_release(&out);
1543 return 1;
1546 strbuf_trim(&out);
1547 ret = !strcmp("true", out.buf);
1548 strbuf_release(&out);
1549 return ret;
1552 int cmd_stash(int argc, const char **argv, const char *prefix)
1554 int i = -1;
1555 pid_t pid = getpid();
1556 const char *index_file;
1557 struct argv_array args = ARGV_ARRAY_INIT;
1559 struct option options[] = {
1560 OPT_END()
1563 if (!use_builtin_stash()) {
1564 const char *path = mkpath("%s/git-legacy-stash",
1565 git_exec_path());
1567 if (sane_execvp(path, (char **)argv) < 0)
1568 die_errno(_("could not exec %s"), path);
1569 else
1570 BUG("sane_execvp() returned???");
1573 prefix = setup_git_directory();
1574 trace_repo_setup(prefix);
1575 setup_work_tree();
1577 git_config(git_diff_basic_config, NULL);
1579 argc = parse_options(argc, argv, prefix, options, git_stash_usage,
1580 PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
1582 index_file = get_index_file();
1583 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1584 (uintmax_t)pid);
1586 if (!argc)
1587 return !!push_stash(0, NULL, prefix);
1588 else if (!strcmp(argv[0], "apply"))
1589 return !!apply_stash(argc, argv, prefix);
1590 else if (!strcmp(argv[0], "clear"))
1591 return !!clear_stash(argc, argv, prefix);
1592 else if (!strcmp(argv[0], "drop"))
1593 return !!drop_stash(argc, argv, prefix);
1594 else if (!strcmp(argv[0], "pop"))
1595 return !!pop_stash(argc, argv, prefix);
1596 else if (!strcmp(argv[0], "branch"))
1597 return !!branch_stash(argc, argv, prefix);
1598 else if (!strcmp(argv[0], "list"))
1599 return !!list_stash(argc, argv, prefix);
1600 else if (!strcmp(argv[0], "show"))
1601 return !!show_stash(argc, argv, prefix);
1602 else if (!strcmp(argv[0], "store"))
1603 return !!store_stash(argc, argv, prefix);
1604 else if (!strcmp(argv[0], "create"))
1605 return !!create_stash(argc, argv, prefix);
1606 else if (!strcmp(argv[0], "push"))
1607 return !!push_stash(argc, argv, prefix);
1608 else if (!strcmp(argv[0], "save"))
1609 return !!save_stash(argc, argv, prefix);
1610 else if (*argv[0] != '-')
1611 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
1612 git_stash_usage, options);
1614 if (strcmp(argv[0], "-p")) {
1615 while (++i < argc && strcmp(argv[i], "--")) {
1617 * `akpqu` is a string which contains all short options,
1618 * except `-m` which is verified separately.
1620 if ((strlen(argv[i]) == 2) && *argv[i] == '-' &&
1621 strchr("akpqu", argv[i][1]))
1622 continue;
1624 if (!strcmp(argv[i], "--all") ||
1625 !strcmp(argv[i], "--keep-index") ||
1626 !strcmp(argv[i], "--no-keep-index") ||
1627 !strcmp(argv[i], "--patch") ||
1628 !strcmp(argv[i], "--quiet") ||
1629 !strcmp(argv[i], "--include-untracked"))
1630 continue;
1633 * `-m` and `--message=` are verified separately because
1634 * they need to be immediately followed by a string
1635 * (i.e.`-m"foobar"` or `--message="foobar"`).
1637 if (starts_with(argv[i], "-m") ||
1638 starts_with(argv[i], "--message="))
1639 continue;
1641 usage_with_options(git_stash_usage, options);
1645 argv_array_push(&args, "push");
1646 argv_array_pushv(&args, argv);
1647 return !!push_stash(args.argc, args.argv, prefix);