1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
4 #include "parse-options.h"
7 #include "cache-tree.h"
8 #include "unpack-trees.h"
9 #include "merge-recursive.h"
11 #include "run-command.h"
19 #define INCLUDE_ALL_FILES 2
21 static const char * const git_stash_usage
[] = {
22 N_("git stash list [<options>]"),
23 N_("git stash show [<options>] [<stash>]"),
24 N_("git stash drop [-q|--quiet] [<stash>]"),
25 N_("git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
26 N_("git stash branch <branchname> [<stash>]"),
27 N_("git stash clear"),
28 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
29 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
30 " [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
31 " [--] [<pathspec>...]]"),
32 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
33 " [-u|--include-untracked] [-a|--all] [<message>]"),
37 static const char * const git_stash_list_usage
[] = {
38 N_("git stash list [<options>]"),
42 static const char * const git_stash_show_usage
[] = {
43 N_("git stash show [<options>] [<stash>]"),
47 static const char * const git_stash_drop_usage
[] = {
48 N_("git stash drop [-q|--quiet] [<stash>]"),
52 static const char * const git_stash_pop_usage
[] = {
53 N_("git stash pop [--index] [-q|--quiet] [<stash>]"),
57 static const char * const git_stash_apply_usage
[] = {
58 N_("git stash apply [--index] [-q|--quiet] [<stash>]"),
62 static const char * const git_stash_branch_usage
[] = {
63 N_("git stash branch <branchname> [<stash>]"),
67 static const char * const git_stash_clear_usage
[] = {
68 N_("git stash clear"),
72 static const char * const git_stash_store_usage
[] = {
73 N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"),
77 static const char * const git_stash_push_usage
[] = {
78 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
79 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
80 " [--] [<pathspec>...]]"),
84 static const char * const git_stash_save_usage
[] = {
85 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
86 " [-u|--include-untracked] [-a|--all] [<message>]"),
90 static const char *ref_stash
= "refs/stash";
91 static struct strbuf stash_index_path
= STRBUF_INIT
;
94 * w_commit is set to the commit containing the working tree
95 * b_commit is set to the base commit
96 * i_commit is set to the commit containing the index tree
97 * u_commit is set to the commit containing the untracked files tree
98 * w_tree is set to the working tree
99 * b_tree is set to the base tree
100 * i_tree is set to the index tree
101 * u_tree is set to the untracked files tree
104 struct object_id w_commit
;
105 struct object_id b_commit
;
106 struct object_id i_commit
;
107 struct object_id u_commit
;
108 struct object_id w_tree
;
109 struct object_id b_tree
;
110 struct object_id i_tree
;
111 struct object_id u_tree
;
112 struct strbuf revision
;
117 static void free_stash_info(struct stash_info
*info
)
119 strbuf_release(&info
->revision
);
122 static void assert_stash_like(struct stash_info
*info
, const char *revision
)
124 if (get_oidf(&info
->b_commit
, "%s^1", revision
) ||
125 get_oidf(&info
->w_tree
, "%s:", revision
) ||
126 get_oidf(&info
->b_tree
, "%s^1:", revision
) ||
127 get_oidf(&info
->i_tree
, "%s^2:", revision
))
128 die(_("'%s' is not a stash-like commit"), revision
);
131 static int get_stash_info(struct stash_info
*info
, int argc
, const char **argv
)
136 const char *revision
;
137 const char *commit
= NULL
;
138 struct object_id dummy
;
139 struct strbuf symbolic
= STRBUF_INIT
;
143 struct strbuf refs_msg
= STRBUF_INIT
;
145 for (i
= 0; i
< argc
; i
++)
146 strbuf_addf(&refs_msg
, " '%s'", argv
[i
]);
148 fprintf_ln(stderr
, _("Too many revisions specified:%s"),
150 strbuf_release(&refs_msg
);
158 strbuf_init(&info
->revision
, 0);
160 if (!ref_exists(ref_stash
)) {
161 free_stash_info(info
);
162 fprintf_ln(stderr
, _("No stash entries found."));
166 strbuf_addf(&info
->revision
, "%s@{0}", ref_stash
);
167 } else if (strspn(commit
, "0123456789") == strlen(commit
)) {
168 strbuf_addf(&info
->revision
, "%s@{%s}", ref_stash
, commit
);
170 strbuf_addstr(&info
->revision
, commit
);
173 revision
= info
->revision
.buf
;
175 if (get_oid(revision
, &info
->w_commit
)) {
176 error(_("%s is not a valid reference"), revision
);
177 free_stash_info(info
);
181 assert_stash_like(info
, revision
);
183 info
->has_u
= !get_oidf(&info
->u_tree
, "%s^3:", revision
);
185 end_of_rev
= strchrnul(revision
, '@');
186 strbuf_add(&symbolic
, revision
, end_of_rev
- revision
);
188 ret
= dwim_ref(symbolic
.buf
, symbolic
.len
, &dummy
, &expanded_ref
, 0);
189 strbuf_release(&symbolic
);
191 case 0: /* Not found, but valid ref */
192 info
->is_stash_ref
= 0;
195 info
->is_stash_ref
= !strcmp(expanded_ref
, ref_stash
);
197 default: /* Invalid or ambiguous */
198 free_stash_info(info
);
202 return !(ret
== 0 || ret
== 1);
205 static int do_clear_stash(void)
207 struct object_id obj
;
208 if (get_oid(ref_stash
, &obj
))
211 return delete_ref(NULL
, ref_stash
, &obj
, 0);
214 static int clear_stash(int argc
, const char **argv
, const char *prefix
)
216 struct option options
[] = {
220 argc
= parse_options(argc
, argv
, prefix
, options
,
221 git_stash_clear_usage
,
222 PARSE_OPT_STOP_AT_NON_OPTION
);
225 return error(_("git stash clear with parameters is "
228 return do_clear_stash();
231 static int reset_tree(struct object_id
*i_tree
, int update
, int reset
)
234 struct unpack_trees_options opts
;
235 struct tree_desc t
[MAX_UNPACK_TREES
];
237 struct lock_file lock_file
= LOCK_INIT
;
239 read_cache_preload(NULL
);
240 if (refresh_cache(REFRESH_QUIET
))
243 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
245 memset(&opts
, 0, sizeof(opts
));
247 tree
= parse_tree_indirect(i_tree
);
248 if (parse_tree(tree
))
251 init_tree_desc(t
, tree
->buffer
, tree
->size
);
254 opts
.src_index
= &the_index
;
255 opts
.dst_index
= &the_index
;
258 opts
.update
= update
;
259 opts
.fn
= oneway_merge
;
261 if (unpack_trees(nr_trees
, t
, &opts
))
264 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
265 return error(_("unable to write new index file"));
270 static int diff_tree_binary(struct strbuf
*out
, struct object_id
*w_commit
)
272 struct child_process cp
= CHILD_PROCESS_INIT
;
273 const char *w_commit_hex
= oid_to_hex(w_commit
);
276 * Diff-tree would not be very hard to replace with a native function,
277 * however it should be done together with apply_cached.
280 strvec_pushl(&cp
.args
, "diff-tree", "--binary", NULL
);
281 strvec_pushf(&cp
.args
, "%s^2^..%s^2", w_commit_hex
, w_commit_hex
);
283 return pipe_command(&cp
, NULL
, 0, out
, 0, NULL
, 0);
286 static int apply_cached(struct strbuf
*out
)
288 struct child_process cp
= CHILD_PROCESS_INIT
;
291 * Apply currently only reads either from stdin or a file, thus
292 * apply_all_patches would have to be updated to optionally take a
296 strvec_pushl(&cp
.args
, "apply", "--cached", NULL
);
297 return pipe_command(&cp
, out
->buf
, out
->len
, NULL
, 0, NULL
, 0);
300 static int reset_head(void)
302 struct child_process cp
= CHILD_PROCESS_INIT
;
305 * Reset is overall quite simple, however there is no current public
309 strvec_push(&cp
.args
, "reset");
311 return run_command(&cp
);
314 static void add_diff_to_buf(struct diff_queue_struct
*q
,
315 struct diff_options
*options
,
320 for (i
= 0; i
< q
->nr
; i
++) {
321 strbuf_addstr(data
, q
->queue
[i
]->one
->path
);
323 /* NUL-terminate: will be fed to update-index -z */
324 strbuf_addch(data
, '\0');
328 static int restore_untracked(struct object_id
*u_tree
)
331 struct child_process cp
= CHILD_PROCESS_INIT
;
334 * We need to run restore files from a given index, but without
335 * affecting the current index, so we use GIT_INDEX_FILE with
336 * run_command to fork processes that will not interfere.
339 strvec_push(&cp
.args
, "read-tree");
340 strvec_push(&cp
.args
, oid_to_hex(u_tree
));
341 strvec_pushf(&cp
.env_array
, "GIT_INDEX_FILE=%s",
342 stash_index_path
.buf
);
343 if (run_command(&cp
)) {
344 remove_path(stash_index_path
.buf
);
348 child_process_init(&cp
);
350 strvec_pushl(&cp
.args
, "checkout-index", "--all", NULL
);
351 strvec_pushf(&cp
.env_array
, "GIT_INDEX_FILE=%s",
352 stash_index_path
.buf
);
354 res
= run_command(&cp
);
355 remove_path(stash_index_path
.buf
);
359 static void unstage_changes_unless_new(struct object_id
*orig_tree
)
362 * When we enter this function, there has been a clean merge of
363 * relevant trees, and the merge logic always stages whatever merges
364 * cleanly. We want to unstage those changes, unless it corresponds
365 * to a file that didn't exist as of orig_tree.
367 * However, if any SKIP_WORKTREE path is modified relative to
368 * orig_tree, then we want to clear the SKIP_WORKTREE bit and write
369 * it to the worktree before unstaging.
372 struct checkout state
= CHECKOUT_INIT
;
373 struct diff_options diff_opts
;
374 struct lock_file lock
= LOCK_INIT
;
377 /* If any entries have skip_worktree set, we'll have to check 'em out */
380 state
.refresh_cache
= 1;
381 state
.istate
= &the_index
;
384 * Step 1: get a difference between orig_tree (which corresponding
385 * to the index before a merge was run) and the current index
386 * (reflecting the changes brought in by the merge).
388 diff_setup(&diff_opts
);
389 diff_opts
.flags
.recursive
= 1;
390 diff_opts
.detect_rename
= 0;
391 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
392 diff_setup_done(&diff_opts
);
394 do_diff_cache(orig_tree
, &diff_opts
);
395 diffcore_std(&diff_opts
);
397 /* Iterate over the paths that changed due to the merge... */
398 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
399 struct diff_filepair
*p
;
400 struct cache_entry
*ce
;
403 /* Look up the path's position in the current index. */
404 p
= diff_queued_diff
.queue
[i
];
405 pos
= index_name_pos(&the_index
, p
->two
->path
,
406 strlen(p
->two
->path
));
409 * Step 2: Place changes in the working tree
411 * Stash is about restoring changes *to the working tree*.
412 * So if the merge successfully got a new version of some
413 * path, but left it out of the working tree, then clear the
414 * SKIP_WORKTREE bit and write it to the working tree.
416 if (pos
>= 0 && ce_skip_worktree(active_cache
[pos
])) {
419 ce
= active_cache
[pos
];
420 if (!lstat(ce
->name
, &st
)) {
421 /* Conflicting path present; relocate it */
422 struct strbuf new_path
= STRBUF_INIT
;
425 strbuf_addf(&new_path
,
426 "%s.stash.XXXXXX", ce
->name
);
427 fd
= xmkstemp(new_path
.buf
);
429 printf(_("WARNING: Untracked file in way of "
430 "tracked file! Renaming\n "
433 ce
->name
, new_path
.buf
);
434 if (rename(ce
->name
, new_path
.buf
))
435 die("Failed to move %s to %s\n",
436 ce
->name
, new_path
.buf
);
437 strbuf_release(&new_path
);
439 checkout_entry(ce
, &state
, NULL
, NULL
);
440 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
444 * Step 3: "unstage" changes, as long as they are still tracked
446 if (p
->one
->oid_valid
) {
448 * Path existed in orig_tree; restore index entry
449 * from that tree in order to "unstage" the changes.
451 int option
= ADD_CACHE_OK_TO_REPLACE
;
453 option
= ADD_CACHE_OK_TO_ADD
;
455 ce
= make_cache_entry(&the_index
,
460 add_index_entry(&the_index
, ce
, option
);
463 diff_flush(&diff_opts
);
466 * Step 4: write the new index to disk
468 repo_hold_locked_index(the_repository
, &lock
, LOCK_DIE_ON_ERROR
);
469 if (write_locked_index(&the_index
, &lock
,
470 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
471 die(_("Unable to write index."));
474 static int do_apply_stash(const char *prefix
, struct stash_info
*info
,
475 int index
, int quiet
)
478 int has_index
= index
;
479 struct merge_options o
;
480 struct object_id c_tree
;
481 struct object_id index_tree
;
482 struct commit
*result
;
483 const struct object_id
*bases
[1];
485 read_cache_preload(NULL
);
486 if (refresh_and_write_cache(REFRESH_QUIET
, 0, 0))
489 if (write_cache_as_tree(&c_tree
, 0, NULL
))
490 return error(_("cannot apply a stash in the middle of a merge"));
493 if (oideq(&info
->b_tree
, &info
->i_tree
) ||
494 oideq(&c_tree
, &info
->i_tree
)) {
497 struct strbuf out
= STRBUF_INIT
;
499 if (diff_tree_binary(&out
, &info
->w_commit
)) {
500 strbuf_release(&out
);
501 return error(_("could not generate diff %s^!."),
502 oid_to_hex(&info
->w_commit
));
505 ret
= apply_cached(&out
);
506 strbuf_release(&out
);
508 return error(_("conflicts in index. "
509 "Try without --index."));
513 if (write_cache_as_tree(&index_tree
, 0, NULL
))
514 return error(_("could not save index tree"));
522 if (info
->has_u
&& restore_untracked(&info
->u_tree
))
523 return error(_("could not restore untracked files from stash"));
525 init_merge_options(&o
, the_repository
);
527 o
.branch1
= "Updated upstream";
528 o
.branch2
= "Stashed changes";
530 if (oideq(&info
->b_tree
, &c_tree
))
531 o
.branch1
= "Version stash was based on";
536 if (o
.verbosity
>= 3)
537 printf_ln(_("Merging %s with %s"), o
.branch1
, o
.branch2
);
539 bases
[0] = &info
->b_tree
;
541 ret
= merge_recursive_generic(&o
, &c_tree
, &info
->w_tree
, 1, bases
,
547 fprintf_ln(stderr
, _("Index was not unstashed."));
553 if (reset_tree(&index_tree
, 0, 0))
556 unstage_changes_unless_new(&c_tree
);
560 struct child_process cp
= CHILD_PROCESS_INIT
;
563 * Status is quite simple and could be replaced with calls to
564 * wt_status in the future, but it adds complexities which may
565 * require more tests.
569 strvec_pushf(&cp
.env_array
, GIT_WORK_TREE_ENVIRONMENT
"=%s",
570 absolute_path(get_git_work_tree()));
571 strvec_pushf(&cp
.env_array
, GIT_DIR_ENVIRONMENT
"=%s",
572 absolute_path(get_git_dir()));
573 strvec_push(&cp
.args
, "status");
580 static int apply_stash(int argc
, const char **argv
, const char *prefix
)
585 struct stash_info info
;
586 struct option options
[] = {
587 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
588 OPT_BOOL(0, "index", &index
,
589 N_("attempt to recreate the index")),
593 argc
= parse_options(argc
, argv
, prefix
, options
,
594 git_stash_apply_usage
, 0);
596 if (get_stash_info(&info
, argc
, argv
))
599 ret
= do_apply_stash(prefix
, &info
, index
, quiet
);
600 free_stash_info(&info
);
604 static int reject_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
605 const char *email
, timestamp_t timestamp
, int tz
,
606 const char *message
, void *cb_data
)
611 static int reflog_is_empty(const char *refname
)
613 return !for_each_reflog_ent(refname
, reject_reflog_ent
, NULL
);
616 static int do_drop_stash(struct stash_info
*info
, int quiet
)
619 struct child_process cp_reflog
= CHILD_PROCESS_INIT
;
622 * reflog does not provide a simple function for deleting refs. One will
623 * need to be added to avoid implementing too much reflog code here
626 cp_reflog
.git_cmd
= 1;
627 strvec_pushl(&cp_reflog
.args
, "reflog", "delete", "--updateref",
629 strvec_push(&cp_reflog
.args
, info
->revision
.buf
);
630 ret
= run_command(&cp_reflog
);
633 printf_ln(_("Dropped %s (%s)"), info
->revision
.buf
,
634 oid_to_hex(&info
->w_commit
));
636 return error(_("%s: Could not drop stash entry"),
640 if (reflog_is_empty(ref_stash
))
646 static void assert_stash_ref(struct stash_info
*info
)
648 if (!info
->is_stash_ref
) {
649 error(_("'%s' is not a stash reference"), info
->revision
.buf
);
650 free_stash_info(info
);
655 static int drop_stash(int argc
, const char **argv
, const char *prefix
)
659 struct stash_info info
;
660 struct option options
[] = {
661 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
665 argc
= parse_options(argc
, argv
, prefix
, options
,
666 git_stash_drop_usage
, 0);
668 if (get_stash_info(&info
, argc
, argv
))
671 assert_stash_ref(&info
);
673 ret
= do_drop_stash(&info
, quiet
);
674 free_stash_info(&info
);
678 static int pop_stash(int argc
, const char **argv
, const char *prefix
)
683 struct stash_info info
;
684 struct option options
[] = {
685 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
686 OPT_BOOL(0, "index", &index
,
687 N_("attempt to recreate the index")),
691 argc
= parse_options(argc
, argv
, prefix
, options
,
692 git_stash_pop_usage
, 0);
694 if (get_stash_info(&info
, argc
, argv
))
697 assert_stash_ref(&info
);
698 if ((ret
= do_apply_stash(prefix
, &info
, index
, quiet
)))
699 printf_ln(_("The stash entry is kept in case "
700 "you need it again."));
702 ret
= do_drop_stash(&info
, quiet
);
704 free_stash_info(&info
);
708 static int branch_stash(int argc
, const char **argv
, const char *prefix
)
711 const char *branch
= NULL
;
712 struct stash_info info
;
713 struct child_process cp
= CHILD_PROCESS_INIT
;
714 struct option options
[] = {
718 argc
= parse_options(argc
, argv
, prefix
, options
,
719 git_stash_branch_usage
, 0);
722 fprintf_ln(stderr
, _("No branch name specified"));
728 if (get_stash_info(&info
, argc
- 1, argv
+ 1))
732 strvec_pushl(&cp
.args
, "checkout", "-b", NULL
);
733 strvec_push(&cp
.args
, branch
);
734 strvec_push(&cp
.args
, oid_to_hex(&info
.b_commit
));
735 ret
= run_command(&cp
);
737 ret
= do_apply_stash(prefix
, &info
, 1, 0);
738 if (!ret
&& info
.is_stash_ref
)
739 ret
= do_drop_stash(&info
, 0);
741 free_stash_info(&info
);
746 static int list_stash(int argc
, const char **argv
, const char *prefix
)
748 struct child_process cp
= CHILD_PROCESS_INIT
;
749 struct option options
[] = {
753 argc
= parse_options(argc
, argv
, prefix
, options
,
754 git_stash_list_usage
,
755 PARSE_OPT_KEEP_UNKNOWN
);
757 if (!ref_exists(ref_stash
))
761 strvec_pushl(&cp
.args
, "log", "--format=%gd: %gs", "-g",
762 "--first-parent", "-m", NULL
);
763 strvec_pushv(&cp
.args
, argv
);
764 strvec_push(&cp
.args
, ref_stash
);
765 strvec_push(&cp
.args
, "--");
766 return run_command(&cp
);
769 static int show_stat
= 1;
770 static int show_patch
;
771 static int use_legacy_stash
;
773 static int git_stash_config(const char *var
, const char *value
, void *cb
)
775 if (!strcmp(var
, "stash.showstat")) {
776 show_stat
= git_config_bool(var
, value
);
779 if (!strcmp(var
, "stash.showpatch")) {
780 show_patch
= git_config_bool(var
, value
);
783 if (!strcmp(var
, "stash.usebuiltin")) {
784 use_legacy_stash
= !git_config_bool(var
, value
);
787 return git_diff_basic_config(var
, value
, cb
);
790 static int show_stash(int argc
, const char **argv
, const char *prefix
)
794 struct stash_info info
;
796 struct strvec stash_args
= STRVEC_INIT
;
797 struct strvec revision_args
= STRVEC_INIT
;
798 struct option options
[] = {
802 init_diff_ui_defaults();
803 git_config(git_diff_ui_config
, NULL
);
804 init_revisions(&rev
, prefix
);
806 strvec_push(&revision_args
, argv
[0]);
807 for (i
= 1; i
< argc
; i
++) {
808 if (argv
[i
][0] != '-')
809 strvec_push(&stash_args
, argv
[i
]);
811 strvec_push(&revision_args
, argv
[i
]);
814 ret
= get_stash_info(&info
, stash_args
.nr
, stash_args
.v
);
815 strvec_clear(&stash_args
);
820 * The config settings are applied only if there are not passed
823 if (revision_args
.nr
== 1) {
825 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
;
828 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
830 if (!show_stat
&& !show_patch
) {
831 free_stash_info(&info
);
836 argc
= setup_revisions(revision_args
.nr
, revision_args
.v
, &rev
, NULL
);
838 free_stash_info(&info
);
839 usage_with_options(git_stash_show_usage
, options
);
841 if (!rev
.diffopt
.output_format
) {
842 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
843 diff_setup_done(&rev
.diffopt
);
846 rev
.diffopt
.flags
.recursive
= 1;
847 setup_diff_pager(&rev
.diffopt
);
848 diff_tree_oid(&info
.b_commit
, &info
.w_commit
, "", &rev
.diffopt
);
849 log_tree_diff_flush(&rev
);
851 free_stash_info(&info
);
852 return diff_result_code(&rev
.diffopt
, 0);
855 static int do_store_stash(const struct object_id
*w_commit
, const char *stash_msg
,
859 stash_msg
= "Created via \"git stash store\".";
861 if (update_ref(stash_msg
, ref_stash
, w_commit
, NULL
,
862 REF_FORCE_CREATE_REFLOG
,
863 quiet
? UPDATE_REFS_QUIET_ON_ERR
:
864 UPDATE_REFS_MSG_ON_ERR
)) {
866 fprintf_ln(stderr
, _("Cannot update %s with %s"),
867 ref_stash
, oid_to_hex(w_commit
));
875 static int store_stash(int argc
, const char **argv
, const char *prefix
)
878 const char *stash_msg
= NULL
;
879 struct object_id obj
;
880 struct object_context dummy
;
881 struct option options
[] = {
882 OPT__QUIET(&quiet
, N_("be quiet")),
883 OPT_STRING('m', "message", &stash_msg
, "message",
884 N_("stash message")),
888 argc
= parse_options(argc
, argv
, prefix
, options
,
889 git_stash_store_usage
,
890 PARSE_OPT_KEEP_UNKNOWN
);
894 fprintf_ln(stderr
, _("\"git stash store\" requires one "
895 "<commit> argument"));
899 if (get_oid_with_context(the_repository
,
900 argv
[0], quiet
? GET_OID_QUIETLY
: 0, &obj
,
903 fprintf_ln(stderr
, _("Cannot update %s with %s"),
908 return do_store_stash(&obj
, stash_msg
, quiet
);
911 static void add_pathspecs(struct strvec
*args
,
912 const struct pathspec
*ps
) {
915 for (i
= 0; i
< ps
->nr
; i
++)
916 strvec_push(args
, ps
->items
[i
].original
);
920 * `untracked_files` will be filled with the names of untracked files.
921 * The return value is:
923 * = 0 if there are not any untracked files
924 * > 0 if there are untracked files
926 static int get_untracked_files(const struct pathspec
*ps
, int include_untracked
,
927 struct strbuf
*untracked_files
)
931 struct dir_struct dir
;
934 if (include_untracked
!= INCLUDE_ALL_FILES
)
935 setup_standard_excludes(&dir
);
937 fill_directory(&dir
, the_repository
->index
, ps
);
938 for (i
= 0; i
< dir
.nr
; i
++) {
939 struct dir_entry
*ent
= dir
.entries
[i
];
941 strbuf_addstr(untracked_files
, ent
->name
);
942 /* NUL-terminate: will be fed to update-index -z */
943 strbuf_addch(untracked_files
, '\0');
951 * The return value of `check_changes_tracked_files()` can be:
953 * < 0 if there was an error
954 * = 0 if there are no changes.
955 * > 0 if there are changes.
957 static int check_changes_tracked_files(const struct pathspec
*ps
)
961 struct object_id dummy
;
964 /* No initial commit. */
965 if (get_oid("HEAD", &dummy
))
968 if (read_cache() < 0)
971 init_revisions(&rev
, NULL
);
972 copy_pathspec(&rev
.prune_data
, ps
);
974 rev
.diffopt
.flags
.quick
= 1;
975 rev
.diffopt
.flags
.ignore_submodules
= 1;
978 add_head_to_pending(&rev
);
979 diff_setup_done(&rev
.diffopt
);
981 result
= run_diff_index(&rev
, 1);
982 if (diff_result_code(&rev
.diffopt
, result
)) {
987 object_array_clear(&rev
.pending
);
988 result
= run_diff_files(&rev
, 0);
989 if (diff_result_code(&rev
.diffopt
, result
)) {
995 clear_pathspec(&rev
.prune_data
);
1000 * The function will fill `untracked_files` with the names of untracked files
1001 * It will return 1 if there were any changes and 0 if there were not.
1003 static int check_changes(const struct pathspec
*ps
, int include_untracked
,
1004 struct strbuf
*untracked_files
)
1007 if (check_changes_tracked_files(ps
))
1010 if (include_untracked
&& get_untracked_files(ps
, include_untracked
,
1017 static int save_untracked_files(struct stash_info
*info
, struct strbuf
*msg
,
1018 struct strbuf files
)
1021 struct strbuf untracked_msg
= STRBUF_INIT
;
1022 struct child_process cp_upd_index
= CHILD_PROCESS_INIT
;
1023 struct index_state istate
= { NULL
};
1025 cp_upd_index
.git_cmd
= 1;
1026 strvec_pushl(&cp_upd_index
.args
, "update-index", "-z", "--add",
1027 "--remove", "--stdin", NULL
);
1028 strvec_pushf(&cp_upd_index
.env_array
, "GIT_INDEX_FILE=%s",
1029 stash_index_path
.buf
);
1031 strbuf_addf(&untracked_msg
, "untracked files on %s\n", msg
->buf
);
1032 if (pipe_command(&cp_upd_index
, files
.buf
, files
.len
, NULL
, 0,
1038 if (write_index_as_tree(&info
->u_tree
, &istate
, stash_index_path
.buf
, 0,
1044 if (commit_tree(untracked_msg
.buf
, untracked_msg
.len
,
1045 &info
->u_tree
, NULL
, &info
->u_commit
, NULL
, NULL
)) {
1051 discard_index(&istate
);
1052 strbuf_release(&untracked_msg
);
1053 remove_path(stash_index_path
.buf
);
1057 static int stash_patch(struct stash_info
*info
, const struct pathspec
*ps
,
1058 struct strbuf
*out_patch
, int quiet
)
1061 struct child_process cp_read_tree
= CHILD_PROCESS_INIT
;
1062 struct child_process cp_diff_tree
= CHILD_PROCESS_INIT
;
1063 struct index_state istate
= { NULL
};
1064 char *old_index_env
= NULL
, *old_repo_index_file
;
1066 remove_path(stash_index_path
.buf
);
1068 cp_read_tree
.git_cmd
= 1;
1069 strvec_pushl(&cp_read_tree
.args
, "read-tree", "HEAD", NULL
);
1070 strvec_pushf(&cp_read_tree
.env_array
, "GIT_INDEX_FILE=%s",
1071 stash_index_path
.buf
);
1072 if (run_command(&cp_read_tree
)) {
1077 /* Find out what the user wants. */
1078 old_repo_index_file
= the_repository
->index_file
;
1079 the_repository
->index_file
= stash_index_path
.buf
;
1080 old_index_env
= xstrdup_or_null(getenv(INDEX_ENVIRONMENT
));
1081 setenv(INDEX_ENVIRONMENT
, the_repository
->index_file
, 1);
1083 ret
= run_add_interactive(NULL
, "--patch=stash", ps
);
1085 the_repository
->index_file
= old_repo_index_file
;
1086 if (old_index_env
&& *old_index_env
)
1087 setenv(INDEX_ENVIRONMENT
, old_index_env
, 1);
1089 unsetenv(INDEX_ENVIRONMENT
);
1090 FREE_AND_NULL(old_index_env
);
1092 /* State of the working tree. */
1093 if (write_index_as_tree(&info
->w_tree
, &istate
, stash_index_path
.buf
, 0,
1099 cp_diff_tree
.git_cmd
= 1;
1100 strvec_pushl(&cp_diff_tree
.args
, "diff-tree", "-p", "-U1", "HEAD",
1101 oid_to_hex(&info
->w_tree
), "--", NULL
);
1102 if (pipe_command(&cp_diff_tree
, NULL
, 0, out_patch
, 0, NULL
, 0)) {
1107 if (!out_patch
->len
) {
1109 fprintf_ln(stderr
, _("No changes selected"));
1114 discard_index(&istate
);
1115 remove_path(stash_index_path
.buf
);
1119 static int stash_working_tree(struct stash_info
*info
, const struct pathspec
*ps
)
1122 struct rev_info rev
;
1123 struct child_process cp_upd_index
= CHILD_PROCESS_INIT
;
1124 struct strbuf diff_output
= STRBUF_INIT
;
1125 struct index_state istate
= { NULL
};
1127 init_revisions(&rev
, NULL
);
1128 copy_pathspec(&rev
.prune_data
, ps
);
1130 set_alternate_index_output(stash_index_path
.buf
);
1131 if (reset_tree(&info
->i_tree
, 0, 0)) {
1135 set_alternate_index_output(NULL
);
1137 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
1138 rev
.diffopt
.format_callback
= add_diff_to_buf
;
1139 rev
.diffopt
.format_callback_data
= &diff_output
;
1141 if (read_cache_preload(&rev
.diffopt
.pathspec
) < 0) {
1146 add_pending_object(&rev
, parse_object(the_repository
, &info
->b_commit
),
1148 if (run_diff_index(&rev
, 0)) {
1153 cp_upd_index
.git_cmd
= 1;
1154 strvec_pushl(&cp_upd_index
.args
, "update-index",
1155 "--ignore-skip-worktree-entries",
1156 "-z", "--add", "--remove", "--stdin", NULL
);
1157 strvec_pushf(&cp_upd_index
.env_array
, "GIT_INDEX_FILE=%s",
1158 stash_index_path
.buf
);
1160 if (pipe_command(&cp_upd_index
, diff_output
.buf
, diff_output
.len
,
1161 NULL
, 0, NULL
, 0)) {
1166 if (write_index_as_tree(&info
->w_tree
, &istate
, stash_index_path
.buf
, 0,
1173 discard_index(&istate
);
1175 object_array_clear(&rev
.pending
);
1176 clear_pathspec(&rev
.prune_data
);
1177 strbuf_release(&diff_output
);
1178 remove_path(stash_index_path
.buf
);
1182 static int do_create_stash(const struct pathspec
*ps
, struct strbuf
*stash_msg_buf
,
1183 int include_untracked
, int patch_mode
,
1184 struct stash_info
*info
, struct strbuf
*patch
,
1189 int untracked_commit_option
= 0;
1190 const char *head_short_sha1
= NULL
;
1191 const char *branch_ref
= NULL
;
1192 const char *branch_name
= "(no branch)";
1193 struct commit
*head_commit
= NULL
;
1194 struct commit_list
*parents
= NULL
;
1195 struct strbuf msg
= STRBUF_INIT
;
1196 struct strbuf commit_tree_label
= STRBUF_INIT
;
1197 struct strbuf untracked_files
= STRBUF_INIT
;
1199 prepare_fallback_ident("git stash", "git@stash");
1201 read_cache_preload(NULL
);
1202 if (refresh_and_write_cache(REFRESH_QUIET
, 0, 0) < 0) {
1207 if (get_oid("HEAD", &info
->b_commit
)) {
1209 fprintf_ln(stderr
, _("You do not have "
1210 "the initial commit yet"));
1214 head_commit
= lookup_commit(the_repository
, &info
->b_commit
);
1217 if (!check_changes(ps
, include_untracked
, &untracked_files
)) {
1222 branch_ref
= resolve_ref_unsafe("HEAD", 0, NULL
, &flags
);
1223 if (flags
& REF_ISSYMREF
)
1224 branch_name
= strrchr(branch_ref
, '/') + 1;
1225 head_short_sha1
= find_unique_abbrev(&head_commit
->object
.oid
,
1227 strbuf_addf(&msg
, "%s: %s ", branch_name
, head_short_sha1
);
1228 pp_commit_easy(CMIT_FMT_ONELINE
, head_commit
, &msg
);
1230 strbuf_addf(&commit_tree_label
, "index on %s\n", msg
.buf
);
1231 commit_list_insert(head_commit
, &parents
);
1232 if (write_cache_as_tree(&info
->i_tree
, 0, NULL
) ||
1233 commit_tree(commit_tree_label
.buf
, commit_tree_label
.len
,
1234 &info
->i_tree
, parents
, &info
->i_commit
, NULL
, NULL
)) {
1236 fprintf_ln(stderr
, _("Cannot save the current "
1242 if (include_untracked
) {
1243 if (save_untracked_files(info
, &msg
, untracked_files
)) {
1245 fprintf_ln(stderr
, _("Cannot save "
1246 "the untracked files"));
1250 untracked_commit_option
= 1;
1253 ret
= stash_patch(info
, ps
, patch
, quiet
);
1256 fprintf_ln(stderr
, _("Cannot save the current "
1259 } else if (ret
> 0) {
1263 if (stash_working_tree(info
, ps
)) {
1265 fprintf_ln(stderr
, _("Cannot save the current "
1272 if (!stash_msg_buf
->len
)
1273 strbuf_addf(stash_msg_buf
, "WIP on %s", msg
.buf
);
1275 strbuf_insertf(stash_msg_buf
, 0, "On %s: ", branch_name
);
1278 * `parents` will be empty after calling `commit_tree()`, so there is
1279 * no need to call `free_commit_list()`
1282 if (untracked_commit_option
)
1283 commit_list_insert(lookup_commit(the_repository
,
1286 commit_list_insert(lookup_commit(the_repository
, &info
->i_commit
),
1288 commit_list_insert(head_commit
, &parents
);
1290 if (commit_tree(stash_msg_buf
->buf
, stash_msg_buf
->len
, &info
->w_tree
,
1291 parents
, &info
->w_commit
, NULL
, NULL
)) {
1293 fprintf_ln(stderr
, _("Cannot record "
1294 "working tree state"));
1300 strbuf_release(&commit_tree_label
);
1301 strbuf_release(&msg
);
1302 strbuf_release(&untracked_files
);
1306 static int create_stash(int argc
, const char **argv
, const char *prefix
)
1309 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1310 struct stash_info info
;
1313 /* Starting with argv[1], since argv[0] is "create" */
1314 strbuf_join_argv(&stash_msg_buf
, argc
- 1, ++argv
, ' ');
1316 memset(&ps
, 0, sizeof(ps
));
1317 if (!check_changes_tracked_files(&ps
))
1320 ret
= do_create_stash(&ps
, &stash_msg_buf
, 0, 0, &info
,
1323 printf_ln("%s", oid_to_hex(&info
.w_commit
));
1325 strbuf_release(&stash_msg_buf
);
1329 static int do_push_stash(const struct pathspec
*ps
, const char *stash_msg
, int quiet
,
1330 int keep_index
, int patch_mode
, int include_untracked
)
1333 struct stash_info info
;
1334 struct strbuf patch
= STRBUF_INIT
;
1335 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1336 struct strbuf untracked_files
= STRBUF_INIT
;
1338 if (patch_mode
&& keep_index
== -1)
1341 if (patch_mode
&& include_untracked
) {
1342 fprintf_ln(stderr
, _("Can't use --patch and --include-untracked"
1343 " or --all at the same time"));
1348 read_cache_preload(NULL
);
1349 if (!include_untracked
&& ps
->nr
) {
1351 char *ps_matched
= xcalloc(ps
->nr
, 1);
1353 for (i
= 0; i
< active_nr
; i
++)
1354 ce_path_match(&the_index
, active_cache
[i
], ps
,
1357 if (report_path_error(ps_matched
, ps
)) {
1358 fprintf_ln(stderr
, _("Did you forget to 'git add'?"));
1366 if (refresh_and_write_cache(REFRESH_QUIET
, 0, 0)) {
1371 if (!check_changes(ps
, include_untracked
, &untracked_files
)) {
1373 printf_ln(_("No local changes to save"));
1377 if (!reflog_exists(ref_stash
) && do_clear_stash()) {
1380 fprintf_ln(stderr
, _("Cannot initialize stash"));
1385 strbuf_addstr(&stash_msg_buf
, stash_msg
);
1386 if (do_create_stash(ps
, &stash_msg_buf
, include_untracked
, patch_mode
,
1387 &info
, &patch
, quiet
)) {
1392 if (do_store_stash(&info
.w_commit
, stash_msg_buf
.buf
, 1)) {
1395 fprintf_ln(stderr
, _("Cannot save the current status"));
1400 printf_ln(_("Saved working directory and index state %s"),
1404 if (include_untracked
&& !ps
->nr
) {
1405 struct child_process cp
= CHILD_PROCESS_INIT
;
1408 strvec_pushl(&cp
.args
, "clean", "--force",
1409 "--quiet", "-d", NULL
);
1410 if (include_untracked
== INCLUDE_ALL_FILES
)
1411 strvec_push(&cp
.args
, "-x");
1412 if (run_command(&cp
)) {
1419 struct child_process cp_add
= CHILD_PROCESS_INIT
;
1420 struct child_process cp_diff
= CHILD_PROCESS_INIT
;
1421 struct child_process cp_apply
= CHILD_PROCESS_INIT
;
1422 struct strbuf out
= STRBUF_INIT
;
1425 strvec_push(&cp_add
.args
, "add");
1426 if (!include_untracked
)
1427 strvec_push(&cp_add
.args
, "-u");
1428 if (include_untracked
== INCLUDE_ALL_FILES
)
1429 strvec_push(&cp_add
.args
, "--force");
1430 strvec_push(&cp_add
.args
, "--");
1431 add_pathspecs(&cp_add
.args
, ps
);
1432 if (run_command(&cp_add
)) {
1437 cp_diff
.git_cmd
= 1;
1438 strvec_pushl(&cp_diff
.args
, "diff-index", "-p",
1439 "--cached", "--binary", "HEAD", "--",
1441 add_pathspecs(&cp_diff
.args
, ps
);
1442 if (pipe_command(&cp_diff
, NULL
, 0, &out
, 0, NULL
, 0)) {
1447 cp_apply
.git_cmd
= 1;
1448 strvec_pushl(&cp_apply
.args
, "apply", "--index",
1450 if (pipe_command(&cp_apply
, out
.buf
, out
.len
, NULL
, 0,
1456 struct child_process cp
= CHILD_PROCESS_INIT
;
1458 strvec_pushl(&cp
.args
, "reset", "--hard", "-q",
1459 "--no-recurse-submodules", NULL
);
1460 if (run_command(&cp
)) {
1466 if (keep_index
== 1 && !is_null_oid(&info
.i_tree
)) {
1467 struct child_process cp
= CHILD_PROCESS_INIT
;
1470 strvec_pushl(&cp
.args
, "checkout", "--no-overlay",
1471 oid_to_hex(&info
.i_tree
), "--", NULL
);
1473 strvec_push(&cp
.args
, ":/");
1475 add_pathspecs(&cp
.args
, ps
);
1476 if (run_command(&cp
)) {
1483 struct child_process cp
= CHILD_PROCESS_INIT
;
1486 strvec_pushl(&cp
.args
, "apply", "-R", NULL
);
1488 if (pipe_command(&cp
, patch
.buf
, patch
.len
, NULL
, 0, NULL
, 0)) {
1490 fprintf_ln(stderr
, _("Cannot remove "
1491 "worktree changes"));
1496 if (keep_index
< 1) {
1497 struct child_process cp
= CHILD_PROCESS_INIT
;
1500 strvec_pushl(&cp
.args
, "reset", "-q", "--", NULL
);
1501 add_pathspecs(&cp
.args
, ps
);
1502 if (run_command(&cp
)) {
1511 strbuf_release(&stash_msg_buf
);
1515 static int push_stash(int argc
, const char **argv
, const char *prefix
,
1518 int force_assume
= 0;
1519 int keep_index
= -1;
1521 int include_untracked
= 0;
1523 int pathspec_file_nul
= 0;
1524 const char *stash_msg
= NULL
;
1525 const char *pathspec_from_file
= NULL
;
1527 struct option options
[] = {
1528 OPT_BOOL('k', "keep-index", &keep_index
,
1530 OPT_BOOL('p', "patch", &patch_mode
,
1531 N_("stash in patch mode")),
1532 OPT__QUIET(&quiet
, N_("quiet mode")),
1533 OPT_BOOL('u', "include-untracked", &include_untracked
,
1534 N_("include untracked files in stash")),
1535 OPT_SET_INT('a', "all", &include_untracked
,
1536 N_("include ignore files"), 2),
1537 OPT_STRING('m', "message", &stash_msg
, N_("message"),
1538 N_("stash message")),
1539 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
1540 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
1545 force_assume
= !strcmp(argv
[0], "-p");
1546 argc
= parse_options(argc
, argv
, prefix
, options
,
1547 git_stash_push_usage
,
1548 PARSE_OPT_KEEP_DASHDASH
);
1552 if (!strcmp(argv
[0], "--")) {
1555 } else if (push_assumed
&& !force_assume
) {
1556 die("subcommand wasn't specified; 'push' can't be assumed due to unexpected token '%s'",
1561 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_FULL
| PATHSPEC_PREFIX_ORIGIN
,
1564 if (pathspec_from_file
) {
1566 die(_("--pathspec-from-file is incompatible with --patch"));
1569 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
1571 parse_pathspec_file(&ps
, 0,
1572 PATHSPEC_PREFER_FULL
| PATHSPEC_PREFIX_ORIGIN
,
1573 prefix
, pathspec_from_file
, pathspec_file_nul
);
1574 } else if (pathspec_file_nul
) {
1575 die(_("--pathspec-file-nul requires --pathspec-from-file"));
1578 return do_push_stash(&ps
, stash_msg
, quiet
, keep_index
, patch_mode
,
1582 static int save_stash(int argc
, const char **argv
, const char *prefix
)
1584 int keep_index
= -1;
1586 int include_untracked
= 0;
1589 const char *stash_msg
= NULL
;
1591 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1592 struct option options
[] = {
1593 OPT_BOOL('k', "keep-index", &keep_index
,
1595 OPT_BOOL('p', "patch", &patch_mode
,
1596 N_("stash in patch mode")),
1597 OPT__QUIET(&quiet
, N_("quiet mode")),
1598 OPT_BOOL('u', "include-untracked", &include_untracked
,
1599 N_("include untracked files in stash")),
1600 OPT_SET_INT('a', "all", &include_untracked
,
1601 N_("include ignore files"), 2),
1602 OPT_STRING('m', "message", &stash_msg
, "message",
1603 N_("stash message")),
1607 argc
= parse_options(argc
, argv
, prefix
, options
,
1608 git_stash_save_usage
,
1609 PARSE_OPT_KEEP_DASHDASH
);
1612 stash_msg
= strbuf_join_argv(&stash_msg_buf
, argc
, argv
, ' ');
1614 memset(&ps
, 0, sizeof(ps
));
1615 ret
= do_push_stash(&ps
, stash_msg
, quiet
, keep_index
,
1616 patch_mode
, include_untracked
);
1618 strbuf_release(&stash_msg_buf
);
1622 int cmd_stash(int argc
, const char **argv
, const char *prefix
)
1624 pid_t pid
= getpid();
1625 const char *index_file
;
1626 struct strvec args
= STRVEC_INIT
;
1628 struct option options
[] = {
1632 git_config(git_stash_config
, NULL
);
1634 if (use_legacy_stash
||
1635 !git_env_bool("GIT_TEST_STASH_USE_BUILTIN", -1))
1636 warning(_("the stash.useBuiltin support has been removed!\n"
1637 "See its entry in 'git help config' for details."));
1639 argc
= parse_options(argc
, argv
, prefix
, options
, git_stash_usage
,
1640 PARSE_OPT_KEEP_UNKNOWN
| PARSE_OPT_KEEP_DASHDASH
);
1642 index_file
= get_index_file();
1643 strbuf_addf(&stash_index_path
, "%s.stash.%" PRIuMAX
, index_file
,
1647 return !!push_stash(0, NULL
, prefix
, 0);
1648 else if (!strcmp(argv
[0], "apply"))
1649 return !!apply_stash(argc
, argv
, prefix
);
1650 else if (!strcmp(argv
[0], "clear"))
1651 return !!clear_stash(argc
, argv
, prefix
);
1652 else if (!strcmp(argv
[0], "drop"))
1653 return !!drop_stash(argc
, argv
, prefix
);
1654 else if (!strcmp(argv
[0], "pop"))
1655 return !!pop_stash(argc
, argv
, prefix
);
1656 else if (!strcmp(argv
[0], "branch"))
1657 return !!branch_stash(argc
, argv
, prefix
);
1658 else if (!strcmp(argv
[0], "list"))
1659 return !!list_stash(argc
, argv
, prefix
);
1660 else if (!strcmp(argv
[0], "show"))
1661 return !!show_stash(argc
, argv
, prefix
);
1662 else if (!strcmp(argv
[0], "store"))
1663 return !!store_stash(argc
, argv
, prefix
);
1664 else if (!strcmp(argv
[0], "create"))
1665 return !!create_stash(argc
, argv
, prefix
);
1666 else if (!strcmp(argv
[0], "push"))
1667 return !!push_stash(argc
, argv
, prefix
, 0);
1668 else if (!strcmp(argv
[0], "save"))
1669 return !!save_stash(argc
, argv
, prefix
);
1670 else if (*argv
[0] != '-')
1671 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv
[0]),
1672 git_stash_usage
, options
);
1674 /* Assume 'stash push' */
1675 strvec_push(&args
, "push");
1676 strvec_pushv(&args
, argv
);
1677 return !!push_stash(args
.nr
, args
.v
, prefix
, 1);