]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/stash.c
preload-index.h: move declarations for preload-index.c from elsewhere
[thirdparty/git.git] / builtin / stash.c
1 #define USE_THE_INDEX_VARIABLE
2 #include "builtin.h"
3 #include "abspath.h"
4 #include "config.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "object-name.h"
9 #include "parse-options.h"
10 #include "refs.h"
11 #include "lockfile.h"
12 #include "cache-tree.h"
13 #include "unpack-trees.h"
14 #include "merge-recursive.h"
15 #include "merge-ort-wrappers.h"
16 #include "strvec.h"
17 #include "run-command.h"
18 #include "dir.h"
19 #include "entry.h"
20 #include "preload-index.h"
21 #include "rerere.h"
22 #include "revision.h"
23 #include "setup.h"
24 #include "sparse-index.h"
25 #include "log-tree.h"
26 #include "diffcore.h"
27 #include "exec-cmd.h"
28 #include "reflog.h"
29 #include "add-interactive.h"
30
31 #define INCLUDE_ALL_FILES 2
32
33 #define BUILTIN_STASH_LIST_USAGE \
34 N_("git stash list [<log-options>]")
35 #define BUILTIN_STASH_SHOW_USAGE \
36 N_("git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]")
37 #define BUILTIN_STASH_DROP_USAGE \
38 N_("git stash drop [-q | --quiet] [<stash>]")
39 #define BUILTIN_STASH_POP_USAGE \
40 N_("git stash pop [--index] [-q | --quiet] [<stash>]")
41 #define BUILTIN_STASH_APPLY_USAGE \
42 N_("git stash apply [--index] [-q | --quiet] [<stash>]")
43 #define BUILTIN_STASH_BRANCH_USAGE \
44 N_("git stash branch <branchname> [<stash>]")
45 #define BUILTIN_STASH_STORE_USAGE \
46 N_("git stash store [(-m | --message) <message>] [-q | --quiet] <commit>")
47 #define BUILTIN_STASH_PUSH_USAGE \
48 N_("git stash [push [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]\n" \
49 " [-u | --include-untracked] [-a | --all] [(-m | --message) <message>]\n" \
50 " [--pathspec-from-file=<file> [--pathspec-file-nul]]\n" \
51 " [--] [<pathspec>...]]")
52 #define BUILTIN_STASH_SAVE_USAGE \
53 N_("git stash save [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]\n" \
54 " [-u | --include-untracked] [-a | --all] [<message>]")
55 #define BUILTIN_STASH_CREATE_USAGE \
56 N_("git stash create [<message>]")
57 #define BUILTIN_STASH_CLEAR_USAGE \
58 "git stash clear"
59
60 static const char * const git_stash_usage[] = {
61 BUILTIN_STASH_LIST_USAGE,
62 BUILTIN_STASH_SHOW_USAGE,
63 BUILTIN_STASH_DROP_USAGE,
64 BUILTIN_STASH_POP_USAGE,
65 BUILTIN_STASH_APPLY_USAGE,
66 BUILTIN_STASH_BRANCH_USAGE,
67 BUILTIN_STASH_PUSH_USAGE,
68 BUILTIN_STASH_SAVE_USAGE,
69 BUILTIN_STASH_CLEAR_USAGE,
70 BUILTIN_STASH_CREATE_USAGE,
71 BUILTIN_STASH_STORE_USAGE,
72 NULL
73 };
74
75 static const char * const git_stash_list_usage[] = {
76 BUILTIN_STASH_LIST_USAGE,
77 NULL
78 };
79
80 static const char * const git_stash_show_usage[] = {
81 BUILTIN_STASH_SHOW_USAGE,
82 NULL
83 };
84
85 static const char * const git_stash_drop_usage[] = {
86 BUILTIN_STASH_DROP_USAGE,
87 NULL
88 };
89
90 static const char * const git_stash_pop_usage[] = {
91 BUILTIN_STASH_POP_USAGE,
92 NULL
93 };
94
95 static const char * const git_stash_apply_usage[] = {
96 BUILTIN_STASH_APPLY_USAGE,
97 NULL
98 };
99
100 static const char * const git_stash_branch_usage[] = {
101 BUILTIN_STASH_BRANCH_USAGE,
102 NULL
103 };
104
105 static const char * const git_stash_clear_usage[] = {
106 BUILTIN_STASH_CLEAR_USAGE,
107 NULL
108 };
109
110 static const char * const git_stash_store_usage[] = {
111 BUILTIN_STASH_STORE_USAGE,
112 NULL
113 };
114
115 static const char * const git_stash_push_usage[] = {
116 BUILTIN_STASH_PUSH_USAGE,
117 NULL
118 };
119
120 static const char * const git_stash_save_usage[] = {
121 BUILTIN_STASH_SAVE_USAGE,
122 NULL
123 };
124
125 static const char ref_stash[] = "refs/stash";
126 static struct strbuf stash_index_path = STRBUF_INIT;
127
128 /*
129 * w_commit is set to the commit containing the working tree
130 * b_commit is set to the base commit
131 * i_commit is set to the commit containing the index tree
132 * u_commit is set to the commit containing the untracked files tree
133 * w_tree is set to the working tree
134 * b_tree is set to the base tree
135 * i_tree is set to the index tree
136 * u_tree is set to the untracked files tree
137 */
138 struct stash_info {
139 struct object_id w_commit;
140 struct object_id b_commit;
141 struct object_id i_commit;
142 struct object_id u_commit;
143 struct object_id w_tree;
144 struct object_id b_tree;
145 struct object_id i_tree;
146 struct object_id u_tree;
147 struct strbuf revision;
148 int is_stash_ref;
149 int has_u;
150 };
151
152 #define STASH_INFO_INIT { \
153 .revision = STRBUF_INIT, \
154 }
155
156 static void free_stash_info(struct stash_info *info)
157 {
158 strbuf_release(&info->revision);
159 }
160
161 static void assert_stash_like(struct stash_info *info, const char *revision)
162 {
163 if (get_oidf(&info->b_commit, "%s^1", revision) ||
164 get_oidf(&info->w_tree, "%s:", revision) ||
165 get_oidf(&info->b_tree, "%s^1:", revision) ||
166 get_oidf(&info->i_tree, "%s^2:", revision))
167 die(_("'%s' is not a stash-like commit"), revision);
168 }
169
170 static int get_stash_info(struct stash_info *info, int argc, const char **argv)
171 {
172 int ret;
173 char *end_of_rev;
174 char *expanded_ref;
175 const char *revision;
176 const char *commit = NULL;
177 struct object_id dummy;
178 struct strbuf symbolic = STRBUF_INIT;
179
180 if (argc > 1) {
181 int i;
182 struct strbuf refs_msg = STRBUF_INIT;
183
184 for (i = 0; i < argc; i++)
185 strbuf_addf(&refs_msg, " '%s'", argv[i]);
186
187 fprintf_ln(stderr, _("Too many revisions specified:%s"),
188 refs_msg.buf);
189 strbuf_release(&refs_msg);
190
191 return -1;
192 }
193
194 if (argc == 1)
195 commit = argv[0];
196
197 if (!commit) {
198 if (!ref_exists(ref_stash)) {
199 fprintf_ln(stderr, _("No stash entries found."));
200 return -1;
201 }
202
203 strbuf_addf(&info->revision, "%s@{0}", ref_stash);
204 } else if (strspn(commit, "0123456789") == strlen(commit)) {
205 strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
206 } else {
207 strbuf_addstr(&info->revision, commit);
208 }
209
210 revision = info->revision.buf;
211
212 if (repo_get_oid(the_repository, revision, &info->w_commit))
213 return error(_("%s is not a valid reference"), revision);
214
215 assert_stash_like(info, revision);
216
217 info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision);
218
219 end_of_rev = strchrnul(revision, '@');
220 strbuf_add(&symbolic, revision, end_of_rev - revision);
221
222 ret = repo_dwim_ref(the_repository, symbolic.buf, symbolic.len,
223 &dummy, &expanded_ref, 0);
224 strbuf_release(&symbolic);
225 switch (ret) {
226 case 0: /* Not found, but valid ref */
227 info->is_stash_ref = 0;
228 break;
229 case 1:
230 info->is_stash_ref = !strcmp(expanded_ref, ref_stash);
231 break;
232 default: /* Invalid or ambiguous */
233 break;
234 }
235
236 free(expanded_ref);
237 return !(ret == 0 || ret == 1);
238 }
239
240 static int do_clear_stash(void)
241 {
242 struct object_id obj;
243 if (repo_get_oid(the_repository, ref_stash, &obj))
244 return 0;
245
246 return delete_ref(NULL, ref_stash, &obj, 0);
247 }
248
249 static int clear_stash(int argc, const char **argv, const char *prefix)
250 {
251 struct option options[] = {
252 OPT_END()
253 };
254
255 argc = parse_options(argc, argv, prefix, options,
256 git_stash_clear_usage,
257 PARSE_OPT_STOP_AT_NON_OPTION);
258
259 if (argc)
260 return error(_("git stash clear with arguments is "
261 "unimplemented"));
262
263 return do_clear_stash();
264 }
265
266 static int reset_tree(struct object_id *i_tree, int update, int reset)
267 {
268 int nr_trees = 1;
269 struct unpack_trees_options opts;
270 struct tree_desc t[MAX_UNPACK_TREES];
271 struct tree *tree;
272 struct lock_file lock_file = LOCK_INIT;
273
274 repo_read_index_preload(the_repository, NULL, 0);
275 if (refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL))
276 return -1;
277
278 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
279
280 memset(&opts, 0, sizeof(opts));
281
282 tree = parse_tree_indirect(i_tree);
283 if (parse_tree(tree))
284 return -1;
285
286 init_tree_desc(t, tree->buffer, tree->size);
287
288 opts.head_idx = 1;
289 opts.src_index = &the_index;
290 opts.dst_index = &the_index;
291 opts.merge = 1;
292 opts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;
293 opts.update = update;
294 if (update)
295 opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
296 opts.fn = oneway_merge;
297
298 if (unpack_trees(nr_trees, t, &opts))
299 return -1;
300
301 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
302 return error(_("unable to write new index file"));
303
304 return 0;
305 }
306
307 static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit)
308 {
309 struct child_process cp = CHILD_PROCESS_INIT;
310 const char *w_commit_hex = oid_to_hex(w_commit);
311
312 /*
313 * Diff-tree would not be very hard to replace with a native function,
314 * however it should be done together with apply_cached.
315 */
316 cp.git_cmd = 1;
317 strvec_pushl(&cp.args, "diff-tree", "--binary", NULL);
318 strvec_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex);
319
320 return pipe_command(&cp, NULL, 0, out, 0, NULL, 0);
321 }
322
323 static int apply_cached(struct strbuf *out)
324 {
325 struct child_process cp = CHILD_PROCESS_INIT;
326
327 /*
328 * Apply currently only reads either from stdin or a file, thus
329 * apply_all_patches would have to be updated to optionally take a
330 * buffer.
331 */
332 cp.git_cmd = 1;
333 strvec_pushl(&cp.args, "apply", "--cached", NULL);
334 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0);
335 }
336
337 static int reset_head(void)
338 {
339 struct child_process cp = CHILD_PROCESS_INIT;
340
341 /*
342 * Reset is overall quite simple, however there is no current public
343 * API for resetting.
344 */
345 cp.git_cmd = 1;
346 strvec_pushl(&cp.args, "reset", "--quiet", "--refresh", NULL);
347
348 return run_command(&cp);
349 }
350
351 static int is_path_a_directory(const char *path)
352 {
353 /*
354 * This function differs from abspath.c:is_directory() in that
355 * here we use lstat() instead of stat(); we do not want to
356 * follow symbolic links here.
357 */
358 struct stat st;
359 return (!lstat(path, &st) && S_ISDIR(st.st_mode));
360 }
361
362 static void add_diff_to_buf(struct diff_queue_struct *q,
363 struct diff_options *options,
364 void *data)
365 {
366 int i;
367
368 for (i = 0; i < q->nr; i++) {
369 if (is_path_a_directory(q->queue[i]->one->path))
370 continue;
371
372 strbuf_addstr(data, q->queue[i]->one->path);
373
374 /* NUL-terminate: will be fed to update-index -z */
375 strbuf_addch(data, '\0');
376 }
377 }
378
379 static int restore_untracked(struct object_id *u_tree)
380 {
381 int res;
382 struct child_process cp = CHILD_PROCESS_INIT;
383
384 /*
385 * We need to run restore files from a given index, but without
386 * affecting the current index, so we use GIT_INDEX_FILE with
387 * run_command to fork processes that will not interfere.
388 */
389 cp.git_cmd = 1;
390 strvec_push(&cp.args, "read-tree");
391 strvec_push(&cp.args, oid_to_hex(u_tree));
392 strvec_pushf(&cp.env, "GIT_INDEX_FILE=%s",
393 stash_index_path.buf);
394 if (run_command(&cp)) {
395 remove_path(stash_index_path.buf);
396 return -1;
397 }
398
399 child_process_init(&cp);
400 cp.git_cmd = 1;
401 strvec_pushl(&cp.args, "checkout-index", "--all", NULL);
402 strvec_pushf(&cp.env, "GIT_INDEX_FILE=%s",
403 stash_index_path.buf);
404
405 res = run_command(&cp);
406 remove_path(stash_index_path.buf);
407 return res;
408 }
409
410 static void unstage_changes_unless_new(struct object_id *orig_tree)
411 {
412 /*
413 * When we enter this function, there has been a clean merge of
414 * relevant trees, and the merge logic always stages whatever merges
415 * cleanly. We want to unstage those changes, unless it corresponds
416 * to a file that didn't exist as of orig_tree.
417 *
418 * However, if any SKIP_WORKTREE path is modified relative to
419 * orig_tree, then we want to clear the SKIP_WORKTREE bit and write
420 * it to the worktree before unstaging.
421 */
422
423 struct checkout state = CHECKOUT_INIT;
424 struct diff_options diff_opts;
425 struct lock_file lock = LOCK_INIT;
426 int i;
427
428 /* If any entries have skip_worktree set, we'll have to check 'em out */
429 state.force = 1;
430 state.quiet = 1;
431 state.refresh_cache = 1;
432 state.istate = &the_index;
433
434 /*
435 * Step 1: get a difference between orig_tree (which corresponding
436 * to the index before a merge was run) and the current index
437 * (reflecting the changes brought in by the merge).
438 */
439 repo_diff_setup(the_repository, &diff_opts);
440 diff_opts.flags.recursive = 1;
441 diff_opts.detect_rename = 0;
442 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
443 diff_setup_done(&diff_opts);
444
445 do_diff_cache(orig_tree, &diff_opts);
446 diffcore_std(&diff_opts);
447
448 /* Iterate over the paths that changed due to the merge... */
449 for (i = 0; i < diff_queued_diff.nr; i++) {
450 struct diff_filepair *p;
451 struct cache_entry *ce;
452 int pos;
453
454 /* Look up the path's position in the current index. */
455 p = diff_queued_diff.queue[i];
456 pos = index_name_pos(&the_index, p->two->path,
457 strlen(p->two->path));
458
459 /*
460 * Step 2: Place changes in the working tree
461 *
462 * Stash is about restoring changes *to the working tree*.
463 * So if the merge successfully got a new version of some
464 * path, but left it out of the working tree, then clear the
465 * SKIP_WORKTREE bit and write it to the working tree.
466 */
467 if (pos >= 0 && ce_skip_worktree(the_index.cache[pos])) {
468 struct stat st;
469
470 ce = the_index.cache[pos];
471 if (!lstat(ce->name, &st)) {
472 /* Conflicting path present; relocate it */
473 struct strbuf new_path = STRBUF_INIT;
474 int fd;
475
476 strbuf_addf(&new_path,
477 "%s.stash.XXXXXX", ce->name);
478 fd = xmkstemp(new_path.buf);
479 close(fd);
480 printf(_("WARNING: Untracked file in way of "
481 "tracked file! Renaming\n "
482 " %s -> %s\n"
483 " to make room.\n"),
484 ce->name, new_path.buf);
485 if (rename(ce->name, new_path.buf))
486 die("Failed to move %s to %s\n",
487 ce->name, new_path.buf);
488 strbuf_release(&new_path);
489 }
490 checkout_entry(ce, &state, NULL, NULL);
491 ce->ce_flags &= ~CE_SKIP_WORKTREE;
492 }
493
494 /*
495 * Step 3: "unstage" changes, as long as they are still tracked
496 */
497 if (p->one->oid_valid) {
498 /*
499 * Path existed in orig_tree; restore index entry
500 * from that tree in order to "unstage" the changes.
501 */
502 int option = ADD_CACHE_OK_TO_REPLACE;
503 if (pos < 0)
504 option = ADD_CACHE_OK_TO_ADD;
505
506 ce = make_cache_entry(&the_index,
507 p->one->mode,
508 &p->one->oid,
509 p->one->path,
510 0, 0);
511 add_index_entry(&the_index, ce, option);
512 }
513 }
514 diff_flush(&diff_opts);
515
516 /*
517 * Step 4: write the new index to disk
518 */
519 repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
520 if (write_locked_index(&the_index, &lock,
521 COMMIT_LOCK | SKIP_IF_UNCHANGED))
522 die(_("Unable to write index."));
523 }
524
525 static int do_apply_stash(const char *prefix, struct stash_info *info,
526 int index, int quiet)
527 {
528 int clean, ret;
529 int has_index = index;
530 struct merge_options o;
531 struct object_id c_tree;
532 struct object_id index_tree;
533 struct tree *head, *merge, *merge_base;
534 struct lock_file lock = LOCK_INIT;
535
536 repo_read_index_preload(the_repository, NULL, 0);
537 if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
538 NULL, NULL, NULL))
539 return -1;
540
541 if (write_index_as_tree(&c_tree, &the_index, get_index_file(), 0,
542 NULL))
543 return error(_("cannot apply a stash in the middle of a merge"));
544
545 if (index) {
546 if (oideq(&info->b_tree, &info->i_tree) ||
547 oideq(&c_tree, &info->i_tree)) {
548 has_index = 0;
549 } else {
550 struct strbuf out = STRBUF_INIT;
551
552 if (diff_tree_binary(&out, &info->w_commit)) {
553 strbuf_release(&out);
554 return error(_("could not generate diff %s^!."),
555 oid_to_hex(&info->w_commit));
556 }
557
558 ret = apply_cached(&out);
559 strbuf_release(&out);
560 if (ret)
561 return error(_("conflicts in index. "
562 "Try without --index."));
563
564 discard_index(&the_index);
565 repo_read_index(the_repository);
566 if (write_index_as_tree(&index_tree, &the_index,
567 get_index_file(), 0, NULL))
568 return error(_("could not save index tree"));
569
570 reset_head();
571 discard_index(&the_index);
572 repo_read_index(the_repository);
573 }
574 }
575
576 init_merge_options(&o, the_repository);
577
578 o.branch1 = "Updated upstream";
579 o.branch2 = "Stashed changes";
580 o.ancestor = "Stash base";
581
582 if (oideq(&info->b_tree, &c_tree))
583 o.branch1 = "Version stash was based on";
584
585 if (quiet)
586 o.verbosity = 0;
587
588 if (o.verbosity >= 3)
589 printf_ln(_("Merging %s with %s"), o.branch1, o.branch2);
590
591 head = lookup_tree(o.repo, &c_tree);
592 merge = lookup_tree(o.repo, &info->w_tree);
593 merge_base = lookup_tree(o.repo, &info->b_tree);
594
595 repo_hold_locked_index(o.repo, &lock, LOCK_DIE_ON_ERROR);
596 clean = merge_ort_nonrecursive(&o, head, merge, merge_base);
597
598 /*
599 * If 'clean' >= 0, reverse the value for 'ret' so 'ret' is 0 when the
600 * merge was clean, and nonzero if the merge was unclean or encountered
601 * an error.
602 */
603 ret = clean >= 0 ? !clean : clean;
604
605 if (ret < 0)
606 rollback_lock_file(&lock);
607 else if (write_locked_index(o.repo->index, &lock,
608 COMMIT_LOCK | SKIP_IF_UNCHANGED))
609 ret = error(_("could not write index"));
610
611 if (ret) {
612 repo_rerere(the_repository, 0);
613
614 if (index)
615 fprintf_ln(stderr, _("Index was not unstashed."));
616
617 goto restore_untracked;
618 }
619
620 if (has_index) {
621 if (reset_tree(&index_tree, 0, 0))
622 ret = -1;
623 } else {
624 unstage_changes_unless_new(&c_tree);
625 }
626
627 restore_untracked:
628 if (info->has_u && restore_untracked(&info->u_tree))
629 ret = error(_("could not restore untracked files from stash"));
630
631 if (!quiet) {
632 struct child_process cp = CHILD_PROCESS_INIT;
633
634 /*
635 * Status is quite simple and could be replaced with calls to
636 * wt_status in the future, but it adds complexities which may
637 * require more tests.
638 */
639 cp.git_cmd = 1;
640 cp.dir = prefix;
641 strvec_pushf(&cp.env, GIT_WORK_TREE_ENVIRONMENT"=%s",
642 absolute_path(get_git_work_tree()));
643 strvec_pushf(&cp.env, GIT_DIR_ENVIRONMENT"=%s",
644 absolute_path(get_git_dir()));
645 strvec_push(&cp.args, "status");
646 run_command(&cp);
647 }
648
649 return ret;
650 }
651
652 static int apply_stash(int argc, const char **argv, const char *prefix)
653 {
654 int ret = -1;
655 int quiet = 0;
656 int index = 0;
657 struct stash_info info = STASH_INFO_INIT;
658 struct option options[] = {
659 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
660 OPT_BOOL(0, "index", &index,
661 N_("attempt to recreate the index")),
662 OPT_END()
663 };
664
665 argc = parse_options(argc, argv, prefix, options,
666 git_stash_apply_usage, 0);
667
668 if (get_stash_info(&info, argc, argv))
669 goto cleanup;
670
671 ret = do_apply_stash(prefix, &info, index, quiet);
672 cleanup:
673 free_stash_info(&info);
674 return ret;
675 }
676
677 static int reject_reflog_ent(struct object_id *ooid UNUSED,
678 struct object_id *noid UNUSED,
679 const char *email UNUSED,
680 timestamp_t timestamp UNUSED,
681 int tz UNUSED, const char *message UNUSED,
682 void *cb_data UNUSED)
683 {
684 return 1;
685 }
686
687 static int reflog_is_empty(const char *refname)
688 {
689 return !for_each_reflog_ent(refname, reject_reflog_ent, NULL);
690 }
691
692 static int do_drop_stash(struct stash_info *info, int quiet)
693 {
694 if (!reflog_delete(info->revision.buf,
695 EXPIRE_REFLOGS_REWRITE | EXPIRE_REFLOGS_UPDATE_REF,
696 0)) {
697 if (!quiet)
698 printf_ln(_("Dropped %s (%s)"), info->revision.buf,
699 oid_to_hex(&info->w_commit));
700 } else {
701 return error(_("%s: Could not drop stash entry"),
702 info->revision.buf);
703 }
704
705 if (reflog_is_empty(ref_stash))
706 do_clear_stash();
707
708 return 0;
709 }
710
711 static int get_stash_info_assert(struct stash_info *info, int argc,
712 const char **argv)
713 {
714 int ret = get_stash_info(info, argc, argv);
715
716 if (ret < 0)
717 return ret;
718
719 if (!info->is_stash_ref)
720 return error(_("'%s' is not a stash reference"), info->revision.buf);
721
722 return 0;
723 }
724
725 static int drop_stash(int argc, const char **argv, const char *prefix)
726 {
727 int ret = -1;
728 int quiet = 0;
729 struct stash_info info = STASH_INFO_INIT;
730 struct option options[] = {
731 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
732 OPT_END()
733 };
734
735 argc = parse_options(argc, argv, prefix, options,
736 git_stash_drop_usage, 0);
737
738 if (get_stash_info_assert(&info, argc, argv))
739 goto cleanup;
740
741 ret = do_drop_stash(&info, quiet);
742 cleanup:
743 free_stash_info(&info);
744 return ret;
745 }
746
747 static int pop_stash(int argc, const char **argv, const char *prefix)
748 {
749 int ret = -1;
750 int index = 0;
751 int quiet = 0;
752 struct stash_info info = STASH_INFO_INIT;
753 struct option options[] = {
754 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
755 OPT_BOOL(0, "index", &index,
756 N_("attempt to recreate the index")),
757 OPT_END()
758 };
759
760 argc = parse_options(argc, argv, prefix, options,
761 git_stash_pop_usage, 0);
762
763 if (get_stash_info_assert(&info, argc, argv))
764 goto cleanup;
765
766 if ((ret = do_apply_stash(prefix, &info, index, quiet)))
767 printf_ln(_("The stash entry is kept in case "
768 "you need it again."));
769 else
770 ret = do_drop_stash(&info, quiet);
771
772 cleanup:
773 free_stash_info(&info);
774 return ret;
775 }
776
777 static int branch_stash(int argc, const char **argv, const char *prefix)
778 {
779 int ret = -1;
780 const char *branch = NULL;
781 struct stash_info info = STASH_INFO_INIT;
782 struct child_process cp = CHILD_PROCESS_INIT;
783 struct option options[] = {
784 OPT_END()
785 };
786
787 argc = parse_options(argc, argv, prefix, options,
788 git_stash_branch_usage, 0);
789
790 if (!argc) {
791 fprintf_ln(stderr, _("No branch name specified"));
792 return -1;
793 }
794
795 branch = argv[0];
796
797 if (get_stash_info(&info, argc - 1, argv + 1))
798 goto cleanup;
799
800 cp.git_cmd = 1;
801 strvec_pushl(&cp.args, "checkout", "-b", NULL);
802 strvec_push(&cp.args, branch);
803 strvec_push(&cp.args, oid_to_hex(&info.b_commit));
804 ret = run_command(&cp);
805 if (!ret)
806 ret = do_apply_stash(prefix, &info, 1, 0);
807 if (!ret && info.is_stash_ref)
808 ret = do_drop_stash(&info, 0);
809
810 cleanup:
811 free_stash_info(&info);
812 return ret;
813 }
814
815 static int list_stash(int argc, const char **argv, const char *prefix)
816 {
817 struct child_process cp = CHILD_PROCESS_INIT;
818 struct option options[] = {
819 OPT_END()
820 };
821
822 argc = parse_options(argc, argv, prefix, options,
823 git_stash_list_usage,
824 PARSE_OPT_KEEP_UNKNOWN_OPT);
825
826 if (!ref_exists(ref_stash))
827 return 0;
828
829 cp.git_cmd = 1;
830 strvec_pushl(&cp.args, "log", "--format=%gd: %gs", "-g",
831 "--first-parent", NULL);
832 strvec_pushv(&cp.args, argv);
833 strvec_push(&cp.args, ref_stash);
834 strvec_push(&cp.args, "--");
835 return run_command(&cp);
836 }
837
838 static int show_stat = 1;
839 static int show_patch;
840 static int show_include_untracked;
841
842 static int git_stash_config(const char *var, const char *value, void *cb)
843 {
844 if (!strcmp(var, "stash.showstat")) {
845 show_stat = git_config_bool(var, value);
846 return 0;
847 }
848 if (!strcmp(var, "stash.showpatch")) {
849 show_patch = git_config_bool(var, value);
850 return 0;
851 }
852 if (!strcmp(var, "stash.showincludeuntracked")) {
853 show_include_untracked = git_config_bool(var, value);
854 return 0;
855 }
856 return git_diff_basic_config(var, value, cb);
857 }
858
859 static void diff_include_untracked(const struct stash_info *info, struct diff_options *diff_opt)
860 {
861 const struct object_id *oid[] = { &info->w_commit, &info->u_tree };
862 struct tree *tree[ARRAY_SIZE(oid)];
863 struct tree_desc tree_desc[ARRAY_SIZE(oid)];
864 struct unpack_trees_options unpack_tree_opt = { 0 };
865 int i;
866
867 for (i = 0; i < ARRAY_SIZE(oid); i++) {
868 tree[i] = parse_tree_indirect(oid[i]);
869 if (parse_tree(tree[i]) < 0)
870 die(_("failed to parse tree"));
871 init_tree_desc(&tree_desc[i], tree[i]->buffer, tree[i]->size);
872 }
873
874 unpack_tree_opt.head_idx = -1;
875 unpack_tree_opt.src_index = &the_index;
876 unpack_tree_opt.dst_index = &the_index;
877 unpack_tree_opt.merge = 1;
878 unpack_tree_opt.fn = stash_worktree_untracked_merge;
879
880 if (unpack_trees(ARRAY_SIZE(tree_desc), tree_desc, &unpack_tree_opt))
881 die(_("failed to unpack trees"));
882
883 do_diff_cache(&info->b_commit, diff_opt);
884 }
885
886 static int show_stash(int argc, const char **argv, const char *prefix)
887 {
888 int i;
889 int ret = -1;
890 struct stash_info info = STASH_INFO_INIT;
891 struct rev_info rev;
892 struct strvec stash_args = STRVEC_INIT;
893 struct strvec revision_args = STRVEC_INIT;
894 enum {
895 UNTRACKED_NONE,
896 UNTRACKED_INCLUDE,
897 UNTRACKED_ONLY
898 } show_untracked = show_include_untracked ? UNTRACKED_INCLUDE : UNTRACKED_NONE;
899 struct option options[] = {
900 OPT_SET_INT('u', "include-untracked", &show_untracked,
901 N_("include untracked files in the stash"),
902 UNTRACKED_INCLUDE),
903 OPT_SET_INT_F(0, "only-untracked", &show_untracked,
904 N_("only show untracked files in the stash"),
905 UNTRACKED_ONLY, PARSE_OPT_NONEG),
906 OPT_END()
907 };
908 int do_usage = 0;
909
910 init_diff_ui_defaults();
911 git_config(git_diff_ui_config, NULL);
912 repo_init_revisions(the_repository, &rev, prefix);
913
914 argc = parse_options(argc, argv, prefix, options, git_stash_show_usage,
915 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT |
916 PARSE_OPT_KEEP_DASHDASH);
917
918 strvec_push(&revision_args, argv[0]);
919 for (i = 1; i < argc; i++) {
920 if (argv[i][0] != '-')
921 strvec_push(&stash_args, argv[i]);
922 else
923 strvec_push(&revision_args, argv[i]);
924 }
925
926 if (get_stash_info(&info, stash_args.nr, stash_args.v))
927 goto cleanup;
928
929 /*
930 * The config settings are applied only if there are not passed
931 * any options.
932 */
933 if (revision_args.nr == 1) {
934 if (show_stat)
935 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;
936
937 if (show_patch)
938 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
939
940 if (!show_stat && !show_patch) {
941 ret = 0;
942 goto cleanup;
943 }
944 }
945
946 argc = setup_revisions(revision_args.nr, revision_args.v, &rev, NULL);
947 if (argc > 1)
948 goto usage;
949 if (!rev.diffopt.output_format) {
950 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
951 diff_setup_done(&rev.diffopt);
952 }
953
954 rev.diffopt.flags.recursive = 1;
955 setup_diff_pager(&rev.diffopt);
956 switch (show_untracked) {
957 case UNTRACKED_NONE:
958 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
959 break;
960 case UNTRACKED_ONLY:
961 if (info.has_u)
962 diff_root_tree_oid(&info.u_tree, "", &rev.diffopt);
963 break;
964 case UNTRACKED_INCLUDE:
965 if (info.has_u)
966 diff_include_untracked(&info, &rev.diffopt);
967 else
968 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
969 break;
970 }
971 log_tree_diff_flush(&rev);
972
973 ret = diff_result_code(&rev.diffopt, 0);
974 cleanup:
975 strvec_clear(&stash_args);
976 free_stash_info(&info);
977 release_revisions(&rev);
978 if (do_usage)
979 usage_with_options(git_stash_show_usage, options);
980 return ret;
981 usage:
982 do_usage = 1;
983 goto cleanup;
984 }
985
986 static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
987 int quiet)
988 {
989 if (!stash_msg)
990 stash_msg = "Created via \"git stash store\".";
991
992 if (update_ref(stash_msg, ref_stash, w_commit, NULL,
993 REF_FORCE_CREATE_REFLOG,
994 quiet ? UPDATE_REFS_QUIET_ON_ERR :
995 UPDATE_REFS_MSG_ON_ERR)) {
996 if (!quiet) {
997 fprintf_ln(stderr, _("Cannot update %s with %s"),
998 ref_stash, oid_to_hex(w_commit));
999 }
1000 return -1;
1001 }
1002
1003 return 0;
1004 }
1005
1006 static int store_stash(int argc, const char **argv, const char *prefix)
1007 {
1008 int quiet = 0;
1009 const char *stash_msg = NULL;
1010 struct object_id obj;
1011 struct object_context dummy;
1012 struct option options[] = {
1013 OPT__QUIET(&quiet, N_("be quiet")),
1014 OPT_STRING('m', "message", &stash_msg, "message",
1015 N_("stash message")),
1016 OPT_END()
1017 };
1018
1019 argc = parse_options(argc, argv, prefix, options,
1020 git_stash_store_usage,
1021 PARSE_OPT_KEEP_UNKNOWN_OPT);
1022
1023 if (argc != 1) {
1024 if (!quiet)
1025 fprintf_ln(stderr, _("\"git stash store\" requires one "
1026 "<commit> argument"));
1027 return -1;
1028 }
1029
1030 if (get_oid_with_context(the_repository,
1031 argv[0], quiet ? GET_OID_QUIETLY : 0, &obj,
1032 &dummy)) {
1033 if (!quiet)
1034 fprintf_ln(stderr, _("Cannot update %s with %s"),
1035 ref_stash, argv[0]);
1036 return -1;
1037 }
1038
1039 return do_store_stash(&obj, stash_msg, quiet);
1040 }
1041
1042 static void add_pathspecs(struct strvec *args,
1043 const struct pathspec *ps) {
1044 int i;
1045
1046 for (i = 0; i < ps->nr; i++)
1047 strvec_push(args, ps->items[i].original);
1048 }
1049
1050 /*
1051 * `untracked_files` will be filled with the names of untracked files.
1052 * The return value is:
1053 *
1054 * = 0 if there are not any untracked files
1055 * > 0 if there are untracked files
1056 */
1057 static int get_untracked_files(const struct pathspec *ps, int include_untracked,
1058 struct strbuf *untracked_files)
1059 {
1060 int i;
1061 int found = 0;
1062 struct dir_struct dir = DIR_INIT;
1063
1064 if (include_untracked != INCLUDE_ALL_FILES)
1065 setup_standard_excludes(&dir);
1066
1067 fill_directory(&dir, the_repository->index, ps);
1068 for (i = 0; i < dir.nr; i++) {
1069 struct dir_entry *ent = dir.entries[i];
1070 found++;
1071 strbuf_addstr(untracked_files, ent->name);
1072 /* NUL-terminate: will be fed to update-index -z */
1073 strbuf_addch(untracked_files, '\0');
1074 }
1075
1076 dir_clear(&dir);
1077 return found;
1078 }
1079
1080 /*
1081 * The return value of `check_changes_tracked_files()` can be:
1082 *
1083 * < 0 if there was an error
1084 * = 0 if there are no changes.
1085 * > 0 if there are changes.
1086 */
1087 static int check_changes_tracked_files(const struct pathspec *ps)
1088 {
1089 int result;
1090 struct rev_info rev;
1091 struct object_id dummy;
1092 int ret = 0;
1093
1094 /* No initial commit. */
1095 if (repo_get_oid(the_repository, "HEAD", &dummy))
1096 return -1;
1097
1098 if (repo_read_index(the_repository) < 0)
1099 return -1;
1100
1101 repo_init_revisions(the_repository, &rev, NULL);
1102 copy_pathspec(&rev.prune_data, ps);
1103
1104 rev.diffopt.flags.quick = 1;
1105 rev.diffopt.flags.ignore_submodules = 1;
1106 rev.abbrev = 0;
1107
1108 add_head_to_pending(&rev);
1109 diff_setup_done(&rev.diffopt);
1110
1111 result = run_diff_index(&rev, 1);
1112 if (diff_result_code(&rev.diffopt, result)) {
1113 ret = 1;
1114 goto done;
1115 }
1116
1117 result = run_diff_files(&rev, 0);
1118 if (diff_result_code(&rev.diffopt, result)) {
1119 ret = 1;
1120 goto done;
1121 }
1122
1123 done:
1124 release_revisions(&rev);
1125 return ret;
1126 }
1127
1128 /*
1129 * The function will fill `untracked_files` with the names of untracked files
1130 * It will return 1 if there were any changes and 0 if there were not.
1131 */
1132 static int check_changes(const struct pathspec *ps, int include_untracked,
1133 struct strbuf *untracked_files)
1134 {
1135 int ret = 0;
1136 if (check_changes_tracked_files(ps))
1137 ret = 1;
1138
1139 if (include_untracked && get_untracked_files(ps, include_untracked,
1140 untracked_files))
1141 ret = 1;
1142
1143 return ret;
1144 }
1145
1146 static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
1147 struct strbuf files)
1148 {
1149 int ret = 0;
1150 struct strbuf untracked_msg = STRBUF_INIT;
1151 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1152 struct index_state istate = INDEX_STATE_INIT(the_repository);
1153
1154 cp_upd_index.git_cmd = 1;
1155 strvec_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
1156 "--remove", "--stdin", NULL);
1157 strvec_pushf(&cp_upd_index.env, "GIT_INDEX_FILE=%s",
1158 stash_index_path.buf);
1159
1160 strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
1161 if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,
1162 NULL, 0)) {
1163 ret = -1;
1164 goto done;
1165 }
1166
1167 if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
1168 NULL)) {
1169 ret = -1;
1170 goto done;
1171 }
1172
1173 if (commit_tree(untracked_msg.buf, untracked_msg.len,
1174 &info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
1175 ret = -1;
1176 goto done;
1177 }
1178
1179 done:
1180 release_index(&istate);
1181 strbuf_release(&untracked_msg);
1182 remove_path(stash_index_path.buf);
1183 return ret;
1184 }
1185
1186 static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
1187 int quiet)
1188 {
1189 int ret = 0;
1190 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1191 struct index_state istate = INDEX_STATE_INIT(the_repository);
1192
1193 if (write_index_as_tree(&info->w_tree, &istate, the_repository->index_file,
1194 0, NULL)) {
1195 ret = -1;
1196 goto done;
1197 }
1198
1199 cp_diff_tree.git_cmd = 1;
1200 strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD",
1201 oid_to_hex(&info->w_tree), "--", NULL);
1202 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
1203 ret = -1;
1204 goto done;
1205 }
1206
1207 if (!out_patch->len) {
1208 if (!quiet)
1209 fprintf_ln(stderr, _("No staged changes"));
1210 ret = 1;
1211 }
1212
1213 done:
1214 release_index(&istate);
1215 return ret;
1216 }
1217
1218 static int stash_patch(struct stash_info *info, const struct pathspec *ps,
1219 struct strbuf *out_patch, int quiet)
1220 {
1221 int ret = 0;
1222 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
1223 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1224 struct index_state istate = INDEX_STATE_INIT(the_repository);
1225 char *old_index_env = NULL, *old_repo_index_file;
1226
1227 remove_path(stash_index_path.buf);
1228
1229 cp_read_tree.git_cmd = 1;
1230 strvec_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL);
1231 strvec_pushf(&cp_read_tree.env, "GIT_INDEX_FILE=%s",
1232 stash_index_path.buf);
1233 if (run_command(&cp_read_tree)) {
1234 ret = -1;
1235 goto done;
1236 }
1237
1238 /* Find out what the user wants. */
1239 old_repo_index_file = the_repository->index_file;
1240 the_repository->index_file = stash_index_path.buf;
1241 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
1242 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
1243
1244 ret = !!run_add_p(the_repository, ADD_P_STASH, NULL, ps);
1245
1246 the_repository->index_file = old_repo_index_file;
1247 if (old_index_env && *old_index_env)
1248 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
1249 else
1250 unsetenv(INDEX_ENVIRONMENT);
1251 FREE_AND_NULL(old_index_env);
1252
1253 /* State of the working tree. */
1254 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1255 NULL)) {
1256 ret = -1;
1257 goto done;
1258 }
1259
1260 cp_diff_tree.git_cmd = 1;
1261 strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD",
1262 oid_to_hex(&info->w_tree), "--", NULL);
1263 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
1264 ret = -1;
1265 goto done;
1266 }
1267
1268 if (!out_patch->len) {
1269 if (!quiet)
1270 fprintf_ln(stderr, _("No changes selected"));
1271 ret = 1;
1272 }
1273
1274 done:
1275 release_index(&istate);
1276 remove_path(stash_index_path.buf);
1277 return ret;
1278 }
1279
1280 static int stash_working_tree(struct stash_info *info, const struct pathspec *ps)
1281 {
1282 int ret = 0;
1283 struct rev_info rev;
1284 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1285 struct strbuf diff_output = STRBUF_INIT;
1286 struct index_state istate = INDEX_STATE_INIT(the_repository);
1287
1288 repo_init_revisions(the_repository, &rev, NULL);
1289 copy_pathspec(&rev.prune_data, ps);
1290
1291 set_alternate_index_output(stash_index_path.buf);
1292 if (reset_tree(&info->i_tree, 0, 0)) {
1293 ret = -1;
1294 goto done;
1295 }
1296 set_alternate_index_output(NULL);
1297
1298 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
1299 rev.diffopt.format_callback = add_diff_to_buf;
1300 rev.diffopt.format_callback_data = &diff_output;
1301
1302 if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
1303 ret = -1;
1304 goto done;
1305 }
1306
1307 add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
1308 "");
1309 if (run_diff_index(&rev, 0)) {
1310 ret = -1;
1311 goto done;
1312 }
1313
1314 cp_upd_index.git_cmd = 1;
1315 strvec_pushl(&cp_upd_index.args, "update-index",
1316 "--ignore-skip-worktree-entries",
1317 "-z", "--add", "--remove", "--stdin", NULL);
1318 strvec_pushf(&cp_upd_index.env, "GIT_INDEX_FILE=%s",
1319 stash_index_path.buf);
1320
1321 if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,
1322 NULL, 0, NULL, 0)) {
1323 ret = -1;
1324 goto done;
1325 }
1326
1327 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1328 NULL)) {
1329 ret = -1;
1330 goto done;
1331 }
1332
1333 done:
1334 release_index(&istate);
1335 release_revisions(&rev);
1336 strbuf_release(&diff_output);
1337 remove_path(stash_index_path.buf);
1338 return ret;
1339 }
1340
1341 static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,
1342 int include_untracked, int patch_mode, int only_staged,
1343 struct stash_info *info, struct strbuf *patch,
1344 int quiet)
1345 {
1346 int ret = 0;
1347 int flags = 0;
1348 int untracked_commit_option = 0;
1349 const char *head_short_sha1 = NULL;
1350 const char *branch_ref = NULL;
1351 const char *branch_name = "(no branch)";
1352 struct commit *head_commit = NULL;
1353 struct commit_list *parents = NULL;
1354 struct strbuf msg = STRBUF_INIT;
1355 struct strbuf commit_tree_label = STRBUF_INIT;
1356 struct strbuf untracked_files = STRBUF_INIT;
1357
1358 prepare_fallback_ident("git stash", "git@stash");
1359
1360 repo_read_index_preload(the_repository, NULL, 0);
1361 if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
1362 NULL, NULL, NULL) < 0) {
1363 ret = -1;
1364 goto done;
1365 }
1366
1367 if (repo_get_oid(the_repository, "HEAD", &info->b_commit)) {
1368 if (!quiet)
1369 fprintf_ln(stderr, _("You do not have "
1370 "the initial commit yet"));
1371 ret = -1;
1372 goto done;
1373 } else {
1374 head_commit = lookup_commit(the_repository, &info->b_commit);
1375 }
1376
1377 if (!check_changes(ps, include_untracked, &untracked_files)) {
1378 ret = 1;
1379 goto done;
1380 }
1381
1382 branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
1383 if (flags & REF_ISSYMREF)
1384 skip_prefix(branch_ref, "refs/heads/", &branch_name);
1385 head_short_sha1 = repo_find_unique_abbrev(the_repository,
1386 &head_commit->object.oid,
1387 DEFAULT_ABBREV);
1388 strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);
1389 pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
1390
1391 strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
1392 commit_list_insert(head_commit, &parents);
1393 if (write_index_as_tree(&info->i_tree, &the_index, get_index_file(), 0,
1394 NULL) ||
1395 commit_tree(commit_tree_label.buf, commit_tree_label.len,
1396 &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1397 if (!quiet)
1398 fprintf_ln(stderr, _("Cannot save the current "
1399 "index state"));
1400 ret = -1;
1401 goto done;
1402 }
1403
1404 if (include_untracked) {
1405 if (save_untracked_files(info, &msg, untracked_files)) {
1406 if (!quiet)
1407 fprintf_ln(stderr, _("Cannot save "
1408 "the untracked files"));
1409 ret = -1;
1410 goto done;
1411 }
1412 untracked_commit_option = 1;
1413 }
1414 if (patch_mode) {
1415 ret = stash_patch(info, ps, patch, quiet);
1416 if (ret < 0) {
1417 if (!quiet)
1418 fprintf_ln(stderr, _("Cannot save the current "
1419 "worktree state"));
1420 goto done;
1421 } else if (ret > 0) {
1422 goto done;
1423 }
1424 } else if (only_staged) {
1425 ret = stash_staged(info, patch, quiet);
1426 if (ret < 0) {
1427 if (!quiet)
1428 fprintf_ln(stderr, _("Cannot save the current "
1429 "staged state"));
1430 goto done;
1431 } else if (ret > 0) {
1432 goto done;
1433 }
1434 } else {
1435 if (stash_working_tree(info, ps)) {
1436 if (!quiet)
1437 fprintf_ln(stderr, _("Cannot save the current "
1438 "worktree state"));
1439 ret = -1;
1440 goto done;
1441 }
1442 }
1443
1444 if (!stash_msg_buf->len)
1445 strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf);
1446 else
1447 strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name);
1448
1449 /*
1450 * `parents` will be empty after calling `commit_tree()`, so there is
1451 * no need to call `free_commit_list()`
1452 */
1453 parents = NULL;
1454 if (untracked_commit_option)
1455 commit_list_insert(lookup_commit(the_repository,
1456 &info->u_commit),
1457 &parents);
1458 commit_list_insert(lookup_commit(the_repository, &info->i_commit),
1459 &parents);
1460 commit_list_insert(head_commit, &parents);
1461
1462 if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
1463 parents, &info->w_commit, NULL, NULL)) {
1464 if (!quiet)
1465 fprintf_ln(stderr, _("Cannot record "
1466 "working tree state"));
1467 ret = -1;
1468 goto done;
1469 }
1470
1471 done:
1472 strbuf_release(&commit_tree_label);
1473 strbuf_release(&msg);
1474 strbuf_release(&untracked_files);
1475 return ret;
1476 }
1477
1478 static int create_stash(int argc, const char **argv, const char *prefix UNUSED)
1479 {
1480 int ret;
1481 struct strbuf stash_msg_buf = STRBUF_INIT;
1482 struct stash_info info = STASH_INFO_INIT;
1483 struct pathspec ps;
1484
1485 /* Starting with argv[1], since argv[0] is "create" */
1486 strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
1487
1488 memset(&ps, 0, sizeof(ps));
1489 if (!check_changes_tracked_files(&ps))
1490 return 0;
1491
1492 ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, 0, &info,
1493 NULL, 0);
1494 if (!ret)
1495 printf_ln("%s", oid_to_hex(&info.w_commit));
1496
1497 free_stash_info(&info);
1498 strbuf_release(&stash_msg_buf);
1499 return ret;
1500 }
1501
1502 static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,
1503 int keep_index, int patch_mode, int include_untracked, int only_staged)
1504 {
1505 int ret = 0;
1506 struct stash_info info = STASH_INFO_INIT;
1507 struct strbuf patch = STRBUF_INIT;
1508 struct strbuf stash_msg_buf = STRBUF_INIT;
1509 struct strbuf untracked_files = STRBUF_INIT;
1510
1511 if (patch_mode && keep_index == -1)
1512 keep_index = 1;
1513
1514 if (patch_mode && include_untracked) {
1515 fprintf_ln(stderr, _("Can't use --patch and --include-untracked"
1516 " or --all at the same time"));
1517 ret = -1;
1518 goto done;
1519 }
1520
1521 /* --patch overrides --staged */
1522 if (patch_mode)
1523 only_staged = 0;
1524
1525 if (only_staged && include_untracked) {
1526 fprintf_ln(stderr, _("Can't use --staged and --include-untracked"
1527 " or --all at the same time"));
1528 ret = -1;
1529 goto done;
1530 }
1531
1532 repo_read_index_preload(the_repository, NULL, 0);
1533 if (!include_untracked && ps->nr) {
1534 int i;
1535 char *ps_matched = xcalloc(ps->nr, 1);
1536
1537 /* TODO: audit for interaction with sparse-index. */
1538 ensure_full_index(&the_index);
1539 for (i = 0; i < the_index.cache_nr; i++)
1540 ce_path_match(&the_index, the_index.cache[i], ps,
1541 ps_matched);
1542
1543 if (report_path_error(ps_matched, ps)) {
1544 fprintf_ln(stderr, _("Did you forget to 'git add'?"));
1545 ret = -1;
1546 free(ps_matched);
1547 goto done;
1548 }
1549 free(ps_matched);
1550 }
1551
1552 if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
1553 NULL, NULL, NULL)) {
1554 ret = -1;
1555 goto done;
1556 }
1557
1558 if (!check_changes(ps, include_untracked, &untracked_files)) {
1559 if (!quiet)
1560 printf_ln(_("No local changes to save"));
1561 goto done;
1562 }
1563
1564 if (!reflog_exists(ref_stash) && do_clear_stash()) {
1565 ret = -1;
1566 if (!quiet)
1567 fprintf_ln(stderr, _("Cannot initialize stash"));
1568 goto done;
1569 }
1570
1571 if (stash_msg)
1572 strbuf_addstr(&stash_msg_buf, stash_msg);
1573 if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode, only_staged,
1574 &info, &patch, quiet)) {
1575 ret = -1;
1576 goto done;
1577 }
1578
1579 if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
1580 ret = -1;
1581 if (!quiet)
1582 fprintf_ln(stderr, _("Cannot save the current status"));
1583 goto done;
1584 }
1585
1586 if (!quiet)
1587 printf_ln(_("Saved working directory and index state %s"),
1588 stash_msg_buf.buf);
1589
1590 if (!(patch_mode || only_staged)) {
1591 if (include_untracked && !ps->nr) {
1592 struct child_process cp = CHILD_PROCESS_INIT;
1593
1594 cp.git_cmd = 1;
1595 if (startup_info->original_cwd) {
1596 cp.dir = startup_info->original_cwd;
1597 strvec_pushf(&cp.env, "%s=%s",
1598 GIT_WORK_TREE_ENVIRONMENT,
1599 the_repository->worktree);
1600 }
1601 strvec_pushl(&cp.args, "clean", "--force",
1602 "--quiet", "-d", ":/", NULL);
1603 if (include_untracked == INCLUDE_ALL_FILES)
1604 strvec_push(&cp.args, "-x");
1605 if (run_command(&cp)) {
1606 ret = -1;
1607 goto done;
1608 }
1609 }
1610 discard_index(&the_index);
1611 if (ps->nr) {
1612 struct child_process cp_add = CHILD_PROCESS_INIT;
1613 struct child_process cp_diff = CHILD_PROCESS_INIT;
1614 struct child_process cp_apply = CHILD_PROCESS_INIT;
1615 struct strbuf out = STRBUF_INIT;
1616
1617 cp_add.git_cmd = 1;
1618 strvec_push(&cp_add.args, "add");
1619 if (!include_untracked)
1620 strvec_push(&cp_add.args, "-u");
1621 if (include_untracked == INCLUDE_ALL_FILES)
1622 strvec_push(&cp_add.args, "--force");
1623 strvec_push(&cp_add.args, "--");
1624 add_pathspecs(&cp_add.args, ps);
1625 if (run_command(&cp_add)) {
1626 ret = -1;
1627 goto done;
1628 }
1629
1630 cp_diff.git_cmd = 1;
1631 strvec_pushl(&cp_diff.args, "diff-index", "-p",
1632 "--cached", "--binary", "HEAD", "--",
1633 NULL);
1634 add_pathspecs(&cp_diff.args, ps);
1635 if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {
1636 ret = -1;
1637 goto done;
1638 }
1639
1640 cp_apply.git_cmd = 1;
1641 strvec_pushl(&cp_apply.args, "apply", "--index",
1642 "-R", NULL);
1643 if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,
1644 NULL, 0)) {
1645 ret = -1;
1646 goto done;
1647 }
1648 } else {
1649 struct child_process cp = CHILD_PROCESS_INIT;
1650 cp.git_cmd = 1;
1651 /* BUG: this nukes untracked files in the way */
1652 strvec_pushl(&cp.args, "reset", "--hard", "-q",
1653 "--no-recurse-submodules", NULL);
1654 if (run_command(&cp)) {
1655 ret = -1;
1656 goto done;
1657 }
1658 }
1659
1660 if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1661 struct child_process cp = CHILD_PROCESS_INIT;
1662
1663 cp.git_cmd = 1;
1664 strvec_pushl(&cp.args, "checkout", "--no-overlay",
1665 oid_to_hex(&info.i_tree), "--", NULL);
1666 if (!ps->nr)
1667 strvec_push(&cp.args, ":/");
1668 else
1669 add_pathspecs(&cp.args, ps);
1670 if (run_command(&cp)) {
1671 ret = -1;
1672 goto done;
1673 }
1674 }
1675 goto done;
1676 } else {
1677 struct child_process cp = CHILD_PROCESS_INIT;
1678
1679 cp.git_cmd = 1;
1680 strvec_pushl(&cp.args, "apply", "-R", NULL);
1681
1682 if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1683 if (!quiet)
1684 fprintf_ln(stderr, _("Cannot remove "
1685 "worktree changes"));
1686 ret = -1;
1687 goto done;
1688 }
1689
1690 if (keep_index < 1) {
1691 struct child_process cp = CHILD_PROCESS_INIT;
1692
1693 cp.git_cmd = 1;
1694 strvec_pushl(&cp.args, "reset", "-q", "--refresh", "--",
1695 NULL);
1696 add_pathspecs(&cp.args, ps);
1697 if (run_command(&cp)) {
1698 ret = -1;
1699 goto done;
1700 }
1701 }
1702 goto done;
1703 }
1704
1705 done:
1706 strbuf_release(&patch);
1707 free_stash_info(&info);
1708 strbuf_release(&stash_msg_buf);
1709 strbuf_release(&untracked_files);
1710 return ret;
1711 }
1712
1713 static int push_stash(int argc, const char **argv, const char *prefix,
1714 int push_assumed)
1715 {
1716 int force_assume = 0;
1717 int keep_index = -1;
1718 int only_staged = 0;
1719 int patch_mode = 0;
1720 int include_untracked = 0;
1721 int quiet = 0;
1722 int pathspec_file_nul = 0;
1723 const char *stash_msg = NULL;
1724 const char *pathspec_from_file = NULL;
1725 struct pathspec ps;
1726 struct option options[] = {
1727 OPT_BOOL('k', "keep-index", &keep_index,
1728 N_("keep index")),
1729 OPT_BOOL('S', "staged", &only_staged,
1730 N_("stash staged changes only")),
1731 OPT_BOOL('p', "patch", &patch_mode,
1732 N_("stash in patch mode")),
1733 OPT__QUIET(&quiet, N_("quiet mode")),
1734 OPT_BOOL('u', "include-untracked", &include_untracked,
1735 N_("include untracked files in stash")),
1736 OPT_SET_INT('a', "all", &include_untracked,
1737 N_("include ignore files"), 2),
1738 OPT_STRING('m', "message", &stash_msg, N_("message"),
1739 N_("stash message")),
1740 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1741 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
1742 OPT_END()
1743 };
1744 int ret;
1745
1746 if (argc) {
1747 force_assume = !strcmp(argv[0], "-p");
1748 argc = parse_options(argc, argv, prefix, options,
1749 push_assumed ? git_stash_usage :
1750 git_stash_push_usage,
1751 PARSE_OPT_KEEP_DASHDASH);
1752 }
1753
1754 if (argc) {
1755 if (!strcmp(argv[0], "--")) {
1756 argc--;
1757 argv++;
1758 } else if (push_assumed && !force_assume) {
1759 die("subcommand wasn't specified; 'push' can't be assumed due to unexpected token '%s'",
1760 argv[0]);
1761 }
1762 }
1763
1764 parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1765 prefix, argv);
1766
1767 if (pathspec_from_file) {
1768 if (patch_mode)
1769 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
1770
1771 if (only_staged)
1772 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--staged");
1773
1774 if (ps.nr)
1775 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
1776
1777 parse_pathspec_file(&ps, 0,
1778 PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1779 prefix, pathspec_from_file, pathspec_file_nul);
1780 } else if (pathspec_file_nul) {
1781 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
1782 }
1783
1784 ret = do_push_stash(&ps, stash_msg, quiet, keep_index, patch_mode,
1785 include_untracked, only_staged);
1786 clear_pathspec(&ps);
1787 return ret;
1788 }
1789
1790 static int push_stash_unassumed(int argc, const char **argv, const char *prefix)
1791 {
1792 return push_stash(argc, argv, prefix, 0);
1793 }
1794
1795 static int save_stash(int argc, const char **argv, const char *prefix)
1796 {
1797 int keep_index = -1;
1798 int only_staged = 0;
1799 int patch_mode = 0;
1800 int include_untracked = 0;
1801 int quiet = 0;
1802 int ret = 0;
1803 const char *stash_msg = NULL;
1804 struct pathspec ps;
1805 struct strbuf stash_msg_buf = STRBUF_INIT;
1806 struct option options[] = {
1807 OPT_BOOL('k', "keep-index", &keep_index,
1808 N_("keep index")),
1809 OPT_BOOL('S', "staged", &only_staged,
1810 N_("stash staged changes only")),
1811 OPT_BOOL('p', "patch", &patch_mode,
1812 N_("stash in patch mode")),
1813 OPT__QUIET(&quiet, N_("quiet mode")),
1814 OPT_BOOL('u', "include-untracked", &include_untracked,
1815 N_("include untracked files in stash")),
1816 OPT_SET_INT('a', "all", &include_untracked,
1817 N_("include ignore files"), 2),
1818 OPT_STRING('m', "message", &stash_msg, "message",
1819 N_("stash message")),
1820 OPT_END()
1821 };
1822
1823 argc = parse_options(argc, argv, prefix, options,
1824 git_stash_save_usage,
1825 PARSE_OPT_KEEP_DASHDASH);
1826
1827 if (argc)
1828 stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
1829
1830 memset(&ps, 0, sizeof(ps));
1831 ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
1832 patch_mode, include_untracked, only_staged);
1833
1834 strbuf_release(&stash_msg_buf);
1835 return ret;
1836 }
1837
1838 int cmd_stash(int argc, const char **argv, const char *prefix)
1839 {
1840 pid_t pid = getpid();
1841 const char *index_file;
1842 struct strvec args = STRVEC_INIT;
1843 parse_opt_subcommand_fn *fn = NULL;
1844 struct option options[] = {
1845 OPT_SUBCOMMAND("apply", &fn, apply_stash),
1846 OPT_SUBCOMMAND("clear", &fn, clear_stash),
1847 OPT_SUBCOMMAND("drop", &fn, drop_stash),
1848 OPT_SUBCOMMAND("pop", &fn, pop_stash),
1849 OPT_SUBCOMMAND("branch", &fn, branch_stash),
1850 OPT_SUBCOMMAND("list", &fn, list_stash),
1851 OPT_SUBCOMMAND("show", &fn, show_stash),
1852 OPT_SUBCOMMAND("store", &fn, store_stash),
1853 OPT_SUBCOMMAND("create", &fn, create_stash),
1854 OPT_SUBCOMMAND("push", &fn, push_stash_unassumed),
1855 OPT_SUBCOMMAND_F("save", &fn, save_stash, PARSE_OPT_NOCOMPLETE),
1856 OPT_END()
1857 };
1858
1859 git_config(git_stash_config, NULL);
1860
1861 argc = parse_options(argc, argv, prefix, options, git_stash_usage,
1862 PARSE_OPT_SUBCOMMAND_OPTIONAL |
1863 PARSE_OPT_KEEP_UNKNOWN_OPT |
1864 PARSE_OPT_KEEP_DASHDASH);
1865
1866 prepare_repo_settings(the_repository);
1867 the_repository->settings.command_requires_full_index = 0;
1868
1869 index_file = get_index_file();
1870 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1871 (uintmax_t)pid);
1872
1873 if (fn)
1874 return !!fn(argc, argv, prefix);
1875 else if (!argc)
1876 return !!push_stash_unassumed(0, NULL, prefix);
1877
1878 /* Assume 'stash push' */
1879 strvec_push(&args, "push");
1880 strvec_pushv(&args, argv);
1881 return !!push_stash(args.nr, args.v, prefix, 1);
1882 }