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