4 #include "environment.h"
8 #include "object-name.h"
9 #include "parse-options.h"
12 #include "cache-tree.h"
13 #include "unpack-trees.h"
14 #include "merge-recursive.h"
15 #include "merge-ort-wrappers.h"
17 #include "run-command.h"
20 #include "preload-index.h"
21 #include "read-cache.h"
25 #include "sparse-index.h"
29 #include "add-interactive.h"
31 #define INCLUDE_ALL_FILES 2
33 #define BUILTIN_STASH_LIST_USAGE \
34 N_("git stash list [<log-options>]")
35 #define BUILTIN_STASH_SHOW_USAGE \
36 N_("git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]")
37 #define BUILTIN_STASH_DROP_USAGE \
38 N_("git stash drop [-q | --quiet] [<stash>]")
39 #define BUILTIN_STASH_POP_USAGE \
40 N_("git stash pop [--index] [-q | --quiet] [<stash>]")
41 #define BUILTIN_STASH_APPLY_USAGE \
42 N_("git stash apply [--index] [-q | --quiet] [<stash>]")
43 #define BUILTIN_STASH_BRANCH_USAGE \
44 N_("git stash branch <branchname> [<stash>]")
45 #define BUILTIN_STASH_STORE_USAGE \
46 N_("git stash store [(-m | --message) <message>] [-q | --quiet] <commit>")
47 #define BUILTIN_STASH_PUSH_USAGE \
48 N_("git stash [push [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]\n" \
49 " [-u | --include-untracked] [-a | --all] [(-m | --message) <message>]\n" \
50 " [--pathspec-from-file=<file> [--pathspec-file-nul]]\n" \
51 " [--] [<pathspec>...]]")
52 #define BUILTIN_STASH_SAVE_USAGE \
53 N_("git stash save [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]\n" \
54 " [-u | --include-untracked] [-a | --all] [<message>]")
55 #define BUILTIN_STASH_CREATE_USAGE \
56 N_("git stash create [<message>]")
57 #define BUILTIN_STASH_CLEAR_USAGE \
60 static const char * const git_stash_usage
[] = {
61 BUILTIN_STASH_LIST_USAGE
,
62 BUILTIN_STASH_SHOW_USAGE
,
63 BUILTIN_STASH_DROP_USAGE
,
64 BUILTIN_STASH_POP_USAGE
,
65 BUILTIN_STASH_APPLY_USAGE
,
66 BUILTIN_STASH_BRANCH_USAGE
,
67 BUILTIN_STASH_PUSH_USAGE
,
68 BUILTIN_STASH_SAVE_USAGE
,
69 BUILTIN_STASH_CLEAR_USAGE
,
70 BUILTIN_STASH_CREATE_USAGE
,
71 BUILTIN_STASH_STORE_USAGE
,
75 static const char * const git_stash_list_usage
[] = {
76 BUILTIN_STASH_LIST_USAGE
,
80 static const char * const git_stash_show_usage
[] = {
81 BUILTIN_STASH_SHOW_USAGE
,
85 static const char * const git_stash_drop_usage
[] = {
86 BUILTIN_STASH_DROP_USAGE
,
90 static const char * const git_stash_pop_usage
[] = {
91 BUILTIN_STASH_POP_USAGE
,
95 static const char * const git_stash_apply_usage
[] = {
96 BUILTIN_STASH_APPLY_USAGE
,
100 static const char * const git_stash_branch_usage
[] = {
101 BUILTIN_STASH_BRANCH_USAGE
,
105 static const char * const git_stash_clear_usage
[] = {
106 BUILTIN_STASH_CLEAR_USAGE
,
110 static const char * const git_stash_store_usage
[] = {
111 BUILTIN_STASH_STORE_USAGE
,
115 static const char * const git_stash_push_usage
[] = {
116 BUILTIN_STASH_PUSH_USAGE
,
120 static const char * const git_stash_save_usage
[] = {
121 BUILTIN_STASH_SAVE_USAGE
,
125 static const char ref_stash
[] = "refs/stash";
126 static struct strbuf stash_index_path
= STRBUF_INIT
;
129 * w_commit is set to the commit containing the working tree
130 * b_commit is set to the base commit
131 * i_commit is set to the commit containing the index tree
132 * u_commit is set to the commit containing the untracked files tree
133 * w_tree is set to the working tree
134 * b_tree is set to the base tree
135 * i_tree is set to the index tree
136 * u_tree is set to the untracked files tree
139 struct object_id w_commit
;
140 struct object_id b_commit
;
141 struct object_id i_commit
;
142 struct object_id u_commit
;
143 struct object_id w_tree
;
144 struct object_id b_tree
;
145 struct object_id i_tree
;
146 struct object_id u_tree
;
147 struct strbuf revision
;
152 #define STASH_INFO_INIT { \
153 .revision = STRBUF_INIT, \
156 static void free_stash_info(struct stash_info
*info
)
158 strbuf_release(&info
->revision
);
161 static void assert_stash_like(struct stash_info
*info
, const char *revision
)
163 if (get_oidf(&info
->b_commit
, "%s^1", revision
) ||
164 get_oidf(&info
->w_tree
, "%s:", revision
) ||
165 get_oidf(&info
->b_tree
, "%s^1:", revision
) ||
166 get_oidf(&info
->i_tree
, "%s^2:", revision
))
167 die(_("'%s' is not a stash-like commit"), revision
);
170 static int get_stash_info(struct stash_info
*info
, int argc
, const char **argv
)
175 const char *revision
;
176 const char *commit
= NULL
;
177 struct object_id dummy
;
178 struct strbuf symbolic
= STRBUF_INIT
;
182 struct strbuf refs_msg
= STRBUF_INIT
;
184 for (i
= 0; i
< argc
; i
++)
185 strbuf_addf(&refs_msg
, " '%s'", argv
[i
]);
187 fprintf_ln(stderr
, _("Too many revisions specified:%s"),
189 strbuf_release(&refs_msg
);
198 if (!refs_ref_exists(get_main_ref_store(the_repository
), ref_stash
)) {
199 fprintf_ln(stderr
, _("No stash entries found."));
203 strbuf_addf(&info
->revision
, "%s@{0}", ref_stash
);
204 } else if (strspn(commit
, "0123456789") == strlen(commit
)) {
205 strbuf_addf(&info
->revision
, "%s@{%s}", ref_stash
, commit
);
207 strbuf_addstr(&info
->revision
, commit
);
210 revision
= info
->revision
.buf
;
212 if (repo_get_oid(the_repository
, revision
, &info
->w_commit
))
213 return error(_("%s is not a valid reference"), revision
);
215 assert_stash_like(info
, revision
);
217 info
->has_u
= !get_oidf(&info
->u_tree
, "%s^3:", revision
);
219 end_of_rev
= strchrnul(revision
, '@');
220 strbuf_add(&symbolic
, revision
, end_of_rev
- revision
);
222 ret
= repo_dwim_ref(the_repository
, symbolic
.buf
, symbolic
.len
,
223 &dummy
, &expanded_ref
, 0);
224 strbuf_release(&symbolic
);
226 case 0: /* Not found, but valid ref */
227 info
->is_stash_ref
= 0;
230 info
->is_stash_ref
= !strcmp(expanded_ref
, ref_stash
);
232 default: /* Invalid or ambiguous */
237 return !(ret
== 0 || ret
== 1);
240 static int do_clear_stash(void)
242 struct object_id obj
;
243 if (repo_get_oid(the_repository
, ref_stash
, &obj
))
246 return refs_delete_ref(get_main_ref_store(the_repository
), NULL
,
250 static int clear_stash(int argc
, const char **argv
, const char *prefix
)
252 struct option options
[] = {
256 argc
= parse_options(argc
, argv
, prefix
, options
,
257 git_stash_clear_usage
,
258 PARSE_OPT_STOP_AT_NON_OPTION
);
261 return error(_("git stash clear with arguments is "
264 return do_clear_stash();
267 static int reset_tree(struct object_id
*i_tree
, int update
, int reset
)
270 struct unpack_trees_options opts
;
271 struct tree_desc t
[MAX_UNPACK_TREES
];
273 struct lock_file lock_file
= LOCK_INIT
;
275 repo_read_index_preload(the_repository
, NULL
, 0);
276 if (refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
))
279 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
281 memset(&opts
, 0, sizeof(opts
));
283 tree
= parse_tree_indirect(i_tree
);
284 if (parse_tree(tree
))
287 init_tree_desc(t
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
290 opts
.src_index
= the_repository
->index
;
291 opts
.dst_index
= the_repository
->index
;
293 opts
.reset
= reset
? UNPACK_RESET_PROTECT_UNTRACKED
: 0;
294 opts
.update
= update
;
296 opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
297 opts
.fn
= oneway_merge
;
299 if (unpack_trees(nr_trees
, t
, &opts
))
302 if (write_locked_index(the_repository
->index
, &lock_file
, COMMIT_LOCK
))
303 return error(_("unable to write new index file"));
308 static int diff_tree_binary(struct strbuf
*out
, struct object_id
*w_commit
)
310 struct child_process cp
= CHILD_PROCESS_INIT
;
311 const char *w_commit_hex
= oid_to_hex(w_commit
);
314 * Diff-tree would not be very hard to replace with a native function,
315 * however it should be done together with apply_cached.
318 strvec_pushl(&cp
.args
, "diff-tree", "--binary", NULL
);
319 strvec_pushf(&cp
.args
, "%s^2^..%s^2", w_commit_hex
, w_commit_hex
);
321 return pipe_command(&cp
, NULL
, 0, out
, 0, NULL
, 0);
324 static int apply_cached(struct strbuf
*out
)
326 struct child_process cp
= CHILD_PROCESS_INIT
;
329 * Apply currently only reads either from stdin or a file, thus
330 * apply_all_patches would have to be updated to optionally take a
334 strvec_pushl(&cp
.args
, "apply", "--cached", NULL
);
335 return pipe_command(&cp
, out
->buf
, out
->len
, NULL
, 0, NULL
, 0);
338 static int reset_head(void)
340 struct child_process cp
= CHILD_PROCESS_INIT
;
343 * Reset is overall quite simple, however there is no current public
347 strvec_pushl(&cp
.args
, "reset", "--quiet", "--refresh", NULL
);
349 return run_command(&cp
);
352 static int is_path_a_directory(const char *path
)
355 * This function differs from abspath.c:is_directory() in that
356 * here we use lstat() instead of stat(); we do not want to
357 * follow symbolic links here.
360 return (!lstat(path
, &st
) && S_ISDIR(st
.st_mode
));
363 static void add_diff_to_buf(struct diff_queue_struct
*q
,
364 struct diff_options
*options UNUSED
,
369 for (i
= 0; i
< q
->nr
; i
++) {
370 if (is_path_a_directory(q
->queue
[i
]->one
->path
))
373 strbuf_addstr(data
, q
->queue
[i
]->one
->path
);
375 /* NUL-terminate: will be fed to update-index -z */
376 strbuf_addch(data
, '\0');
380 static int restore_untracked(struct object_id
*u_tree
)
383 struct child_process cp
= CHILD_PROCESS_INIT
;
386 * We need to run restore files from a given index, but without
387 * affecting the current index, so we use GIT_INDEX_FILE with
388 * run_command to fork processes that will not interfere.
391 strvec_push(&cp
.args
, "read-tree");
392 strvec_push(&cp
.args
, oid_to_hex(u_tree
));
393 strvec_pushf(&cp
.env
, "GIT_INDEX_FILE=%s",
394 stash_index_path
.buf
);
395 if (run_command(&cp
)) {
396 remove_path(stash_index_path
.buf
);
400 child_process_init(&cp
);
402 strvec_pushl(&cp
.args
, "checkout-index", "--all", NULL
);
403 strvec_pushf(&cp
.env
, "GIT_INDEX_FILE=%s",
404 stash_index_path
.buf
);
406 res
= run_command(&cp
);
407 remove_path(stash_index_path
.buf
);
411 static void unstage_changes_unless_new(struct object_id
*orig_tree
)
414 * When we enter this function, there has been a clean merge of
415 * relevant trees, and the merge logic always stages whatever merges
416 * cleanly. We want to unstage those changes, unless it corresponds
417 * to a file that didn't exist as of orig_tree.
419 * However, if any SKIP_WORKTREE path is modified relative to
420 * orig_tree, then we want to clear the SKIP_WORKTREE bit and write
421 * it to the worktree before unstaging.
424 struct checkout state
= CHECKOUT_INIT
;
425 struct diff_options diff_opts
;
426 struct lock_file lock
= LOCK_INIT
;
429 /* If any entries have skip_worktree set, we'll have to check 'em out */
432 state
.refresh_cache
= 1;
433 state
.istate
= the_repository
->index
;
436 * Step 1: get a difference between orig_tree (which corresponding
437 * to the index before a merge was run) and the current index
438 * (reflecting the changes brought in by the merge).
440 repo_diff_setup(the_repository
, &diff_opts
);
441 diff_opts
.flags
.recursive
= 1;
442 diff_opts
.detect_rename
= 0;
443 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
444 diff_setup_done(&diff_opts
);
446 do_diff_cache(orig_tree
, &diff_opts
);
447 diffcore_std(&diff_opts
);
449 /* Iterate over the paths that changed due to the merge... */
450 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
451 struct diff_filepair
*p
;
452 struct cache_entry
*ce
;
455 /* Look up the path's position in the current index. */
456 p
= diff_queued_diff
.queue
[i
];
457 pos
= index_name_pos(the_repository
->index
, p
->two
->path
,
458 strlen(p
->two
->path
));
461 * Step 2: Place changes in the working tree
463 * Stash is about restoring changes *to the working tree*.
464 * So if the merge successfully got a new version of some
465 * path, but left it out of the working tree, then clear the
466 * SKIP_WORKTREE bit and write it to the working tree.
468 if (pos
>= 0 && ce_skip_worktree(the_repository
->index
->cache
[pos
])) {
471 ce
= the_repository
->index
->cache
[pos
];
472 if (!lstat(ce
->name
, &st
)) {
473 /* Conflicting path present; relocate it */
474 struct strbuf new_path
= STRBUF_INIT
;
477 strbuf_addf(&new_path
,
478 "%s.stash.XXXXXX", ce
->name
);
479 fd
= xmkstemp(new_path
.buf
);
481 printf(_("WARNING: Untracked file in way of "
482 "tracked file! Renaming\n "
485 ce
->name
, new_path
.buf
);
486 if (rename(ce
->name
, new_path
.buf
))
487 die("Failed to move %s to %s\n",
488 ce
->name
, new_path
.buf
);
489 strbuf_release(&new_path
);
491 checkout_entry(ce
, &state
, NULL
, NULL
);
492 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
496 * Step 3: "unstage" changes, as long as they are still tracked
498 if (p
->one
->oid_valid
) {
500 * Path existed in orig_tree; restore index entry
501 * from that tree in order to "unstage" the changes.
503 int option
= ADD_CACHE_OK_TO_REPLACE
;
505 option
= ADD_CACHE_OK_TO_ADD
;
507 ce
= make_cache_entry(the_repository
->index
,
512 add_index_entry(the_repository
->index
, ce
, option
);
515 diff_flush(&diff_opts
);
518 * Step 4: write the new index to disk
520 repo_hold_locked_index(the_repository
, &lock
, LOCK_DIE_ON_ERROR
);
521 if (write_locked_index(the_repository
->index
, &lock
,
522 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
523 die(_("could not write index"));
526 static int do_apply_stash(const char *prefix
, struct stash_info
*info
,
527 int index
, int quiet
)
530 int has_index
= index
;
531 struct merge_options o
;
532 struct object_id c_tree
;
533 struct object_id index_tree
;
534 struct tree
*head
, *merge
, *merge_base
;
535 struct lock_file lock
= LOCK_INIT
;
537 repo_read_index_preload(the_repository
, NULL
, 0);
538 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
, 0, 0,
540 return error(_("could not write index"));
542 if (write_index_as_tree(&c_tree
, the_repository
->index
, get_index_file(), 0,
544 return error(_("cannot apply a stash in the middle of a merge"));
547 if (oideq(&info
->b_tree
, &info
->i_tree
) ||
548 oideq(&c_tree
, &info
->i_tree
)) {
551 struct strbuf out
= STRBUF_INIT
;
553 if (diff_tree_binary(&out
, &info
->w_commit
)) {
554 strbuf_release(&out
);
555 return error(_("could not generate diff %s^!."),
556 oid_to_hex(&info
->w_commit
));
559 ret
= apply_cached(&out
);
560 strbuf_release(&out
);
562 return error(_("conflicts in index. "
563 "Try without --index."));
565 discard_index(the_repository
->index
);
566 repo_read_index(the_repository
);
567 if (write_index_as_tree(&index_tree
, the_repository
->index
,
568 get_index_file(), 0, NULL
))
569 return error(_("could not save index tree"));
572 discard_index(the_repository
->index
);
573 repo_read_index(the_repository
);
577 init_merge_options(&o
, the_repository
);
579 o
.branch1
= "Updated upstream";
580 o
.branch2
= "Stashed changes";
581 o
.ancestor
= "Stash base";
583 if (oideq(&info
->b_tree
, &c_tree
))
584 o
.branch1
= "Version stash was based on";
589 if (o
.verbosity
>= 3)
590 printf_ln(_("Merging %s with %s"), o
.branch1
, o
.branch2
);
592 head
= lookup_tree(o
.repo
, &c_tree
);
593 merge
= lookup_tree(o
.repo
, &info
->w_tree
);
594 merge_base
= lookup_tree(o
.repo
, &info
->b_tree
);
596 repo_hold_locked_index(o
.repo
, &lock
, LOCK_DIE_ON_ERROR
);
597 clean
= merge_ort_nonrecursive(&o
, head
, merge
, merge_base
);
600 * If 'clean' >= 0, reverse the value for 'ret' so 'ret' is 0 when the
601 * merge was clean, and nonzero if the merge was unclean or encountered
604 ret
= clean
>= 0 ? !clean
: clean
;
607 rollback_lock_file(&lock
);
608 else if (write_locked_index(o
.repo
->index
, &lock
,
609 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
610 ret
= error(_("could not write index"));
613 repo_rerere(the_repository
, 0);
616 fprintf_ln(stderr
, _("Index was not unstashed."));
618 goto restore_untracked
;
622 if (reset_tree(&index_tree
, 0, 0))
625 unstage_changes_unless_new(&c_tree
);
629 if (info
->has_u
&& restore_untracked(&info
->u_tree
))
630 ret
= error(_("could not restore untracked files from stash"));
633 struct child_process cp
= CHILD_PROCESS_INIT
;
636 * Status is quite simple and could be replaced with calls to
637 * wt_status in the future, but it adds complexities which may
638 * require more tests.
642 strvec_pushf(&cp
.env
, GIT_WORK_TREE_ENVIRONMENT
"=%s",
643 absolute_path(get_git_work_tree()));
644 strvec_pushf(&cp
.env
, GIT_DIR_ENVIRONMENT
"=%s",
645 absolute_path(get_git_dir()));
646 strvec_push(&cp
.args
, "status");
653 static int apply_stash(int argc
, const char **argv
, const char *prefix
)
658 struct stash_info info
= STASH_INFO_INIT
;
659 struct option options
[] = {
660 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
661 OPT_BOOL(0, "index", &index
,
662 N_("attempt to recreate the index")),
666 argc
= parse_options(argc
, argv
, prefix
, options
,
667 git_stash_apply_usage
, 0);
669 if (get_stash_info(&info
, argc
, argv
))
672 ret
= do_apply_stash(prefix
, &info
, index
, quiet
);
674 free_stash_info(&info
);
678 static int reject_reflog_ent(struct object_id
*ooid UNUSED
,
679 struct object_id
*noid UNUSED
,
680 const char *email UNUSED
,
681 timestamp_t timestamp UNUSED
,
682 int tz UNUSED
, const char *message UNUSED
,
683 void *cb_data UNUSED
)
688 static int reflog_is_empty(const char *refname
)
690 return !refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
691 refname
, reject_reflog_ent
, NULL
);
694 static int do_drop_stash(struct stash_info
*info
, int quiet
)
696 if (!reflog_delete(info
->revision
.buf
,
697 EXPIRE_REFLOGS_REWRITE
| EXPIRE_REFLOGS_UPDATE_REF
,
700 printf_ln(_("Dropped %s (%s)"), info
->revision
.buf
,
701 oid_to_hex(&info
->w_commit
));
703 return error(_("%s: Could not drop stash entry"),
707 if (reflog_is_empty(ref_stash
))
713 static int get_stash_info_assert(struct stash_info
*info
, int argc
,
716 int ret
= get_stash_info(info
, argc
, argv
);
721 if (!info
->is_stash_ref
)
722 return error(_("'%s' is not a stash reference"), info
->revision
.buf
);
727 static int drop_stash(int argc
, const char **argv
, const char *prefix
)
731 struct stash_info info
= STASH_INFO_INIT
;
732 struct option options
[] = {
733 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
737 argc
= parse_options(argc
, argv
, prefix
, options
,
738 git_stash_drop_usage
, 0);
740 if (get_stash_info_assert(&info
, argc
, argv
))
743 ret
= do_drop_stash(&info
, quiet
);
745 free_stash_info(&info
);
749 static int pop_stash(int argc
, const char **argv
, const char *prefix
)
754 struct stash_info info
= STASH_INFO_INIT
;
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")),
762 argc
= parse_options(argc
, argv
, prefix
, options
,
763 git_stash_pop_usage
, 0);
765 if (get_stash_info_assert(&info
, argc
, argv
))
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."));
772 ret
= do_drop_stash(&info
, quiet
);
775 free_stash_info(&info
);
779 static int branch_stash(int argc
, const char **argv
, const char *prefix
)
782 const char *branch
= NULL
;
783 struct stash_info info
= STASH_INFO_INIT
;
784 struct child_process cp
= CHILD_PROCESS_INIT
;
785 struct option options
[] = {
789 argc
= parse_options(argc
, argv
, prefix
, options
,
790 git_stash_branch_usage
, 0);
793 fprintf_ln(stderr
, _("No branch name specified"));
799 if (get_stash_info(&info
, argc
- 1, argv
+ 1))
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
));
806 ret
= run_command(&cp
);
808 ret
= do_apply_stash(prefix
, &info
, 1, 0);
809 if (!ret
&& info
.is_stash_ref
)
810 ret
= do_drop_stash(&info
, 0);
813 free_stash_info(&info
);
817 static int list_stash(int argc
, const char **argv
, const char *prefix
)
819 struct child_process cp
= CHILD_PROCESS_INIT
;
820 struct option options
[] = {
824 argc
= parse_options(argc
, argv
, prefix
, options
,
825 git_stash_list_usage
,
826 PARSE_OPT_KEEP_UNKNOWN_OPT
);
828 if (!refs_ref_exists(get_main_ref_store(the_repository
), ref_stash
))
832 strvec_pushl(&cp
.args
, "log", "--format=%gd: %gs", "-g",
833 "--first-parent", NULL
);
834 strvec_pushv(&cp
.args
, argv
);
835 strvec_push(&cp
.args
, ref_stash
);
836 strvec_push(&cp
.args
, "--");
837 return run_command(&cp
);
840 static int show_stat
= 1;
841 static int show_patch
;
842 static int show_include_untracked
;
844 static int git_stash_config(const char *var
, const char *value
,
845 const struct config_context
*ctx
, void *cb
)
847 if (!strcmp(var
, "stash.showstat")) {
848 show_stat
= git_config_bool(var
, value
);
851 if (!strcmp(var
, "stash.showpatch")) {
852 show_patch
= git_config_bool(var
, value
);
855 if (!strcmp(var
, "stash.showincludeuntracked")) {
856 show_include_untracked
= git_config_bool(var
, value
);
859 return git_diff_basic_config(var
, value
, ctx
, cb
);
862 static void diff_include_untracked(const struct stash_info
*info
, struct diff_options
*diff_opt
)
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 };
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
]->object
.oid
,
875 tree
[i
]->buffer
, tree
[i
]->size
);
878 unpack_tree_opt
.head_idx
= -1;
879 unpack_tree_opt
.src_index
= the_repository
->index
;
880 unpack_tree_opt
.dst_index
= the_repository
->index
;
881 unpack_tree_opt
.merge
= 1;
882 unpack_tree_opt
.fn
= stash_worktree_untracked_merge
;
884 if (unpack_trees(ARRAY_SIZE(tree_desc
), tree_desc
, &unpack_tree_opt
))
885 die(_("failed to unpack trees"));
887 do_diff_cache(&info
->b_commit
, diff_opt
);
890 static int show_stash(int argc
, const char **argv
, const char *prefix
)
894 struct stash_info info
= STASH_INFO_INIT
;
896 struct strvec stash_args
= STRVEC_INIT
;
897 struct strvec revision_args
= STRVEC_INIT
;
902 } show_untracked
= show_include_untracked
? UNTRACKED_INCLUDE
: UNTRACKED_NONE
;
903 struct option options
[] = {
904 OPT_SET_INT('u', "include-untracked", &show_untracked
,
905 N_("include untracked files in the stash"),
907 OPT_SET_INT_F(0, "only-untracked", &show_untracked
,
908 N_("only show untracked files in the stash"),
909 UNTRACKED_ONLY
, PARSE_OPT_NONEG
),
914 init_diff_ui_defaults();
915 git_config(git_diff_ui_config
, NULL
);
916 repo_init_revisions(the_repository
, &rev
, prefix
);
918 argc
= parse_options(argc
, argv
, prefix
, options
, git_stash_show_usage
,
919 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
|
920 PARSE_OPT_KEEP_DASHDASH
);
922 strvec_push(&revision_args
, argv
[0]);
923 for (i
= 1; i
< argc
; i
++) {
924 if (argv
[i
][0] != '-')
925 strvec_push(&stash_args
, argv
[i
]);
927 strvec_push(&revision_args
, argv
[i
]);
930 if (get_stash_info(&info
, stash_args
.nr
, stash_args
.v
))
934 * The config settings are applied only if there are not passed
937 if (revision_args
.nr
== 1) {
939 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
;
942 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
944 if (!show_stat
&& !show_patch
) {
950 argc
= setup_revisions(revision_args
.nr
, revision_args
.v
, &rev
, NULL
);
953 if (!rev
.diffopt
.output_format
) {
954 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
955 diff_setup_done(&rev
.diffopt
);
958 rev
.diffopt
.flags
.recursive
= 1;
959 setup_diff_pager(&rev
.diffopt
);
960 switch (show_untracked
) {
962 diff_tree_oid(&info
.b_commit
, &info
.w_commit
, "", &rev
.diffopt
);
966 diff_root_tree_oid(&info
.u_tree
, "", &rev
.diffopt
);
968 case UNTRACKED_INCLUDE
:
970 diff_include_untracked(&info
, &rev
.diffopt
);
972 diff_tree_oid(&info
.b_commit
, &info
.w_commit
, "", &rev
.diffopt
);
975 log_tree_diff_flush(&rev
);
977 ret
= diff_result_code(&rev
.diffopt
);
979 strvec_clear(&stash_args
);
980 free_stash_info(&info
);
981 release_revisions(&rev
);
983 usage_with_options(git_stash_show_usage
, options
);
990 static int do_store_stash(const struct object_id
*w_commit
, const char *stash_msg
,
993 struct stash_info info
;
994 char revision
[GIT_MAX_HEXSZ
];
996 oid_to_hex_r(revision
, w_commit
);
997 assert_stash_like(&info
, revision
);
1000 stash_msg
= "Created via \"git stash store\".";
1002 if (refs_update_ref(get_main_ref_store(the_repository
), stash_msg
, ref_stash
, w_commit
, NULL
,
1003 REF_FORCE_CREATE_REFLOG
,
1004 quiet
? UPDATE_REFS_QUIET_ON_ERR
:
1005 UPDATE_REFS_MSG_ON_ERR
)) {
1007 fprintf_ln(stderr
, _("Cannot update %s with %s"),
1008 ref_stash
, oid_to_hex(w_commit
));
1016 static int store_stash(int argc
, const char **argv
, const char *prefix
)
1019 const char *stash_msg
= NULL
;
1020 struct object_id obj
;
1021 struct object_context dummy
;
1022 struct option options
[] = {
1023 OPT__QUIET(&quiet
, N_("be quiet")),
1024 OPT_STRING('m', "message", &stash_msg
, "message",
1025 N_("stash message")),
1029 argc
= parse_options(argc
, argv
, prefix
, options
,
1030 git_stash_store_usage
,
1031 PARSE_OPT_KEEP_UNKNOWN_OPT
);
1035 fprintf_ln(stderr
, _("\"git stash store\" requires one "
1036 "<commit> argument"));
1040 if (get_oid_with_context(the_repository
,
1041 argv
[0], quiet
? GET_OID_QUIETLY
: 0, &obj
,
1044 fprintf_ln(stderr
, _("Cannot update %s with %s"),
1045 ref_stash
, argv
[0]);
1049 return do_store_stash(&obj
, stash_msg
, quiet
);
1052 static void add_pathspecs(struct strvec
*args
,
1053 const struct pathspec
*ps
) {
1056 for (i
= 0; i
< ps
->nr
; i
++)
1057 strvec_push(args
, ps
->items
[i
].original
);
1061 * `untracked_files` will be filled with the names of untracked files.
1062 * The return value is:
1064 * = 0 if there are not any untracked files
1065 * > 0 if there are untracked files
1067 static int get_untracked_files(const struct pathspec
*ps
, int include_untracked
,
1068 struct strbuf
*untracked_files
)
1072 struct dir_struct dir
= DIR_INIT
;
1074 if (include_untracked
!= INCLUDE_ALL_FILES
)
1075 setup_standard_excludes(&dir
);
1077 fill_directory(&dir
, the_repository
->index
, ps
);
1078 for (i
= 0; i
< dir
.nr
; i
++) {
1079 struct dir_entry
*ent
= dir
.entries
[i
];
1081 strbuf_addstr(untracked_files
, ent
->name
);
1082 /* NUL-terminate: will be fed to update-index -z */
1083 strbuf_addch(untracked_files
, '\0');
1091 * The return value of `check_changes_tracked_files()` can be:
1093 * < 0 if there was an error
1094 * = 0 if there are no changes.
1095 * > 0 if there are changes.
1097 static int check_changes_tracked_files(const struct pathspec
*ps
)
1099 struct rev_info rev
;
1100 struct object_id dummy
;
1103 /* No initial commit. */
1104 if (repo_get_oid(the_repository
, "HEAD", &dummy
))
1107 if (repo_read_index(the_repository
) < 0)
1110 repo_init_revisions(the_repository
, &rev
, NULL
);
1111 copy_pathspec(&rev
.prune_data
, ps
);
1113 rev
.diffopt
.flags
.quick
= 1;
1114 rev
.diffopt
.flags
.ignore_submodules
= 1;
1117 add_head_to_pending(&rev
);
1118 diff_setup_done(&rev
.diffopt
);
1120 run_diff_index(&rev
, DIFF_INDEX_CACHED
);
1121 if (diff_result_code(&rev
.diffopt
)) {
1126 run_diff_files(&rev
, 0);
1127 if (diff_result_code(&rev
.diffopt
)) {
1133 release_revisions(&rev
);
1138 * The function will fill `untracked_files` with the names of untracked files
1139 * It will return 1 if there were any changes and 0 if there were not.
1141 static int check_changes(const struct pathspec
*ps
, int include_untracked
,
1142 struct strbuf
*untracked_files
)
1145 if (check_changes_tracked_files(ps
))
1148 if (include_untracked
&& get_untracked_files(ps
, include_untracked
,
1155 static int save_untracked_files(struct stash_info
*info
, struct strbuf
*msg
,
1156 struct strbuf files
)
1159 struct strbuf untracked_msg
= STRBUF_INIT
;
1160 struct child_process cp_upd_index
= CHILD_PROCESS_INIT
;
1161 struct index_state istate
= INDEX_STATE_INIT(the_repository
);
1163 cp_upd_index
.git_cmd
= 1;
1164 strvec_pushl(&cp_upd_index
.args
, "update-index", "-z", "--add",
1165 "--remove", "--stdin", NULL
);
1166 strvec_pushf(&cp_upd_index
.env
, "GIT_INDEX_FILE=%s",
1167 stash_index_path
.buf
);
1169 strbuf_addf(&untracked_msg
, "untracked files on %s\n", msg
->buf
);
1170 if (pipe_command(&cp_upd_index
, files
.buf
, files
.len
, NULL
, 0,
1176 if (write_index_as_tree(&info
->u_tree
, &istate
, stash_index_path
.buf
, 0,
1182 if (commit_tree(untracked_msg
.buf
, untracked_msg
.len
,
1183 &info
->u_tree
, NULL
, &info
->u_commit
, NULL
, NULL
)) {
1189 release_index(&istate
);
1190 strbuf_release(&untracked_msg
);
1191 remove_path(stash_index_path
.buf
);
1195 static int stash_staged(struct stash_info
*info
, struct strbuf
*out_patch
,
1199 struct child_process cp_diff_tree
= CHILD_PROCESS_INIT
;
1200 struct index_state istate
= INDEX_STATE_INIT(the_repository
);
1202 if (write_index_as_tree(&info
->w_tree
, &istate
, the_repository
->index_file
,
1208 cp_diff_tree
.git_cmd
= 1;
1209 strvec_pushl(&cp_diff_tree
.args
, "diff-tree", "-p", "--binary",
1210 "-U1", "HEAD", oid_to_hex(&info
->w_tree
), "--", NULL
);
1211 if (pipe_command(&cp_diff_tree
, NULL
, 0, out_patch
, 0, NULL
, 0)) {
1216 if (!out_patch
->len
) {
1218 fprintf_ln(stderr
, _("No staged changes"));
1223 release_index(&istate
);
1227 static int stash_patch(struct stash_info
*info
, const struct pathspec
*ps
,
1228 struct strbuf
*out_patch
, int quiet
)
1231 struct child_process cp_read_tree
= CHILD_PROCESS_INIT
;
1232 struct child_process cp_diff_tree
= CHILD_PROCESS_INIT
;
1233 struct index_state istate
= INDEX_STATE_INIT(the_repository
);
1234 char *old_index_env
= NULL
, *old_repo_index_file
;
1236 remove_path(stash_index_path
.buf
);
1238 cp_read_tree
.git_cmd
= 1;
1239 strvec_pushl(&cp_read_tree
.args
, "read-tree", "HEAD", NULL
);
1240 strvec_pushf(&cp_read_tree
.env
, "GIT_INDEX_FILE=%s",
1241 stash_index_path
.buf
);
1242 if (run_command(&cp_read_tree
)) {
1247 /* Find out what the user wants. */
1248 old_repo_index_file
= the_repository
->index_file
;
1249 the_repository
->index_file
= stash_index_path
.buf
;
1250 old_index_env
= xstrdup_or_null(getenv(INDEX_ENVIRONMENT
));
1251 setenv(INDEX_ENVIRONMENT
, the_repository
->index_file
, 1);
1253 ret
= !!run_add_p(the_repository
, ADD_P_STASH
, NULL
, ps
);
1255 the_repository
->index_file
= old_repo_index_file
;
1256 if (old_index_env
&& *old_index_env
)
1257 setenv(INDEX_ENVIRONMENT
, old_index_env
, 1);
1259 unsetenv(INDEX_ENVIRONMENT
);
1260 FREE_AND_NULL(old_index_env
);
1262 /* State of the working tree. */
1263 if (write_index_as_tree(&info
->w_tree
, &istate
, stash_index_path
.buf
, 0,
1269 cp_diff_tree
.git_cmd
= 1;
1270 strvec_pushl(&cp_diff_tree
.args
, "diff-tree", "-p", "-U1", "HEAD",
1271 oid_to_hex(&info
->w_tree
), "--", NULL
);
1272 if (pipe_command(&cp_diff_tree
, NULL
, 0, out_patch
, 0, NULL
, 0)) {
1277 if (!out_patch
->len
) {
1279 fprintf_ln(stderr
, _("No changes selected"));
1284 release_index(&istate
);
1285 remove_path(stash_index_path
.buf
);
1289 static int stash_working_tree(struct stash_info
*info
, const struct pathspec
*ps
)
1292 struct rev_info rev
;
1293 struct child_process cp_upd_index
= CHILD_PROCESS_INIT
;
1294 struct strbuf diff_output
= STRBUF_INIT
;
1295 struct index_state istate
= INDEX_STATE_INIT(the_repository
);
1297 repo_init_revisions(the_repository
, &rev
, NULL
);
1298 copy_pathspec(&rev
.prune_data
, ps
);
1300 set_alternate_index_output(stash_index_path
.buf
);
1301 if (reset_tree(&info
->i_tree
, 0, 0)) {
1305 set_alternate_index_output(NULL
);
1307 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
1308 rev
.diffopt
.format_callback
= add_diff_to_buf
;
1309 rev
.diffopt
.format_callback_data
= &diff_output
;
1311 if (repo_read_index_preload(the_repository
, &rev
.diffopt
.pathspec
, 0) < 0) {
1316 add_pending_object(&rev
, parse_object(the_repository
, &info
->b_commit
),
1318 run_diff_index(&rev
, 0);
1320 cp_upd_index
.git_cmd
= 1;
1321 strvec_pushl(&cp_upd_index
.args
, "update-index",
1322 "--ignore-skip-worktree-entries",
1323 "-z", "--add", "--remove", "--stdin", NULL
);
1324 strvec_pushf(&cp_upd_index
.env
, "GIT_INDEX_FILE=%s",
1325 stash_index_path
.buf
);
1327 if (pipe_command(&cp_upd_index
, diff_output
.buf
, diff_output
.len
,
1328 NULL
, 0, NULL
, 0)) {
1333 if (write_index_as_tree(&info
->w_tree
, &istate
, stash_index_path
.buf
, 0,
1340 release_index(&istate
);
1341 release_revisions(&rev
);
1342 strbuf_release(&diff_output
);
1343 remove_path(stash_index_path
.buf
);
1347 static int do_create_stash(const struct pathspec
*ps
, struct strbuf
*stash_msg_buf
,
1348 int include_untracked
, int patch_mode
, int only_staged
,
1349 struct stash_info
*info
, struct strbuf
*patch
,
1354 int untracked_commit_option
= 0;
1355 const char *head_short_sha1
= NULL
;
1356 const char *branch_ref
= NULL
;
1357 const char *branch_name
= "(no branch)";
1358 struct commit
*head_commit
= NULL
;
1359 struct commit_list
*parents
= NULL
;
1360 struct strbuf msg
= STRBUF_INIT
;
1361 struct strbuf commit_tree_label
= STRBUF_INIT
;
1362 struct strbuf untracked_files
= STRBUF_INIT
;
1364 prepare_fallback_ident("git stash", "git@stash");
1366 repo_read_index_preload(the_repository
, NULL
, 0);
1367 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
, 0, 0,
1368 NULL
, NULL
, NULL
) < 0) {
1369 ret
= error(_("could not write index"));
1373 if (repo_get_oid(the_repository
, "HEAD", &info
->b_commit
)) {
1375 fprintf_ln(stderr
, _("You do not have "
1376 "the initial commit yet"));
1380 head_commit
= lookup_commit(the_repository
, &info
->b_commit
);
1383 if (!check_changes(ps
, include_untracked
, &untracked_files
)) {
1388 branch_ref
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
1389 "HEAD", 0, NULL
, &flags
);
1390 if (flags
& REF_ISSYMREF
)
1391 skip_prefix(branch_ref
, "refs/heads/", &branch_name
);
1392 head_short_sha1
= repo_find_unique_abbrev(the_repository
,
1393 &head_commit
->object
.oid
,
1395 strbuf_addf(&msg
, "%s: %s ", branch_name
, head_short_sha1
);
1396 pp_commit_easy(CMIT_FMT_ONELINE
, head_commit
, &msg
);
1398 strbuf_addf(&commit_tree_label
, "index on %s\n", msg
.buf
);
1399 commit_list_insert(head_commit
, &parents
);
1400 if (write_index_as_tree(&info
->i_tree
, the_repository
->index
, get_index_file(), 0,
1402 commit_tree(commit_tree_label
.buf
, commit_tree_label
.len
,
1403 &info
->i_tree
, parents
, &info
->i_commit
, NULL
, NULL
)) {
1405 fprintf_ln(stderr
, _("Cannot save the current "
1411 if (include_untracked
) {
1412 if (save_untracked_files(info
, &msg
, untracked_files
)) {
1414 fprintf_ln(stderr
, _("Cannot save "
1415 "the untracked files"));
1419 untracked_commit_option
= 1;
1422 ret
= stash_patch(info
, ps
, patch
, quiet
);
1425 fprintf_ln(stderr
, _("Cannot save the current "
1428 } else if (ret
> 0) {
1431 } else if (only_staged
) {
1432 ret
= stash_staged(info
, patch
, quiet
);
1435 fprintf_ln(stderr
, _("Cannot save the current "
1438 } else if (ret
> 0) {
1442 if (stash_working_tree(info
, ps
)) {
1444 fprintf_ln(stderr
, _("Cannot save the current "
1451 if (!stash_msg_buf
->len
)
1452 strbuf_addf(stash_msg_buf
, "WIP on %s", msg
.buf
);
1454 strbuf_insertf(stash_msg_buf
, 0, "On %s: ", branch_name
);
1457 * `parents` will be empty after calling `commit_tree()`, so there is
1458 * no need to call `free_commit_list()`
1461 if (untracked_commit_option
)
1462 commit_list_insert(lookup_commit(the_repository
,
1465 commit_list_insert(lookup_commit(the_repository
, &info
->i_commit
),
1467 commit_list_insert(head_commit
, &parents
);
1469 if (commit_tree(stash_msg_buf
->buf
, stash_msg_buf
->len
, &info
->w_tree
,
1470 parents
, &info
->w_commit
, NULL
, NULL
)) {
1472 fprintf_ln(stderr
, _("Cannot record "
1473 "working tree state"));
1479 strbuf_release(&commit_tree_label
);
1480 strbuf_release(&msg
);
1481 strbuf_release(&untracked_files
);
1485 static int create_stash(int argc
, const char **argv
, const char *prefix UNUSED
)
1488 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1489 struct stash_info info
= STASH_INFO_INIT
;
1492 /* Starting with argv[1], since argv[0] is "create" */
1493 strbuf_join_argv(&stash_msg_buf
, argc
- 1, ++argv
, ' ');
1495 memset(&ps
, 0, sizeof(ps
));
1496 if (!check_changes_tracked_files(&ps
))
1499 ret
= do_create_stash(&ps
, &stash_msg_buf
, 0, 0, 0, &info
,
1502 printf_ln("%s", oid_to_hex(&info
.w_commit
));
1504 free_stash_info(&info
);
1505 strbuf_release(&stash_msg_buf
);
1509 static int do_push_stash(const struct pathspec
*ps
, const char *stash_msg
, int quiet
,
1510 int keep_index
, int patch_mode
, int include_untracked
, int only_staged
)
1513 struct stash_info info
= STASH_INFO_INIT
;
1514 struct strbuf patch
= STRBUF_INIT
;
1515 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1516 struct strbuf untracked_files
= STRBUF_INIT
;
1518 if (patch_mode
&& keep_index
== -1)
1521 if (patch_mode
&& include_untracked
) {
1522 fprintf_ln(stderr
, _("Can't use --patch and --include-untracked"
1523 " or --all at the same time"));
1528 /* --patch overrides --staged */
1532 if (only_staged
&& include_untracked
) {
1533 fprintf_ln(stderr
, _("Can't use --staged and --include-untracked"
1534 " or --all at the same time"));
1539 repo_read_index_preload(the_repository
, NULL
, 0);
1540 if (!include_untracked
&& ps
->nr
) {
1542 char *ps_matched
= xcalloc(ps
->nr
, 1);
1544 /* TODO: audit for interaction with sparse-index. */
1545 ensure_full_index(the_repository
->index
);
1546 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++)
1547 ce_path_match(the_repository
->index
, the_repository
->index
->cache
[i
], ps
,
1550 if (report_path_error(ps_matched
, ps
)) {
1551 fprintf_ln(stderr
, _("Did you forget to 'git add'?"));
1559 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
, 0, 0,
1560 NULL
, NULL
, NULL
)) {
1561 ret
= error(_("could not write index"));
1565 if (!check_changes(ps
, include_untracked
, &untracked_files
)) {
1567 printf_ln(_("No local changes to save"));
1571 if (!refs_reflog_exists(get_main_ref_store(the_repository
), ref_stash
) && do_clear_stash()) {
1574 fprintf_ln(stderr
, _("Cannot initialize stash"));
1579 strbuf_addstr(&stash_msg_buf
, stash_msg
);
1580 if (do_create_stash(ps
, &stash_msg_buf
, include_untracked
, patch_mode
, only_staged
,
1581 &info
, &patch
, quiet
)) {
1586 if (do_store_stash(&info
.w_commit
, stash_msg_buf
.buf
, 1)) {
1589 fprintf_ln(stderr
, _("Cannot save the current status"));
1594 printf_ln(_("Saved working directory and index state %s"),
1597 if (!(patch_mode
|| only_staged
)) {
1598 if (include_untracked
&& !ps
->nr
) {
1599 struct child_process cp
= CHILD_PROCESS_INIT
;
1602 if (startup_info
->original_cwd
) {
1603 cp
.dir
= startup_info
->original_cwd
;
1604 strvec_pushf(&cp
.env
, "%s=%s",
1605 GIT_WORK_TREE_ENVIRONMENT
,
1606 the_repository
->worktree
);
1608 strvec_pushl(&cp
.args
, "clean", "--force",
1609 "--quiet", "-d", ":/", NULL
);
1610 if (include_untracked
== INCLUDE_ALL_FILES
)
1611 strvec_push(&cp
.args
, "-x");
1612 if (run_command(&cp
)) {
1617 discard_index(the_repository
->index
);
1619 struct child_process cp_add
= CHILD_PROCESS_INIT
;
1620 struct child_process cp_diff
= CHILD_PROCESS_INIT
;
1621 struct child_process cp_apply
= CHILD_PROCESS_INIT
;
1622 struct strbuf out
= STRBUF_INIT
;
1625 strvec_push(&cp_add
.args
, "add");
1626 if (!include_untracked
)
1627 strvec_push(&cp_add
.args
, "-u");
1628 if (include_untracked
== INCLUDE_ALL_FILES
)
1629 strvec_push(&cp_add
.args
, "--force");
1630 strvec_push(&cp_add
.args
, "--");
1631 add_pathspecs(&cp_add
.args
, ps
);
1632 if (run_command(&cp_add
)) {
1637 cp_diff
.git_cmd
= 1;
1638 strvec_pushl(&cp_diff
.args
, "diff-index", "-p",
1639 "--cached", "--binary", "HEAD", "--",
1641 add_pathspecs(&cp_diff
.args
, ps
);
1642 if (pipe_command(&cp_diff
, NULL
, 0, &out
, 0, NULL
, 0)) {
1647 cp_apply
.git_cmd
= 1;
1648 strvec_pushl(&cp_apply
.args
, "apply", "--index",
1650 if (pipe_command(&cp_apply
, out
.buf
, out
.len
, NULL
, 0,
1656 struct child_process cp
= CHILD_PROCESS_INIT
;
1658 /* BUG: this nukes untracked files in the way */
1659 strvec_pushl(&cp
.args
, "reset", "--hard", "-q",
1660 "--no-recurse-submodules", NULL
);
1661 if (run_command(&cp
)) {
1667 if (keep_index
== 1 && !is_null_oid(&info
.i_tree
)) {
1668 struct child_process cp
= CHILD_PROCESS_INIT
;
1671 strvec_pushl(&cp
.args
, "checkout", "--no-overlay",
1672 oid_to_hex(&info
.i_tree
), "--", NULL
);
1674 strvec_push(&cp
.args
, ":/");
1676 add_pathspecs(&cp
.args
, ps
);
1677 if (run_command(&cp
)) {
1684 struct child_process cp
= CHILD_PROCESS_INIT
;
1687 strvec_pushl(&cp
.args
, "apply", "-R", NULL
);
1689 if (pipe_command(&cp
, patch
.buf
, patch
.len
, NULL
, 0, NULL
, 0)) {
1691 fprintf_ln(stderr
, _("Cannot remove "
1692 "worktree changes"));
1697 if (keep_index
< 1) {
1698 struct child_process cp
= CHILD_PROCESS_INIT
;
1701 strvec_pushl(&cp
.args
, "reset", "-q", "--refresh", "--",
1703 add_pathspecs(&cp
.args
, ps
);
1704 if (run_command(&cp
)) {
1713 strbuf_release(&patch
);
1714 free_stash_info(&info
);
1715 strbuf_release(&stash_msg_buf
);
1716 strbuf_release(&untracked_files
);
1720 static int push_stash(int argc
, const char **argv
, const char *prefix
,
1723 int force_assume
= 0;
1724 int keep_index
= -1;
1725 int only_staged
= 0;
1727 int include_untracked
= 0;
1729 int pathspec_file_nul
= 0;
1730 const char *stash_msg
= NULL
;
1731 const char *pathspec_from_file
= NULL
;
1733 struct option options
[] = {
1734 OPT_BOOL('k', "keep-index", &keep_index
,
1736 OPT_BOOL('S', "staged", &only_staged
,
1737 N_("stash staged changes only")),
1738 OPT_BOOL('p', "patch", &patch_mode
,
1739 N_("stash in patch mode")),
1740 OPT__QUIET(&quiet
, N_("quiet mode")),
1741 OPT_BOOL('u', "include-untracked", &include_untracked
,
1742 N_("include untracked files in stash")),
1743 OPT_SET_INT('a', "all", &include_untracked
,
1744 N_("include ignore files"), 2),
1745 OPT_STRING('m', "message", &stash_msg
, N_("message"),
1746 N_("stash message")),
1747 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
1748 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
1754 force_assume
= !strcmp(argv
[0], "-p");
1755 argc
= parse_options(argc
, argv
, prefix
, options
,
1756 push_assumed
? git_stash_usage
:
1757 git_stash_push_usage
,
1758 PARSE_OPT_KEEP_DASHDASH
);
1762 if (!strcmp(argv
[0], "--")) {
1765 } else if (push_assumed
&& !force_assume
) {
1766 die("subcommand wasn't specified; 'push' can't be assumed due to unexpected token '%s'",
1771 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_FULL
| PATHSPEC_PREFIX_ORIGIN
,
1774 if (pathspec_from_file
) {
1776 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
1779 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--staged");
1782 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
1784 parse_pathspec_file(&ps
, 0,
1785 PATHSPEC_PREFER_FULL
| PATHSPEC_PREFIX_ORIGIN
,
1786 prefix
, pathspec_from_file
, pathspec_file_nul
);
1787 } else if (pathspec_file_nul
) {
1788 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
1791 ret
= do_push_stash(&ps
, stash_msg
, quiet
, keep_index
, patch_mode
,
1792 include_untracked
, only_staged
);
1793 clear_pathspec(&ps
);
1797 static int push_stash_unassumed(int argc
, const char **argv
, const char *prefix
)
1799 return push_stash(argc
, argv
, prefix
, 0);
1802 static int save_stash(int argc
, const char **argv
, const char *prefix
)
1804 int keep_index
= -1;
1805 int only_staged
= 0;
1807 int include_untracked
= 0;
1810 const char *stash_msg
= NULL
;
1812 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1813 struct option options
[] = {
1814 OPT_BOOL('k', "keep-index", &keep_index
,
1816 OPT_BOOL('S', "staged", &only_staged
,
1817 N_("stash staged changes only")),
1818 OPT_BOOL('p', "patch", &patch_mode
,
1819 N_("stash in patch mode")),
1820 OPT__QUIET(&quiet
, N_("quiet mode")),
1821 OPT_BOOL('u', "include-untracked", &include_untracked
,
1822 N_("include untracked files in stash")),
1823 OPT_SET_INT('a', "all", &include_untracked
,
1824 N_("include ignore files"), 2),
1825 OPT_STRING('m', "message", &stash_msg
, "message",
1826 N_("stash message")),
1830 argc
= parse_options(argc
, argv
, prefix
, options
,
1831 git_stash_save_usage
,
1832 PARSE_OPT_KEEP_DASHDASH
);
1835 stash_msg
= strbuf_join_argv(&stash_msg_buf
, argc
, argv
, ' ');
1837 memset(&ps
, 0, sizeof(ps
));
1838 ret
= do_push_stash(&ps
, stash_msg
, quiet
, keep_index
,
1839 patch_mode
, include_untracked
, only_staged
);
1841 strbuf_release(&stash_msg_buf
);
1845 int cmd_stash(int argc
, const char **argv
, const char *prefix
)
1847 pid_t pid
= getpid();
1848 const char *index_file
;
1849 struct strvec args
= STRVEC_INIT
;
1850 parse_opt_subcommand_fn
*fn
= NULL
;
1851 struct option options
[] = {
1852 OPT_SUBCOMMAND("apply", &fn
, apply_stash
),
1853 OPT_SUBCOMMAND("clear", &fn
, clear_stash
),
1854 OPT_SUBCOMMAND("drop", &fn
, drop_stash
),
1855 OPT_SUBCOMMAND("pop", &fn
, pop_stash
),
1856 OPT_SUBCOMMAND("branch", &fn
, branch_stash
),
1857 OPT_SUBCOMMAND("list", &fn
, list_stash
),
1858 OPT_SUBCOMMAND("show", &fn
, show_stash
),
1859 OPT_SUBCOMMAND("store", &fn
, store_stash
),
1860 OPT_SUBCOMMAND("create", &fn
, create_stash
),
1861 OPT_SUBCOMMAND("push", &fn
, push_stash_unassumed
),
1862 OPT_SUBCOMMAND_F("save", &fn
, save_stash
, PARSE_OPT_NOCOMPLETE
),
1866 git_config(git_stash_config
, NULL
);
1868 argc
= parse_options(argc
, argv
, prefix
, options
, git_stash_usage
,
1869 PARSE_OPT_SUBCOMMAND_OPTIONAL
|
1870 PARSE_OPT_KEEP_UNKNOWN_OPT
|
1871 PARSE_OPT_KEEP_DASHDASH
);
1873 prepare_repo_settings(the_repository
);
1874 the_repository
->settings
.command_requires_full_index
= 0;
1876 index_file
= get_index_file();
1877 strbuf_addf(&stash_index_path
, "%s.stash.%" PRIuMAX
, index_file
,
1881 return !!fn(argc
, argv
, prefix
);
1883 return !!push_stash_unassumed(0, NULL
, prefix
);
1885 /* Assume 'stash push' */
1886 strvec_push(&args
, "push");
1887 strvec_pushv(&args
, argv
);
1888 return !!push_stash(args
.nr
, args
.v
, prefix
, 1);