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