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"
20 #define INCLUDE_ALL_FILES 2
22 static const char * const git_stash_usage
[] = {
23 N_("git stash list [<options>]"),
24 N_("git stash show [<options>] [<stash>]"),
25 N_("git stash drop [-q|--quiet] [<stash>]"),
26 N_("git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
27 N_("git stash branch <branchname> [<stash>]"),
28 N_("git stash clear"),
29 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
30 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
31 " [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
32 " [--] [<pathspec>...]]"),
33 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
34 " [-u|--include-untracked] [-a|--all] [<message>]"),
38 static const char * const git_stash_list_usage
[] = {
39 N_("git stash list [<options>]"),
43 static const char * const git_stash_show_usage
[] = {
44 N_("git stash show [<options>] [<stash>]"),
48 static const char * const git_stash_drop_usage
[] = {
49 N_("git stash drop [-q|--quiet] [<stash>]"),
53 static const char * const git_stash_pop_usage
[] = {
54 N_("git stash pop [--index] [-q|--quiet] [<stash>]"),
58 static const char * const git_stash_apply_usage
[] = {
59 N_("git stash apply [--index] [-q|--quiet] [<stash>]"),
63 static const char * const git_stash_branch_usage
[] = {
64 N_("git stash branch <branchname> [<stash>]"),
68 static const char * const git_stash_clear_usage
[] = {
69 N_("git stash clear"),
73 static const char * const git_stash_store_usage
[] = {
74 N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"),
78 static const char * const git_stash_push_usage
[] = {
79 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
80 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
81 " [--] [<pathspec>...]]"),
85 static const char * const git_stash_save_usage
[] = {
86 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
87 " [-u|--include-untracked] [-a|--all] [<message>]"),
91 static const char ref_stash
[] = "refs/stash";
92 static struct strbuf stash_index_path
= STRBUF_INIT
;
95 * w_commit is set to the commit containing the working tree
96 * b_commit is set to the base commit
97 * i_commit is set to the commit containing the index tree
98 * u_commit is set to the commit containing the untracked files tree
99 * w_tree is set to the working tree
100 * b_tree is set to the base tree
101 * i_tree is set to the index tree
102 * u_tree is set to the untracked files tree
105 struct object_id w_commit
;
106 struct object_id b_commit
;
107 struct object_id i_commit
;
108 struct object_id u_commit
;
109 struct object_id w_tree
;
110 struct object_id b_tree
;
111 struct object_id i_tree
;
112 struct object_id u_tree
;
113 struct strbuf revision
;
118 static void free_stash_info(struct stash_info
*info
)
120 strbuf_release(&info
->revision
);
123 static void assert_stash_like(struct stash_info
*info
, const char *revision
)
125 if (get_oidf(&info
->b_commit
, "%s^1", revision
) ||
126 get_oidf(&info
->w_tree
, "%s:", revision
) ||
127 get_oidf(&info
->b_tree
, "%s^1:", revision
) ||
128 get_oidf(&info
->i_tree
, "%s^2:", revision
))
129 die(_("'%s' is not a stash-like commit"), revision
);
132 static int get_stash_info(struct stash_info
*info
, int argc
, const char **argv
)
137 const char *revision
;
138 const char *commit
= NULL
;
139 struct object_id dummy
;
140 struct strbuf symbolic
= STRBUF_INIT
;
144 struct strbuf refs_msg
= STRBUF_INIT
;
146 for (i
= 0; i
< argc
; i
++)
147 strbuf_addf(&refs_msg
, " '%s'", argv
[i
]);
149 fprintf_ln(stderr
, _("Too many revisions specified:%s"),
151 strbuf_release(&refs_msg
);
159 strbuf_init(&info
->revision
, 0);
161 if (!ref_exists(ref_stash
)) {
162 free_stash_info(info
);
163 fprintf_ln(stderr
, _("No stash entries found."));
167 strbuf_addf(&info
->revision
, "%s@{0}", ref_stash
);
168 } else if (strspn(commit
, "0123456789") == strlen(commit
)) {
169 strbuf_addf(&info
->revision
, "%s@{%s}", ref_stash
, commit
);
171 strbuf_addstr(&info
->revision
, commit
);
174 revision
= info
->revision
.buf
;
176 if (get_oid(revision
, &info
->w_commit
)) {
177 error(_("%s is not a valid reference"), revision
);
178 free_stash_info(info
);
182 assert_stash_like(info
, revision
);
184 info
->has_u
= !get_oidf(&info
->u_tree
, "%s^3:", revision
);
186 end_of_rev
= strchrnul(revision
, '@');
187 strbuf_add(&symbolic
, revision
, end_of_rev
- revision
);
189 ret
= dwim_ref(symbolic
.buf
, symbolic
.len
, &dummy
, &expanded_ref
, 0);
190 strbuf_release(&symbolic
);
192 case 0: /* Not found, but valid ref */
193 info
->is_stash_ref
= 0;
196 info
->is_stash_ref
= !strcmp(expanded_ref
, ref_stash
);
198 default: /* Invalid or ambiguous */
199 free_stash_info(info
);
203 return !(ret
== 0 || ret
== 1);
206 static int do_clear_stash(void)
208 struct object_id obj
;
209 if (get_oid(ref_stash
, &obj
))
212 return delete_ref(NULL
, ref_stash
, &obj
, 0);
215 static int clear_stash(int argc
, const char **argv
, const char *prefix
)
217 struct option options
[] = {
221 argc
= parse_options(argc
, argv
, prefix
, options
,
222 git_stash_clear_usage
,
223 PARSE_OPT_STOP_AT_NON_OPTION
);
226 return error(_("git stash clear with arguments is "
229 return do_clear_stash();
232 static int reset_tree(struct object_id
*i_tree
, int update
, int reset
)
235 struct unpack_trees_options opts
;
236 struct tree_desc t
[MAX_UNPACK_TREES
];
238 struct lock_file lock_file
= LOCK_INIT
;
240 read_cache_preload(NULL
);
241 if (refresh_cache(REFRESH_QUIET
))
244 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
246 memset(&opts
, 0, sizeof(opts
));
248 tree
= parse_tree_indirect(i_tree
);
249 if (parse_tree(tree
))
252 init_tree_desc(t
, tree
->buffer
, tree
->size
);
255 opts
.src_index
= &the_index
;
256 opts
.dst_index
= &the_index
;
259 opts
.update
= update
;
260 opts
.fn
= oneway_merge
;
262 if (unpack_trees(nr_trees
, t
, &opts
))
265 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
266 return error(_("unable to write new index file"));
271 static int diff_tree_binary(struct strbuf
*out
, struct object_id
*w_commit
)
273 struct child_process cp
= CHILD_PROCESS_INIT
;
274 const char *w_commit_hex
= oid_to_hex(w_commit
);
277 * Diff-tree would not be very hard to replace with a native function,
278 * however it should be done together with apply_cached.
281 strvec_pushl(&cp
.args
, "diff-tree", "--binary", NULL
);
282 strvec_pushf(&cp
.args
, "%s^2^..%s^2", w_commit_hex
, w_commit_hex
);
284 return pipe_command(&cp
, NULL
, 0, out
, 0, NULL
, 0);
287 static int apply_cached(struct strbuf
*out
)
289 struct child_process cp
= CHILD_PROCESS_INIT
;
292 * Apply currently only reads either from stdin or a file, thus
293 * apply_all_patches would have to be updated to optionally take a
297 strvec_pushl(&cp
.args
, "apply", "--cached", NULL
);
298 return pipe_command(&cp
, out
->buf
, out
->len
, NULL
, 0, NULL
, 0);
301 static int reset_head(void)
303 struct child_process cp
= CHILD_PROCESS_INIT
;
306 * Reset is overall quite simple, however there is no current public
310 strvec_push(&cp
.args
, "reset");
312 return run_command(&cp
);
315 static void add_diff_to_buf(struct diff_queue_struct
*q
,
316 struct diff_options
*options
,
321 for (i
= 0; i
< q
->nr
; i
++) {
322 strbuf_addstr(data
, q
->queue
[i
]->one
->path
);
324 /* NUL-terminate: will be fed to update-index -z */
325 strbuf_addch(data
, '\0');
329 static int restore_untracked(struct object_id
*u_tree
)
332 struct child_process cp
= CHILD_PROCESS_INIT
;
335 * We need to run restore files from a given index, but without
336 * affecting the current index, so we use GIT_INDEX_FILE with
337 * run_command to fork processes that will not interfere.
340 strvec_push(&cp
.args
, "read-tree");
341 strvec_push(&cp
.args
, oid_to_hex(u_tree
));
342 strvec_pushf(&cp
.env_array
, "GIT_INDEX_FILE=%s",
343 stash_index_path
.buf
);
344 if (run_command(&cp
)) {
345 remove_path(stash_index_path
.buf
);
349 child_process_init(&cp
);
351 strvec_pushl(&cp
.args
, "checkout-index", "--all", NULL
);
352 strvec_pushf(&cp
.env_array
, "GIT_INDEX_FILE=%s",
353 stash_index_path
.buf
);
355 res
= run_command(&cp
);
356 remove_path(stash_index_path
.buf
);
360 static void unstage_changes_unless_new(struct object_id
*orig_tree
)
363 * When we enter this function, there has been a clean merge of
364 * relevant trees, and the merge logic always stages whatever merges
365 * cleanly. We want to unstage those changes, unless it corresponds
366 * to a file that didn't exist as of orig_tree.
368 * However, if any SKIP_WORKTREE path is modified relative to
369 * orig_tree, then we want to clear the SKIP_WORKTREE bit and write
370 * it to the worktree before unstaging.
373 struct checkout state
= CHECKOUT_INIT
;
374 struct diff_options diff_opts
;
375 struct lock_file lock
= LOCK_INIT
;
378 /* If any entries have skip_worktree set, we'll have to check 'em out */
381 state
.refresh_cache
= 1;
382 state
.istate
= &the_index
;
385 * Step 1: get a difference between orig_tree (which corresponding
386 * to the index before a merge was run) and the current index
387 * (reflecting the changes brought in by the merge).
389 diff_setup(&diff_opts
);
390 diff_opts
.flags
.recursive
= 1;
391 diff_opts
.detect_rename
= 0;
392 diff_opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
393 diff_setup_done(&diff_opts
);
395 do_diff_cache(orig_tree
, &diff_opts
);
396 diffcore_std(&diff_opts
);
398 /* Iterate over the paths that changed due to the merge... */
399 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
400 struct diff_filepair
*p
;
401 struct cache_entry
*ce
;
404 /* Look up the path's position in the current index. */
405 p
= diff_queued_diff
.queue
[i
];
406 pos
= index_name_pos(&the_index
, p
->two
->path
,
407 strlen(p
->two
->path
));
410 * Step 2: Place changes in the working tree
412 * Stash is about restoring changes *to the working tree*.
413 * So if the merge successfully got a new version of some
414 * path, but left it out of the working tree, then clear the
415 * SKIP_WORKTREE bit and write it to the working tree.
417 if (pos
>= 0 && ce_skip_worktree(active_cache
[pos
])) {
420 ce
= active_cache
[pos
];
421 if (!lstat(ce
->name
, &st
)) {
422 /* Conflicting path present; relocate it */
423 struct strbuf new_path
= STRBUF_INIT
;
426 strbuf_addf(&new_path
,
427 "%s.stash.XXXXXX", ce
->name
);
428 fd
= xmkstemp(new_path
.buf
);
430 printf(_("WARNING: Untracked file in way of "
431 "tracked file! Renaming\n "
434 ce
->name
, new_path
.buf
);
435 if (rename(ce
->name
, new_path
.buf
))
436 die("Failed to move %s to %s\n",
437 ce
->name
, new_path
.buf
);
438 strbuf_release(&new_path
);
440 checkout_entry(ce
, &state
, NULL
, NULL
);
441 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
445 * Step 3: "unstage" changes, as long as they are still tracked
447 if (p
->one
->oid_valid
) {
449 * Path existed in orig_tree; restore index entry
450 * from that tree in order to "unstage" the changes.
452 int option
= ADD_CACHE_OK_TO_REPLACE
;
454 option
= ADD_CACHE_OK_TO_ADD
;
456 ce
= make_cache_entry(&the_index
,
461 add_index_entry(&the_index
, ce
, option
);
464 diff_flush(&diff_opts
);
467 * Step 4: write the new index to disk
469 repo_hold_locked_index(the_repository
, &lock
, LOCK_DIE_ON_ERROR
);
470 if (write_locked_index(&the_index
, &lock
,
471 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
472 die(_("Unable to write index."));
475 static int do_apply_stash(const char *prefix
, struct stash_info
*info
,
476 int index
, int quiet
)
479 int has_index
= index
;
480 struct merge_options o
;
481 struct object_id c_tree
;
482 struct object_id index_tree
;
483 struct commit
*result
;
484 const struct object_id
*bases
[1];
486 read_cache_preload(NULL
);
487 if (refresh_and_write_cache(REFRESH_QUIET
, 0, 0))
490 if (write_cache_as_tree(&c_tree
, 0, NULL
))
491 return error(_("cannot apply a stash in the middle of a merge"));
494 if (oideq(&info
->b_tree
, &info
->i_tree
) ||
495 oideq(&c_tree
, &info
->i_tree
)) {
498 struct strbuf out
= STRBUF_INIT
;
500 if (diff_tree_binary(&out
, &info
->w_commit
)) {
501 strbuf_release(&out
);
502 return error(_("could not generate diff %s^!."),
503 oid_to_hex(&info
->w_commit
));
506 ret
= apply_cached(&out
);
507 strbuf_release(&out
);
509 return error(_("conflicts in index. "
510 "Try without --index."));
514 if (write_cache_as_tree(&index_tree
, 0, NULL
))
515 return error(_("could not save index tree"));
523 if (info
->has_u
&& restore_untracked(&info
->u_tree
))
524 return error(_("could not restore untracked files from stash"));
526 init_merge_options(&o
, the_repository
);
528 o
.branch1
= "Updated upstream";
529 o
.branch2
= "Stashed changes";
531 if (oideq(&info
->b_tree
, &c_tree
))
532 o
.branch1
= "Version stash was based on";
537 if (o
.verbosity
>= 3)
538 printf_ln(_("Merging %s with %s"), o
.branch1
, o
.branch2
);
540 bases
[0] = &info
->b_tree
;
542 ret
= merge_recursive_generic(&o
, &c_tree
, &info
->w_tree
, 1, bases
,
548 fprintf_ln(stderr
, _("Index was not unstashed."));
554 if (reset_tree(&index_tree
, 0, 0))
557 unstage_changes_unless_new(&c_tree
);
561 struct child_process cp
= CHILD_PROCESS_INIT
;
564 * Status is quite simple and could be replaced with calls to
565 * wt_status in the future, but it adds complexities which may
566 * require more tests.
570 strvec_pushf(&cp
.env_array
, GIT_WORK_TREE_ENVIRONMENT
"=%s",
571 absolute_path(get_git_work_tree()));
572 strvec_pushf(&cp
.env_array
, GIT_DIR_ENVIRONMENT
"=%s",
573 absolute_path(get_git_dir()));
574 strvec_push(&cp
.args
, "status");
581 static int apply_stash(int argc
, const char **argv
, const char *prefix
)
586 struct stash_info info
;
587 struct option options
[] = {
588 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
589 OPT_BOOL(0, "index", &index
,
590 N_("attempt to recreate the index")),
594 argc
= parse_options(argc
, argv
, prefix
, options
,
595 git_stash_apply_usage
, 0);
597 if (get_stash_info(&info
, argc
, argv
))
600 ret
= do_apply_stash(prefix
, &info
, index
, quiet
);
601 free_stash_info(&info
);
605 static int reject_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
606 const char *email
, timestamp_t timestamp
, int tz
,
607 const char *message
, void *cb_data
)
612 static int reflog_is_empty(const char *refname
)
614 return !for_each_reflog_ent(refname
, reject_reflog_ent
, NULL
);
617 static int do_drop_stash(struct stash_info
*info
, int quiet
)
620 struct child_process cp_reflog
= CHILD_PROCESS_INIT
;
623 * reflog does not provide a simple function for deleting refs. One will
624 * need to be added to avoid implementing too much reflog code here
627 cp_reflog
.git_cmd
= 1;
628 strvec_pushl(&cp_reflog
.args
, "reflog", "delete", "--updateref",
630 strvec_push(&cp_reflog
.args
, info
->revision
.buf
);
631 ret
= run_command(&cp_reflog
);
634 printf_ln(_("Dropped %s (%s)"), info
->revision
.buf
,
635 oid_to_hex(&info
->w_commit
));
637 return error(_("%s: Could not drop stash entry"),
641 if (reflog_is_empty(ref_stash
))
647 static void assert_stash_ref(struct stash_info
*info
)
649 if (!info
->is_stash_ref
) {
650 error(_("'%s' is not a stash reference"), info
->revision
.buf
);
651 free_stash_info(info
);
656 static int drop_stash(int argc
, const char **argv
, const char *prefix
)
660 struct stash_info info
;
661 struct option options
[] = {
662 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
666 argc
= parse_options(argc
, argv
, prefix
, options
,
667 git_stash_drop_usage
, 0);
669 if (get_stash_info(&info
, argc
, argv
))
672 assert_stash_ref(&info
);
674 ret
= do_drop_stash(&info
, quiet
);
675 free_stash_info(&info
);
679 static int pop_stash(int argc
, const char **argv
, const char *prefix
)
684 struct stash_info info
;
685 struct option options
[] = {
686 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
687 OPT_BOOL(0, "index", &index
,
688 N_("attempt to recreate the index")),
692 argc
= parse_options(argc
, argv
, prefix
, options
,
693 git_stash_pop_usage
, 0);
695 if (get_stash_info(&info
, argc
, argv
))
698 assert_stash_ref(&info
);
699 if ((ret
= do_apply_stash(prefix
, &info
, index
, quiet
)))
700 printf_ln(_("The stash entry is kept in case "
701 "you need it again."));
703 ret
= do_drop_stash(&info
, quiet
);
705 free_stash_info(&info
);
709 static int branch_stash(int argc
, const char **argv
, const char *prefix
)
712 const char *branch
= NULL
;
713 struct stash_info info
;
714 struct child_process cp
= CHILD_PROCESS_INIT
;
715 struct option options
[] = {
719 argc
= parse_options(argc
, argv
, prefix
, options
,
720 git_stash_branch_usage
, 0);
723 fprintf_ln(stderr
, _("No branch name specified"));
729 if (get_stash_info(&info
, argc
- 1, argv
+ 1))
733 strvec_pushl(&cp
.args
, "checkout", "-b", NULL
);
734 strvec_push(&cp
.args
, branch
);
735 strvec_push(&cp
.args
, oid_to_hex(&info
.b_commit
));
736 ret
= run_command(&cp
);
738 ret
= do_apply_stash(prefix
, &info
, 1, 0);
739 if (!ret
&& info
.is_stash_ref
)
740 ret
= do_drop_stash(&info
, 0);
742 free_stash_info(&info
);
747 static int list_stash(int argc
, const char **argv
, const char *prefix
)
749 struct child_process cp
= CHILD_PROCESS_INIT
;
750 struct option options
[] = {
754 argc
= parse_options(argc
, argv
, prefix
, options
,
755 git_stash_list_usage
,
756 PARSE_OPT_KEEP_UNKNOWN
);
758 if (!ref_exists(ref_stash
))
762 strvec_pushl(&cp
.args
, "log", "--format=%gd: %gs", "-g",
763 "--first-parent", "-m", NULL
);
764 strvec_pushv(&cp
.args
, argv
);
765 strvec_push(&cp
.args
, ref_stash
);
766 strvec_push(&cp
.args
, "--");
767 return run_command(&cp
);
770 static int show_stat
= 1;
771 static int show_patch
;
772 static int use_legacy_stash
;
774 static int git_stash_config(const char *var
, const char *value
, void *cb
)
776 if (!strcmp(var
, "stash.showstat")) {
777 show_stat
= git_config_bool(var
, value
);
780 if (!strcmp(var
, "stash.showpatch")) {
781 show_patch
= git_config_bool(var
, value
);
784 if (!strcmp(var
, "stash.usebuiltin")) {
785 use_legacy_stash
= !git_config_bool(var
, value
);
788 return git_diff_basic_config(var
, value
, cb
);
791 static int show_stash(int argc
, const char **argv
, const char *prefix
)
795 struct stash_info info
;
797 struct strvec stash_args
= STRVEC_INIT
;
798 struct strvec revision_args
= STRVEC_INIT
;
799 struct option options
[] = {
803 init_diff_ui_defaults();
804 git_config(git_diff_ui_config
, NULL
);
805 init_revisions(&rev
, prefix
);
807 strvec_push(&revision_args
, argv
[0]);
808 for (i
= 1; i
< argc
; i
++) {
809 if (argv
[i
][0] != '-')
810 strvec_push(&stash_args
, argv
[i
]);
812 strvec_push(&revision_args
, argv
[i
]);
815 ret
= get_stash_info(&info
, stash_args
.nr
, stash_args
.v
);
816 strvec_clear(&stash_args
);
821 * The config settings are applied only if there are not passed
824 if (revision_args
.nr
== 1) {
826 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
;
829 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
831 if (!show_stat
&& !show_patch
) {
832 free_stash_info(&info
);
837 argc
= setup_revisions(revision_args
.nr
, revision_args
.v
, &rev
, NULL
);
839 free_stash_info(&info
);
840 usage_with_options(git_stash_show_usage
, options
);
842 if (!rev
.diffopt
.output_format
) {
843 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
844 diff_setup_done(&rev
.diffopt
);
847 rev
.diffopt
.flags
.recursive
= 1;
848 setup_diff_pager(&rev
.diffopt
);
849 diff_tree_oid(&info
.b_commit
, &info
.w_commit
, "", &rev
.diffopt
);
850 log_tree_diff_flush(&rev
);
852 free_stash_info(&info
);
853 return diff_result_code(&rev
.diffopt
, 0);
856 static int do_store_stash(const struct object_id
*w_commit
, const char *stash_msg
,
860 stash_msg
= "Created via \"git stash store\".";
862 if (update_ref(stash_msg
, ref_stash
, w_commit
, NULL
,
863 REF_FORCE_CREATE_REFLOG
,
864 quiet
? UPDATE_REFS_QUIET_ON_ERR
:
865 UPDATE_REFS_MSG_ON_ERR
)) {
867 fprintf_ln(stderr
, _("Cannot update %s with %s"),
868 ref_stash
, oid_to_hex(w_commit
));
876 static int store_stash(int argc
, const char **argv
, const char *prefix
)
879 const char *stash_msg
= NULL
;
880 struct object_id obj
;
881 struct object_context dummy
;
882 struct option options
[] = {
883 OPT__QUIET(&quiet
, N_("be quiet")),
884 OPT_STRING('m', "message", &stash_msg
, "message",
885 N_("stash message")),
889 argc
= parse_options(argc
, argv
, prefix
, options
,
890 git_stash_store_usage
,
891 PARSE_OPT_KEEP_UNKNOWN
);
895 fprintf_ln(stderr
, _("\"git stash store\" requires one "
896 "<commit> argument"));
900 if (get_oid_with_context(the_repository
,
901 argv
[0], quiet
? GET_OID_QUIETLY
: 0, &obj
,
904 fprintf_ln(stderr
, _("Cannot update %s with %s"),
909 return do_store_stash(&obj
, stash_msg
, quiet
);
912 static void add_pathspecs(struct strvec
*args
,
913 const struct pathspec
*ps
) {
916 for (i
= 0; i
< ps
->nr
; i
++)
917 strvec_push(args
, ps
->items
[i
].original
);
921 * `untracked_files` will be filled with the names of untracked files.
922 * The return value is:
924 * = 0 if there are not any untracked files
925 * > 0 if there are untracked files
927 static int get_untracked_files(const struct pathspec
*ps
, int include_untracked
,
928 struct strbuf
*untracked_files
)
932 struct dir_struct dir
;
935 if (include_untracked
!= INCLUDE_ALL_FILES
)
936 setup_standard_excludes(&dir
);
938 fill_directory(&dir
, the_repository
->index
, ps
);
939 for (i
= 0; i
< dir
.nr
; i
++) {
940 struct dir_entry
*ent
= dir
.entries
[i
];
942 strbuf_addstr(untracked_files
, ent
->name
);
943 /* NUL-terminate: will be fed to update-index -z */
944 strbuf_addch(untracked_files
, '\0');
952 * The return value of `check_changes_tracked_files()` can be:
954 * < 0 if there was an error
955 * = 0 if there are no changes.
956 * > 0 if there are changes.
958 static int check_changes_tracked_files(const struct pathspec
*ps
)
962 struct object_id dummy
;
965 /* No initial commit. */
966 if (get_oid("HEAD", &dummy
))
969 if (read_cache() < 0)
972 init_revisions(&rev
, NULL
);
973 copy_pathspec(&rev
.prune_data
, ps
);
975 rev
.diffopt
.flags
.quick
= 1;
976 rev
.diffopt
.flags
.ignore_submodules
= 1;
979 add_head_to_pending(&rev
);
980 diff_setup_done(&rev
.diffopt
);
982 result
= run_diff_index(&rev
, 1);
983 if (diff_result_code(&rev
.diffopt
, result
)) {
988 object_array_clear(&rev
.pending
);
989 result
= run_diff_files(&rev
, 0);
990 if (diff_result_code(&rev
.diffopt
, result
)) {
996 clear_pathspec(&rev
.prune_data
);
1001 * The function will fill `untracked_files` with the names of untracked files
1002 * It will return 1 if there were any changes and 0 if there were not.
1004 static int check_changes(const struct pathspec
*ps
, int include_untracked
,
1005 struct strbuf
*untracked_files
)
1008 if (check_changes_tracked_files(ps
))
1011 if (include_untracked
&& get_untracked_files(ps
, include_untracked
,
1018 static int save_untracked_files(struct stash_info
*info
, struct strbuf
*msg
,
1019 struct strbuf files
)
1022 struct strbuf untracked_msg
= STRBUF_INIT
;
1023 struct child_process cp_upd_index
= CHILD_PROCESS_INIT
;
1024 struct index_state istate
= { NULL
};
1026 cp_upd_index
.git_cmd
= 1;
1027 strvec_pushl(&cp_upd_index
.args
, "update-index", "-z", "--add",
1028 "--remove", "--stdin", NULL
);
1029 strvec_pushf(&cp_upd_index
.env_array
, "GIT_INDEX_FILE=%s",
1030 stash_index_path
.buf
);
1032 strbuf_addf(&untracked_msg
, "untracked files on %s\n", msg
->buf
);
1033 if (pipe_command(&cp_upd_index
, files
.buf
, files
.len
, NULL
, 0,
1039 if (write_index_as_tree(&info
->u_tree
, &istate
, stash_index_path
.buf
, 0,
1045 if (commit_tree(untracked_msg
.buf
, untracked_msg
.len
,
1046 &info
->u_tree
, NULL
, &info
->u_commit
, NULL
, NULL
)) {
1052 discard_index(&istate
);
1053 strbuf_release(&untracked_msg
);
1054 remove_path(stash_index_path
.buf
);
1058 static int stash_patch(struct stash_info
*info
, const struct pathspec
*ps
,
1059 struct strbuf
*out_patch
, int quiet
)
1062 struct child_process cp_read_tree
= CHILD_PROCESS_INIT
;
1063 struct child_process cp_diff_tree
= CHILD_PROCESS_INIT
;
1064 struct index_state istate
= { NULL
};
1065 char *old_index_env
= NULL
, *old_repo_index_file
;
1067 remove_path(stash_index_path
.buf
);
1069 cp_read_tree
.git_cmd
= 1;
1070 strvec_pushl(&cp_read_tree
.args
, "read-tree", "HEAD", NULL
);
1071 strvec_pushf(&cp_read_tree
.env_array
, "GIT_INDEX_FILE=%s",
1072 stash_index_path
.buf
);
1073 if (run_command(&cp_read_tree
)) {
1078 /* Find out what the user wants. */
1079 old_repo_index_file
= the_repository
->index_file
;
1080 the_repository
->index_file
= stash_index_path
.buf
;
1081 old_index_env
= xstrdup_or_null(getenv(INDEX_ENVIRONMENT
));
1082 setenv(INDEX_ENVIRONMENT
, the_repository
->index_file
, 1);
1084 ret
= run_add_interactive(NULL
, "--patch=stash", ps
);
1086 the_repository
->index_file
= old_repo_index_file
;
1087 if (old_index_env
&& *old_index_env
)
1088 setenv(INDEX_ENVIRONMENT
, old_index_env
, 1);
1090 unsetenv(INDEX_ENVIRONMENT
);
1091 FREE_AND_NULL(old_index_env
);
1093 /* State of the working tree. */
1094 if (write_index_as_tree(&info
->w_tree
, &istate
, stash_index_path
.buf
, 0,
1100 cp_diff_tree
.git_cmd
= 1;
1101 strvec_pushl(&cp_diff_tree
.args
, "diff-tree", "-p", "-U1", "HEAD",
1102 oid_to_hex(&info
->w_tree
), "--", NULL
);
1103 if (pipe_command(&cp_diff_tree
, NULL
, 0, out_patch
, 0, NULL
, 0)) {
1108 if (!out_patch
->len
) {
1110 fprintf_ln(stderr
, _("No changes selected"));
1115 discard_index(&istate
);
1116 remove_path(stash_index_path
.buf
);
1120 static int stash_working_tree(struct stash_info
*info
, const struct pathspec
*ps
)
1123 struct rev_info rev
;
1124 struct child_process cp_upd_index
= CHILD_PROCESS_INIT
;
1125 struct strbuf diff_output
= STRBUF_INIT
;
1126 struct index_state istate
= { NULL
};
1128 init_revisions(&rev
, NULL
);
1129 copy_pathspec(&rev
.prune_data
, ps
);
1131 set_alternate_index_output(stash_index_path
.buf
);
1132 if (reset_tree(&info
->i_tree
, 0, 0)) {
1136 set_alternate_index_output(NULL
);
1138 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
1139 rev
.diffopt
.format_callback
= add_diff_to_buf
;
1140 rev
.diffopt
.format_callback_data
= &diff_output
;
1142 if (read_cache_preload(&rev
.diffopt
.pathspec
) < 0) {
1147 add_pending_object(&rev
, parse_object(the_repository
, &info
->b_commit
),
1149 if (run_diff_index(&rev
, 0)) {
1154 cp_upd_index
.git_cmd
= 1;
1155 strvec_pushl(&cp_upd_index
.args
, "update-index",
1156 "--ignore-skip-worktree-entries",
1157 "-z", "--add", "--remove", "--stdin", NULL
);
1158 strvec_pushf(&cp_upd_index
.env_array
, "GIT_INDEX_FILE=%s",
1159 stash_index_path
.buf
);
1161 if (pipe_command(&cp_upd_index
, diff_output
.buf
, diff_output
.len
,
1162 NULL
, 0, NULL
, 0)) {
1167 if (write_index_as_tree(&info
->w_tree
, &istate
, stash_index_path
.buf
, 0,
1174 discard_index(&istate
);
1176 object_array_clear(&rev
.pending
);
1177 clear_pathspec(&rev
.prune_data
);
1178 strbuf_release(&diff_output
);
1179 remove_path(stash_index_path
.buf
);
1183 static int do_create_stash(const struct pathspec
*ps
, struct strbuf
*stash_msg_buf
,
1184 int include_untracked
, int patch_mode
,
1185 struct stash_info
*info
, struct strbuf
*patch
,
1190 int untracked_commit_option
= 0;
1191 const char *head_short_sha1
= NULL
;
1192 const char *branch_ref
= NULL
;
1193 const char *branch_name
= "(no branch)";
1194 struct commit
*head_commit
= NULL
;
1195 struct commit_list
*parents
= NULL
;
1196 struct strbuf msg
= STRBUF_INIT
;
1197 struct strbuf commit_tree_label
= STRBUF_INIT
;
1198 struct strbuf untracked_files
= STRBUF_INIT
;
1200 prepare_fallback_ident("git stash", "git@stash");
1202 read_cache_preload(NULL
);
1203 if (refresh_and_write_cache(REFRESH_QUIET
, 0, 0) < 0) {
1208 if (get_oid("HEAD", &info
->b_commit
)) {
1210 fprintf_ln(stderr
, _("You do not have "
1211 "the initial commit yet"));
1215 head_commit
= lookup_commit(the_repository
, &info
->b_commit
);
1218 if (!check_changes(ps
, include_untracked
, &untracked_files
)) {
1223 branch_ref
= resolve_ref_unsafe("HEAD", 0, NULL
, &flags
);
1224 if (flags
& REF_ISSYMREF
)
1225 branch_name
= strrchr(branch_ref
, '/') + 1;
1226 head_short_sha1
= find_unique_abbrev(&head_commit
->object
.oid
,
1228 strbuf_addf(&msg
, "%s: %s ", branch_name
, head_short_sha1
);
1229 pp_commit_easy(CMIT_FMT_ONELINE
, head_commit
, &msg
);
1231 strbuf_addf(&commit_tree_label
, "index on %s\n", msg
.buf
);
1232 commit_list_insert(head_commit
, &parents
);
1233 if (write_cache_as_tree(&info
->i_tree
, 0, NULL
) ||
1234 commit_tree(commit_tree_label
.buf
, commit_tree_label
.len
,
1235 &info
->i_tree
, parents
, &info
->i_commit
, NULL
, NULL
)) {
1237 fprintf_ln(stderr
, _("Cannot save the current "
1243 if (include_untracked
) {
1244 if (save_untracked_files(info
, &msg
, untracked_files
)) {
1246 fprintf_ln(stderr
, _("Cannot save "
1247 "the untracked files"));
1251 untracked_commit_option
= 1;
1254 ret
= stash_patch(info
, ps
, patch
, quiet
);
1257 fprintf_ln(stderr
, _("Cannot save the current "
1260 } else if (ret
> 0) {
1264 if (stash_working_tree(info
, ps
)) {
1266 fprintf_ln(stderr
, _("Cannot save the current "
1273 if (!stash_msg_buf
->len
)
1274 strbuf_addf(stash_msg_buf
, "WIP on %s", msg
.buf
);
1276 strbuf_insertf(stash_msg_buf
, 0, "On %s: ", branch_name
);
1279 * `parents` will be empty after calling `commit_tree()`, so there is
1280 * no need to call `free_commit_list()`
1283 if (untracked_commit_option
)
1284 commit_list_insert(lookup_commit(the_repository
,
1287 commit_list_insert(lookup_commit(the_repository
, &info
->i_commit
),
1289 commit_list_insert(head_commit
, &parents
);
1291 if (commit_tree(stash_msg_buf
->buf
, stash_msg_buf
->len
, &info
->w_tree
,
1292 parents
, &info
->w_commit
, NULL
, NULL
)) {
1294 fprintf_ln(stderr
, _("Cannot record "
1295 "working tree state"));
1301 strbuf_release(&commit_tree_label
);
1302 strbuf_release(&msg
);
1303 strbuf_release(&untracked_files
);
1307 static int create_stash(int argc
, const char **argv
, const char *prefix
)
1310 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1311 struct stash_info info
;
1314 /* Starting with argv[1], since argv[0] is "create" */
1315 strbuf_join_argv(&stash_msg_buf
, argc
- 1, ++argv
, ' ');
1317 memset(&ps
, 0, sizeof(ps
));
1318 if (!check_changes_tracked_files(&ps
))
1321 ret
= do_create_stash(&ps
, &stash_msg_buf
, 0, 0, &info
,
1324 printf_ln("%s", oid_to_hex(&info
.w_commit
));
1326 strbuf_release(&stash_msg_buf
);
1330 static int do_push_stash(const struct pathspec
*ps
, const char *stash_msg
, int quiet
,
1331 int keep_index
, int patch_mode
, int include_untracked
)
1334 struct stash_info info
;
1335 struct strbuf patch
= STRBUF_INIT
;
1336 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1337 struct strbuf untracked_files
= STRBUF_INIT
;
1339 if (patch_mode
&& keep_index
== -1)
1342 if (patch_mode
&& include_untracked
) {
1343 fprintf_ln(stderr
, _("Can't use --patch and --include-untracked"
1344 " or --all at the same time"));
1349 read_cache_preload(NULL
);
1350 if (!include_untracked
&& ps
->nr
) {
1352 char *ps_matched
= xcalloc(ps
->nr
, 1);
1354 for (i
= 0; i
< active_nr
; i
++)
1355 ce_path_match(&the_index
, active_cache
[i
], ps
,
1358 if (report_path_error(ps_matched
, ps
)) {
1359 fprintf_ln(stderr
, _("Did you forget to 'git add'?"));
1367 if (refresh_and_write_cache(REFRESH_QUIET
, 0, 0)) {
1372 if (!check_changes(ps
, include_untracked
, &untracked_files
)) {
1374 printf_ln(_("No local changes to save"));
1378 if (!reflog_exists(ref_stash
) && do_clear_stash()) {
1381 fprintf_ln(stderr
, _("Cannot initialize stash"));
1386 strbuf_addstr(&stash_msg_buf
, stash_msg
);
1387 if (do_create_stash(ps
, &stash_msg_buf
, include_untracked
, patch_mode
,
1388 &info
, &patch
, quiet
)) {
1393 if (do_store_stash(&info
.w_commit
, stash_msg_buf
.buf
, 1)) {
1396 fprintf_ln(stderr
, _("Cannot save the current status"));
1401 printf_ln(_("Saved working directory and index state %s"),
1405 if (include_untracked
&& !ps
->nr
) {
1406 struct child_process cp
= CHILD_PROCESS_INIT
;
1409 strvec_pushl(&cp
.args
, "clean", "--force",
1410 "--quiet", "-d", NULL
);
1411 if (include_untracked
== INCLUDE_ALL_FILES
)
1412 strvec_push(&cp
.args
, "-x");
1413 if (run_command(&cp
)) {
1420 struct child_process cp_add
= CHILD_PROCESS_INIT
;
1421 struct child_process cp_diff
= CHILD_PROCESS_INIT
;
1422 struct child_process cp_apply
= CHILD_PROCESS_INIT
;
1423 struct strbuf out
= STRBUF_INIT
;
1426 strvec_push(&cp_add
.args
, "add");
1427 if (!include_untracked
)
1428 strvec_push(&cp_add
.args
, "-u");
1429 if (include_untracked
== INCLUDE_ALL_FILES
)
1430 strvec_push(&cp_add
.args
, "--force");
1431 strvec_push(&cp_add
.args
, "--");
1432 add_pathspecs(&cp_add
.args
, ps
);
1433 if (run_command(&cp_add
)) {
1438 cp_diff
.git_cmd
= 1;
1439 strvec_pushl(&cp_diff
.args
, "diff-index", "-p",
1440 "--cached", "--binary", "HEAD", "--",
1442 add_pathspecs(&cp_diff
.args
, ps
);
1443 if (pipe_command(&cp_diff
, NULL
, 0, &out
, 0, NULL
, 0)) {
1448 cp_apply
.git_cmd
= 1;
1449 strvec_pushl(&cp_apply
.args
, "apply", "--index",
1451 if (pipe_command(&cp_apply
, out
.buf
, out
.len
, NULL
, 0,
1457 struct child_process cp
= CHILD_PROCESS_INIT
;
1459 strvec_pushl(&cp
.args
, "reset", "--hard", "-q",
1460 "--no-recurse-submodules", NULL
);
1461 if (run_command(&cp
)) {
1467 if (keep_index
== 1 && !is_null_oid(&info
.i_tree
)) {
1468 struct child_process cp
= CHILD_PROCESS_INIT
;
1471 strvec_pushl(&cp
.args
, "checkout", "--no-overlay",
1472 oid_to_hex(&info
.i_tree
), "--", NULL
);
1474 strvec_push(&cp
.args
, ":/");
1476 add_pathspecs(&cp
.args
, ps
);
1477 if (run_command(&cp
)) {
1484 struct child_process cp
= CHILD_PROCESS_INIT
;
1487 strvec_pushl(&cp
.args
, "apply", "-R", NULL
);
1489 if (pipe_command(&cp
, patch
.buf
, patch
.len
, NULL
, 0, NULL
, 0)) {
1491 fprintf_ln(stderr
, _("Cannot remove "
1492 "worktree changes"));
1497 if (keep_index
< 1) {
1498 struct child_process cp
= CHILD_PROCESS_INIT
;
1501 strvec_pushl(&cp
.args
, "reset", "-q", "--", NULL
);
1502 add_pathspecs(&cp
.args
, ps
);
1503 if (run_command(&cp
)) {
1512 strbuf_release(&stash_msg_buf
);
1516 static int push_stash(int argc
, const char **argv
, const char *prefix
,
1519 int force_assume
= 0;
1520 int keep_index
= -1;
1522 int include_untracked
= 0;
1524 int pathspec_file_nul
= 0;
1525 const char *stash_msg
= NULL
;
1526 const char *pathspec_from_file
= NULL
;
1528 struct option options
[] = {
1529 OPT_BOOL('k', "keep-index", &keep_index
,
1531 OPT_BOOL('p', "patch", &patch_mode
,
1532 N_("stash in patch mode")),
1533 OPT__QUIET(&quiet
, N_("quiet mode")),
1534 OPT_BOOL('u', "include-untracked", &include_untracked
,
1535 N_("include untracked files in stash")),
1536 OPT_SET_INT('a', "all", &include_untracked
,
1537 N_("include ignore files"), 2),
1538 OPT_STRING('m', "message", &stash_msg
, N_("message"),
1539 N_("stash message")),
1540 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
1541 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
1546 force_assume
= !strcmp(argv
[0], "-p");
1547 argc
= parse_options(argc
, argv
, prefix
, options
,
1548 git_stash_push_usage
,
1549 PARSE_OPT_KEEP_DASHDASH
);
1553 if (!strcmp(argv
[0], "--")) {
1556 } else if (push_assumed
&& !force_assume
) {
1557 die("subcommand wasn't specified; 'push' can't be assumed due to unexpected token '%s'",
1562 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_FULL
| PATHSPEC_PREFIX_ORIGIN
,
1565 if (pathspec_from_file
) {
1567 die(_("--pathspec-from-file is incompatible with --patch"));
1570 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
1572 parse_pathspec_file(&ps
, 0,
1573 PATHSPEC_PREFER_FULL
| PATHSPEC_PREFIX_ORIGIN
,
1574 prefix
, pathspec_from_file
, pathspec_file_nul
);
1575 } else if (pathspec_file_nul
) {
1576 die(_("--pathspec-file-nul requires --pathspec-from-file"));
1579 return do_push_stash(&ps
, stash_msg
, quiet
, keep_index
, patch_mode
,
1583 static int save_stash(int argc
, const char **argv
, const char *prefix
)
1585 int keep_index
= -1;
1587 int include_untracked
= 0;
1590 const char *stash_msg
= NULL
;
1592 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1593 struct option options
[] = {
1594 OPT_BOOL('k', "keep-index", &keep_index
,
1596 OPT_BOOL('p', "patch", &patch_mode
,
1597 N_("stash in patch mode")),
1598 OPT__QUIET(&quiet
, N_("quiet mode")),
1599 OPT_BOOL('u', "include-untracked", &include_untracked
,
1600 N_("include untracked files in stash")),
1601 OPT_SET_INT('a', "all", &include_untracked
,
1602 N_("include ignore files"), 2),
1603 OPT_STRING('m', "message", &stash_msg
, "message",
1604 N_("stash message")),
1608 argc
= parse_options(argc
, argv
, prefix
, options
,
1609 git_stash_save_usage
,
1610 PARSE_OPT_KEEP_DASHDASH
);
1613 stash_msg
= strbuf_join_argv(&stash_msg_buf
, argc
, argv
, ' ');
1615 memset(&ps
, 0, sizeof(ps
));
1616 ret
= do_push_stash(&ps
, stash_msg
, quiet
, keep_index
,
1617 patch_mode
, include_untracked
);
1619 strbuf_release(&stash_msg_buf
);
1623 int cmd_stash(int argc
, const char **argv
, const char *prefix
)
1625 pid_t pid
= getpid();
1626 const char *index_file
;
1627 struct strvec args
= STRVEC_INIT
;
1629 struct option options
[] = {
1633 git_config(git_stash_config
, NULL
);
1635 if (use_legacy_stash
||
1636 !git_env_bool("GIT_TEST_STASH_USE_BUILTIN", -1))
1637 warning(_("the stash.useBuiltin support has been removed!\n"
1638 "See its entry in 'git help config' for details."));
1640 argc
= parse_options(argc
, argv
, prefix
, options
, git_stash_usage
,
1641 PARSE_OPT_KEEP_UNKNOWN
| PARSE_OPT_KEEP_DASHDASH
);
1643 index_file
= get_index_file();
1644 strbuf_addf(&stash_index_path
, "%s.stash.%" PRIuMAX
, index_file
,
1648 return !!push_stash(0, NULL
, prefix
, 0);
1649 else if (!strcmp(argv
[0], "apply"))
1650 return !!apply_stash(argc
, argv
, prefix
);
1651 else if (!strcmp(argv
[0], "clear"))
1652 return !!clear_stash(argc
, argv
, prefix
);
1653 else if (!strcmp(argv
[0], "drop"))
1654 return !!drop_stash(argc
, argv
, prefix
);
1655 else if (!strcmp(argv
[0], "pop"))
1656 return !!pop_stash(argc
, argv
, prefix
);
1657 else if (!strcmp(argv
[0], "branch"))
1658 return !!branch_stash(argc
, argv
, prefix
);
1659 else if (!strcmp(argv
[0], "list"))
1660 return !!list_stash(argc
, argv
, prefix
);
1661 else if (!strcmp(argv
[0], "show"))
1662 return !!show_stash(argc
, argv
, prefix
);
1663 else if (!strcmp(argv
[0], "store"))
1664 return !!store_stash(argc
, argv
, prefix
);
1665 else if (!strcmp(argv
[0], "create"))
1666 return !!create_stash(argc
, argv
, prefix
);
1667 else if (!strcmp(argv
[0], "push"))
1668 return !!push_stash(argc
, argv
, prefix
, 0);
1669 else if (!strcmp(argv
[0], "save"))
1670 return !!save_stash(argc
, argv
, prefix
);
1671 else if (*argv
[0] != '-')
1672 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv
[0]),
1673 git_stash_usage
, options
);
1675 /* Assume 'stash push' */
1676 strvec_push(&args
, "push");
1677 strvec_pushv(&args
, argv
);
1678 return !!push_stash(args
.nr
, args
.v
, prefix
, 1);