1#include "builtin.h"
2#include "config.h"
3#include "parse-options.h"
4#include "refs.h"
5#include "lockfile.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"
11#include "dir.h"
12#include "rerere.h"
13#include "revision.h"
14#include "log-tree.h"
15#include "diffcore.h"
16#include "exec-cmd.h"
17
18#define INCLUDE_ALL_FILES 2
19
20static 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>]"),
32 NULL
33};
34
35static const char * const git_stash_list_usage[] = {
36 N_("git stash list [<options>]"),
37 NULL
38};
39
40static const char * const git_stash_show_usage[] = {
41 N_("git stash show [<options>] [<stash>]"),
42 NULL
43};
44
45static const char * const git_stash_drop_usage[] = {
46 N_("git stash drop [-q|--quiet] [<stash>]"),
47 NULL
48};
49
50static const char * const git_stash_pop_usage[] = {
51 N_("git stash pop [--index] [-q|--quiet] [<stash>]"),
52 NULL
53};
54
55static const char * const git_stash_apply_usage[] = {
56 N_("git stash apply [--index] [-q|--quiet] [<stash>]"),
57 NULL
58};
59
60static const char * const git_stash_branch_usage[] = {
61 N_("git stash branch <branchname> [<stash>]"),
62 NULL
63};
64
65static const char * const git_stash_clear_usage[] = {
66 N_("git stash clear"),
67 NULL
68};
69
70static const char * const git_stash_store_usage[] = {
71 N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"),
72 NULL
73};
74
75static 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>...]]"),
79 NULL
80};
81
82static 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>]"),
85 NULL
86};
87
88static const char *ref_stash = "refs/stash";
89static struct strbuf stash_index_path = STRBUF_INIT;
90
91/*
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
100 */
101struct stash_info {
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;
111 int is_stash_ref;
112 int has_u;
113};
114
115static void free_stash_info(struct stash_info *info)
116{
117 strbuf_release(&info->revision);
118}
119
120static void assert_stash_like(struct stash_info *info, const char *revision)
121{
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);
127}
128
129static int get_stash_info(struct stash_info *info, int argc, const char **argv)
130{
131 int ret;
132 char *end_of_rev;
133 char *expanded_ref;
134 const char *revision;
135 const char *commit = NULL;
136 struct object_id dummy;
137 struct strbuf symbolic = STRBUF_INIT;
138
139 if (argc > 1) {
140 int i;
141 struct strbuf refs_msg = STRBUF_INIT;
142
143 for (i = 0; i < argc; i++)
144 strbuf_addf(&refs_msg, " '%s'", argv[i]);
145
146 fprintf_ln(stderr, _("Too many revisions specified:%s"),
147 refs_msg.buf);
148 strbuf_release(&refs_msg);
149
150 return -1;
151 }
152
153 if (argc == 1)
154 commit = argv[0];
155
156 strbuf_init(&info->revision, 0);
157 if (!commit) {
158 if (!ref_exists(ref_stash)) {
159 free_stash_info(info);
160 fprintf_ln(stderr, _("No stash entries found."));
161 return -1;
162 }
163
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);
167 } else {
168 strbuf_addstr(&info->revision, commit);
169 }
170
171 revision = info->revision.buf;
172
173 if (get_oid(revision, &info->w_commit)) {
174 error(_("%s is not a valid reference"), revision);
175 free_stash_info(info);
176 return -1;
177 }
178
179 assert_stash_like(info, revision);
180
181 info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision);
182
183 end_of_rev = strchrnul(revision, '@');
184 strbuf_add(&symbolic, revision, end_of_rev - revision);
185
186 ret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref);
187 strbuf_release(&symbolic);
188 switch (ret) {
189 case 0: /* Not found, but valid ref */
190 info->is_stash_ref = 0;
191 break;
192 case 1:
193 info->is_stash_ref = !strcmp(expanded_ref, ref_stash);
194 break;
195 default: /* Invalid or ambiguous */
196 free_stash_info(info);
197 }
198
199 free(expanded_ref);
200 return !(ret == 0 || ret == 1);
201}
202
203static int do_clear_stash(void)
204{
205 struct object_id obj;
206 if (get_oid(ref_stash, &obj))
207 return 0;
208
209 return delete_ref(NULL, ref_stash, &obj, 0);
210}
211
212static int clear_stash(int argc, const char **argv, const char *prefix)
213{
214 struct option options[] = {
215 OPT_END()
216 };
217
218 argc = parse_options(argc, argv, prefix, options,
219 git_stash_clear_usage,
220 PARSE_OPT_STOP_AT_NON_OPTION);
221
222 if (argc)
223 return error(_("git stash clear with parameters is "
224 "unimplemented"));
225
226 return do_clear_stash();
227}
228
229static int reset_tree(struct object_id *i_tree, int update, int reset)
230{
231 int nr_trees = 1;
232 struct unpack_trees_options opts;
233 struct tree_desc t[MAX_UNPACK_TREES];
234 struct tree *tree;
235 struct lock_file lock_file = LOCK_INIT;
236
237 read_cache_preload(NULL);
238 if (refresh_cache(REFRESH_QUIET))
239 return -1;
240
241 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
242
243 memset(&opts, 0, sizeof(opts));
244
245 tree = parse_tree_indirect(i_tree);
246 if (parse_tree(tree))
247 return -1;
248
249 init_tree_desc(t, tree->buffer, tree->size);
250
251 opts.head_idx = 1;
252 opts.src_index = &the_index;
253 opts.dst_index = &the_index;
254 opts.merge = 1;
255 opts.reset = reset;
256 opts.update = update;
257 opts.fn = oneway_merge;
258
259 if (unpack_trees(nr_trees, t, &opts))
260 return -1;
261
262 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
263 return error(_("unable to write new index file"));
264
265 return 0;
266}
267
268static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
269{
270 struct child_process cp = CHILD_PROCESS_INIT;
271 const char *w_commit_hex = oid_to_hex(w_commit);
272
273 /*
274 * Diff-tree would not be very hard to replace with a native function,
275 * however it should be done together with apply_cached.
276 */
277 cp.git_cmd = 1;
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);
280
281 return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
282}
283
284static int apply_cached(struct strbuf *out)
285{
286 struct child_process cp = CHILD_PROCESS_INIT;
287
288 /*
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
291 * buffer.
292 */
293 cp.git_cmd = 1;
294 argv_array_pushl(&cp.args, "apply", "--cached", NULL);
295 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
296}
297
298static int reset_head(void)
299{
300 struct child_process cp = CHILD_PROCESS_INIT;
301
302 /*
303 * Reset is overall quite simple, however there is no current public
304 * API for resetting.
305 */
306 cp.git_cmd = 1;
307 argv_array_push(&cp.args, "reset");
308
309 return run_command(&cp);
310}
311
312static void add_diff_to_buf(struct diff_queue_struct *q,
313 struct diff_options *options,
314 void *data)
315{
316 int i;
317
318 for (i = 0; i < q->nr; i++) {
319 strbuf_addstr(data, q->queue[i]->one->path);
320
321 /* NUL-terminate: will be fed to update-index -z */
322 strbuf_addch(data, '\0');
323 }
324}
325
326static int get_newly_staged(struct strbuf *out, struct object_id *c_tree)
327{
328 struct child_process cp = CHILD_PROCESS_INIT;
329 const char *c_tree_hex = oid_to_hex(c_tree);
330
331 /*
332 * diff-index is very similar to diff-tree above, and should be
333 * converted together with update_index.
334 */
335 cp.git_cmd = 1;
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);
340}
341
342static int update_index(struct strbuf *out)
343{
344 struct child_process cp = CHILD_PROCESS_INIT;
345
346 /*
347 * Update-index is very complicated and may need to have a public
348 * function exposed in order to remove this forking.
349 */
350 cp.git_cmd = 1;
351 argv_array_pushl(&cp.args, "update-index", "--add", "--stdin", NULL);
352 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
353}
354
355static int restore_untracked(struct object_id *u_tree)
356{
357 int res;
358 struct child_process cp = CHILD_PROCESS_INIT;
359
360 /*
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.
364 */
365 cp.git_cmd = 1;
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);
372 return -1;
373 }
374
375 child_process_init(&cp);
376 cp.git_cmd = 1;
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);
380
381 res = run_command(&cp);
382 remove_path(stash_index_path.buf);
383 return res;
384}
385
386static int do_apply_stash(const char *prefix, struct stash_info *info,
387 int index, int quiet)
388{
389 int ret;
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];
396
397 read_cache_preload(NULL);
398 if (refresh_cache(REFRESH_QUIET))
399 return -1;
400
401 if (write_cache_as_tree(&c_tree, 0, NULL))
402 return error(_("cannot apply a stash in the middle of a merge"));
403
404 if (index) {
405 if (oideq(&info->b_tree, &info->i_tree) ||
406 oideq(&c_tree, &info->i_tree)) {
407 has_index = 0;
408 } else {
409 struct strbuf out = STRBUF_INIT;
410
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));
415 }
416
417 ret = apply_cached(&out);
418 strbuf_release(&out);
419 if (ret)
420 return error(_("conflicts in index."
421 "Try without --index."));
422
423 discard_cache();
424 read_cache();
425 if (write_cache_as_tree(&index_tree, 0, NULL))
426 return error(_("could not save index tree"));
427
428 reset_head();
429 }
430 }
431
432 if (info->has_u && restore_untracked(&info->u_tree))
433 return error(_("could not restore untracked files from stash"));
434
435 init_merge_options(&o);
436
437 o.branch1 = "Updated upstream";
438 o.branch2 = "Stashed changes";
439
440 if (oideq(&info->b_tree, &c_tree))
441 o.branch1 = "Version stash was based on";
442
443 if (quiet)
444 o.verbosity = 0;
445
446 if (o.verbosity >= 3)
447 printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
448
449 bases[0] = &info->b_tree;
450
451 ret = merge_recursive_generic(&o, &c_tree, &info->w_tree, 1, bases,
452 &result);
453 if (ret) {
454 rerere(0);
455
456 if (index)
457 fprintf_ln(stderr, _("Index was not unstashed."));
458
459 return ret;
460 }
461
462 if (has_index) {
463 if (reset_tree(&index_tree, 0, 0))
464 return -1;
465 } else {
466 struct strbuf out = STRBUF_INIT;
467
468 if (get_newly_staged(&out, &c_tree)) {
469 strbuf_release(&out);
470 return -1;
471 }
472
473 if (reset_tree(&c_tree, 0, 1)) {
474 strbuf_release(&out);
475 return -1;
476 }
477
478 ret = update_index(&out);
479 strbuf_release(&out);
480 if (ret)
481 return -1;
482
483 discard_cache();
484 }
485
486 if (quiet) {
487 if (refresh_cache(REFRESH_QUIET))
488 warning("could not refresh index");
489 } else {
490 struct child_process cp = CHILD_PROCESS_INIT;
491
492 /*
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.
496 */
497 cp.git_cmd = 1;
498 cp.dir = prefix;
499 argv_array_push(&cp.args, "status");
500 run_command(&cp);
501 }
502
503 return 0;
504}
505
506static int apply_stash(int argc, const char **argv, const char *prefix)
507{
508 int ret;
509 int quiet = 0;
510 int index = 0;
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")),
516 OPT_END()
517 };
518
519 argc = parse_options(argc, argv, prefix, options,
520 git_stash_apply_usage, 0);
521
522 if (get_stash_info(&info, argc, argv))
523 return -1;
524
525 ret = do_apply_stash(prefix, &info, index, quiet);
526 free_stash_info(&info);
527 return ret;
528}
529
530static int do_drop_stash(const char *prefix, struct stash_info *info, int quiet)
531{
532 int ret;
533 struct child_process cp_reflog = CHILD_PROCESS_INIT;
534 struct child_process cp = CHILD_PROCESS_INIT;
535
536 /*
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
539 */
540
541 cp_reflog.git_cmd = 1;
542 argv_array_pushl(&cp_reflog.args, "reflog", "delete", "--updateref",
543 "--rewrite", NULL);
544 argv_array_push(&cp_reflog.args, info->revision.buf);
545 ret = run_command(&cp_reflog);
546 if (!ret) {
547 if (!quiet)
548 printf_ln(_("Dropped %s (%s)"), info->revision.buf,
549 oid_to_hex(&info->w_commit));
550 } else {
551 return error(_("%s: Could not drop stash entry"),
552 info->revision.buf);
553 }
554
555 /*
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.
558 */
559 cp.git_cmd = 1;
560 /* Even though --quiet is specified, rev-parse still outputs the hash */
561 cp.no_stdout = 1;
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);
565
566 /* do_clear_stash if we just dropped the last stash entry */
567 if (ret)
568 do_clear_stash();
569
570 return 0;
571}
572
573static void assert_stash_ref(struct stash_info *info)
574{
575 if (!info->is_stash_ref) {
576 error(_("'%s' is not a stash reference"), info->revision.buf);
577 free_stash_info(info);
578 exit(1);
579 }
580}
581
582static int drop_stash(int argc, const char **argv, const char *prefix)
583{
584 int ret;
585 int quiet = 0;
586 struct stash_info info;
587 struct option options[] = {
588 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
589 OPT_END()
590 };
591
592 argc = parse_options(argc, argv, prefix, options,
593 git_stash_drop_usage, 0);
594
595 if (get_stash_info(&info, argc, argv))
596 return -1;
597
598 assert_stash_ref(&info);
599
600 ret = do_drop_stash(prefix, &info, quiet);
601 free_stash_info(&info);
602 return ret;
603}
604
605static int pop_stash(int argc, const char **argv, const char *prefix)
606{
607 int ret;
608 int index = 0;
609 int quiet = 0;
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")),
615 OPT_END()
616 };
617
618 argc = parse_options(argc, argv, prefix, options,
619 git_stash_pop_usage, 0);
620
621 if (get_stash_info(&info, argc, argv))
622 return -1;
623
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."));
628 else
629 ret = do_drop_stash(prefix, &info, quiet);
630
631 free_stash_info(&info);
632 return ret;
633}
634
635static int branch_stash(int argc, const char **argv, const char *prefix)
636{
637 int ret;
638 const char *branch = NULL;
639 struct stash_info info;
640 struct child_process cp = CHILD_PROCESS_INIT;
641 struct option options[] = {
642 OPT_END()
643 };
644
645 argc = parse_options(argc, argv, prefix, options,
646 git_stash_branch_usage, 0);
647
648 if (!argc) {
649 fprintf_ln(stderr, _("No branch name specified"));
650 return -1;
651 }
652
653 branch = argv[0];
654
655 if (get_stash_info(&info, argc - 1, argv + 1))
656 return -1;
657
658 cp.git_cmd = 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);
663 if (!ret)
664 ret = do_apply_stash(prefix, &info, 1, 0);
665 if (!ret && info.is_stash_ref)
666 ret = do_drop_stash(prefix, &info, 0);
667
668 free_stash_info(&info);
669
670 return ret;
671}
672
673static int list_stash(int argc, const char **argv, const char *prefix)
674{
675 struct child_process cp = CHILD_PROCESS_INIT;
676 struct option options[] = {
677 OPT_END()
678 };
679
680 argc = parse_options(argc, argv, prefix, options,
681 git_stash_list_usage,
682 PARSE_OPT_KEEP_UNKNOWN);
683
684 if (!ref_exists(ref_stash))
685 return 0;
686
687 cp.git_cmd = 1;
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);
694}
695
696static int show_stat = 1;
697static int show_patch;
698
699static int git_stash_config(const char *var, const char *value, void *cb)
700{
701 if (!strcmp(var, "stash.showstat")) {
702 show_stat = git_config_bool(var, value);
703 return 0;
704 }
705 if (!strcmp(var, "stash.showpatch")) {
706 show_patch = git_config_bool(var, value);
707 return 0;
708 }
709 return git_default_config(var, value, cb);
710}
711
712static int show_stash(int argc, const char **argv, const char *prefix)
713{
714 int i;
715 int opts = 0;
716 int ret = 0;
717 struct stash_info info;
718 struct rev_info rev;
719 struct argv_array stash_args = ARGV_ARRAY_INIT;
720 struct option options[] = {
721 OPT_END()
722 };
723
724 init_diff_ui_defaults();
725 git_config(git_diff_ui_config, NULL);
726 init_revisions(&rev, prefix);
727
728 for (i = 1; i < argc; i++) {
729 if (argv[i][0] != '-')
730 argv_array_push(&stash_args, argv[i]);
731 else
732 opts++;
733 }
734
735 ret = get_stash_info(&info, stash_args.argc, stash_args.argv);
736 argv_array_clear(&stash_args);
737 if (ret)
738 return -1;
739
740 /*
741 * The config settings are applied only if there are not passed
742 * any options.
743 */
744 if (!opts) {
745 git_config(git_stash_config, NULL);
746 if (show_stat)
747 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;
748
749 if (show_patch)
750 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
751
752 if (!show_stat && !show_patch) {
753 free_stash_info(&info);
754 return 0;
755 }
756 }
757
758 argc = setup_revisions(argc, argv, &rev, NULL);
759 if (argc > 1) {
760 free_stash_info(&info);
761 usage_with_options(git_stash_show_usage, options);
762 }
763
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);
768
769 free_stash_info(&info);
770 return diff_result_code(&rev.diffopt, 0);
771}
772
773static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
774 int quiet)
775{
776 if (!stash_msg)
777 stash_msg = "Created via \"git stash store\".";
778
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)) {
783 if (!quiet) {
784 fprintf_ln(stderr, _("Cannot update %s with %s"),
785 ref_stash, oid_to_hex(w_commit));
786 }
787 return -1;
788 }
789
790 return 0;
791}
792
793static int store_stash(int argc, const char **argv, const char *prefix)
794{
795 int quiet = 0;
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")),
803 OPT_END()
804 };
805
806 argc = parse_options(argc, argv, prefix, options,
807 git_stash_store_usage,
808 PARSE_OPT_KEEP_UNKNOWN);
809
810 if (argc != 1) {
811 if (!quiet)
812 fprintf_ln(stderr, _("\"git stash store\" requires one "
813 "<commit> argument"));
814 return -1;
815 }
816
817 if (get_oid_with_context(argv[0], quiet ? GET_OID_QUIETLY : 0, &obj,
818 &dummy)) {
819 if (!quiet)
820 fprintf_ln(stderr, _("Cannot update %s with %s"),
821 ref_stash, argv[0]);
822 return -1;
823 }
824
825 return do_store_stash(&obj, stash_msg, quiet);
826}
827
828static void add_pathspecs(struct argv_array *args,
829 const struct pathspec *ps) {
830 int i;
831
832 for (i = 0; i < ps->nr; i++)
833 argv_array_push(args, ps->items[i].original);
834}
835
836/*
837 * `untracked_files` will be filled with the names of untracked files.
838 * The return value is:
839 *
840 * = 0 if there are not any untracked files
841 * > 0 if there are untracked files
842 */
843static int get_untracked_files(const struct pathspec *ps, int include_untracked,
844 struct strbuf *untracked_files)
845{
846 int i;
847 int max_len;
848 int found = 0;
849 char *seen;
850 struct dir_struct dir;
851
852 memset(&dir, 0, sizeof(dir));
853 if (include_untracked != INCLUDE_ALL_FILES)
854 setup_standard_excludes(&dir);
855
856 seen = xcalloc(ps->nr, 1);
857
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)) {
862 found++;
863 strbuf_addstr(untracked_files, ent->name);
864 /* NUL-terminate: will be fed to update-index -z */
865 strbuf_addch(untracked_files, '\0');
866 }
867 free(ent);
868 }
869
870 free(seen);
871 free(dir.entries);
872 free(dir.ignored);
873 clear_directory(&dir);
874 return found;
875}
876
877/*
878 * The return value of `check_changes_tracked_files()` can be:
879 *
880 * < 0 if there was an error
881 * = 0 if there are no changes.
882 * > 0 if there are changes.
883 */
884static int check_changes_tracked_files(const struct pathspec *ps)
885{
886 int result;
887 struct rev_info rev;
888 struct object_id dummy;
889 int ret = 0;
890
891 /* No initial commit. */
892 if (get_oid("HEAD", &dummy))
893 return -1;
894
895 if (read_cache() < 0)
896 return -1;
897
898 init_revisions(&rev, NULL);
899 copy_pathspec(&rev.prune_data, ps);
900
901 rev.diffopt.flags.quick = 1;
902 rev.diffopt.flags.ignore_submodules = 1;
903 rev.abbrev = 0;
904
905 add_head_to_pending(&rev);
906 diff_setup_done(&rev.diffopt);
907
908 result = run_diff_index(&rev, 1);
909 if (diff_result_code(&rev.diffopt, result)) {
910 ret = 1;
911 goto done;
912 }
913
914 object_array_clear(&rev.pending);
915 result = run_diff_files(&rev, 0);
916 if (diff_result_code(&rev.diffopt, result)) {
917 ret = 1;
918 goto done;
919 }
920
921done:
922 clear_pathspec(&rev.prune_data);
923 return ret;
924}
925
926/*
927 * The function will fill `untracked_files` with the names of untracked files
928 * It will return 1 if there were any changes and 0 if there were not.
929 */
930static int check_changes(const struct pathspec *ps, int include_untracked,
931 struct strbuf *untracked_files)
932{
933 int ret = 0;
934 if (check_changes_tracked_files(ps))
935 ret = 1;
936
937 if (include_untracked && get_untracked_files(ps, include_untracked,
938 untracked_files))
939 ret = 1;
940
941 return ret;
942}
943
944static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
945 struct strbuf files)
946{
947 int ret = 0;
948 struct strbuf untracked_msg = STRBUF_INIT;
949 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
950 struct index_state istate = { NULL };
951
952 cp_upd_index.git_cmd = 1;
953 argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
954 "--remove", "--stdin", NULL);
955 argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
956 stash_index_path.buf);
957
958 strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
959 if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,
960 NULL, 0)) {
961 ret = -1;
962 goto done;
963 }
964
965 if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
966 NULL)) {
967 ret = -1;
968 goto done;
969 }
970
971 if (commit_tree(untracked_msg.buf, untracked_msg.len,
972 &info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
973 ret = -1;
974 goto done;
975 }
976
977done:
978 discard_index(&istate);
979 strbuf_release(&untracked_msg);
980 remove_path(stash_index_path.buf);
981 return ret;
982}
983
984static int stash_patch(struct stash_info *info, const struct pathspec *ps,
985 struct strbuf *out_patch, int quiet)
986{
987 int ret = 0;
988 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
989 struct child_process cp_add_i = CHILD_PROCESS_INIT;
990 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
991 struct index_state istate = { NULL };
992
993 remove_path(stash_index_path.buf);
994
995 cp_read_tree.git_cmd = 1;
996 argv_array_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL);
997 argv_array_pushf(&cp_read_tree.env_array, "GIT_INDEX_FILE=%s",
998 stash_index_path.buf);
999 if (run_command(&cp_read_tree)) {
1000 ret = -1;
1001 goto done;
1002 }
1003
1004 /* Find out what the user wants. */
1005 cp_add_i.git_cmd = 1;
1006 argv_array_pushl(&cp_add_i.args, "add--interactive", "--patch=stash",
1007 "--", NULL);
1008 add_pathspecs(&cp_add_i.args, ps);
1009 argv_array_pushf(&cp_add_i.env_array, "GIT_INDEX_FILE=%s",
1010 stash_index_path.buf);
1011 if (run_command(&cp_add_i)) {
1012 ret = -1;
1013 goto done;
1014 }
1015
1016 /* State of the working tree. */
1017 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1018 NULL)) {
1019 ret = -1;
1020 goto done;
1021 }
1022
1023 cp_diff_tree.git_cmd = 1;
1024 argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD",
1025 oid_to_hex(&info->w_tree), "--", NULL);
1026 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
1027 ret = -1;
1028 goto done;
1029 }
1030
1031 if (!out_patch->len) {
1032 if (!quiet)
1033 fprintf_ln(stderr, _("No changes selected"));
1034 ret = 1;
1035 }
1036
1037done:
1038 discard_index(&istate);
1039 remove_path(stash_index_path.buf);
1040 return ret;
1041}
1042
1043static int stash_working_tree(struct stash_info *info, const struct pathspec *ps)
1044{
1045 int ret = 0;
1046 struct rev_info rev;
1047 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1048 struct strbuf diff_output = STRBUF_INIT;
1049 struct index_state istate = { NULL };
1050
1051 init_revisions(&rev, NULL);
1052 copy_pathspec(&rev.prune_data, ps);
1053
1054 set_alternate_index_output(stash_index_path.buf);
1055 if (reset_tree(&info->i_tree, 0, 0)) {
1056 ret = -1;
1057 goto done;
1058 }
1059 set_alternate_index_output(NULL);
1060
1061 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
1062 rev.diffopt.format_callback = add_diff_to_buf;
1063 rev.diffopt.format_callback_data = &diff_output;
1064
1065 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1066 ret = -1;
1067 goto done;
1068 }
1069
1070 add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
1071 "");
1072 if (run_diff_index(&rev, 0)) {
1073 ret = -1;
1074 goto done;
1075 }
1076
1077 cp_upd_index.git_cmd = 1;
1078 argv_array_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
1079 "--remove", "--stdin", NULL);
1080 argv_array_pushf(&cp_upd_index.env_array, "GIT_INDEX_FILE=%s",
1081 stash_index_path.buf);
1082
1083 if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,
1084 NULL, 0, NULL, 0)) {
1085 ret = -1;
1086 goto done;
1087 }
1088
1089 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1090 NULL)) {
1091 ret = -1;
1092 goto done;
1093 }
1094
1095done:
1096 discard_index(&istate);
1097 UNLEAK(rev);
1098 object_array_clear(&rev.pending);
1099 clear_pathspec(&rev.prune_data);
1100 strbuf_release(&diff_output);
1101 remove_path(stash_index_path.buf);
1102 return ret;
1103}
1104
1105static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,
1106 int include_untracked, int patch_mode,
1107 struct stash_info *info, struct strbuf *patch,
1108 int quiet)
1109{
1110 int ret = 0;
1111 int flags = 0;
1112 int untracked_commit_option = 0;
1113 const char *head_short_sha1 = NULL;
1114 const char *branch_ref = NULL;
1115 const char *branch_name = "(no branch)";
1116 struct commit *head_commit = NULL;
1117 struct commit_list *parents = NULL;
1118 struct strbuf msg = STRBUF_INIT;
1119 struct strbuf commit_tree_label = STRBUF_INIT;
1120 struct strbuf untracked_files = STRBUF_INIT;
1121
1122 prepare_fallback_ident("git stash", "git@stash");
1123
1124 read_cache_preload(NULL);
1125 refresh_cache(REFRESH_QUIET);
1126
1127 if (get_oid("HEAD", &info->b_commit)) {
1128 if (!quiet)
1129 fprintf_ln(stderr, _("You do not have "
1130 "the initial commit yet"));
1131 ret = -1;
1132 goto done;
1133 } else {
1134 head_commit = lookup_commit(the_repository, &info->b_commit);
1135 }
1136
1137 if (!check_changes(ps, include_untracked, &untracked_files)) {
1138 ret = 1;
1139 goto done;
1140 }
1141
1142 branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
1143 if (flags & REF_ISSYMREF)
1144 branch_name = strrchr(branch_ref, '/') + 1;
1145 head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
1146 DEFAULT_ABBREV);
1147 strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);
1148 pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
1149
1150 strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
1151 commit_list_insert(head_commit, &parents);
1152 if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
1153 commit_tree(commit_tree_label.buf, commit_tree_label.len,
1154 &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1155 if (!quiet)
1156 fprintf_ln(stderr, _("Cannot save the current "
1157 "index state"));
1158 ret = -1;
1159 goto done;
1160 }
1161
1162 if (include_untracked) {
1163 if (save_untracked_files(info, &msg, untracked_files)) {
1164 if (!quiet)
1165 fprintf_ln(stderr, _("Cannot save "
1166 "the untracked files"));
1167 ret = -1;
1168 goto done;
1169 }
1170 untracked_commit_option = 1;
1171 }
1172 if (patch_mode) {
1173 ret = stash_patch(info, ps, patch, quiet);
1174 if (ret < 0) {
1175 if (!quiet)
1176 fprintf_ln(stderr, _("Cannot save the current "
1177 "worktree state"));
1178 goto done;
1179 } else if (ret > 0) {
1180 goto done;
1181 }
1182 } else {
1183 if (stash_working_tree(info, ps)) {
1184 if (!quiet)
1185 fprintf_ln(stderr, _("Cannot save the current "
1186 "worktree state"));
1187 ret = -1;
1188 goto done;
1189 }
1190 }
1191
1192 if (!stash_msg_buf->len)
1193 strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf);
1194 else
1195 strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name);
1196
1197 /*
1198 * `parents` will be empty after calling `commit_tree()`, so there is
1199 * no need to call `free_commit_list()`
1200 */
1201 parents = NULL;
1202 if (untracked_commit_option)
1203 commit_list_insert(lookup_commit(the_repository,
1204 &info->u_commit),
1205 &parents);
1206 commit_list_insert(lookup_commit(the_repository, &info->i_commit),
1207 &parents);
1208 commit_list_insert(head_commit, &parents);
1209
1210 if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
1211 parents, &info->w_commit, NULL, NULL)) {
1212 if (!quiet)
1213 fprintf_ln(stderr, _("Cannot record "
1214 "working tree state"));
1215 ret = -1;
1216 goto done;
1217 }
1218
1219done:
1220 strbuf_release(&commit_tree_label);
1221 strbuf_release(&msg);
1222 strbuf_release(&untracked_files);
1223 return ret;
1224}
1225
1226static int create_stash(int argc, const char **argv, const char *prefix)
1227{
1228 int ret = 0;
1229 struct strbuf stash_msg_buf = STRBUF_INIT;
1230 struct stash_info info;
1231 struct pathspec ps;
1232
1233 /* Starting with argv[1], since argv[0] is "create" */
1234 strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
1235
1236 memset(&ps, 0, sizeof(ps));
1237 if (!check_changes_tracked_files(&ps))
1238 return 0;
1239
1240 ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, &info,
1241 NULL, 0);
1242 if (!ret)
1243 printf_ln("%s", oid_to_hex(&info.w_commit));
1244
1245 strbuf_release(&stash_msg_buf);
1246 return ret;
1247}
1248
1249static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,
1250 int keep_index, int patch_mode, int include_untracked)
1251{
1252 int ret = 0;
1253 struct stash_info info;
1254 struct strbuf patch = STRBUF_INIT;
1255 struct strbuf stash_msg_buf = STRBUF_INIT;
1256 struct strbuf untracked_files = STRBUF_INIT;
1257
1258 if (patch_mode && keep_index == -1)
1259 keep_index = 1;
1260
1261 if (patch_mode && include_untracked) {
1262 fprintf_ln(stderr, _("Can't use --patch and --include-untracked"
1263 " or --all at the same time"));
1264 ret = -1;
1265 goto done;
1266 }
1267
1268 read_cache_preload(NULL);
1269 if (!include_untracked && ps->nr) {
1270 int i;
1271 char *ps_matched = xcalloc(ps->nr, 1);
1272
1273 for (i = 0; i < active_nr; i++)
1274 ce_path_match(&the_index, active_cache[i], ps,
1275 ps_matched);
1276
1277 if (report_path_error(ps_matched, ps, NULL)) {
1278 fprintf_ln(stderr, _("Did you forget to 'git add'?"));
1279 ret = -1;
1280 free(ps_matched);
1281 goto done;
1282 }
1283 free(ps_matched);
1284 }
1285
1286 if (refresh_cache(REFRESH_QUIET)) {
1287 ret = -1;
1288 goto done;
1289 }
1290
1291 if (!check_changes(ps, include_untracked, &untracked_files)) {
1292 if (!quiet)
1293 printf_ln(_("No local changes to save"));
1294 goto done;
1295 }
1296
1297 if (!reflog_exists(ref_stash) && do_clear_stash()) {
1298 ret = -1;
1299 if (!quiet)
1300 fprintf_ln(stderr, _("Cannot initialize stash"));
1301 goto done;
1302 }
1303
1304 if (stash_msg)
1305 strbuf_addstr(&stash_msg_buf, stash_msg);
1306 if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
1307 &info, &patch, quiet)) {
1308 ret = -1;
1309 goto done;
1310 }
1311
1312 if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
1313 ret = -1;
1314 if (!quiet)
1315 fprintf_ln(stderr, _("Cannot save the current status"));
1316 goto done;
1317 }
1318
1319 if (!quiet)
1320 printf_ln(_("Saved working directory and index state %s"),
1321 stash_msg_buf.buf);
1322
1323 if (!patch_mode) {
1324 if (include_untracked && !ps->nr) {
1325 struct child_process cp = CHILD_PROCESS_INIT;
1326
1327 cp.git_cmd = 1;
1328 argv_array_pushl(&cp.args, "clean", "--force",
1329 "--quiet", "-d", NULL);
1330 if (include_untracked == INCLUDE_ALL_FILES)
1331 argv_array_push(&cp.args, "-x");
1332 if (run_command(&cp)) {
1333 ret = -1;
1334 goto done;
1335 }
1336 }
1337 discard_cache();
1338 if (ps->nr) {
1339 struct child_process cp_add = CHILD_PROCESS_INIT;
1340 struct child_process cp_diff = CHILD_PROCESS_INIT;
1341 struct child_process cp_apply = CHILD_PROCESS_INIT;
1342 struct strbuf out = STRBUF_INIT;
1343
1344 cp_add.git_cmd = 1;
1345 argv_array_push(&cp_add.args, "add");
1346 if (!include_untracked)
1347 argv_array_push(&cp_add.args, "-u");
1348 if (include_untracked == INCLUDE_ALL_FILES)
1349 argv_array_push(&cp_add.args, "--force");
1350 argv_array_push(&cp_add.args, "--");
1351 add_pathspecs(&cp_add.args, ps);
1352 if (run_command(&cp_add)) {
1353 ret = -1;
1354 goto done;
1355 }
1356
1357 cp_diff.git_cmd = 1;
1358 argv_array_pushl(&cp_diff.args, "diff-index", "-p",
1359 "--cached", "--binary", "HEAD", "--",
1360 NULL);
1361 add_pathspecs(&cp_diff.args, ps);
1362 if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {
1363 ret = -1;
1364 goto done;
1365 }
1366
1367 cp_apply.git_cmd = 1;
1368 argv_array_pushl(&cp_apply.args, "apply", "--index",
1369 "-R", NULL);
1370 if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,
1371 NULL, 0)) {
1372 ret = -1;
1373 goto done;
1374 }
1375 } else {
1376 struct child_process cp = CHILD_PROCESS_INIT;
1377 cp.git_cmd = 1;
1378 argv_array_pushl(&cp.args, "reset", "--hard", "-q",
1379 NULL);
1380 if (run_command(&cp)) {
1381 ret = -1;
1382 goto done;
1383 }
1384 }
1385
1386 if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1387 struct child_process cp_ls = CHILD_PROCESS_INIT;
1388 struct child_process cp_checkout = CHILD_PROCESS_INIT;
1389 struct strbuf out = STRBUF_INIT;
1390
1391 if (reset_tree(&info.i_tree, 0, 1)) {
1392 ret = -1;
1393 goto done;
1394 }
1395
1396 cp_ls.git_cmd = 1;
1397 argv_array_pushl(&cp_ls.args, "ls-files", "-z",
1398 "--modified", "--", NULL);
1399
1400 add_pathspecs(&cp_ls.args, ps);
1401 if (pipe_command(&cp_ls, NULL, 0, &out, 0, NULL, 0)) {
1402 ret = -1;
1403 goto done;
1404 }
1405
1406 cp_checkout.git_cmd = 1;
1407 argv_array_pushl(&cp_checkout.args, "checkout-index",
1408 "-z", "--force", "--stdin", NULL);
1409 if (pipe_command(&cp_checkout, out.buf, out.len, NULL,
1410 0, NULL, 0)) {
1411 ret = -1;
1412 goto done;
1413 }
1414 }
1415 goto done;
1416 } else {
1417 struct child_process cp = CHILD_PROCESS_INIT;
1418
1419 cp.git_cmd = 1;
1420 argv_array_pushl(&cp.args, "apply", "-R", NULL);
1421
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;
1428 }
1429
1430 if (keep_index < 1) {
1431 struct child_process cp = CHILD_PROCESS_INIT;
1432
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;
1439 }
1440 }
1441 goto done;
1442 }
1443
1444done:
1445 strbuf_release(&stash_msg_buf);
1446 return ret;
1447}
1448
1449static int push_stash(int argc, const char **argv, const char *prefix)
1450{
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()
1470 };
1471
1472 if (argc)
1473 argc = parse_options(argc, argv, prefix, options,
1474 git_stash_push_usage,
1475 0);
1476
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);
1481}
1482
1483static int save_stash(int argc, const char **argv, const char *prefix)
1484{
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()
1506 };
1507
1508 argc = parse_options(argc, argv, prefix, options,
1509 git_stash_save_usage,
1510 PARSE_OPT_KEEP_DASHDASH);
1511
1512 if (argc)
1513 stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
1514
1515 memset(&ps, 0, sizeof(ps));
1516 ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
1517 patch_mode, include_untracked);
1518
1519 strbuf_release(&stash_msg_buf);
1520 return ret;
1521}
1522
1523static int use_builtin_stash(void)
1524{
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);
1528
1529 if (env != -1)
1530 return env;
1531
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;
1538 }
1539
1540 strbuf_trim(&out);
1541 ret = !strcmp("true", out.buf);
1542 strbuf_release(&out);
1543 return ret;
1544}
1545
1546int cmd_stash(int argc, const char **argv, const char *prefix)
1547{
1548 int i = -1;
1549 pid_t pid = getpid();
1550 const char *index_file;
1551 struct argv_array args = ARGV_ARRAY_INIT;
1552
1553 struct option options[] = {
1554 OPT_END()
1555 };
1556
1557 if (!use_builtin_stash()) {
1558 const char *path = mkpath("%s/git-legacy-stash",
1559 git_exec_path());
1560
1561 if (sane_execvp(path, (char **)argv) < 0)
1562 die_errno(_("could not exec %s"), path);
1563 else
1564 BUG("sane_execvp() returned???");
1565 }
1566
1567 prefix = setup_git_directory();
1568 trace_repo_setup(prefix);
1569 setup_work_tree();
1570
1571 git_config(git_diff_basic_config, NULL);
1572
1573 argc = parse_options(argc, argv, prefix, options, git_stash_usage,
1574 PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
1575
1576 index_file = get_index_file();
1577 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1578 (uintmax_t)pid);
1579
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);
1607
1608 if (strcmp(argv[0], "-p")) {
1609 while (++i < argc && strcmp(argv[i], "--")) {
1610 /*
1611 * `akpqu` is a string which contains all short options,
1612 * except `-m` which is verified separately.
1613 */
1614 if ((strlen(argv[i]) == 2) && *argv[i] == '-' &&
1615 strchr("akpqu", argv[i][1]))
1616 continue;
1617
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;
1625
1626 /*
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"`).
1630 */
1631 if (starts_with(argv[i], "-m") ||
1632 starts_with(argv[i], "--message="))
1633 continue;
1634
1635 usage_with_options(git_stash_usage, options);
1636 }
1637 }
1638
1639 argv_array_push(&args, "push");
1640 argv_array_pushv(&args, argv);
1641 return !!push_stash(args.argc, args.argv, prefix);
1642}