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