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