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