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