3 #include "parse-options.h"
6 #include "cache-tree.h"
7 #include "unpack-trees.h"
8 #include "merge-recursive.h"
9 #include "argv-array.h"
10 #include "run-command.h"
17 #define INCLUDE_ALL_FILES 2
19 static const char * const git_stash_usage
[] = {
20 N_("git stash list [<options>]"),
21 N_("git stash show [<options>] [<stash>]"),
22 N_("git stash drop [-q|--quiet] [<stash>]"),
23 N_("git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
24 N_("git stash branch <branchname> [<stash>]"),
25 N_("git stash clear"),
26 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
27 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
28 " [--] [<pathspec>...]]"),
29 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
30 " [-u|--include-untracked] [-a|--all] [<message>]"),
34 static const char * const git_stash_list_usage
[] = {
35 N_("git stash list [<options>]"),
39 static const char * const git_stash_show_usage
[] = {
40 N_("git stash show [<options>] [<stash>]"),
44 static const char * const git_stash_drop_usage
[] = {
45 N_("git stash drop [-q|--quiet] [<stash>]"),
49 static const char * const git_stash_pop_usage
[] = {
50 N_("git stash pop [--index] [-q|--quiet] [<stash>]"),
54 static const char * const git_stash_apply_usage
[] = {
55 N_("git stash apply [--index] [-q|--quiet] [<stash>]"),
59 static const char * const git_stash_branch_usage
[] = {
60 N_("git stash branch <branchname> [<stash>]"),
64 static const char * const git_stash_clear_usage
[] = {
65 N_("git stash clear"),
69 static const char * const git_stash_store_usage
[] = {
70 N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"),
74 static const char * const git_stash_push_usage
[] = {
75 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
76 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
77 " [--] [<pathspec>...]]"),
81 static const char * const git_stash_save_usage
[] = {
82 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
83 " [-u|--include-untracked] [-a|--all] [<message>]"),
87 static const char *ref_stash
= "refs/stash";
88 static struct strbuf stash_index_path
= STRBUF_INIT
;
91 * w_commit is set to the commit containing the working tree
92 * b_commit is set to the base commit
93 * i_commit is set to the commit containing the index tree
94 * u_commit is set to the commit containing the untracked files tree
95 * w_tree is set to the working tree
96 * b_tree is set to the base tree
97 * i_tree is set to the index tree
98 * u_tree is set to the untracked files tree
101 struct object_id w_commit
;
102 struct object_id b_commit
;
103 struct object_id i_commit
;
104 struct object_id u_commit
;
105 struct object_id w_tree
;
106 struct object_id b_tree
;
107 struct object_id i_tree
;
108 struct object_id u_tree
;
109 struct strbuf revision
;
114 static void free_stash_info(struct stash_info
*info
)
116 strbuf_release(&info
->revision
);
119 static void assert_stash_like(struct stash_info
*info
, const char *revision
)
121 if (get_oidf(&info
->b_commit
, "%s^1", revision
) ||
122 get_oidf(&info
->w_tree
, "%s:", revision
) ||
123 get_oidf(&info
->b_tree
, "%s^1:", revision
) ||
124 get_oidf(&info
->i_tree
, "%s^2:", revision
))
125 die(_("'%s' is not a stash-like commit"), revision
);
128 static int get_stash_info(struct stash_info
*info
, int argc
, const char **argv
)
133 const char *revision
;
134 const char *commit
= NULL
;
135 struct object_id dummy
;
136 struct strbuf symbolic
= STRBUF_INIT
;
140 struct strbuf refs_msg
= STRBUF_INIT
;
142 for (i
= 0; i
< argc
; i
++)
143 strbuf_addf(&refs_msg
, " '%s'", argv
[i
]);
145 fprintf_ln(stderr
, _("Too many revisions specified:%s"),
147 strbuf_release(&refs_msg
);
155 strbuf_init(&info
->revision
, 0);
157 if (!ref_exists(ref_stash
)) {
158 free_stash_info(info
);
159 fprintf_ln(stderr
, _("No stash entries found."));
163 strbuf_addf(&info
->revision
, "%s@{0}", ref_stash
);
164 } else if (strspn(commit
, "0123456789") == strlen(commit
)) {
165 strbuf_addf(&info
->revision
, "%s@{%s}", ref_stash
, commit
);
167 strbuf_addstr(&info
->revision
, commit
);
170 revision
= info
->revision
.buf
;
172 if (get_oid(revision
, &info
->w_commit
)) {
173 error(_("%s is not a valid reference"), revision
);
174 free_stash_info(info
);
178 assert_stash_like(info
, revision
);
180 info
->has_u
= !get_oidf(&info
->u_tree
, "%s^3:", revision
);
182 end_of_rev
= strchrnul(revision
, '@');
183 strbuf_add(&symbolic
, revision
, end_of_rev
- revision
);
185 ret
= dwim_ref(symbolic
.buf
, symbolic
.len
, &dummy
, &expanded_ref
);
186 strbuf_release(&symbolic
);
188 case 0: /* Not found, but valid ref */
189 info
->is_stash_ref
= 0;
192 info
->is_stash_ref
= !strcmp(expanded_ref
, ref_stash
);
194 default: /* Invalid or ambiguous */
195 free_stash_info(info
);
199 return !(ret
== 0 || ret
== 1);
202 static int do_clear_stash(void)
204 struct object_id obj
;
205 if (get_oid(ref_stash
, &obj
))
208 return delete_ref(NULL
, ref_stash
, &obj
, 0);
211 static int clear_stash(int argc
, const char **argv
, const char *prefix
)
213 struct option options
[] = {
217 argc
= parse_options(argc
, argv
, prefix
, options
,
218 git_stash_clear_usage
,
219 PARSE_OPT_STOP_AT_NON_OPTION
);
222 return error(_("git stash clear with parameters is "
225 return do_clear_stash();
228 static int reset_tree(struct object_id
*i_tree
, int update
, int reset
)
231 struct unpack_trees_options opts
;
232 struct tree_desc t
[MAX_UNPACK_TREES
];
234 struct lock_file lock_file
= LOCK_INIT
;
236 read_cache_preload(NULL
);
237 if (refresh_cache(REFRESH_QUIET
))
240 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
242 memset(&opts
, 0, sizeof(opts
));
244 tree
= parse_tree_indirect(i_tree
);
245 if (parse_tree(tree
))
248 init_tree_desc(t
, tree
->buffer
, tree
->size
);
251 opts
.src_index
= &the_index
;
252 opts
.dst_index
= &the_index
;
255 opts
.update
= update
;
256 opts
.fn
= oneway_merge
;
258 if (unpack_trees(nr_trees
, t
, &opts
))
261 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
262 return error(_("unable to write new index file"));
267 static int diff_tree_binary(struct strbuf
*out
, struct object_id
*w_commit
)
269 struct child_process cp
= CHILD_PROCESS_INIT
;
270 const char *w_commit_hex
= oid_to_hex(w_commit
);
273 * Diff-tree would not be very hard to replace with a native function,
274 * however it should be done together with apply_cached.
277 argv_array_pushl(&cp
.args
, "diff-tree", "--binary", NULL
);
278 argv_array_pushf(&cp
.args
, "%s^2^..%s^2", w_commit_hex
, w_commit_hex
);
280 return pipe_command(&cp
, NULL
, 0, out
, 0, NULL
, 0);
283 static int apply_cached(struct strbuf
*out
)
285 struct child_process cp
= CHILD_PROCESS_INIT
;
288 * Apply currently only reads either from stdin or a file, thus
289 * apply_all_patches would have to be updated to optionally take a
293 argv_array_pushl(&cp
.args
, "apply", "--cached", NULL
);
294 return pipe_command(&cp
, out
->buf
, out
->len
, NULL
, 0, NULL
, 0);
297 static int reset_head(void)
299 struct child_process cp
= CHILD_PROCESS_INIT
;
302 * Reset is overall quite simple, however there is no current public
306 argv_array_push(&cp
.args
, "reset");
308 return run_command(&cp
);
311 static void add_diff_to_buf(struct diff_queue_struct
*q
,
312 struct diff_options
*options
,
317 for (i
= 0; i
< q
->nr
; i
++) {
318 strbuf_addstr(data
, q
->queue
[i
]->one
->path
);
320 /* NUL-terminate: will be fed to update-index -z */
321 strbuf_addch(data
, '\0');
325 static int get_newly_staged(struct strbuf
*out
, struct object_id
*c_tree
)
327 struct child_process cp
= CHILD_PROCESS_INIT
;
328 const char *c_tree_hex
= oid_to_hex(c_tree
);
331 * diff-index is very similar to diff-tree above, and should be
332 * converted together with update_index.
335 argv_array_pushl(&cp
.args
, "diff-index", "--cached", "--name-only",
336 "--diff-filter=A", NULL
);
337 argv_array_push(&cp
.args
, c_tree_hex
);
338 return pipe_command(&cp
, NULL
, 0, out
, 0, NULL
, 0);
341 static int update_index(struct strbuf
*out
)
343 struct child_process cp
= CHILD_PROCESS_INIT
;
346 * Update-index is very complicated and may need to have a public
347 * function exposed in order to remove this forking.
350 argv_array_pushl(&cp
.args
, "update-index", "--add", "--stdin", NULL
);
351 return pipe_command(&cp
, out
->buf
, out
->len
, NULL
, 0, NULL
, 0);
354 static int restore_untracked(struct object_id
*u_tree
)
357 struct child_process cp
= CHILD_PROCESS_INIT
;
360 * We need to run restore files from a given index, but without
361 * affecting the current index, so we use GIT_INDEX_FILE with
362 * run_command to fork processes that will not interfere.
365 argv_array_push(&cp
.args
, "read-tree");
366 argv_array_push(&cp
.args
, oid_to_hex(u_tree
));
367 argv_array_pushf(&cp
.env_array
, "GIT_INDEX_FILE=%s",
368 stash_index_path
.buf
);
369 if (run_command(&cp
)) {
370 remove_path(stash_index_path
.buf
);
374 child_process_init(&cp
);
376 argv_array_pushl(&cp
.args
, "checkout-index", "--all", NULL
);
377 argv_array_pushf(&cp
.env_array
, "GIT_INDEX_FILE=%s",
378 stash_index_path
.buf
);
380 res
= run_command(&cp
);
381 remove_path(stash_index_path
.buf
);
385 static int do_apply_stash(const char *prefix
, struct stash_info
*info
,
386 int index
, int quiet
)
389 int has_index
= index
;
390 struct merge_options o
;
391 struct object_id c_tree
;
392 struct object_id index_tree
;
393 struct commit
*result
;
394 const struct object_id
*bases
[1];
396 read_cache_preload(NULL
);
397 if (refresh_cache(REFRESH_QUIET
))
400 if (write_cache_as_tree(&c_tree
, 0, NULL
))
401 return error(_("cannot apply a stash in the middle of a merge"));
404 if (oideq(&info
->b_tree
, &info
->i_tree
) ||
405 oideq(&c_tree
, &info
->i_tree
)) {
408 struct strbuf out
= STRBUF_INIT
;
410 if (diff_tree_binary(&out
, &info
->w_commit
)) {
411 strbuf_release(&out
);
412 return error(_("could not generate diff %s^!."),
413 oid_to_hex(&info
->w_commit
));
416 ret
= apply_cached(&out
);
417 strbuf_release(&out
);
419 return error(_("conflicts in index."
420 "Try without --index."));
424 if (write_cache_as_tree(&index_tree
, 0, NULL
))
425 return error(_("could not save index tree"));
431 if (info
->has_u
&& restore_untracked(&info
->u_tree
))
432 return error(_("could not restore untracked files from stash"));
434 init_merge_options(&o
);
436 o
.branch1
= "Updated upstream";
437 o
.branch2
= "Stashed changes";
439 if (oideq(&info
->b_tree
, &c_tree
))
440 o
.branch1
= "Version stash was based on";
445 if (o
.verbosity
>= 3)
446 printf_ln(_("Merging %s with %s"), o
.branch1
, o
.branch2
);
448 bases
[0] = &info
->b_tree
;
450 ret
= merge_recursive_generic(&o
, &c_tree
, &info
->w_tree
, 1, bases
,
456 fprintf_ln(stderr
, _("Index was not unstashed."));
462 if (reset_tree(&index_tree
, 0, 0))
465 struct strbuf out
= STRBUF_INIT
;
467 if (get_newly_staged(&out
, &c_tree
)) {
468 strbuf_release(&out
);
472 if (reset_tree(&c_tree
, 0, 1)) {
473 strbuf_release(&out
);
477 ret
= update_index(&out
);
478 strbuf_release(&out
);
486 if (refresh_cache(REFRESH_QUIET
))
487 warning("could not refresh index");
489 struct child_process cp
= CHILD_PROCESS_INIT
;
492 * Status is quite simple and could be replaced with calls to
493 * wt_status in the future, but it adds complexities which may
494 * require more tests.
498 argv_array_push(&cp
.args
, "status");
505 static int apply_stash(int argc
, const char **argv
, const char *prefix
)
510 struct stash_info info
;
511 struct option options
[] = {
512 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
513 OPT_BOOL(0, "index", &index
,
514 N_("attempt to recreate the index")),
518 argc
= parse_options(argc
, argv
, prefix
, options
,
519 git_stash_apply_usage
, 0);
521 if (get_stash_info(&info
, argc
, argv
))
524 ret
= do_apply_stash(prefix
, &info
, index
, quiet
);
525 free_stash_info(&info
);
529 static int do_drop_stash(const char *prefix
, struct stash_info
*info
, int quiet
)
532 struct child_process cp_reflog
= CHILD_PROCESS_INIT
;
533 struct child_process cp
= CHILD_PROCESS_INIT
;
536 * reflog does not provide a simple function for deleting refs. One will
537 * need to be added to avoid implementing too much reflog code here
540 cp_reflog
.git_cmd
= 1;
541 argv_array_pushl(&cp_reflog
.args
, "reflog", "delete", "--updateref",
543 argv_array_push(&cp_reflog
.args
, info
->revision
.buf
);
544 ret
= run_command(&cp_reflog
);
547 printf_ln(_("Dropped %s (%s)"), info
->revision
.buf
,
548 oid_to_hex(&info
->w_commit
));
550 return error(_("%s: Could not drop stash entry"),
555 * This could easily be replaced by get_oid, but currently it will throw
556 * a fatal error when a reflog is empty, which we can not recover from.
559 /* Even though --quiet is specified, rev-parse still outputs the hash */
561 argv_array_pushl(&cp
.args
, "rev-parse", "--verify", "--quiet", NULL
);
562 argv_array_pushf(&cp
.args
, "%s@{0}", ref_stash
);
563 ret
= run_command(&cp
);
565 /* do_clear_stash if we just dropped the last stash entry */
572 static void assert_stash_ref(struct stash_info
*info
)
574 if (!info
->is_stash_ref
) {
575 error(_("'%s' is not a stash reference"), info
->revision
.buf
);
576 free_stash_info(info
);
581 static int drop_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")),
591 argc
= parse_options(argc
, argv
, prefix
, options
,
592 git_stash_drop_usage
, 0);
594 if (get_stash_info(&info
, argc
, argv
))
597 assert_stash_ref(&info
);
599 ret
= do_drop_stash(prefix
, &info
, quiet
);
600 free_stash_info(&info
);
604 static int pop_stash(int argc
, const char **argv
, const char *prefix
)
609 struct stash_info info
;
610 struct option options
[] = {
611 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
612 OPT_BOOL(0, "index", &index
,
613 N_("attempt to recreate the index")),
617 argc
= parse_options(argc
, argv
, prefix
, options
,
618 git_stash_pop_usage
, 0);
620 if (get_stash_info(&info
, argc
, argv
))
623 assert_stash_ref(&info
);
624 if ((ret
= do_apply_stash(prefix
, &info
, index
, quiet
)))
625 printf_ln(_("The stash entry is kept in case "
626 "you need it again."));
628 ret
= do_drop_stash(prefix
, &info
, quiet
);
630 free_stash_info(&info
);
634 static int branch_stash(int argc
, const char **argv
, const char *prefix
)
637 const char *branch
= NULL
;
638 struct stash_info info
;
639 struct child_process cp
= CHILD_PROCESS_INIT
;
640 struct option options
[] = {
644 argc
= parse_options(argc
, argv
, prefix
, options
,
645 git_stash_branch_usage
, 0);
648 fprintf_ln(stderr
, _("No branch name specified"));
654 if (get_stash_info(&info
, argc
- 1, argv
+ 1))
658 argv_array_pushl(&cp
.args
, "checkout", "-b", NULL
);
659 argv_array_push(&cp
.args
, branch
);
660 argv_array_push(&cp
.args
, oid_to_hex(&info
.b_commit
));
661 ret
= run_command(&cp
);
663 ret
= do_apply_stash(prefix
, &info
, 1, 0);
664 if (!ret
&& info
.is_stash_ref
)
665 ret
= do_drop_stash(prefix
, &info
, 0);
667 free_stash_info(&info
);
672 static int list_stash(int argc
, const char **argv
, const char *prefix
)
674 struct child_process cp
= CHILD_PROCESS_INIT
;
675 struct option options
[] = {
679 argc
= parse_options(argc
, argv
, prefix
, options
,
680 git_stash_list_usage
,
681 PARSE_OPT_KEEP_UNKNOWN
);
683 if (!ref_exists(ref_stash
))
687 argv_array_pushl(&cp
.args
, "log", "--format=%gd: %gs", "-g",
688 "--first-parent", "-m", NULL
);
689 argv_array_pushv(&cp
.args
, argv
);
690 argv_array_push(&cp
.args
, ref_stash
);
691 argv_array_push(&cp
.args
, "--");
692 return run_command(&cp
);
695 static int show_stat
= 1;
696 static int show_patch
;
698 static int git_stash_config(const char *var
, const char *value
, void *cb
)
700 if (!strcmp(var
, "stash.showstat")) {
701 show_stat
= git_config_bool(var
, value
);
704 if (!strcmp(var
, "stash.showpatch")) {
705 show_patch
= git_config_bool(var
, value
);
708 return git_default_config(var
, value
, cb
);
711 static int show_stash(int argc
, const char **argv
, const char *prefix
)
716 struct stash_info info
;
718 struct argv_array stash_args
= ARGV_ARRAY_INIT
;
719 struct option options
[] = {
723 init_diff_ui_defaults();
724 git_config(git_diff_ui_config
, NULL
);
725 init_revisions(&rev
, prefix
);
727 for (i
= 1; i
< argc
; i
++) {
728 if (argv
[i
][0] != '-')
729 argv_array_push(&stash_args
, argv
[i
]);
734 ret
= get_stash_info(&info
, stash_args
.argc
, stash_args
.argv
);
735 argv_array_clear(&stash_args
);
740 * The config settings are applied only if there are not passed
744 git_config(git_stash_config
, NULL
);
746 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
;
749 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
751 if (!show_stat
&& !show_patch
) {
752 free_stash_info(&info
);
757 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
759 free_stash_info(&info
);
760 usage_with_options(git_stash_show_usage
, options
);
763 rev
.diffopt
.flags
.recursive
= 1;
764 setup_diff_pager(&rev
.diffopt
);
765 diff_tree_oid(&info
.b_commit
, &info
.w_commit
, "", &rev
.diffopt
);
766 log_tree_diff_flush(&rev
);
768 free_stash_info(&info
);
769 return diff_result_code(&rev
.diffopt
, 0);
772 static int do_store_stash(const struct object_id
*w_commit
, const char *stash_msg
,
776 stash_msg
= "Created via \"git stash store\".";
778 if (update_ref(stash_msg
, ref_stash
, w_commit
, NULL
,
779 REF_FORCE_CREATE_REFLOG
,
780 quiet
? UPDATE_REFS_QUIET_ON_ERR
:
781 UPDATE_REFS_MSG_ON_ERR
)) {
783 fprintf_ln(stderr
, _("Cannot update %s with %s"),
784 ref_stash
, oid_to_hex(w_commit
));
792 static int store_stash(int argc
, const char **argv
, const char *prefix
)
795 const char *stash_msg
= NULL
;
796 struct object_id obj
;
797 struct object_context dummy
;
798 struct option options
[] = {
799 OPT__QUIET(&quiet
, N_("be quiet")),
800 OPT_STRING('m', "message", &stash_msg
, "message",
801 N_("stash message")),
805 argc
= parse_options(argc
, argv
, prefix
, options
,
806 git_stash_store_usage
,
807 PARSE_OPT_KEEP_UNKNOWN
);
811 fprintf_ln(stderr
, _("\"git stash store\" requires one "
812 "<commit> argument"));
816 if (get_oid_with_context(argv
[0], quiet
? GET_OID_QUIETLY
: 0, &obj
,
819 fprintf_ln(stderr
, _("Cannot update %s with %s"),
824 return do_store_stash(&obj
, stash_msg
, quiet
);
827 static void add_pathspecs(struct argv_array
*args
,
828 struct pathspec ps
) {
831 for (i
= 0; i
< ps
.nr
; i
++)
832 argv_array_push(args
, ps
.items
[i
].match
);
836 * `untracked_files` will be filled with the names of untracked files.
837 * The return value is:
839 * = 0 if there are not any untracked files
840 * > 0 if there are untracked files
842 static int get_untracked_files(struct pathspec ps
, int include_untracked
,
843 struct strbuf
*untracked_files
)
849 struct dir_struct dir
;
851 memset(&dir
, 0, sizeof(dir
));
852 if (include_untracked
!= INCLUDE_ALL_FILES
)
853 setup_standard_excludes(&dir
);
855 seen
= xcalloc(ps
.nr
, 1);
857 max_len
= fill_directory(&dir
, the_repository
->index
, &ps
);
858 for (i
= 0; i
< dir
.nr
; i
++) {
859 struct dir_entry
*ent
= dir
.entries
[i
];
860 if (dir_path_match(&the_index
, ent
, &ps
, max_len
, seen
)) {
862 strbuf_addstr(untracked_files
, ent
->name
);
863 /* NUL-terminate: will be fed to update-index -z */
864 strbuf_addch(untracked_files
, '\0');
872 clear_directory(&dir
);
877 * The return value of `check_changes_tracked_files()` can be:
879 * < 0 if there was an error
880 * = 0 if there are no changes.
881 * > 0 if there are changes.
883 static int check_changes_tracked_files(struct pathspec ps
)
887 struct object_id dummy
;
889 /* No initial commit. */
890 if (get_oid("HEAD", &dummy
))
893 if (read_cache() < 0)
896 init_revisions(&rev
, NULL
);
899 rev
.diffopt
.flags
.quick
= 1;
900 rev
.diffopt
.flags
.ignore_submodules
= 1;
903 add_head_to_pending(&rev
);
904 diff_setup_done(&rev
.diffopt
);
906 result
= run_diff_index(&rev
, 1);
907 if (diff_result_code(&rev
.diffopt
, result
))
910 object_array_clear(&rev
.pending
);
911 result
= run_diff_files(&rev
, 0);
912 if (diff_result_code(&rev
.diffopt
, result
))
919 * The function will fill `untracked_files` with the names of untracked files
920 * It will return 1 if there were any changes and 0 if there were not.
922 static int check_changes(struct pathspec ps
, int include_untracked
,
923 struct strbuf
*untracked_files
)
926 if (check_changes_tracked_files(ps
))
929 if (include_untracked
&& get_untracked_files(ps
, include_untracked
,
936 static int save_untracked_files(struct stash_info
*info
, struct strbuf
*msg
,
940 struct strbuf untracked_msg
= STRBUF_INIT
;
941 struct child_process cp_upd_index
= CHILD_PROCESS_INIT
;
942 struct index_state istate
= { NULL
};
944 cp_upd_index
.git_cmd
= 1;
945 argv_array_pushl(&cp_upd_index
.args
, "update-index", "-z", "--add",
946 "--remove", "--stdin", NULL
);
947 argv_array_pushf(&cp_upd_index
.env_array
, "GIT_INDEX_FILE=%s",
948 stash_index_path
.buf
);
950 strbuf_addf(&untracked_msg
, "untracked files on %s\n", msg
->buf
);
951 if (pipe_command(&cp_upd_index
, files
.buf
, files
.len
, NULL
, 0,
957 if (write_index_as_tree(&info
->u_tree
, &istate
, stash_index_path
.buf
, 0,
963 if (commit_tree(untracked_msg
.buf
, untracked_msg
.len
,
964 &info
->u_tree
, NULL
, &info
->u_commit
, NULL
, NULL
)) {
970 discard_index(&istate
);
971 strbuf_release(&untracked_msg
);
972 remove_path(stash_index_path
.buf
);
976 static int stash_patch(struct stash_info
*info
, struct pathspec ps
,
977 struct strbuf
*out_patch
, int quiet
)
980 struct child_process cp_read_tree
= CHILD_PROCESS_INIT
;
981 struct child_process cp_add_i
= CHILD_PROCESS_INIT
;
982 struct child_process cp_diff_tree
= CHILD_PROCESS_INIT
;
983 struct index_state istate
= { NULL
};
985 remove_path(stash_index_path
.buf
);
987 cp_read_tree
.git_cmd
= 1;
988 argv_array_pushl(&cp_read_tree
.args
, "read-tree", "HEAD", NULL
);
989 argv_array_pushf(&cp_read_tree
.env_array
, "GIT_INDEX_FILE=%s",
990 stash_index_path
.buf
);
991 if (run_command(&cp_read_tree
)) {
996 /* Find out what the user wants. */
997 cp_add_i
.git_cmd
= 1;
998 argv_array_pushl(&cp_add_i
.args
, "add--interactive", "--patch=stash",
1000 add_pathspecs(&cp_add_i
.args
, ps
);
1001 argv_array_pushf(&cp_add_i
.env_array
, "GIT_INDEX_FILE=%s",
1002 stash_index_path
.buf
);
1003 if (run_command(&cp_add_i
)) {
1008 /* State of the working tree. */
1009 if (write_index_as_tree(&info
->w_tree
, &istate
, stash_index_path
.buf
, 0,
1015 cp_diff_tree
.git_cmd
= 1;
1016 argv_array_pushl(&cp_diff_tree
.args
, "diff-tree", "-p", "HEAD",
1017 oid_to_hex(&info
->w_tree
), "--", NULL
);
1018 if (pipe_command(&cp_diff_tree
, NULL
, 0, out_patch
, 0, NULL
, 0)) {
1023 if (!out_patch
->len
) {
1025 fprintf_ln(stderr
, _("No changes selected"));
1030 discard_index(&istate
);
1031 remove_path(stash_index_path
.buf
);
1035 static int stash_working_tree(struct stash_info
*info
, struct pathspec ps
)
1038 struct rev_info rev
;
1039 struct child_process cp_upd_index
= CHILD_PROCESS_INIT
;
1040 struct strbuf diff_output
= STRBUF_INIT
;
1041 struct index_state istate
= { NULL
};
1043 init_revisions(&rev
, NULL
);
1045 set_alternate_index_output(stash_index_path
.buf
);
1046 if (reset_tree(&info
->i_tree
, 0, 0)) {
1050 set_alternate_index_output(NULL
);
1052 rev
.prune_data
= ps
;
1053 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
1054 rev
.diffopt
.format_callback
= add_diff_to_buf
;
1055 rev
.diffopt
.format_callback_data
= &diff_output
;
1057 if (read_cache_preload(&rev
.diffopt
.pathspec
) < 0) {
1062 add_pending_object(&rev
, parse_object(the_repository
, &info
->b_commit
),
1064 if (run_diff_index(&rev
, 0)) {
1069 cp_upd_index
.git_cmd
= 1;
1070 argv_array_pushl(&cp_upd_index
.args
, "update-index", "-z", "--add",
1071 "--remove", "--stdin", NULL
);
1072 argv_array_pushf(&cp_upd_index
.env_array
, "GIT_INDEX_FILE=%s",
1073 stash_index_path
.buf
);
1075 if (pipe_command(&cp_upd_index
, diff_output
.buf
, diff_output
.len
,
1076 NULL
, 0, NULL
, 0)) {
1081 if (write_index_as_tree(&info
->w_tree
, &istate
, stash_index_path
.buf
, 0,
1088 discard_index(&istate
);
1090 object_array_clear(&rev
.pending
);
1091 strbuf_release(&diff_output
);
1092 remove_path(stash_index_path
.buf
);
1096 static int do_create_stash(struct pathspec ps
, struct strbuf
*stash_msg_buf
,
1097 int include_untracked
, int patch_mode
,
1098 struct stash_info
*info
, struct strbuf
*patch
,
1103 int untracked_commit_option
= 0;
1104 const char *head_short_sha1
= NULL
;
1105 const char *branch_ref
= NULL
;
1106 const char *branch_name
= "(no branch)";
1107 struct commit
*head_commit
= NULL
;
1108 struct commit_list
*parents
= NULL
;
1109 struct strbuf msg
= STRBUF_INIT
;
1110 struct strbuf commit_tree_label
= STRBUF_INIT
;
1111 struct strbuf untracked_files
= STRBUF_INIT
;
1113 prepare_fallback_ident("git stash", "git@stash");
1115 read_cache_preload(NULL
);
1116 refresh_cache(REFRESH_QUIET
);
1118 if (get_oid("HEAD", &info
->b_commit
)) {
1120 fprintf_ln(stderr
, _("You do not have "
1121 "the initial commit yet"));
1125 head_commit
= lookup_commit(the_repository
, &info
->b_commit
);
1128 if (!check_changes(ps
, include_untracked
, &untracked_files
)) {
1133 branch_ref
= resolve_ref_unsafe("HEAD", 0, NULL
, &flags
);
1134 if (flags
& REF_ISSYMREF
)
1135 branch_name
= strrchr(branch_ref
, '/') + 1;
1136 head_short_sha1
= find_unique_abbrev(&head_commit
->object
.oid
,
1138 strbuf_addf(&msg
, "%s: %s ", branch_name
, head_short_sha1
);
1139 pp_commit_easy(CMIT_FMT_ONELINE
, head_commit
, &msg
);
1141 strbuf_addf(&commit_tree_label
, "index on %s\n", msg
.buf
);
1142 commit_list_insert(head_commit
, &parents
);
1143 if (write_cache_as_tree(&info
->i_tree
, 0, NULL
) ||
1144 commit_tree(commit_tree_label
.buf
, commit_tree_label
.len
,
1145 &info
->i_tree
, parents
, &info
->i_commit
, NULL
, NULL
)) {
1147 fprintf_ln(stderr
, _("Cannot save the current "
1153 if (include_untracked
) {
1154 if (save_untracked_files(info
, &msg
, untracked_files
)) {
1156 fprintf_ln(stderr
, _("Cannot save "
1157 "the untracked files"));
1161 untracked_commit_option
= 1;
1164 ret
= stash_patch(info
, ps
, patch
, quiet
);
1167 fprintf_ln(stderr
, _("Cannot save the current "
1170 } else if (ret
> 0) {
1174 if (stash_working_tree(info
, ps
)) {
1176 fprintf_ln(stderr
, _("Cannot save the current "
1183 if (!stash_msg_buf
->len
)
1184 strbuf_addf(stash_msg_buf
, "WIP on %s", msg
.buf
);
1186 strbuf_insertf(stash_msg_buf
, 0, "On %s: ", branch_name
);
1189 * `parents` will be empty after calling `commit_tree()`, so there is
1190 * no need to call `free_commit_list()`
1193 if (untracked_commit_option
)
1194 commit_list_insert(lookup_commit(the_repository
,
1197 commit_list_insert(lookup_commit(the_repository
, &info
->i_commit
),
1199 commit_list_insert(head_commit
, &parents
);
1201 if (commit_tree(stash_msg_buf
->buf
, stash_msg_buf
->len
, &info
->w_tree
,
1202 parents
, &info
->w_commit
, NULL
, NULL
)) {
1204 fprintf_ln(stderr
, _("Cannot record "
1205 "working tree state"));
1211 strbuf_release(&commit_tree_label
);
1212 strbuf_release(&msg
);
1213 strbuf_release(&untracked_files
);
1217 static int create_stash(int argc
, const char **argv
, const char *prefix
)
1220 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1221 struct stash_info info
;
1224 /* Starting with argv[1], since argv[0] is "create" */
1225 strbuf_join_argv(&stash_msg_buf
, argc
- 1, ++argv
, ' ');
1227 memset(&ps
, 0, sizeof(ps
));
1228 if (!check_changes_tracked_files(ps
))
1231 ret
= do_create_stash(ps
, &stash_msg_buf
, 0, 0, &info
,
1234 printf_ln("%s", oid_to_hex(&info
.w_commit
));
1236 strbuf_release(&stash_msg_buf
);
1240 static int do_push_stash(struct pathspec ps
, const char *stash_msg
, int quiet
,
1241 int keep_index
, int patch_mode
, int include_untracked
)
1244 struct stash_info info
;
1245 struct strbuf patch
= STRBUF_INIT
;
1246 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1247 struct strbuf untracked_files
= STRBUF_INIT
;
1249 if (patch_mode
&& keep_index
== -1)
1252 if (patch_mode
&& include_untracked
) {
1253 fprintf_ln(stderr
, _("Can't use --patch and --include-untracked"
1254 " or --all at the same time"));
1259 read_cache_preload(NULL
);
1260 if (!include_untracked
&& ps
.nr
) {
1262 char *ps_matched
= xcalloc(ps
.nr
, 1);
1264 for (i
= 0; i
< active_nr
; i
++)
1265 ce_path_match(&the_index
, active_cache
[i
], &ps
,
1268 if (report_path_error(ps_matched
, &ps
, NULL
)) {
1269 fprintf_ln(stderr
, _("Did you forget to 'git add'?"));
1277 if (refresh_cache(REFRESH_QUIET
)) {
1282 if (!check_changes(ps
, include_untracked
, &untracked_files
)) {
1284 printf_ln(_("No local changes to save"));
1288 if (!reflog_exists(ref_stash
) && do_clear_stash()) {
1291 fprintf_ln(stderr
, _("Cannot initialize stash"));
1296 strbuf_addstr(&stash_msg_buf
, stash_msg
);
1297 if (do_create_stash(ps
, &stash_msg_buf
, include_untracked
, patch_mode
,
1298 &info
, &patch
, quiet
)) {
1303 if (do_store_stash(&info
.w_commit
, stash_msg_buf
.buf
, 1)) {
1306 fprintf_ln(stderr
, _("Cannot save the current status"));
1311 printf_ln(_("Saved working directory and index state %s"),
1315 if (include_untracked
&& !ps
.nr
) {
1316 struct child_process cp
= CHILD_PROCESS_INIT
;
1319 argv_array_pushl(&cp
.args
, "clean", "--force",
1320 "--quiet", "-d", NULL
);
1321 if (include_untracked
== INCLUDE_ALL_FILES
)
1322 argv_array_push(&cp
.args
, "-x");
1323 if (run_command(&cp
)) {
1330 struct child_process cp_add
= CHILD_PROCESS_INIT
;
1331 struct child_process cp_diff
= CHILD_PROCESS_INIT
;
1332 struct child_process cp_apply
= CHILD_PROCESS_INIT
;
1333 struct strbuf out
= STRBUF_INIT
;
1336 argv_array_push(&cp_add
.args
, "add");
1337 if (!include_untracked
)
1338 argv_array_push(&cp_add
.args
, "-u");
1339 if (include_untracked
== INCLUDE_ALL_FILES
)
1340 argv_array_push(&cp_add
.args
, "--force");
1341 argv_array_push(&cp_add
.args
, "--");
1342 add_pathspecs(&cp_add
.args
, ps
);
1343 if (run_command(&cp_add
)) {
1348 cp_diff
.git_cmd
= 1;
1349 argv_array_pushl(&cp_diff
.args
, "diff-index", "-p",
1350 "--cached", "--binary", "HEAD", "--",
1352 add_pathspecs(&cp_diff
.args
, ps
);
1353 if (pipe_command(&cp_diff
, NULL
, 0, &out
, 0, NULL
, 0)) {
1358 cp_apply
.git_cmd
= 1;
1359 argv_array_pushl(&cp_apply
.args
, "apply", "--index",
1361 if (pipe_command(&cp_apply
, out
.buf
, out
.len
, NULL
, 0,
1367 struct child_process cp
= CHILD_PROCESS_INIT
;
1369 argv_array_pushl(&cp
.args
, "reset", "--hard", "-q",
1371 if (run_command(&cp
)) {
1377 if (keep_index
== 1 && !is_null_oid(&info
.i_tree
)) {
1378 struct child_process cp_ls
= CHILD_PROCESS_INIT
;
1379 struct child_process cp_checkout
= CHILD_PROCESS_INIT
;
1380 struct strbuf out
= STRBUF_INIT
;
1382 if (reset_tree(&info
.i_tree
, 0, 1)) {
1388 argv_array_pushl(&cp_ls
.args
, "ls-files", "-z",
1389 "--modified", "--", NULL
);
1391 add_pathspecs(&cp_ls
.args
, ps
);
1392 if (pipe_command(&cp_ls
, NULL
, 0, &out
, 0, NULL
, 0)) {
1397 cp_checkout
.git_cmd
= 1;
1398 argv_array_pushl(&cp_checkout
.args
, "checkout-index",
1399 "-z", "--force", "--stdin", NULL
);
1400 if (pipe_command(&cp_checkout
, out
.buf
, out
.len
, NULL
,
1408 struct child_process cp
= CHILD_PROCESS_INIT
;
1411 argv_array_pushl(&cp
.args
, "apply", "-R", NULL
);
1413 if (pipe_command(&cp
, patch
.buf
, patch
.len
, NULL
, 0, NULL
, 0)) {
1415 fprintf_ln(stderr
, _("Cannot remove "
1416 "worktree changes"));
1421 if (keep_index
< 1) {
1422 struct child_process cp
= CHILD_PROCESS_INIT
;
1425 argv_array_pushl(&cp
.args
, "reset", "-q", "--", NULL
);
1426 add_pathspecs(&cp
.args
, ps
);
1427 if (run_command(&cp
)) {
1436 strbuf_release(&stash_msg_buf
);
1440 static int push_stash(int argc
, const char **argv
, const char *prefix
)
1442 int keep_index
= -1;
1444 int include_untracked
= 0;
1446 const char *stash_msg
= NULL
;
1448 struct option options
[] = {
1449 OPT_BOOL('k', "keep-index", &keep_index
,
1451 OPT_BOOL('p', "patch", &patch_mode
,
1452 N_("stash in patch mode")),
1453 OPT__QUIET(&quiet
, N_("quiet mode")),
1454 OPT_BOOL('u', "include-untracked", &include_untracked
,
1455 N_("include untracked files in stash")),
1456 OPT_SET_INT('a', "all", &include_untracked
,
1457 N_("include ignore files"), 2),
1458 OPT_STRING('m', "message", &stash_msg
, N_("message"),
1459 N_("stash message")),
1464 argc
= parse_options(argc
, argv
, prefix
, options
,
1465 git_stash_push_usage
,
1468 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_FULL
, prefix
, argv
);
1469 return do_push_stash(ps
, stash_msg
, quiet
, keep_index
, patch_mode
,
1473 static int save_stash(int argc
, const char **argv
, const char *prefix
)
1475 int keep_index
= -1;
1477 int include_untracked
= 0;
1480 const char *stash_msg
= NULL
;
1482 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1483 struct option options
[] = {
1484 OPT_BOOL('k', "keep-index", &keep_index
,
1486 OPT_BOOL('p', "patch", &patch_mode
,
1487 N_("stash in patch mode")),
1488 OPT__QUIET(&quiet
, N_("quiet mode")),
1489 OPT_BOOL('u', "include-untracked", &include_untracked
,
1490 N_("include untracked files in stash")),
1491 OPT_SET_INT('a', "all", &include_untracked
,
1492 N_("include ignore files"), 2),
1493 OPT_STRING('m', "message", &stash_msg
, "message",
1494 N_("stash message")),
1498 argc
= parse_options(argc
, argv
, prefix
, options
,
1499 git_stash_save_usage
,
1500 PARSE_OPT_KEEP_DASHDASH
);
1503 stash_msg
= strbuf_join_argv(&stash_msg_buf
, argc
, argv
, ' ');
1505 memset(&ps
, 0, sizeof(ps
));
1506 ret
= do_push_stash(ps
, stash_msg
, quiet
, keep_index
,
1507 patch_mode
, include_untracked
);
1509 strbuf_release(&stash_msg_buf
);
1513 int cmd_stash(int argc
, const char **argv
, const char *prefix
)
1516 pid_t pid
= getpid();
1517 const char *index_file
;
1518 struct argv_array args
= ARGV_ARRAY_INIT
;
1520 struct option options
[] = {
1524 git_config(git_diff_basic_config
, NULL
);
1526 argc
= parse_options(argc
, argv
, prefix
, options
, git_stash_usage
,
1527 PARSE_OPT_KEEP_UNKNOWN
| PARSE_OPT_KEEP_DASHDASH
);
1529 index_file
= get_index_file();
1530 strbuf_addf(&stash_index_path
, "%s.stash.%" PRIuMAX
, index_file
,
1534 return !!push_stash(0, NULL
, prefix
);
1535 else if (!strcmp(argv
[0], "apply"))
1536 return !!apply_stash(argc
, argv
, prefix
);
1537 else if (!strcmp(argv
[0], "clear"))
1538 return !!clear_stash(argc
, argv
, prefix
);
1539 else if (!strcmp(argv
[0], "drop"))
1540 return !!drop_stash(argc
, argv
, prefix
);
1541 else if (!strcmp(argv
[0], "pop"))
1542 return !!pop_stash(argc
, argv
, prefix
);
1543 else if (!strcmp(argv
[0], "branch"))
1544 return !!branch_stash(argc
, argv
, prefix
);
1545 else if (!strcmp(argv
[0], "list"))
1546 return !!list_stash(argc
, argv
, prefix
);
1547 else if (!strcmp(argv
[0], "show"))
1548 return !!show_stash(argc
, argv
, prefix
);
1549 else if (!strcmp(argv
[0], "store"))
1550 return !!store_stash(argc
, argv
, prefix
);
1551 else if (!strcmp(argv
[0], "create"))
1552 return !!create_stash(argc
, argv
, prefix
);
1553 else if (!strcmp(argv
[0], "push"))
1554 return !!push_stash(argc
, argv
, prefix
);
1555 else if (!strcmp(argv
[0], "save"))
1556 return !!save_stash(argc
, argv
, prefix
);
1557 else if (*argv
[0] != '-')
1558 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv
[0]),
1559 git_stash_usage
, options
);
1561 if (strcmp(argv
[0], "-p")) {
1562 while (++i
< argc
&& strcmp(argv
[i
], "--")) {
1564 * `akpqu` is a string which contains all short options,
1565 * except `-m` which is verified separately.
1567 if ((strlen(argv
[i
]) == 2) && *argv
[i
] == '-' &&
1568 strchr("akpqu", argv
[i
][1]))
1571 if (!strcmp(argv
[i
], "--all") ||
1572 !strcmp(argv
[i
], "--keep-index") ||
1573 !strcmp(argv
[i
], "--no-keep-index") ||
1574 !strcmp(argv
[i
], "--patch") ||
1575 !strcmp(argv
[i
], "--quiet") ||
1576 !strcmp(argv
[i
], "--include-untracked"))
1580 * `-m` and `--message=` are verified separately because
1581 * they need to be immediately followed by a string
1582 * (i.e.`-m"foobar"` or `--message="foobar"`).
1584 if (starts_with(argv
[i
], "-m") ||
1585 starts_with(argv
[i
], "--message="))
1588 usage_with_options(git_stash_usage
, options
);
1592 argv_array_push(&args
, "push");
1593 argv_array_pushv(&args
, argv
);
1594 return !!push_stash(args
.argc
, args
.argv
, prefix
);