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_ui_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
);
980 strvec_clear(&revision_args
);
981 strvec_clear(&stash_args
);
982 free_stash_info(&info
);
983 release_revisions(&rev
);
985 usage_with_options(git_stash_show_usage
, options
);
992 static int do_store_stash(const struct object_id
*w_commit
, const char *stash_msg
,
995 struct stash_info info
;
996 char revision
[GIT_MAX_HEXSZ
];
998 oid_to_hex_r(revision
, w_commit
);
999 assert_stash_like(&info
, revision
);
1002 stash_msg
= "Created via \"git stash store\".";
1004 if (refs_update_ref(get_main_ref_store(the_repository
), stash_msg
, ref_stash
, w_commit
, NULL
,
1005 REF_FORCE_CREATE_REFLOG
,
1006 quiet
? UPDATE_REFS_QUIET_ON_ERR
:
1007 UPDATE_REFS_MSG_ON_ERR
)) {
1009 fprintf_ln(stderr
, _("Cannot update %s with %s"),
1010 ref_stash
, oid_to_hex(w_commit
));
1018 static int store_stash(int argc
, const char **argv
, const char *prefix
)
1021 const char *stash_msg
= NULL
;
1022 struct object_id obj
;
1023 struct object_context dummy
= {0};
1024 struct option options
[] = {
1025 OPT__QUIET(&quiet
, N_("be quiet")),
1026 OPT_STRING('m', "message", &stash_msg
, "message",
1027 N_("stash message")),
1032 argc
= parse_options(argc
, argv
, prefix
, options
,
1033 git_stash_store_usage
,
1034 PARSE_OPT_KEEP_UNKNOWN_OPT
);
1038 fprintf_ln(stderr
, _("\"git stash store\" requires one "
1039 "<commit> argument"));
1043 if (get_oid_with_context(the_repository
,
1044 argv
[0], quiet
? GET_OID_QUIETLY
: 0, &obj
,
1047 fprintf_ln(stderr
, _("Cannot update %s with %s"),
1048 ref_stash
, argv
[0]);
1053 ret
= do_store_stash(&obj
, stash_msg
, quiet
);
1056 object_context_release(&dummy
);
1060 static void add_pathspecs(struct strvec
*args
,
1061 const struct pathspec
*ps
) {
1064 for (i
= 0; i
< ps
->nr
; i
++)
1065 strvec_push(args
, ps
->items
[i
].original
);
1069 * `untracked_files` will be filled with the names of untracked files.
1070 * The return value is:
1072 * = 0 if there are not any untracked files
1073 * > 0 if there are untracked files
1075 static int get_untracked_files(const struct pathspec
*ps
, int include_untracked
,
1076 struct strbuf
*untracked_files
)
1080 struct dir_struct dir
= DIR_INIT
;
1082 if (include_untracked
!= INCLUDE_ALL_FILES
)
1083 setup_standard_excludes(&dir
);
1085 fill_directory(&dir
, the_repository
->index
, ps
);
1086 for (i
= 0; i
< dir
.nr
; i
++) {
1087 struct dir_entry
*ent
= dir
.entries
[i
];
1089 strbuf_addstr(untracked_files
, ent
->name
);
1090 /* NUL-terminate: will be fed to update-index -z */
1091 strbuf_addch(untracked_files
, '\0');
1099 * The return value of `check_changes_tracked_files()` can be:
1101 * < 0 if there was an error
1102 * = 0 if there are no changes.
1103 * > 0 if there are changes.
1105 static int check_changes_tracked_files(const struct pathspec
*ps
)
1107 struct rev_info rev
;
1108 struct object_id dummy
;
1111 /* No initial commit. */
1112 if (repo_get_oid(the_repository
, "HEAD", &dummy
))
1115 if (repo_read_index(the_repository
) < 0)
1118 repo_init_revisions(the_repository
, &rev
, NULL
);
1119 copy_pathspec(&rev
.prune_data
, ps
);
1121 rev
.diffopt
.flags
.quick
= 1;
1122 rev
.diffopt
.flags
.ignore_submodules
= 1;
1125 add_head_to_pending(&rev
);
1126 diff_setup_done(&rev
.diffopt
);
1128 run_diff_index(&rev
, DIFF_INDEX_CACHED
);
1129 if (diff_result_code(&rev
.diffopt
)) {
1134 run_diff_files(&rev
, 0);
1135 if (diff_result_code(&rev
.diffopt
)) {
1141 release_revisions(&rev
);
1146 * The function will fill `untracked_files` with the names of untracked files
1147 * It will return 1 if there were any changes and 0 if there were not.
1149 static int check_changes(const struct pathspec
*ps
, int include_untracked
,
1150 struct strbuf
*untracked_files
)
1153 if (check_changes_tracked_files(ps
))
1156 if (include_untracked
&& get_untracked_files(ps
, include_untracked
,
1163 static int save_untracked_files(struct stash_info
*info
, struct strbuf
*msg
,
1164 struct strbuf files
)
1167 struct strbuf untracked_msg
= STRBUF_INIT
;
1168 struct child_process cp_upd_index
= CHILD_PROCESS_INIT
;
1169 struct index_state istate
= INDEX_STATE_INIT(the_repository
);
1171 cp_upd_index
.git_cmd
= 1;
1172 strvec_pushl(&cp_upd_index
.args
, "update-index", "-z", "--add",
1173 "--remove", "--stdin", NULL
);
1174 strvec_pushf(&cp_upd_index
.env
, "GIT_INDEX_FILE=%s",
1175 stash_index_path
.buf
);
1177 strbuf_addf(&untracked_msg
, "untracked files on %s\n", msg
->buf
);
1178 if (pipe_command(&cp_upd_index
, files
.buf
, files
.len
, NULL
, 0,
1184 if (write_index_as_tree(&info
->u_tree
, &istate
, stash_index_path
.buf
, 0,
1190 if (commit_tree(untracked_msg
.buf
, untracked_msg
.len
,
1191 &info
->u_tree
, NULL
, &info
->u_commit
, NULL
, NULL
)) {
1197 release_index(&istate
);
1198 strbuf_release(&untracked_msg
);
1199 remove_path(stash_index_path
.buf
);
1203 static int stash_staged(struct stash_info
*info
, struct strbuf
*out_patch
,
1207 struct child_process cp_diff_tree
= CHILD_PROCESS_INIT
;
1208 struct index_state istate
= INDEX_STATE_INIT(the_repository
);
1210 if (write_index_as_tree(&info
->w_tree
, &istate
, the_repository
->index_file
,
1216 cp_diff_tree
.git_cmd
= 1;
1217 strvec_pushl(&cp_diff_tree
.args
, "diff-tree", "-p", "--binary",
1218 "-U1", "HEAD", oid_to_hex(&info
->w_tree
), "--", NULL
);
1219 if (pipe_command(&cp_diff_tree
, NULL
, 0, out_patch
, 0, NULL
, 0)) {
1224 if (!out_patch
->len
) {
1226 fprintf_ln(stderr
, _("No staged changes"));
1231 release_index(&istate
);
1235 static int stash_patch(struct stash_info
*info
, const struct pathspec
*ps
,
1236 struct strbuf
*out_patch
, int quiet
)
1239 struct child_process cp_read_tree
= CHILD_PROCESS_INIT
;
1240 struct child_process cp_diff_tree
= CHILD_PROCESS_INIT
;
1241 struct index_state istate
= INDEX_STATE_INIT(the_repository
);
1242 char *old_index_env
= NULL
, *old_repo_index_file
;
1244 remove_path(stash_index_path
.buf
);
1246 cp_read_tree
.git_cmd
= 1;
1247 strvec_pushl(&cp_read_tree
.args
, "read-tree", "HEAD", NULL
);
1248 strvec_pushf(&cp_read_tree
.env
, "GIT_INDEX_FILE=%s",
1249 stash_index_path
.buf
);
1250 if (run_command(&cp_read_tree
)) {
1255 /* Find out what the user wants. */
1256 old_repo_index_file
= the_repository
->index_file
;
1257 the_repository
->index_file
= stash_index_path
.buf
;
1258 old_index_env
= xstrdup_or_null(getenv(INDEX_ENVIRONMENT
));
1259 setenv(INDEX_ENVIRONMENT
, the_repository
->index_file
, 1);
1261 ret
= !!run_add_p(the_repository
, ADD_P_STASH
, NULL
, ps
);
1263 the_repository
->index_file
= old_repo_index_file
;
1264 if (old_index_env
&& *old_index_env
)
1265 setenv(INDEX_ENVIRONMENT
, old_index_env
, 1);
1267 unsetenv(INDEX_ENVIRONMENT
);
1268 FREE_AND_NULL(old_index_env
);
1270 /* State of the working tree. */
1271 if (write_index_as_tree(&info
->w_tree
, &istate
, stash_index_path
.buf
, 0,
1277 cp_diff_tree
.git_cmd
= 1;
1278 strvec_pushl(&cp_diff_tree
.args
, "diff-tree", "-p", "-U1", "HEAD",
1279 oid_to_hex(&info
->w_tree
), "--", NULL
);
1280 if (pipe_command(&cp_diff_tree
, NULL
, 0, out_patch
, 0, NULL
, 0)) {
1285 if (!out_patch
->len
) {
1287 fprintf_ln(stderr
, _("No changes selected"));
1292 release_index(&istate
);
1293 remove_path(stash_index_path
.buf
);
1297 static int stash_working_tree(struct stash_info
*info
, const struct pathspec
*ps
)
1300 struct rev_info rev
;
1301 struct child_process cp_upd_index
= CHILD_PROCESS_INIT
;
1302 struct strbuf diff_output
= STRBUF_INIT
;
1303 struct index_state istate
= INDEX_STATE_INIT(the_repository
);
1305 repo_init_revisions(the_repository
, &rev
, NULL
);
1306 copy_pathspec(&rev
.prune_data
, ps
);
1308 set_alternate_index_output(stash_index_path
.buf
);
1309 if (reset_tree(&info
->i_tree
, 0, 0)) {
1313 set_alternate_index_output(NULL
);
1315 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
1316 rev
.diffopt
.format_callback
= add_diff_to_buf
;
1317 rev
.diffopt
.format_callback_data
= &diff_output
;
1319 if (repo_read_index_preload(the_repository
, &rev
.diffopt
.pathspec
, 0) < 0) {
1324 add_pending_object(&rev
, parse_object(the_repository
, &info
->b_commit
),
1326 run_diff_index(&rev
, 0);
1328 cp_upd_index
.git_cmd
= 1;
1329 strvec_pushl(&cp_upd_index
.args
, "update-index",
1330 "--ignore-skip-worktree-entries",
1331 "-z", "--add", "--remove", "--stdin", NULL
);
1332 strvec_pushf(&cp_upd_index
.env
, "GIT_INDEX_FILE=%s",
1333 stash_index_path
.buf
);
1335 if (pipe_command(&cp_upd_index
, diff_output
.buf
, diff_output
.len
,
1336 NULL
, 0, NULL
, 0)) {
1341 if (write_index_as_tree(&info
->w_tree
, &istate
, stash_index_path
.buf
, 0,
1348 release_index(&istate
);
1349 release_revisions(&rev
);
1350 strbuf_release(&diff_output
);
1351 remove_path(stash_index_path
.buf
);
1355 static int do_create_stash(const struct pathspec
*ps
, struct strbuf
*stash_msg_buf
,
1356 int include_untracked
, int patch_mode
, int only_staged
,
1357 struct stash_info
*info
, struct strbuf
*patch
,
1362 int untracked_commit_option
= 0;
1363 const char *head_short_sha1
= NULL
;
1364 const char *branch_ref
= NULL
;
1365 const char *branch_name
= "(no branch)";
1366 struct commit
*head_commit
= NULL
;
1367 struct commit_list
*parents
= NULL
;
1368 struct strbuf msg
= STRBUF_INIT
;
1369 struct strbuf commit_tree_label
= STRBUF_INIT
;
1370 struct strbuf untracked_files
= STRBUF_INIT
;
1372 prepare_fallback_ident("git stash", "git@stash");
1374 repo_read_index_preload(the_repository
, NULL
, 0);
1375 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
, 0, 0,
1376 NULL
, NULL
, NULL
) < 0) {
1377 ret
= error(_("could not write index"));
1381 if (repo_get_oid(the_repository
, "HEAD", &info
->b_commit
)) {
1383 fprintf_ln(stderr
, _("You do not have "
1384 "the initial commit yet"));
1388 head_commit
= lookup_commit(the_repository
, &info
->b_commit
);
1391 if (!check_changes(ps
, include_untracked
, &untracked_files
)) {
1396 branch_ref
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
1397 "HEAD", 0, NULL
, &flags
);
1398 if (flags
& REF_ISSYMREF
)
1399 skip_prefix(branch_ref
, "refs/heads/", &branch_name
);
1400 head_short_sha1
= repo_find_unique_abbrev(the_repository
,
1401 &head_commit
->object
.oid
,
1403 strbuf_addf(&msg
, "%s: %s ", branch_name
, head_short_sha1
);
1404 pp_commit_easy(CMIT_FMT_ONELINE
, head_commit
, &msg
);
1406 strbuf_addf(&commit_tree_label
, "index on %s\n", msg
.buf
);
1407 commit_list_insert(head_commit
, &parents
);
1408 if (write_index_as_tree(&info
->i_tree
, the_repository
->index
, get_index_file(), 0,
1410 commit_tree(commit_tree_label
.buf
, commit_tree_label
.len
,
1411 &info
->i_tree
, parents
, &info
->i_commit
, NULL
, NULL
)) {
1413 fprintf_ln(stderr
, _("Cannot save the current "
1419 free_commit_list(parents
);
1422 if (include_untracked
) {
1423 if (save_untracked_files(info
, &msg
, untracked_files
)) {
1425 fprintf_ln(stderr
, _("Cannot save "
1426 "the untracked files"));
1430 untracked_commit_option
= 1;
1433 ret
= stash_patch(info
, ps
, patch
, quiet
);
1436 fprintf_ln(stderr
, _("Cannot save the current "
1439 } else if (ret
> 0) {
1442 } else if (only_staged
) {
1443 ret
= stash_staged(info
, patch
, quiet
);
1446 fprintf_ln(stderr
, _("Cannot save the current "
1449 } else if (ret
> 0) {
1453 if (stash_working_tree(info
, ps
)) {
1455 fprintf_ln(stderr
, _("Cannot save the current "
1462 if (!stash_msg_buf
->len
)
1463 strbuf_addf(stash_msg_buf
, "WIP on %s", msg
.buf
);
1465 strbuf_insertf(stash_msg_buf
, 0, "On %s: ", branch_name
);
1467 if (untracked_commit_option
)
1468 commit_list_insert(lookup_commit(the_repository
,
1471 commit_list_insert(lookup_commit(the_repository
, &info
->i_commit
),
1473 commit_list_insert(head_commit
, &parents
);
1475 if (commit_tree(stash_msg_buf
->buf
, stash_msg_buf
->len
, &info
->w_tree
,
1476 parents
, &info
->w_commit
, NULL
, NULL
)) {
1478 fprintf_ln(stderr
, _("Cannot record "
1479 "working tree state"));
1485 strbuf_release(&commit_tree_label
);
1486 strbuf_release(&msg
);
1487 strbuf_release(&untracked_files
);
1488 free_commit_list(parents
);
1492 static int create_stash(int argc
, const char **argv
, const char *prefix UNUSED
)
1495 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1496 struct stash_info info
= STASH_INFO_INIT
;
1499 /* Starting with argv[1], since argv[0] is "create" */
1500 strbuf_join_argv(&stash_msg_buf
, argc
- 1, ++argv
, ' ');
1502 memset(&ps
, 0, sizeof(ps
));
1503 if (!check_changes_tracked_files(&ps
))
1506 ret
= do_create_stash(&ps
, &stash_msg_buf
, 0, 0, 0, &info
,
1509 printf_ln("%s", oid_to_hex(&info
.w_commit
));
1511 free_stash_info(&info
);
1512 strbuf_release(&stash_msg_buf
);
1516 static int do_push_stash(const struct pathspec
*ps
, const char *stash_msg
, int quiet
,
1517 int keep_index
, int patch_mode
, int include_untracked
, int only_staged
)
1520 struct stash_info info
= STASH_INFO_INIT
;
1521 struct strbuf patch
= STRBUF_INIT
;
1522 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1523 struct strbuf untracked_files
= STRBUF_INIT
;
1524 struct strbuf out
= STRBUF_INIT
;
1526 if (patch_mode
&& keep_index
== -1)
1529 if (patch_mode
&& include_untracked
) {
1530 fprintf_ln(stderr
, _("Can't use --patch and --include-untracked"
1531 " or --all at the same time"));
1536 /* --patch overrides --staged */
1540 if (only_staged
&& include_untracked
) {
1541 fprintf_ln(stderr
, _("Can't use --staged and --include-untracked"
1542 " or --all at the same time"));
1547 repo_read_index_preload(the_repository
, NULL
, 0);
1548 if (!include_untracked
&& ps
->nr
) {
1550 char *ps_matched
= xcalloc(ps
->nr
, 1);
1552 /* TODO: audit for interaction with sparse-index. */
1553 ensure_full_index(the_repository
->index
);
1554 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++)
1555 ce_path_match(the_repository
->index
, the_repository
->index
->cache
[i
], ps
,
1558 if (report_path_error(ps_matched
, ps
)) {
1559 fprintf_ln(stderr
, _("Did you forget to 'git add'?"));
1567 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
, 0, 0,
1568 NULL
, NULL
, NULL
)) {
1569 ret
= error(_("could not write index"));
1573 if (!check_changes(ps
, include_untracked
, &untracked_files
)) {
1575 printf_ln(_("No local changes to save"));
1579 if (!refs_reflog_exists(get_main_ref_store(the_repository
), ref_stash
) && do_clear_stash()) {
1582 fprintf_ln(stderr
, _("Cannot initialize stash"));
1587 strbuf_addstr(&stash_msg_buf
, stash_msg
);
1588 if (do_create_stash(ps
, &stash_msg_buf
, include_untracked
, patch_mode
, only_staged
,
1589 &info
, &patch
, quiet
)) {
1594 if (do_store_stash(&info
.w_commit
, stash_msg_buf
.buf
, 1)) {
1597 fprintf_ln(stderr
, _("Cannot save the current status"));
1602 printf_ln(_("Saved working directory and index state %s"),
1605 if (!(patch_mode
|| only_staged
)) {
1606 if (include_untracked
&& !ps
->nr
) {
1607 struct child_process cp
= CHILD_PROCESS_INIT
;
1610 if (startup_info
->original_cwd
) {
1611 cp
.dir
= startup_info
->original_cwd
;
1612 strvec_pushf(&cp
.env
, "%s=%s",
1613 GIT_WORK_TREE_ENVIRONMENT
,
1614 the_repository
->worktree
);
1616 strvec_pushl(&cp
.args
, "clean", "--force",
1617 "--quiet", "-d", ":/", NULL
);
1618 if (include_untracked
== INCLUDE_ALL_FILES
)
1619 strvec_push(&cp
.args
, "-x");
1620 if (run_command(&cp
)) {
1625 discard_index(the_repository
->index
);
1627 struct child_process cp_add
= CHILD_PROCESS_INIT
;
1628 struct child_process cp_diff
= CHILD_PROCESS_INIT
;
1629 struct child_process cp_apply
= CHILD_PROCESS_INIT
;
1632 strvec_push(&cp_add
.args
, "add");
1633 if (!include_untracked
)
1634 strvec_push(&cp_add
.args
, "-u");
1635 if (include_untracked
== INCLUDE_ALL_FILES
)
1636 strvec_push(&cp_add
.args
, "--force");
1637 strvec_push(&cp_add
.args
, "--");
1638 add_pathspecs(&cp_add
.args
, ps
);
1639 if (run_command(&cp_add
)) {
1644 cp_diff
.git_cmd
= 1;
1645 strvec_pushl(&cp_diff
.args
, "diff-index", "-p",
1646 "--cached", "--binary", "HEAD", "--",
1648 add_pathspecs(&cp_diff
.args
, ps
);
1649 if (pipe_command(&cp_diff
, NULL
, 0, &out
, 0, NULL
, 0)) {
1654 cp_apply
.git_cmd
= 1;
1655 strvec_pushl(&cp_apply
.args
, "apply", "--index",
1657 if (pipe_command(&cp_apply
, out
.buf
, out
.len
, NULL
, 0,
1663 struct child_process cp
= CHILD_PROCESS_INIT
;
1665 /* BUG: this nukes untracked files in the way */
1666 strvec_pushl(&cp
.args
, "reset", "--hard", "-q",
1667 "--no-recurse-submodules", NULL
);
1668 if (run_command(&cp
)) {
1674 if (keep_index
== 1 && !is_null_oid(&info
.i_tree
)) {
1675 struct child_process cp
= CHILD_PROCESS_INIT
;
1678 strvec_pushl(&cp
.args
, "checkout", "--no-overlay",
1679 oid_to_hex(&info
.i_tree
), "--", NULL
);
1681 strvec_push(&cp
.args
, ":/");
1683 add_pathspecs(&cp
.args
, ps
);
1684 if (run_command(&cp
)) {
1691 struct child_process cp
= CHILD_PROCESS_INIT
;
1694 strvec_pushl(&cp
.args
, "apply", "-R", NULL
);
1696 if (pipe_command(&cp
, patch
.buf
, patch
.len
, NULL
, 0, NULL
, 0)) {
1698 fprintf_ln(stderr
, _("Cannot remove "
1699 "worktree changes"));
1704 if (keep_index
< 1) {
1705 struct child_process cp
= CHILD_PROCESS_INIT
;
1708 strvec_pushl(&cp
.args
, "reset", "-q", "--refresh", "--",
1710 add_pathspecs(&cp
.args
, ps
);
1711 if (run_command(&cp
)) {
1720 strbuf_release(&patch
);
1721 strbuf_release(&out
);
1722 free_stash_info(&info
);
1723 strbuf_release(&stash_msg_buf
);
1724 strbuf_release(&untracked_files
);
1728 static int push_stash(int argc
, const char **argv
, const char *prefix
,
1731 int force_assume
= 0;
1732 int keep_index
= -1;
1733 int only_staged
= 0;
1735 int include_untracked
= 0;
1737 int pathspec_file_nul
= 0;
1738 const char *stash_msg
= NULL
;
1739 const char *pathspec_from_file
= NULL
;
1741 struct option options
[] = {
1742 OPT_BOOL('k', "keep-index", &keep_index
,
1744 OPT_BOOL('S', "staged", &only_staged
,
1745 N_("stash staged changes only")),
1746 OPT_BOOL('p', "patch", &patch_mode
,
1747 N_("stash in patch mode")),
1748 OPT__QUIET(&quiet
, N_("quiet mode")),
1749 OPT_BOOL('u', "include-untracked", &include_untracked
,
1750 N_("include untracked files in stash")),
1751 OPT_SET_INT('a', "all", &include_untracked
,
1752 N_("include ignore files"), 2),
1753 OPT_STRING('m', "message", &stash_msg
, N_("message"),
1754 N_("stash message")),
1755 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
1756 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
1762 force_assume
= !strcmp(argv
[0], "-p");
1763 argc
= parse_options(argc
, argv
, prefix
, options
,
1764 push_assumed
? git_stash_usage
:
1765 git_stash_push_usage
,
1766 PARSE_OPT_KEEP_DASHDASH
);
1770 if (!strcmp(argv
[0], "--")) {
1773 } else if (push_assumed
&& !force_assume
) {
1774 die("subcommand wasn't specified; 'push' can't be assumed due to unexpected token '%s'",
1779 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_FULL
| PATHSPEC_PREFIX_ORIGIN
,
1782 if (pathspec_from_file
) {
1784 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
1787 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--staged");
1790 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
1792 parse_pathspec_file(&ps
, 0,
1793 PATHSPEC_PREFER_FULL
| PATHSPEC_PREFIX_ORIGIN
,
1794 prefix
, pathspec_from_file
, pathspec_file_nul
);
1795 } else if (pathspec_file_nul
) {
1796 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
1799 ret
= do_push_stash(&ps
, stash_msg
, quiet
, keep_index
, patch_mode
,
1800 include_untracked
, only_staged
);
1801 clear_pathspec(&ps
);
1805 static int push_stash_unassumed(int argc
, const char **argv
, const char *prefix
)
1807 return push_stash(argc
, argv
, prefix
, 0);
1810 static int save_stash(int argc
, const char **argv
, const char *prefix
)
1812 int keep_index
= -1;
1813 int only_staged
= 0;
1815 int include_untracked
= 0;
1818 const char *stash_msg
= NULL
;
1820 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1821 struct option options
[] = {
1822 OPT_BOOL('k', "keep-index", &keep_index
,
1824 OPT_BOOL('S', "staged", &only_staged
,
1825 N_("stash staged changes only")),
1826 OPT_BOOL('p', "patch", &patch_mode
,
1827 N_("stash in patch mode")),
1828 OPT__QUIET(&quiet
, N_("quiet mode")),
1829 OPT_BOOL('u', "include-untracked", &include_untracked
,
1830 N_("include untracked files in stash")),
1831 OPT_SET_INT('a', "all", &include_untracked
,
1832 N_("include ignore files"), 2),
1833 OPT_STRING('m', "message", &stash_msg
, "message",
1834 N_("stash message")),
1838 argc
= parse_options(argc
, argv
, prefix
, options
,
1839 git_stash_save_usage
,
1840 PARSE_OPT_KEEP_DASHDASH
);
1843 stash_msg
= strbuf_join_argv(&stash_msg_buf
, argc
, argv
, ' ');
1845 memset(&ps
, 0, sizeof(ps
));
1846 ret
= do_push_stash(&ps
, stash_msg
, quiet
, keep_index
,
1847 patch_mode
, include_untracked
, only_staged
);
1849 strbuf_release(&stash_msg_buf
);
1853 int cmd_stash(int argc
, const char **argv
, const char *prefix
)
1855 pid_t pid
= getpid();
1856 const char *index_file
;
1857 struct strvec args
= STRVEC_INIT
;
1858 parse_opt_subcommand_fn
*fn
= NULL
;
1859 struct option options
[] = {
1860 OPT_SUBCOMMAND("apply", &fn
, apply_stash
),
1861 OPT_SUBCOMMAND("clear", &fn
, clear_stash
),
1862 OPT_SUBCOMMAND("drop", &fn
, drop_stash
),
1863 OPT_SUBCOMMAND("pop", &fn
, pop_stash
),
1864 OPT_SUBCOMMAND("branch", &fn
, branch_stash
),
1865 OPT_SUBCOMMAND("list", &fn
, list_stash
),
1866 OPT_SUBCOMMAND("show", &fn
, show_stash
),
1867 OPT_SUBCOMMAND("store", &fn
, store_stash
),
1868 OPT_SUBCOMMAND("create", &fn
, create_stash
),
1869 OPT_SUBCOMMAND("push", &fn
, push_stash_unassumed
),
1870 OPT_SUBCOMMAND_F("save", &fn
, save_stash
, PARSE_OPT_NOCOMPLETE
),
1873 const char **args_copy
;
1876 git_config(git_stash_config
, NULL
);
1878 argc
= parse_options(argc
, argv
, prefix
, options
, git_stash_usage
,
1879 PARSE_OPT_SUBCOMMAND_OPTIONAL
|
1880 PARSE_OPT_KEEP_UNKNOWN_OPT
|
1881 PARSE_OPT_KEEP_DASHDASH
);
1883 prepare_repo_settings(the_repository
);
1884 the_repository
->settings
.command_requires_full_index
= 0;
1886 index_file
= get_index_file();
1887 strbuf_addf(&stash_index_path
, "%s.stash.%" PRIuMAX
, index_file
,
1891 return !!fn(argc
, argv
, prefix
);
1893 return !!push_stash_unassumed(0, NULL
, prefix
);
1895 /* Assume 'stash push' */
1896 strvec_push(&args
, "push");
1897 strvec_pushv(&args
, argv
);
1900 * `push_stash()` ends up modifying the array, which causes memory
1901 * leaks if we didn't copy the array here.
1903 DUP_ARRAY(args_copy
, args
.v
, args
.nr
);
1905 ret
= !!push_stash(args
.nr
, args_copy
, prefix
, 1);
1907 strvec_clear(&args
);