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