]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/stash.c
Merge branch 'jk/clone-allow-bare-and-o-together'
[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
5cf88fd8
ÆAB
641static int reject_reflog_ent(struct object_id *ooid UNUSED,
642 struct object_id *noid UNUSED,
643 const char *email UNUSED,
644 timestamp_t timestamp UNUSED,
645 int tz UNUSED, const char *message UNUSED,
646 void *cb_data UNUSED)
4f44c565
RS
647{
648 return 1;
649}
650
651static int reflog_is_empty(const char *refname)
652{
653 return !for_each_reflog_ent(refname, reject_reflog_ent, NULL);
654}
655
eabf7405 656static int do_drop_stash(struct stash_info *info, int quiet)
4e2dd393 657{
758b4d2b
JC
658 if (!reflog_delete(info->revision.buf,
659 EXPIRE_REFLOGS_REWRITE | EXPIRE_REFLOGS_UPDATE_REF,
660 0)) {
4e2dd393
JT
661 if (!quiet)
662 printf_ln(_("Dropped %s (%s)"), info->revision.buf,
663 oid_to_hex(&info->w_commit));
664 } else {
665 return error(_("%s: Could not drop stash entry"),
666 info->revision.buf);
667 }
668
4f44c565 669 if (reflog_is_empty(ref_stash))
4e2dd393
JT
670 do_clear_stash();
671
672 return 0;
673}
674
5e480176
ÆAB
675static int get_stash_info_assert(struct stash_info *info, int argc,
676 const char **argv)
4e2dd393 677{
5e480176
ÆAB
678 int ret = get_stash_info(info, argc, argv);
679
680 if (ret < 0)
681 return ret;
682
683 if (!info->is_stash_ref)
684 return error(_("'%s' is not a stash reference"), info->revision.buf);
685
686 return 0;
4e2dd393
JT
687}
688
689static int drop_stash(int argc, const char **argv, const char *prefix)
690{
5e480176 691 int ret = -1;
4e2dd393 692 int quiet = 0;
5e480176 693 struct stash_info info = STASH_INFO_INIT;
4e2dd393
JT
694 struct option options[] = {
695 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
696 OPT_END()
697 };
698
699 argc = parse_options(argc, argv, prefix, options,
40af1468 700 git_stash_drop_usage, 0);
4e2dd393 701
5e480176
ÆAB
702 if (get_stash_info_assert(&info, argc, argv))
703 goto cleanup;
4e2dd393 704
eabf7405 705 ret = do_drop_stash(&info, quiet);
5e480176 706cleanup:
4e2dd393
JT
707 free_stash_info(&info);
708 return ret;
709}
710
c4de61d7
JT
711static int pop_stash(int argc, const char **argv, const char *prefix)
712{
5e480176 713 int ret = -1;
c4de61d7
JT
714 int index = 0;
715 int quiet = 0;
5e480176 716 struct stash_info info = STASH_INFO_INIT;
c4de61d7
JT
717 struct option options[] = {
718 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
719 OPT_BOOL(0, "index", &index,
720 N_("attempt to recreate the index")),
721 OPT_END()
722 };
723
724 argc = parse_options(argc, argv, prefix, options,
40af1468 725 git_stash_pop_usage, 0);
c4de61d7 726
5e480176
ÆAB
727 if (get_stash_info_assert(&info, argc, argv))
728 goto cleanup;
c4de61d7 729
c4de61d7
JT
730 if ((ret = do_apply_stash(prefix, &info, index, quiet)))
731 printf_ln(_("The stash entry is kept in case "
732 "you need it again."));
733 else
eabf7405 734 ret = do_drop_stash(&info, quiet);
c4de61d7 735
5e480176 736cleanup:
c4de61d7
JT
737 free_stash_info(&info);
738 return ret;
739}
740
577c1995
JT
741static int branch_stash(int argc, const char **argv, const char *prefix)
742{
5e480176 743 int ret = -1;
577c1995 744 const char *branch = NULL;
5e480176 745 struct stash_info info = STASH_INFO_INIT;
577c1995
JT
746 struct child_process cp = CHILD_PROCESS_INIT;
747 struct option options[] = {
748 OPT_END()
749 };
750
751 argc = parse_options(argc, argv, prefix, options,
40af1468 752 git_stash_branch_usage, 0);
577c1995
JT
753
754 if (!argc) {
755 fprintf_ln(stderr, _("No branch name specified"));
756 return -1;
757 }
758
759 branch = argv[0];
760
761 if (get_stash_info(&info, argc - 1, argv + 1))
5e480176 762 goto cleanup;
577c1995
JT
763
764 cp.git_cmd = 1;
22f9b7f3
JK
765 strvec_pushl(&cp.args, "checkout", "-b", NULL);
766 strvec_push(&cp.args, branch);
767 strvec_push(&cp.args, oid_to_hex(&info.b_commit));
577c1995
JT
768 ret = run_command(&cp);
769 if (!ret)
770 ret = do_apply_stash(prefix, &info, 1, 0);
771 if (!ret && info.is_stash_ref)
eabf7405 772 ret = do_drop_stash(&info, 0);
577c1995 773
5e480176 774cleanup:
577c1995 775 free_stash_info(&info);
577c1995
JT
776 return ret;
777}
778
130f2697
PSU
779static int list_stash(int argc, const char **argv, const char *prefix)
780{
781 struct child_process cp = CHILD_PROCESS_INIT;
782 struct option options[] = {
783 OPT_END()
784 };
785
786 argc = parse_options(argc, argv, prefix, options,
40af1468 787 git_stash_list_usage,
99d86d60 788 PARSE_OPT_KEEP_UNKNOWN_OPT);
130f2697
PSU
789
790 if (!ref_exists(ref_stash))
791 return 0;
792
793 cp.git_cmd = 1;
22f9b7f3 794 strvec_pushl(&cp.args, "log", "--format=%gd: %gs", "-g",
1e20a407 795 "--first-parent", NULL);
22f9b7f3
JK
796 strvec_pushv(&cp.args, argv);
797 strvec_push(&cp.args, ref_stash);
798 strvec_push(&cp.args, "--");
130f2697
PSU
799 return run_command(&cp);
800}
801
dc7bd382
PSU
802static int show_stat = 1;
803static int show_patch;
0af760e2 804static int show_include_untracked;
dc7bd382
PSU
805
806static int git_stash_config(const char *var, const char *value, void *cb)
807{
808 if (!strcmp(var, "stash.showstat")) {
809 show_stat = git_config_bool(var, value);
810 return 0;
811 }
812 if (!strcmp(var, "stash.showpatch")) {
813 show_patch = git_config_bool(var, value);
814 return 0;
815 }
0af760e2
DL
816 if (!strcmp(var, "stash.showincludeuntracked")) {
817 show_include_untracked = git_config_bool(var, value);
818 return 0;
819 }
b0c7362d 820 return git_diff_basic_config(var, value, cb);
dc7bd382
PSU
821}
822
d3c7bf73
DL
823static void diff_include_untracked(const struct stash_info *info, struct diff_options *diff_opt)
824{
825 const struct object_id *oid[] = { &info->w_commit, &info->u_tree };
826 struct tree *tree[ARRAY_SIZE(oid)];
827 struct tree_desc tree_desc[ARRAY_SIZE(oid)];
828 struct unpack_trees_options unpack_tree_opt = { 0 };
829 int i;
830
831 for (i = 0; i < ARRAY_SIZE(oid); i++) {
832 tree[i] = parse_tree_indirect(oid[i]);
833 if (parse_tree(tree[i]) < 0)
834 die(_("failed to parse tree"));
835 init_tree_desc(&tree_desc[i], tree[i]->buffer, tree[i]->size);
836 }
837
838 unpack_tree_opt.head_idx = -1;
839 unpack_tree_opt.src_index = &the_index;
840 unpack_tree_opt.dst_index = &the_index;
841 unpack_tree_opt.merge = 1;
842 unpack_tree_opt.fn = stash_worktree_untracked_merge;
843
844 if (unpack_trees(ARRAY_SIZE(tree_desc), tree_desc, &unpack_tree_opt))
845 die(_("failed to unpack trees"));
846
847 do_diff_cache(&info->b_commit, diff_opt);
848}
849
dc7bd382
PSU
850static int show_stash(int argc, const char **argv, const char *prefix)
851{
852 int i;
5e480176
ÆAB
853 int ret = -1;
854 struct stash_info info = STASH_INFO_INIT;
dc7bd382 855 struct rev_info rev;
22f9b7f3
JK
856 struct strvec stash_args = STRVEC_INIT;
857 struct strvec revision_args = STRVEC_INIT;
d3c7bf73
DL
858 enum {
859 UNTRACKED_NONE,
860 UNTRACKED_INCLUDE,
861 UNTRACKED_ONLY
af5cd44b 862 } show_untracked = show_include_untracked ? UNTRACKED_INCLUDE : UNTRACKED_NONE;
dc7bd382 863 struct option options[] = {
d3c7bf73
DL
864 OPT_SET_INT('u', "include-untracked", &show_untracked,
865 N_("include untracked files in the stash"),
866 UNTRACKED_INCLUDE),
867 OPT_SET_INT_F(0, "only-untracked", &show_untracked,
868 N_("only show untracked files in the stash"),
869 UNTRACKED_ONLY, PARSE_OPT_NONEG),
dc7bd382
PSU
870 OPT_END()
871 };
5e480176 872 int do_usage = 0;
dc7bd382
PSU
873
874 init_diff_ui_defaults();
875 git_config(git_diff_ui_config, NULL);
876 init_revisions(&rev, prefix);
877
d3c7bf73 878 argc = parse_options(argc, argv, prefix, options, git_stash_show_usage,
99d86d60 879 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT |
d3c7bf73
DL
880 PARSE_OPT_KEEP_DASHDASH);
881
22f9b7f3 882 strvec_push(&revision_args, argv[0]);
dc7bd382
PSU
883 for (i = 1; i < argc; i++) {
884 if (argv[i][0] != '-')
22f9b7f3 885 strvec_push(&stash_args, argv[i]);
dc7bd382 886 else
22f9b7f3 887 strvec_push(&revision_args, argv[i]);
dc7bd382
PSU
888 }
889
5e480176
ÆAB
890 if (get_stash_info(&info, stash_args.nr, stash_args.v))
891 goto cleanup;
dc7bd382
PSU
892
893 /*
894 * The config settings are applied only if there are not passed
895 * any options.
896 */
d70a9eb6 897 if (revision_args.nr == 1) {
dc7bd382
PSU
898 if (show_stat)
899 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;
900
901 if (show_patch)
902 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
903
904 if (!show_stat && !show_patch) {
5e480176
ÆAB
905 ret = 0;
906 goto cleanup;
dc7bd382
PSU
907 }
908 }
909
d70a9eb6 910 argc = setup_revisions(revision_args.nr, revision_args.v, &rev, NULL);
5e480176
ÆAB
911 if (argc > 1)
912 goto usage;
8e407bc8
TG
913 if (!rev.diffopt.output_format) {
914 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
915 diff_setup_done(&rev.diffopt);
916 }
dc7bd382
PSU
917
918 rev.diffopt.flags.recursive = 1;
919 setup_diff_pager(&rev.diffopt);
d3c7bf73
DL
920 switch (show_untracked) {
921 case UNTRACKED_NONE:
922 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
923 break;
924 case UNTRACKED_ONLY:
1ff595d2
DL
925 if (info.has_u)
926 diff_root_tree_oid(&info.u_tree, "", &rev.diffopt);
d3c7bf73
DL
927 break;
928 case UNTRACKED_INCLUDE:
1ff595d2
DL
929 if (info.has_u)
930 diff_include_untracked(&info, &rev.diffopt);
931 else
932 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
d3c7bf73
DL
933 break;
934 }
dc7bd382
PSU
935 log_tree_diff_flush(&rev);
936
5e480176
ÆAB
937 ret = diff_result_code(&rev.diffopt, 0);
938cleanup:
939 strvec_clear(&stash_args);
dc7bd382 940 free_stash_info(&info);
0139c58a 941 release_revisions(&rev);
5e480176
ÆAB
942 if (do_usage)
943 usage_with_options(git_stash_show_usage, options);
944 return ret;
945usage:
946 do_usage = 1;
947 goto cleanup;
dc7bd382
PSU
948}
949
41e0dd55
PSU
950static int do_store_stash(const struct object_id *w_commit, const char *stash_msg,
951 int quiet)
952{
953 if (!stash_msg)
954 stash_msg = "Created via \"git stash store\".";
955
956 if (update_ref(stash_msg, ref_stash, w_commit, NULL,
957 REF_FORCE_CREATE_REFLOG,
958 quiet ? UPDATE_REFS_QUIET_ON_ERR :
959 UPDATE_REFS_MSG_ON_ERR)) {
960 if (!quiet) {
961 fprintf_ln(stderr, _("Cannot update %s with %s"),
962 ref_stash, oid_to_hex(w_commit));
963 }
964 return -1;
965 }
966
967 return 0;
968}
969
970static int store_stash(int argc, const char **argv, const char *prefix)
971{
972 int quiet = 0;
973 const char *stash_msg = NULL;
974 struct object_id obj;
975 struct object_context dummy;
976 struct option options[] = {
977 OPT__QUIET(&quiet, N_("be quiet")),
978 OPT_STRING('m', "message", &stash_msg, "message",
979 N_("stash message")),
980 OPT_END()
981 };
982
983 argc = parse_options(argc, argv, prefix, options,
40af1468 984 git_stash_store_usage,
99d86d60 985 PARSE_OPT_KEEP_UNKNOWN_OPT);
41e0dd55
PSU
986
987 if (argc != 1) {
988 if (!quiet)
989 fprintf_ln(stderr, _("\"git stash store\" requires one "
990 "<commit> argument"));
991 return -1;
992 }
993
e36adf71
JH
994 if (get_oid_with_context(the_repository,
995 argv[0], quiet ? GET_OID_QUIETLY : 0, &obj,
41e0dd55
PSU
996 &dummy)) {
997 if (!quiet)
998 fprintf_ln(stderr, _("Cannot update %s with %s"),
999 ref_stash, argv[0]);
1000 return -1;
1001 }
1002
1003 return do_store_stash(&obj, stash_msg, quiet);
1004}
1005
22f9b7f3 1006static void add_pathspecs(struct strvec *args,
7db9302d 1007 const struct pathspec *ps) {
d4788af8
PSU
1008 int i;
1009
7db9302d 1010 for (i = 0; i < ps->nr; i++)
22f9b7f3 1011 strvec_push(args, ps->items[i].original);
d4788af8
PSU
1012}
1013
1014/*
1015 * `untracked_files` will be filled with the names of untracked files.
1016 * The return value is:
1017 *
1018 * = 0 if there are not any untracked files
1019 * > 0 if there are untracked files
1020 */
7db9302d 1021static int get_untracked_files(const struct pathspec *ps, int include_untracked,
d4788af8
PSU
1022 struct strbuf *untracked_files)
1023{
1024 int i;
d4788af8 1025 int found = 0;
ce93a4c6 1026 struct dir_struct dir = DIR_INIT;
d4788af8 1027
d4788af8
PSU
1028 if (include_untracked != INCLUDE_ALL_FILES)
1029 setup_standard_excludes(&dir);
1030
95c11ecc 1031 fill_directory(&dir, the_repository->index, ps);
d4788af8
PSU
1032 for (i = 0; i < dir.nr; i++) {
1033 struct dir_entry *ent = dir.entries[i];
95c11ecc
EN
1034 found++;
1035 strbuf_addstr(untracked_files, ent->name);
1036 /* NUL-terminate: will be fed to update-index -z */
1037 strbuf_addch(untracked_files, '\0');
d4788af8
PSU
1038 }
1039
eceba532 1040 dir_clear(&dir);
d4788af8
PSU
1041 return found;
1042}
1043
1044/*
ef0f0b45 1045 * The return value of `check_changes_tracked_files()` can be:
d4788af8
PSU
1046 *
1047 * < 0 if there was an error
1048 * = 0 if there are no changes.
1049 * > 0 if there are changes.
1050 */
7db9302d 1051static int check_changes_tracked_files(const struct pathspec *ps)
d4788af8
PSU
1052{
1053 int result;
1054 struct rev_info rev;
1055 struct object_id dummy;
7db9302d 1056 int ret = 0;
d4788af8
PSU
1057
1058 /* No initial commit. */
1059 if (get_oid("HEAD", &dummy))
1060 return -1;
1061
1062 if (read_cache() < 0)
1063 return -1;
1064
1065 init_revisions(&rev, NULL);
7db9302d 1066 copy_pathspec(&rev.prune_data, ps);
d4788af8
PSU
1067
1068 rev.diffopt.flags.quick = 1;
1069 rev.diffopt.flags.ignore_submodules = 1;
1070 rev.abbrev = 0;
1071
1072 add_head_to_pending(&rev);
1073 diff_setup_done(&rev.diffopt);
1074
1075 result = run_diff_index(&rev, 1);
7db9302d
TG
1076 if (diff_result_code(&rev.diffopt, result)) {
1077 ret = 1;
1078 goto done;
1079 }
d4788af8 1080
d4788af8 1081 result = run_diff_files(&rev, 0);
7db9302d
TG
1082 if (diff_result_code(&rev.diffopt, result)) {
1083 ret = 1;
1084 goto done;
1085 }
d4788af8 1086
7db9302d 1087done:
1878b5ed 1088 release_revisions(&rev);
7db9302d 1089 return ret;
ef0f0b45
PSU
1090}
1091
1092/*
1093 * The function will fill `untracked_files` with the names of untracked files
1094 * It will return 1 if there were any changes and 0 if there were not.
1095 */
7db9302d 1096static int check_changes(const struct pathspec *ps, int include_untracked,
ef0f0b45
PSU
1097 struct strbuf *untracked_files)
1098{
1099 int ret = 0;
1100 if (check_changes_tracked_files(ps))
1101 ret = 1;
1102
d4788af8 1103 if (include_untracked && get_untracked_files(ps, include_untracked,
ef0f0b45
PSU
1104 untracked_files))
1105 ret = 1;
d4788af8 1106
ef0f0b45 1107 return ret;
d4788af8
PSU
1108}
1109
1110static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
1111 struct strbuf files)
1112{
1113 int ret = 0;
1114 struct strbuf untracked_msg = STRBUF_INIT;
d4788af8 1115 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
48ee24ab 1116 struct index_state istate = { NULL };
d4788af8
PSU
1117
1118 cp_upd_index.git_cmd = 1;
22f9b7f3 1119 strvec_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
f6d8942b 1120 "--remove", "--stdin", NULL);
29fda24d 1121 strvec_pushf(&cp_upd_index.env, "GIT_INDEX_FILE=%s",
d4788af8
PSU
1122 stash_index_path.buf);
1123
1124 strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
1125 if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,
1126 NULL, 0)) {
1127 ret = -1;
1128 goto done;
1129 }
1130
48ee24ab
PSU
1131 if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0,
1132 NULL)) {
d4788af8
PSU
1133 ret = -1;
1134 goto done;
1135 }
d4788af8
PSU
1136
1137 if (commit_tree(untracked_msg.buf, untracked_msg.len,
1138 &info->u_tree, NULL, &info->u_commit, NULL, NULL)) {
1139 ret = -1;
1140 goto done;
1141 }
1142
1143done:
48ee24ab 1144 discard_index(&istate);
d4788af8 1145 strbuf_release(&untracked_msg);
d4788af8
PSU
1146 remove_path(stash_index_path.buf);
1147 return ret;
1148}
1149
a8a6e068
SO
1150static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
1151 int quiet)
41a28eb6
SO
1152{
1153 int ret = 0;
1154 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1155 struct index_state istate = { NULL };
1156
1157 if (write_index_as_tree(&info->w_tree, &istate, the_repository->index_file,
1158 0, NULL)) {
1159 ret = -1;
1160 goto done;
1161 }
1162
1163 cp_diff_tree.git_cmd = 1;
1164 strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD",
1165 oid_to_hex(&info->w_tree), "--", NULL);
1166 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
1167 ret = -1;
1168 goto done;
1169 }
1170
1171 if (!out_patch->len) {
1172 if (!quiet)
1173 fprintf_ln(stderr, _("No staged changes"));
1174 ret = 1;
1175 }
1176
1177done:
1178 discard_index(&istate);
1179 return ret;
1180}
1181
7db9302d 1182static int stash_patch(struct stash_info *info, const struct pathspec *ps,
1ac528c0 1183 struct strbuf *out_patch, int quiet)
d4788af8
PSU
1184{
1185 int ret = 0;
d4788af8 1186 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
d4788af8 1187 struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
48ee24ab 1188 struct index_state istate = { NULL };
6610e462 1189 char *old_index_env = NULL, *old_repo_index_file;
d4788af8
PSU
1190
1191 remove_path(stash_index_path.buf);
1192
1193 cp_read_tree.git_cmd = 1;
22f9b7f3 1194 strvec_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL);
29fda24d 1195 strvec_pushf(&cp_read_tree.env, "GIT_INDEX_FILE=%s",
f6d8942b 1196 stash_index_path.buf);
d4788af8
PSU
1197 if (run_command(&cp_read_tree)) {
1198 ret = -1;
1199 goto done;
1200 }
1201
1202 /* Find out what the user wants. */
6610e462
JS
1203 old_repo_index_file = the_repository->index_file;
1204 the_repository->index_file = stash_index_path.buf;
1205 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
1206 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
1207
1208 ret = run_add_interactive(NULL, "--patch=stash", ps);
1209
1210 the_repository->index_file = old_repo_index_file;
1211 if (old_index_env && *old_index_env)
1212 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
1213 else
1214 unsetenv(INDEX_ENVIRONMENT);
1215 FREE_AND_NULL(old_index_env);
d4788af8
PSU
1216
1217 /* State of the working tree. */
48ee24ab
PSU
1218 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1219 NULL)) {
d4788af8
PSU
1220 ret = -1;
1221 goto done;
1222 }
1223
d4788af8 1224 cp_diff_tree.git_cmd = 1;
22f9b7f3 1225 strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD",
f6d8942b 1226 oid_to_hex(&info->w_tree), "--", NULL);
d4788af8
PSU
1227 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
1228 ret = -1;
1229 goto done;
1230 }
1231
1232 if (!out_patch->len) {
1ac528c0
PSU
1233 if (!quiet)
1234 fprintf_ln(stderr, _("No changes selected"));
d4788af8
PSU
1235 ret = 1;
1236 }
1237
1238done:
48ee24ab 1239 discard_index(&istate);
d4788af8
PSU
1240 remove_path(stash_index_path.buf);
1241 return ret;
1242}
1243
7db9302d 1244static int stash_working_tree(struct stash_info *info, const struct pathspec *ps)
d4788af8
PSU
1245{
1246 int ret = 0;
1247 struct rev_info rev;
1248 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
d4788af8 1249 struct strbuf diff_output = STRBUF_INIT;
48ee24ab 1250 struct index_state istate = { NULL };
d4788af8
PSU
1251
1252 init_revisions(&rev, NULL);
7db9302d 1253 copy_pathspec(&rev.prune_data, ps);
d4788af8
PSU
1254
1255 set_alternate_index_output(stash_index_path.buf);
1256 if (reset_tree(&info->i_tree, 0, 0)) {
1257 ret = -1;
1258 goto done;
1259 }
1260 set_alternate_index_output(NULL);
1261
d4788af8
PSU
1262 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
1263 rev.diffopt.format_callback = add_diff_to_buf;
1264 rev.diffopt.format_callback_data = &diff_output;
1265
1266 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1267 ret = -1;
1268 goto done;
1269 }
1270
1271 add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
1272 "");
1273 if (run_diff_index(&rev, 0)) {
1274 ret = -1;
1275 goto done;
1276 }
1277
1278 cp_upd_index.git_cmd = 1;
22f9b7f3 1279 strvec_pushl(&cp_upd_index.args, "update-index",
f6d8942b
JK
1280 "--ignore-skip-worktree-entries",
1281 "-z", "--add", "--remove", "--stdin", NULL);
29fda24d 1282 strvec_pushf(&cp_upd_index.env, "GIT_INDEX_FILE=%s",
f6d8942b 1283 stash_index_path.buf);
d4788af8
PSU
1284
1285 if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len,
1286 NULL, 0, NULL, 0)) {
1287 ret = -1;
1288 goto done;
1289 }
1290
48ee24ab
PSU
1291 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
1292 NULL)) {
d4788af8
PSU
1293 ret = -1;
1294 goto done;
1295 }
1296
d4788af8 1297done:
48ee24ab 1298 discard_index(&istate);
1878b5ed 1299 release_revisions(&rev);
d4788af8
PSU
1300 strbuf_release(&diff_output);
1301 remove_path(stash_index_path.buf);
1302 return ret;
1303}
1304
7db9302d 1305static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,
41a28eb6 1306 int include_untracked, int patch_mode, int only_staged,
1ac528c0
PSU
1307 struct stash_info *info, struct strbuf *patch,
1308 int quiet)
d4788af8
PSU
1309{
1310 int ret = 0;
1311 int flags = 0;
1312 int untracked_commit_option = 0;
1313 const char *head_short_sha1 = NULL;
1314 const char *branch_ref = NULL;
1315 const char *branch_name = "(no branch)";
1316 struct commit *head_commit = NULL;
1317 struct commit_list *parents = NULL;
1318 struct strbuf msg = STRBUF_INIT;
1319 struct strbuf commit_tree_label = STRBUF_INIT;
1320 struct strbuf untracked_files = STRBUF_INIT;
d4788af8
PSU
1321
1322 prepare_fallback_ident("git stash", "git@stash");
1323
1324 read_cache_preload(NULL);
34933d0e
TG
1325 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0) < 0) {
1326 ret = -1;
1327 goto done;
1328 }
d4788af8
PSU
1329
1330 if (get_oid("HEAD", &info->b_commit)) {
1ac528c0
PSU
1331 if (!quiet)
1332 fprintf_ln(stderr, _("You do not have "
1333 "the initial commit yet"));
d4788af8
PSU
1334 ret = -1;
1335 goto done;
1336 } else {
1337 head_commit = lookup_commit(the_repository, &info->b_commit);
1338 }
1339
ef0f0b45 1340 if (!check_changes(ps, include_untracked, &untracked_files)) {
d4788af8
PSU
1341 ret = 1;
1342 goto done;
1343 }
1344
1345 branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
1346 if (flags & REF_ISSYMREF)
ceaf037f 1347 skip_prefix(branch_ref, "refs/heads/", &branch_name);
d4788af8
PSU
1348 head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
1349 DEFAULT_ABBREV);
1350 strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);
1351 pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg);
1352
1353 strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
1354 commit_list_insert(head_commit, &parents);
1355 if (write_cache_as_tree(&info->i_tree, 0, NULL) ||
1356 commit_tree(commit_tree_label.buf, commit_tree_label.len,
1357 &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1ac528c0
PSU
1358 if (!quiet)
1359 fprintf_ln(stderr, _("Cannot save the current "
1360 "index state"));
d4788af8
PSU
1361 ret = -1;
1362 goto done;
1363 }
1364
ef0f0b45 1365 if (include_untracked) {
d4788af8 1366 if (save_untracked_files(info, &msg, untracked_files)) {
1ac528c0
PSU
1367 if (!quiet)
1368 fprintf_ln(stderr, _("Cannot save "
1369 "the untracked files"));
d4788af8
PSU
1370 ret = -1;
1371 goto done;
1372 }
1373 untracked_commit_option = 1;
1374 }
1375 if (patch_mode) {
1ac528c0 1376 ret = stash_patch(info, ps, patch, quiet);
d4788af8 1377 if (ret < 0) {
1ac528c0
PSU
1378 if (!quiet)
1379 fprintf_ln(stderr, _("Cannot save the current "
1380 "worktree state"));
d4788af8
PSU
1381 goto done;
1382 } else if (ret > 0) {
1383 goto done;
1384 }
41a28eb6 1385 } else if (only_staged) {
a8a6e068 1386 ret = stash_staged(info, patch, quiet);
41a28eb6
SO
1387 if (ret < 0) {
1388 if (!quiet)
1389 fprintf_ln(stderr, _("Cannot save the current "
1390 "staged state"));
1391 goto done;
1392 } else if (ret > 0) {
1393 goto done;
1394 }
d4788af8
PSU
1395 } else {
1396 if (stash_working_tree(info, ps)) {
1ac528c0
PSU
1397 if (!quiet)
1398 fprintf_ln(stderr, _("Cannot save the current "
1399 "worktree state"));
d4788af8
PSU
1400 ret = -1;
1401 goto done;
1402 }
1403 }
1404
1405 if (!stash_msg_buf->len)
1406 strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf);
1407 else
1408 strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name);
1409
1410 /*
1411 * `parents` will be empty after calling `commit_tree()`, so there is
1412 * no need to call `free_commit_list()`
1413 */
1414 parents = NULL;
1415 if (untracked_commit_option)
1416 commit_list_insert(lookup_commit(the_repository,
1417 &info->u_commit),
1418 &parents);
1419 commit_list_insert(lookup_commit(the_repository, &info->i_commit),
1420 &parents);
1421 commit_list_insert(head_commit, &parents);
1422
1423 if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree,
1424 parents, &info->w_commit, NULL, NULL)) {
1ac528c0
PSU
1425 if (!quiet)
1426 fprintf_ln(stderr, _("Cannot record "
1427 "working tree state"));
d4788af8
PSU
1428 ret = -1;
1429 goto done;
1430 }
1431
1432done:
1433 strbuf_release(&commit_tree_label);
1434 strbuf_release(&msg);
1435 strbuf_release(&untracked_files);
1436 return ret;
1437}
1438
1439static int create_stash(int argc, const char **argv, const char *prefix)
1440{
5e480176 1441 int ret;
d4788af8 1442 struct strbuf stash_msg_buf = STRBUF_INIT;
5e480176 1443 struct stash_info info = STASH_INFO_INIT;
d4788af8 1444 struct pathspec ps;
d4788af8 1445
40af1468
PSU
1446 /* Starting with argv[1], since argv[0] is "create" */
1447 strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
d4788af8
PSU
1448
1449 memset(&ps, 0, sizeof(ps));
7db9302d 1450 if (!check_changes_tracked_files(&ps))
ef0f0b45
PSU
1451 return 0;
1452
41a28eb6 1453 ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, 0, &info,
1ac528c0 1454 NULL, 0);
d4788af8
PSU
1455 if (!ret)
1456 printf_ln("%s", oid_to_hex(&info.w_commit));
1457
5e480176 1458 free_stash_info(&info);
d4788af8 1459 strbuf_release(&stash_msg_buf);
ef0f0b45 1460 return ret;
d4788af8
PSU
1461}
1462
7db9302d 1463static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,
41a28eb6 1464 int keep_index, int patch_mode, int include_untracked, int only_staged)
d553f538
PSU
1465{
1466 int ret = 0;
5e480176 1467 struct stash_info info = STASH_INFO_INIT;
d553f538
PSU
1468 struct strbuf patch = STRBUF_INIT;
1469 struct strbuf stash_msg_buf = STRBUF_INIT;
ef0f0b45 1470 struct strbuf untracked_files = STRBUF_INIT;
d553f538
PSU
1471
1472 if (patch_mode && keep_index == -1)
1473 keep_index = 1;
1474
1475 if (patch_mode && include_untracked) {
1476 fprintf_ln(stderr, _("Can't use --patch and --include-untracked"
1477 " or --all at the same time"));
1478 ret = -1;
1479 goto done;
1480 }
1481
41a28eb6
SO
1482 /* --patch overrides --staged */
1483 if (patch_mode)
1484 only_staged = 0;
1485
1486 if (only_staged && include_untracked) {
1487 fprintf_ln(stderr, _("Can't use --staged and --include-untracked"
1488 " or --all at the same time"));
1489 ret = -1;
1490 goto done;
1491 }
1492
d553f538 1493 read_cache_preload(NULL);
7db9302d 1494 if (!include_untracked && ps->nr) {
d553f538 1495 int i;
7db9302d 1496 char *ps_matched = xcalloc(ps->nr, 1);
d553f538 1497
a0291201
DS
1498 /* TODO: audit for interaction with sparse-index. */
1499 ensure_full_index(&the_index);
d553f538 1500 for (i = 0; i < active_nr; i++)
7db9302d 1501 ce_path_match(&the_index, active_cache[i], ps,
d553f538
PSU
1502 ps_matched);
1503
42844973 1504 if (report_path_error(ps_matched, ps)) {
d553f538
PSU
1505 fprintf_ln(stderr, _("Did you forget to 'git add'?"));
1506 ret = -1;
1507 free(ps_matched);
1508 goto done;
1509 }
1510 free(ps_matched);
1511 }
1512
34933d0e 1513 if (refresh_and_write_cache(REFRESH_QUIET, 0, 0)) {
d553f538
PSU
1514 ret = -1;
1515 goto done;
1516 }
1517
ef0f0b45 1518 if (!check_changes(ps, include_untracked, &untracked_files)) {
d553f538
PSU
1519 if (!quiet)
1520 printf_ln(_("No local changes to save"));
1521 goto done;
1522 }
1523
1524 if (!reflog_exists(ref_stash) && do_clear_stash()) {
1525 ret = -1;
1ac528c0
PSU
1526 if (!quiet)
1527 fprintf_ln(stderr, _("Cannot initialize stash"));
d553f538
PSU
1528 goto done;
1529 }
1530
1531 if (stash_msg)
1532 strbuf_addstr(&stash_msg_buf, stash_msg);
41a28eb6 1533 if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode, only_staged,
1ac528c0 1534 &info, &patch, quiet)) {
d553f538
PSU
1535 ret = -1;
1536 goto done;
1537 }
1538
1539 if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
1540 ret = -1;
1ac528c0
PSU
1541 if (!quiet)
1542 fprintf_ln(stderr, _("Cannot save the current status"));
d553f538
PSU
1543 goto done;
1544 }
1545
1ac528c0
PSU
1546 if (!quiet)
1547 printf_ln(_("Saved working directory and index state %s"),
1548 stash_msg_buf.buf);
d553f538 1549
41a28eb6 1550 if (!(patch_mode || only_staged)) {
7db9302d 1551 if (include_untracked && !ps->nr) {
d553f538
PSU
1552 struct child_process cp = CHILD_PROCESS_INIT;
1553
1554 cp.git_cmd = 1;
ff5b7913 1555 if (startup_info->original_cwd) {
0fce211c 1556 cp.dir = startup_info->original_cwd;
29fda24d 1557 strvec_pushf(&cp.env, "%s=%s",
ff5b7913
EN
1558 GIT_WORK_TREE_ENVIRONMENT,
1559 the_repository->worktree);
1560 }
22f9b7f3 1561 strvec_pushl(&cp.args, "clean", "--force",
0fce211c 1562 "--quiet", "-d", ":/", NULL);
d553f538 1563 if (include_untracked == INCLUDE_ALL_FILES)
22f9b7f3 1564 strvec_push(&cp.args, "-x");
d553f538
PSU
1565 if (run_command(&cp)) {
1566 ret = -1;
1567 goto done;
1568 }
1569 }
1570 discard_cache();
7db9302d 1571 if (ps->nr) {
d553f538
PSU
1572 struct child_process cp_add = CHILD_PROCESS_INIT;
1573 struct child_process cp_diff = CHILD_PROCESS_INIT;
1574 struct child_process cp_apply = CHILD_PROCESS_INIT;
1575 struct strbuf out = STRBUF_INIT;
1576
1577 cp_add.git_cmd = 1;
22f9b7f3 1578 strvec_push(&cp_add.args, "add");
d553f538 1579 if (!include_untracked)
22f9b7f3 1580 strvec_push(&cp_add.args, "-u");
d553f538 1581 if (include_untracked == INCLUDE_ALL_FILES)
22f9b7f3
JK
1582 strvec_push(&cp_add.args, "--force");
1583 strvec_push(&cp_add.args, "--");
d553f538
PSU
1584 add_pathspecs(&cp_add.args, ps);
1585 if (run_command(&cp_add)) {
1586 ret = -1;
1587 goto done;
1588 }
1589
1590 cp_diff.git_cmd = 1;
22f9b7f3 1591 strvec_pushl(&cp_diff.args, "diff-index", "-p",
f6d8942b
JK
1592 "--cached", "--binary", "HEAD", "--",
1593 NULL);
d553f538
PSU
1594 add_pathspecs(&cp_diff.args, ps);
1595 if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) {
1596 ret = -1;
1597 goto done;
1598 }
1599
1600 cp_apply.git_cmd = 1;
22f9b7f3 1601 strvec_pushl(&cp_apply.args, "apply", "--index",
f6d8942b 1602 "-R", NULL);
d553f538
PSU
1603 if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0,
1604 NULL, 0)) {
1605 ret = -1;
1606 goto done;
1607 }
1608 } else {
1609 struct child_process cp = CHILD_PROCESS_INIT;
1610 cp.git_cmd = 1;
94b7f156 1611 /* BUG: this nukes untracked files in the way */
22f9b7f3 1612 strvec_pushl(&cp.args, "reset", "--hard", "-q",
f6d8942b 1613 "--no-recurse-submodules", NULL);
d553f538
PSU
1614 if (run_command(&cp)) {
1615 ret = -1;
1616 goto done;
1617 }
1618 }
1619
1620 if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
b932f6a5 1621 struct child_process cp = CHILD_PROCESS_INIT;
d553f538 1622
b932f6a5 1623 cp.git_cmd = 1;
22f9b7f3 1624 strvec_pushl(&cp.args, "checkout", "--no-overlay",
f6d8942b 1625 oid_to_hex(&info.i_tree), "--", NULL);
b932f6a5 1626 if (!ps->nr)
22f9b7f3 1627 strvec_push(&cp.args, ":/");
b932f6a5
TG
1628 else
1629 add_pathspecs(&cp.args, ps);
1630 if (run_command(&cp)) {
d553f538
PSU
1631 ret = -1;
1632 goto done;
1633 }
1634 }
1635 goto done;
1636 } else {
1637 struct child_process cp = CHILD_PROCESS_INIT;
1638
1639 cp.git_cmd = 1;
22f9b7f3 1640 strvec_pushl(&cp.args, "apply", "-R", NULL);
d553f538
PSU
1641
1642 if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) {
1ac528c0
PSU
1643 if (!quiet)
1644 fprintf_ln(stderr, _("Cannot remove "
1645 "worktree changes"));
d553f538
PSU
1646 ret = -1;
1647 goto done;
1648 }
1649
1650 if (keep_index < 1) {
1651 struct child_process cp = CHILD_PROCESS_INIT;
1652
1653 cp.git_cmd = 1;
4b8b0f6f
VD
1654 strvec_pushl(&cp.args, "reset", "-q", "--refresh", "--",
1655 NULL);
d553f538
PSU
1656 add_pathspecs(&cp.args, ps);
1657 if (run_command(&cp)) {
1658 ret = -1;
1659 goto done;
1660 }
1661 }
1662 goto done;
1663 }
1664
1665done:
5e480176 1666 free_stash_info(&info);
d553f538
PSU
1667 strbuf_release(&stash_msg_buf);
1668 return ret;
1669}
1670
8c3713ce
AM
1671static int push_stash(int argc, const char **argv, const char *prefix,
1672 int push_assumed)
d553f538 1673{
8c3713ce 1674 int force_assume = 0;
d553f538 1675 int keep_index = -1;
41a28eb6 1676 int only_staged = 0;
d553f538
PSU
1677 int patch_mode = 0;
1678 int include_untracked = 0;
1679 int quiet = 0;
8a98758a 1680 int pathspec_file_nul = 0;
d553f538 1681 const char *stash_msg = NULL;
8a98758a 1682 const char *pathspec_from_file = NULL;
d553f538
PSU
1683 struct pathspec ps;
1684 struct option options[] = {
1685 OPT_BOOL('k', "keep-index", &keep_index,
1686 N_("keep index")),
41a28eb6
SO
1687 OPT_BOOL('S', "staged", &only_staged,
1688 N_("stash staged changes only")),
d553f538
PSU
1689 OPT_BOOL('p', "patch", &patch_mode,
1690 N_("stash in patch mode")),
1691 OPT__QUIET(&quiet, N_("quiet mode")),
1692 OPT_BOOL('u', "include-untracked", &include_untracked,
1693 N_("include untracked files in stash")),
1694 OPT_SET_INT('a', "all", &include_untracked,
1695 N_("include ignore files"), 2),
1696 OPT_STRING('m', "message", &stash_msg, N_("message"),
1697 N_("stash message")),
8a98758a
AM
1698 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1699 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
d553f538
PSU
1700 OPT_END()
1701 };
1702
8c3713ce
AM
1703 if (argc) {
1704 force_assume = !strcmp(argv[0], "-p");
40af1468 1705 argc = parse_options(argc, argv, prefix, options,
ca7990ce 1706 push_assumed ? git_stash_usage :
40af1468 1707 git_stash_push_usage,
8c3713ce
AM
1708 PARSE_OPT_KEEP_DASHDASH);
1709 }
1710
1711 if (argc) {
1712 if (!strcmp(argv[0], "--")) {
1713 argc--;
1714 argv++;
1715 } else if (push_assumed && !force_assume) {
1716 die("subcommand wasn't specified; 'push' can't be assumed due to unexpected token '%s'",
1717 argv[0]);
1718 }
1719 }
d553f538 1720
1366c78c
JS
1721 parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1722 prefix, argv);
8a98758a
AM
1723
1724 if (pathspec_from_file) {
1725 if (patch_mode)
12909b6b 1726 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
8a98758a 1727
41a28eb6 1728 if (only_staged)
12909b6b 1729 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--staged");
41a28eb6 1730
8a98758a 1731 if (ps.nr)
246cac85 1732 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
8a98758a
AM
1733
1734 parse_pathspec_file(&ps, 0,
1735 PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
1736 prefix, pathspec_from_file, pathspec_file_nul);
1737 } else if (pathspec_file_nul) {
6fa00ee8 1738 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
8a98758a
AM
1739 }
1740
7db9302d 1741 return do_push_stash(&ps, stash_msg, quiet, keep_index, patch_mode,
41a28eb6 1742 include_untracked, only_staged);
d553f538
PSU
1743}
1744
2b057d97
SG
1745static int push_stash_unassumed(int argc, const char **argv, const char *prefix)
1746{
1747 return push_stash(argc, argv, prefix, 0);
1748}
1749
64fe9c26
PSU
1750static int save_stash(int argc, const char **argv, const char *prefix)
1751{
1752 int keep_index = -1;
41a28eb6 1753 int only_staged = 0;
64fe9c26
PSU
1754 int patch_mode = 0;
1755 int include_untracked = 0;
1756 int quiet = 0;
1757 int ret = 0;
1758 const char *stash_msg = NULL;
1759 struct pathspec ps;
1760 struct strbuf stash_msg_buf = STRBUF_INIT;
1761 struct option options[] = {
1762 OPT_BOOL('k', "keep-index", &keep_index,
1763 N_("keep index")),
41a28eb6
SO
1764 OPT_BOOL('S', "staged", &only_staged,
1765 N_("stash staged changes only")),
64fe9c26
PSU
1766 OPT_BOOL('p', "patch", &patch_mode,
1767 N_("stash in patch mode")),
1768 OPT__QUIET(&quiet, N_("quiet mode")),
1769 OPT_BOOL('u', "include-untracked", &include_untracked,
1770 N_("include untracked files in stash")),
1771 OPT_SET_INT('a', "all", &include_untracked,
1772 N_("include ignore files"), 2),
1773 OPT_STRING('m', "message", &stash_msg, "message",
1774 N_("stash message")),
1775 OPT_END()
1776 };
1777
1778 argc = parse_options(argc, argv, prefix, options,
40af1468 1779 git_stash_save_usage,
64fe9c26
PSU
1780 PARSE_OPT_KEEP_DASHDASH);
1781
1782 if (argc)
1783 stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' ');
1784
1785 memset(&ps, 0, sizeof(ps));
7db9302d 1786 ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
41a28eb6 1787 patch_mode, include_untracked, only_staged);
64fe9c26
PSU
1788
1789 strbuf_release(&stash_msg_buf);
1790 return ret;
1791}
1792
40af1468 1793int cmd_stash(int argc, const char **argv, const char *prefix)
8a0fc8d1
JT
1794{
1795 pid_t pid = getpid();
1796 const char *index_file;
22f9b7f3 1797 struct strvec args = STRVEC_INIT;
2b057d97 1798 parse_opt_subcommand_fn *fn = NULL;
8a0fc8d1 1799 struct option options[] = {
2b057d97
SG
1800 OPT_SUBCOMMAND("apply", &fn, apply_stash),
1801 OPT_SUBCOMMAND("clear", &fn, clear_stash),
1802 OPT_SUBCOMMAND("drop", &fn, drop_stash),
1803 OPT_SUBCOMMAND("pop", &fn, pop_stash),
1804 OPT_SUBCOMMAND("branch", &fn, branch_stash),
1805 OPT_SUBCOMMAND("list", &fn, list_stash),
1806 OPT_SUBCOMMAND("show", &fn, show_stash),
1807 OPT_SUBCOMMAND("store", &fn, store_stash),
1808 OPT_SUBCOMMAND("create", &fn, create_stash),
1809 OPT_SUBCOMMAND("push", &fn, push_stash_unassumed),
1810 OPT_SUBCOMMAND_F("save", &fn, save_stash, PARSE_OPT_NOCOMPLETE),
8a0fc8d1
JT
1811 OPT_END()
1812 };
1813
b0c7362d 1814 git_config(git_stash_config, NULL);
90a46272 1815
40af1468 1816 argc = parse_options(argc, argv, prefix, options, git_stash_usage,
2b057d97
SG
1817 PARSE_OPT_SUBCOMMAND_OPTIONAL |
1818 PARSE_OPT_KEEP_UNKNOWN_OPT |
1819 PARSE_OPT_KEEP_DASHDASH);
8a0fc8d1 1820
3a58792a
VD
1821 prepare_repo_settings(the_repository);
1822 the_repository->settings.command_requires_full_index = 0;
1823
8a0fc8d1
JT
1824 index_file = get_index_file();
1825 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1826 (uintmax_t)pid);
1827
2b057d97
SG
1828 if (fn)
1829 return !!fn(argc, argv, prefix);
1830 else if (!argc)
1831 return !!push_stash_unassumed(0, NULL, prefix);
40af1468 1832
8c3713ce 1833 /* Assume 'stash push' */
22f9b7f3
JK
1834 strvec_push(&args, "push");
1835 strvec_pushv(&args, argv);
d70a9eb6 1836 return !!push_stash(args.nr, args.v, prefix, 1);
8a0fc8d1 1837}