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"
10 #include "argv-array.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>...]]"),
31 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
32 " [-u|--include-untracked] [-a|--all] [<message>]"),
36 static const char * const git_stash_list_usage
[] = {
37 N_("git stash list [<options>]"),
41 static const char * const git_stash_show_usage
[] = {
42 N_("git stash show [<options>] [<stash>]"),
46 static const char * const git_stash_drop_usage
[] = {
47 N_("git stash drop [-q|--quiet] [<stash>]"),
51 static const char * const git_stash_pop_usage
[] = {
52 N_("git stash pop [--index] [-q|--quiet] [<stash>]"),
56 static const char * const git_stash_apply_usage
[] = {
57 N_("git stash apply [--index] [-q|--quiet] [<stash>]"),
61 static const char * const git_stash_branch_usage
[] = {
62 N_("git stash branch <branchname> [<stash>]"),
66 static const char * const git_stash_clear_usage
[] = {
67 N_("git stash clear"),
71 static const char * const git_stash_store_usage
[] = {
72 N_("git stash store [-m|--message <message>] [-q|--quiet] <commit>"),
76 static const char * const git_stash_push_usage
[] = {
77 N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
78 " [-u|--include-untracked] [-a|--all] [-m|--message <message>]\n"
79 " [--] [<pathspec>...]]"),
83 static const char * const git_stash_save_usage
[] = {
84 N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n"
85 " [-u|--include-untracked] [-a|--all] [<message>]"),
89 static const char *ref_stash
= "refs/stash";
90 static struct strbuf stash_index_path
= STRBUF_INIT
;
93 * w_commit is set to the commit containing the working tree
94 * b_commit is set to the base commit
95 * i_commit is set to the commit containing the index tree
96 * u_commit is set to the commit containing the untracked files tree
97 * w_tree is set to the working tree
98 * b_tree is set to the base tree
99 * i_tree is set to the index tree
100 * u_tree is set to the untracked files tree
103 struct object_id w_commit
;
104 struct object_id b_commit
;
105 struct object_id i_commit
;
106 struct object_id u_commit
;
107 struct object_id w_tree
;
108 struct object_id b_tree
;
109 struct object_id i_tree
;
110 struct object_id u_tree
;
111 struct strbuf revision
;
116 static void free_stash_info(struct stash_info
*info
)
118 strbuf_release(&info
->revision
);
121 static void assert_stash_like(struct stash_info
*info
, const char *revision
)
123 if (get_oidf(&info
->b_commit
, "%s^1", revision
) ||
124 get_oidf(&info
->w_tree
, "%s:", revision
) ||
125 get_oidf(&info
->b_tree
, "%s^1:", revision
) ||
126 get_oidf(&info
->i_tree
, "%s^2:", revision
))
127 die(_("'%s' is not a stash-like commit"), revision
);
130 static int get_stash_info(struct stash_info
*info
, int argc
, const char **argv
)
135 const char *revision
;
136 const char *commit
= NULL
;
137 struct object_id dummy
;
138 struct strbuf symbolic
= STRBUF_INIT
;
142 struct strbuf refs_msg
= STRBUF_INIT
;
144 for (i
= 0; i
< argc
; i
++)
145 strbuf_addf(&refs_msg
, " '%s'", argv
[i
]);
147 fprintf_ln(stderr
, _("Too many revisions specified:%s"),
149 strbuf_release(&refs_msg
);
157 strbuf_init(&info
->revision
, 0);
159 if (!ref_exists(ref_stash
)) {
160 free_stash_info(info
);
161 fprintf_ln(stderr
, _("No stash entries found."));
165 strbuf_addf(&info
->revision
, "%s@{0}", ref_stash
);
166 } else if (strspn(commit
, "0123456789") == strlen(commit
)) {
167 strbuf_addf(&info
->revision
, "%s@{%s}", ref_stash
, commit
);
169 strbuf_addstr(&info
->revision
, commit
);
172 revision
= info
->revision
.buf
;
174 if (get_oid(revision
, &info
->w_commit
)) {
175 error(_("%s is not a valid reference"), revision
);
176 free_stash_info(info
);
180 assert_stash_like(info
, revision
);
182 info
->has_u
= !get_oidf(&info
->u_tree
, "%s^3:", revision
);
184 end_of_rev
= strchrnul(revision
, '@');
185 strbuf_add(&symbolic
, revision
, end_of_rev
- revision
);
187 ret
= dwim_ref(symbolic
.buf
, symbolic
.len
, &dummy
, &expanded_ref
);
188 strbuf_release(&symbolic
);
190 case 0: /* Not found, but valid ref */
191 info
->is_stash_ref
= 0;
194 info
->is_stash_ref
= !strcmp(expanded_ref
, ref_stash
);
196 default: /* Invalid or ambiguous */
197 free_stash_info(info
);
201 return !(ret
== 0 || ret
== 1);
204 static int do_clear_stash(void)
206 struct object_id obj
;
207 if (get_oid(ref_stash
, &obj
))
210 return delete_ref(NULL
, ref_stash
, &obj
, 0);
213 static int clear_stash(int argc
, const char **argv
, const char *prefix
)
215 struct option options
[] = {
219 argc
= parse_options(argc
, argv
, prefix
, options
,
220 git_stash_clear_usage
,
221 PARSE_OPT_STOP_AT_NON_OPTION
);
224 return error(_("git stash clear with parameters is "
227 return do_clear_stash();
230 static int reset_tree(struct object_id
*i_tree
, int update
, int reset
)
233 struct unpack_trees_options opts
;
234 struct tree_desc t
[MAX_UNPACK_TREES
];
236 struct lock_file lock_file
= LOCK_INIT
;
238 read_cache_preload(NULL
);
239 if (refresh_cache(REFRESH_QUIET
))
242 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
244 memset(&opts
, 0, sizeof(opts
));
246 tree
= parse_tree_indirect(i_tree
);
247 if (parse_tree(tree
))
250 init_tree_desc(t
, tree
->buffer
, tree
->size
);
253 opts
.src_index
= &the_index
;
254 opts
.dst_index
= &the_index
;
257 opts
.update
= update
;
258 opts
.fn
= oneway_merge
;
260 if (unpack_trees(nr_trees
, t
, &opts
))
263 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
264 return error(_("unable to write new index file"));
269 static int diff_tree_binary(struct strbuf
*out
, struct object_id
*w_commit
)
271 struct child_process cp
= CHILD_PROCESS_INIT
;
272 const char *w_commit_hex
= oid_to_hex(w_commit
);
275 * Diff-tree would not be very hard to replace with a native function,
276 * however it should be done together with apply_cached.
279 argv_array_pushl(&cp
.args
, "diff-tree", "--binary", NULL
);
280 argv_array_pushf(&cp
.args
, "%s^2^..%s^2", w_commit_hex
, w_commit_hex
);
282 return pipe_command(&cp
, NULL
, 0, out
, 0, NULL
, 0);
285 static int apply_cached(struct strbuf
*out
)
287 struct child_process cp
= CHILD_PROCESS_INIT
;
290 * Apply currently only reads either from stdin or a file, thus
291 * apply_all_patches would have to be updated to optionally take a
295 argv_array_pushl(&cp
.args
, "apply", "--cached", NULL
);
296 return pipe_command(&cp
, out
->buf
, out
->len
, NULL
, 0, NULL
, 0);
299 static int reset_head(void)
301 struct child_process cp
= CHILD_PROCESS_INIT
;
304 * Reset is overall quite simple, however there is no current public
308 argv_array_push(&cp
.args
, "reset");
310 return run_command(&cp
);
313 static void add_diff_to_buf(struct diff_queue_struct
*q
,
314 struct diff_options
*options
,
319 for (i
= 0; i
< q
->nr
; i
++) {
320 strbuf_addstr(data
, q
->queue
[i
]->one
->path
);
322 /* NUL-terminate: will be fed to update-index -z */
323 strbuf_addch(data
, '\0');
327 static int get_newly_staged(struct strbuf
*out
, struct object_id
*c_tree
)
329 struct child_process cp
= CHILD_PROCESS_INIT
;
330 const char *c_tree_hex
= oid_to_hex(c_tree
);
333 * diff-index is very similar to diff-tree above, and should be
334 * converted together with update_index.
337 argv_array_pushl(&cp
.args
, "diff-index", "--cached", "--name-only",
338 "--diff-filter=A", NULL
);
339 argv_array_push(&cp
.args
, c_tree_hex
);
340 return pipe_command(&cp
, NULL
, 0, out
, 0, NULL
, 0);
343 static int update_index(struct strbuf
*out
)
345 struct child_process cp
= CHILD_PROCESS_INIT
;
348 * Update-index is very complicated and may need to have a public
349 * function exposed in order to remove this forking.
352 argv_array_pushl(&cp
.args
, "update-index", "--add", "--stdin", NULL
);
353 return pipe_command(&cp
, out
->buf
, out
->len
, NULL
, 0, NULL
, 0);
356 static int restore_untracked(struct object_id
*u_tree
)
359 struct child_process cp
= CHILD_PROCESS_INIT
;
362 * We need to run restore files from a given index, but without
363 * affecting the current index, so we use GIT_INDEX_FILE with
364 * run_command to fork processes that will not interfere.
367 argv_array_push(&cp
.args
, "read-tree");
368 argv_array_push(&cp
.args
, oid_to_hex(u_tree
));
369 argv_array_pushf(&cp
.env_array
, "GIT_INDEX_FILE=%s",
370 stash_index_path
.buf
);
371 if (run_command(&cp
)) {
372 remove_path(stash_index_path
.buf
);
376 child_process_init(&cp
);
378 argv_array_pushl(&cp
.args
, "checkout-index", "--all", NULL
);
379 argv_array_pushf(&cp
.env_array
, "GIT_INDEX_FILE=%s",
380 stash_index_path
.buf
);
382 res
= run_command(&cp
);
383 remove_path(stash_index_path
.buf
);
387 static int do_apply_stash(const char *prefix
, struct stash_info
*info
,
388 int index
, int quiet
)
391 int has_index
= index
;
392 struct merge_options o
;
393 struct object_id c_tree
;
394 struct object_id index_tree
;
395 struct commit
*result
;
396 const struct object_id
*bases
[1];
398 read_cache_preload(NULL
);
399 if (refresh_cache(REFRESH_QUIET
))
402 if (write_cache_as_tree(&c_tree
, 0, NULL
))
403 return error(_("cannot apply a stash in the middle of a merge"));
406 if (oideq(&info
->b_tree
, &info
->i_tree
) ||
407 oideq(&c_tree
, &info
->i_tree
)) {
410 struct strbuf out
= STRBUF_INIT
;
412 if (diff_tree_binary(&out
, &info
->w_commit
)) {
413 strbuf_release(&out
);
414 return error(_("could not generate diff %s^!."),
415 oid_to_hex(&info
->w_commit
));
418 ret
= apply_cached(&out
);
419 strbuf_release(&out
);
421 return error(_("conflicts in index."
422 "Try without --index."));
426 if (write_cache_as_tree(&index_tree
, 0, NULL
))
427 return error(_("could not save index tree"));
435 if (info
->has_u
&& restore_untracked(&info
->u_tree
))
436 return error(_("could not restore untracked files from stash"));
438 init_merge_options(&o
, the_repository
);
440 o
.branch1
= "Updated upstream";
441 o
.branch2
= "Stashed changes";
443 if (oideq(&info
->b_tree
, &c_tree
))
444 o
.branch1
= "Version stash was based on";
449 if (o
.verbosity
>= 3)
450 printf_ln(_("Merging %s with %s"), o
.branch1
, o
.branch2
);
452 bases
[0] = &info
->b_tree
;
454 ret
= merge_recursive_generic(&o
, &c_tree
, &info
->w_tree
, 1, bases
,
460 fprintf_ln(stderr
, _("Index was not unstashed."));
466 if (reset_tree(&index_tree
, 0, 0))
469 struct strbuf out
= STRBUF_INIT
;
471 if (get_newly_staged(&out
, &c_tree
)) {
472 strbuf_release(&out
);
476 if (reset_tree(&c_tree
, 0, 1)) {
477 strbuf_release(&out
);
481 ret
= update_index(&out
);
482 strbuf_release(&out
);
490 if (refresh_cache(REFRESH_QUIET
))
491 warning("could not refresh index");
493 struct child_process cp
= CHILD_PROCESS_INIT
;
496 * Status is quite simple and could be replaced with calls to
497 * wt_status in the future, but it adds complexities which may
498 * require more tests.
502 argv_array_push(&cp
.args
, "status");
509 static int apply_stash(int argc
, const char **argv
, const char *prefix
)
514 struct stash_info info
;
515 struct option options
[] = {
516 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
517 OPT_BOOL(0, "index", &index
,
518 N_("attempt to recreate the index")),
522 argc
= parse_options(argc
, argv
, prefix
, options
,
523 git_stash_apply_usage
, 0);
525 if (get_stash_info(&info
, argc
, argv
))
528 ret
= do_apply_stash(prefix
, &info
, index
, quiet
);
529 free_stash_info(&info
);
533 static int do_drop_stash(struct stash_info
*info
, int quiet
)
536 struct child_process cp_reflog
= CHILD_PROCESS_INIT
;
537 struct child_process cp
= CHILD_PROCESS_INIT
;
540 * reflog does not provide a simple function for deleting refs. One will
541 * need to be added to avoid implementing too much reflog code here
544 cp_reflog
.git_cmd
= 1;
545 argv_array_pushl(&cp_reflog
.args
, "reflog", "delete", "--updateref",
547 argv_array_push(&cp_reflog
.args
, info
->revision
.buf
);
548 ret
= run_command(&cp_reflog
);
551 printf_ln(_("Dropped %s (%s)"), info
->revision
.buf
,
552 oid_to_hex(&info
->w_commit
));
554 return error(_("%s: Could not drop stash entry"),
559 * This could easily be replaced by get_oid, but currently it will throw
560 * a fatal error when a reflog is empty, which we can not recover from.
563 /* Even though --quiet is specified, rev-parse still outputs the hash */
565 argv_array_pushl(&cp
.args
, "rev-parse", "--verify", "--quiet", NULL
);
566 argv_array_pushf(&cp
.args
, "%s@{0}", ref_stash
);
567 ret
= run_command(&cp
);
569 /* do_clear_stash if we just dropped the last stash entry */
576 static void assert_stash_ref(struct stash_info
*info
)
578 if (!info
->is_stash_ref
) {
579 error(_("'%s' is not a stash reference"), info
->revision
.buf
);
580 free_stash_info(info
);
585 static int drop_stash(int argc
, const char **argv
, const char *prefix
)
589 struct stash_info info
;
590 struct option options
[] = {
591 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
595 argc
= parse_options(argc
, argv
, prefix
, options
,
596 git_stash_drop_usage
, 0);
598 if (get_stash_info(&info
, argc
, argv
))
601 assert_stash_ref(&info
);
603 ret
= do_drop_stash(&info
, quiet
);
604 free_stash_info(&info
);
608 static int pop_stash(int argc
, const char **argv
, const char *prefix
)
613 struct stash_info info
;
614 struct option options
[] = {
615 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
616 OPT_BOOL(0, "index", &index
,
617 N_("attempt to recreate the index")),
621 argc
= parse_options(argc
, argv
, prefix
, options
,
622 git_stash_pop_usage
, 0);
624 if (get_stash_info(&info
, argc
, argv
))
627 assert_stash_ref(&info
);
628 if ((ret
= do_apply_stash(prefix
, &info
, index
, quiet
)))
629 printf_ln(_("The stash entry is kept in case "
630 "you need it again."));
632 ret
= do_drop_stash(&info
, quiet
);
634 free_stash_info(&info
);
638 static int branch_stash(int argc
, const char **argv
, const char *prefix
)
641 const char *branch
= NULL
;
642 struct stash_info info
;
643 struct child_process cp
= CHILD_PROCESS_INIT
;
644 struct option options
[] = {
648 argc
= parse_options(argc
, argv
, prefix
, options
,
649 git_stash_branch_usage
, 0);
652 fprintf_ln(stderr
, _("No branch name specified"));
658 if (get_stash_info(&info
, argc
- 1, argv
+ 1))
662 argv_array_pushl(&cp
.args
, "checkout", "-b", NULL
);
663 argv_array_push(&cp
.args
, branch
);
664 argv_array_push(&cp
.args
, oid_to_hex(&info
.b_commit
));
665 ret
= run_command(&cp
);
667 ret
= do_apply_stash(prefix
, &info
, 1, 0);
668 if (!ret
&& info
.is_stash_ref
)
669 ret
= do_drop_stash(&info
, 0);
671 free_stash_info(&info
);
676 static int list_stash(int argc
, const char **argv
, const char *prefix
)
678 struct child_process cp
= CHILD_PROCESS_INIT
;
679 struct option options
[] = {
683 argc
= parse_options(argc
, argv
, prefix
, options
,
684 git_stash_list_usage
,
685 PARSE_OPT_KEEP_UNKNOWN
);
687 if (!ref_exists(ref_stash
))
691 argv_array_pushl(&cp
.args
, "log", "--format=%gd: %gs", "-g",
692 "--first-parent", "-m", NULL
);
693 argv_array_pushv(&cp
.args
, argv
);
694 argv_array_push(&cp
.args
, ref_stash
);
695 argv_array_push(&cp
.args
, "--");
696 return run_command(&cp
);
699 static int show_stat
= 1;
700 static int show_patch
;
702 static int git_stash_config(const char *var
, const char *value
, void *cb
)
704 if (!strcmp(var
, "stash.showstat")) {
705 show_stat
= git_config_bool(var
, value
);
708 if (!strcmp(var
, "stash.showpatch")) {
709 show_patch
= git_config_bool(var
, value
);
712 return git_default_config(var
, value
, cb
);
715 static int show_stash(int argc
, const char **argv
, const char *prefix
)
719 struct stash_info info
;
721 struct argv_array stash_args
= ARGV_ARRAY_INIT
;
722 struct argv_array revision_args
= ARGV_ARRAY_INIT
;
723 struct option options
[] = {
727 init_diff_ui_defaults();
728 git_config(git_diff_ui_config
, NULL
);
729 init_revisions(&rev
, prefix
);
731 argv_array_push(&revision_args
, argv
[0]);
732 for (i
= 1; i
< argc
; i
++) {
733 if (argv
[i
][0] != '-')
734 argv_array_push(&stash_args
, argv
[i
]);
736 argv_array_push(&revision_args
, argv
[i
]);
739 ret
= get_stash_info(&info
, stash_args
.argc
, stash_args
.argv
);
740 argv_array_clear(&stash_args
);
745 * The config settings are applied only if there are not passed
748 if (revision_args
.argc
== 1) {
749 git_config(git_stash_config
, NULL
);
751 rev
.diffopt
.output_format
= DIFF_FORMAT_DIFFSTAT
;
754 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
756 if (!show_stat
&& !show_patch
) {
757 free_stash_info(&info
);
762 argc
= setup_revisions(revision_args
.argc
, revision_args
.argv
, &rev
, NULL
);
764 free_stash_info(&info
);
765 usage_with_options(git_stash_show_usage
, options
);
767 if (!rev
.diffopt
.output_format
) {
768 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
769 diff_setup_done(&rev
.diffopt
);
772 rev
.diffopt
.flags
.recursive
= 1;
773 setup_diff_pager(&rev
.diffopt
);
774 diff_tree_oid(&info
.b_commit
, &info
.w_commit
, "", &rev
.diffopt
);
775 log_tree_diff_flush(&rev
);
777 free_stash_info(&info
);
778 return diff_result_code(&rev
.diffopt
, 0);
781 static int do_store_stash(const struct object_id
*w_commit
, const char *stash_msg
,
785 stash_msg
= "Created via \"git stash store\".";
787 if (update_ref(stash_msg
, ref_stash
, w_commit
, NULL
,
788 REF_FORCE_CREATE_REFLOG
,
789 quiet
? UPDATE_REFS_QUIET_ON_ERR
:
790 UPDATE_REFS_MSG_ON_ERR
)) {
792 fprintf_ln(stderr
, _("Cannot update %s with %s"),
793 ref_stash
, oid_to_hex(w_commit
));
801 static int store_stash(int argc
, const char **argv
, const char *prefix
)
804 const char *stash_msg
= NULL
;
805 struct object_id obj
;
806 struct object_context dummy
;
807 struct option options
[] = {
808 OPT__QUIET(&quiet
, N_("be quiet")),
809 OPT_STRING('m', "message", &stash_msg
, "message",
810 N_("stash message")),
814 argc
= parse_options(argc
, argv
, prefix
, options
,
815 git_stash_store_usage
,
816 PARSE_OPT_KEEP_UNKNOWN
);
820 fprintf_ln(stderr
, _("\"git stash store\" requires one "
821 "<commit> argument"));
825 if (get_oid_with_context(the_repository
,
826 argv
[0], quiet
? GET_OID_QUIETLY
: 0, &obj
,
829 fprintf_ln(stderr
, _("Cannot update %s with %s"),
834 return do_store_stash(&obj
, stash_msg
, quiet
);
837 static void add_pathspecs(struct argv_array
*args
,
838 const struct pathspec
*ps
) {
841 for (i
= 0; i
< ps
->nr
; i
++)
842 argv_array_push(args
, ps
->items
[i
].original
);
846 * `untracked_files` will be filled with the names of untracked files.
847 * The return value is:
849 * = 0 if there are not any untracked files
850 * > 0 if there are untracked files
852 static int get_untracked_files(const struct pathspec
*ps
, int include_untracked
,
853 struct strbuf
*untracked_files
)
859 struct dir_struct dir
;
861 memset(&dir
, 0, sizeof(dir
));
862 if (include_untracked
!= INCLUDE_ALL_FILES
)
863 setup_standard_excludes(&dir
);
865 seen
= xcalloc(ps
->nr
, 1);
867 max_len
= fill_directory(&dir
, the_repository
->index
, ps
);
868 for (i
= 0; i
< dir
.nr
; i
++) {
869 struct dir_entry
*ent
= dir
.entries
[i
];
870 if (dir_path_match(&the_index
, ent
, ps
, max_len
, seen
)) {
872 strbuf_addstr(untracked_files
, ent
->name
);
873 /* NUL-terminate: will be fed to update-index -z */
874 strbuf_addch(untracked_files
, '\0');
882 clear_directory(&dir
);
887 * The return value of `check_changes_tracked_files()` can be:
889 * < 0 if there was an error
890 * = 0 if there are no changes.
891 * > 0 if there are changes.
893 static int check_changes_tracked_files(const struct pathspec
*ps
)
897 struct object_id dummy
;
900 /* No initial commit. */
901 if (get_oid("HEAD", &dummy
))
904 if (read_cache() < 0)
907 init_revisions(&rev
, NULL
);
908 copy_pathspec(&rev
.prune_data
, ps
);
910 rev
.diffopt
.flags
.quick
= 1;
911 rev
.diffopt
.flags
.ignore_submodules
= 1;
914 add_head_to_pending(&rev
);
915 diff_setup_done(&rev
.diffopt
);
917 result
= run_diff_index(&rev
, 1);
918 if (diff_result_code(&rev
.diffopt
, result
)) {
923 object_array_clear(&rev
.pending
);
924 result
= run_diff_files(&rev
, 0);
925 if (diff_result_code(&rev
.diffopt
, result
)) {
931 clear_pathspec(&rev
.prune_data
);
936 * The function will fill `untracked_files` with the names of untracked files
937 * It will return 1 if there were any changes and 0 if there were not.
939 static int check_changes(const struct pathspec
*ps
, int include_untracked
,
940 struct strbuf
*untracked_files
)
943 if (check_changes_tracked_files(ps
))
946 if (include_untracked
&& get_untracked_files(ps
, include_untracked
,
953 static int save_untracked_files(struct stash_info
*info
, struct strbuf
*msg
,
957 struct strbuf untracked_msg
= STRBUF_INIT
;
958 struct child_process cp_upd_index
= CHILD_PROCESS_INIT
;
959 struct index_state istate
= { NULL
};
961 cp_upd_index
.git_cmd
= 1;
962 argv_array_pushl(&cp_upd_index
.args
, "update-index", "-z", "--add",
963 "--remove", "--stdin", NULL
);
964 argv_array_pushf(&cp_upd_index
.env_array
, "GIT_INDEX_FILE=%s",
965 stash_index_path
.buf
);
967 strbuf_addf(&untracked_msg
, "untracked files on %s\n", msg
->buf
);
968 if (pipe_command(&cp_upd_index
, files
.buf
, files
.len
, NULL
, 0,
974 if (write_index_as_tree(&info
->u_tree
, &istate
, stash_index_path
.buf
, 0,
980 if (commit_tree(untracked_msg
.buf
, untracked_msg
.len
,
981 &info
->u_tree
, NULL
, &info
->u_commit
, NULL
, NULL
)) {
987 discard_index(&istate
);
988 strbuf_release(&untracked_msg
);
989 remove_path(stash_index_path
.buf
);
993 static int stash_patch(struct stash_info
*info
, const struct pathspec
*ps
,
994 struct strbuf
*out_patch
, int quiet
)
997 struct child_process cp_read_tree
= CHILD_PROCESS_INIT
;
998 struct child_process cp_add_i
= CHILD_PROCESS_INIT
;
999 struct child_process cp_diff_tree
= CHILD_PROCESS_INIT
;
1000 struct index_state istate
= { NULL
};
1002 remove_path(stash_index_path
.buf
);
1004 cp_read_tree
.git_cmd
= 1;
1005 argv_array_pushl(&cp_read_tree
.args
, "read-tree", "HEAD", NULL
);
1006 argv_array_pushf(&cp_read_tree
.env_array
, "GIT_INDEX_FILE=%s",
1007 stash_index_path
.buf
);
1008 if (run_command(&cp_read_tree
)) {
1013 /* Find out what the user wants. */
1014 cp_add_i
.git_cmd
= 1;
1015 argv_array_pushl(&cp_add_i
.args
, "add--interactive", "--patch=stash",
1017 add_pathspecs(&cp_add_i
.args
, ps
);
1018 argv_array_pushf(&cp_add_i
.env_array
, "GIT_INDEX_FILE=%s",
1019 stash_index_path
.buf
);
1020 if (run_command(&cp_add_i
)) {
1025 /* State of the working tree. */
1026 if (write_index_as_tree(&info
->w_tree
, &istate
, stash_index_path
.buf
, 0,
1032 cp_diff_tree
.git_cmd
= 1;
1033 argv_array_pushl(&cp_diff_tree
.args
, "diff-tree", "-p", "HEAD",
1034 oid_to_hex(&info
->w_tree
), "--", NULL
);
1035 if (pipe_command(&cp_diff_tree
, NULL
, 0, out_patch
, 0, NULL
, 0)) {
1040 if (!out_patch
->len
) {
1042 fprintf_ln(stderr
, _("No changes selected"));
1047 discard_index(&istate
);
1048 remove_path(stash_index_path
.buf
);
1052 static int stash_working_tree(struct stash_info
*info
, const struct pathspec
*ps
)
1055 struct rev_info rev
;
1056 struct child_process cp_upd_index
= CHILD_PROCESS_INIT
;
1057 struct strbuf diff_output
= STRBUF_INIT
;
1058 struct index_state istate
= { NULL
};
1060 init_revisions(&rev
, NULL
);
1061 copy_pathspec(&rev
.prune_data
, ps
);
1063 set_alternate_index_output(stash_index_path
.buf
);
1064 if (reset_tree(&info
->i_tree
, 0, 0)) {
1068 set_alternate_index_output(NULL
);
1070 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
1071 rev
.diffopt
.format_callback
= add_diff_to_buf
;
1072 rev
.diffopt
.format_callback_data
= &diff_output
;
1074 if (read_cache_preload(&rev
.diffopt
.pathspec
) < 0) {
1079 add_pending_object(&rev
, parse_object(the_repository
, &info
->b_commit
),
1081 if (run_diff_index(&rev
, 0)) {
1086 cp_upd_index
.git_cmd
= 1;
1087 argv_array_pushl(&cp_upd_index
.args
, "update-index", "-z", "--add",
1088 "--remove", "--stdin", NULL
);
1089 argv_array_pushf(&cp_upd_index
.env_array
, "GIT_INDEX_FILE=%s",
1090 stash_index_path
.buf
);
1092 if (pipe_command(&cp_upd_index
, diff_output
.buf
, diff_output
.len
,
1093 NULL
, 0, NULL
, 0)) {
1098 if (write_index_as_tree(&info
->w_tree
, &istate
, stash_index_path
.buf
, 0,
1105 discard_index(&istate
);
1107 object_array_clear(&rev
.pending
);
1108 clear_pathspec(&rev
.prune_data
);
1109 strbuf_release(&diff_output
);
1110 remove_path(stash_index_path
.buf
);
1114 static int do_create_stash(const struct pathspec
*ps
, struct strbuf
*stash_msg_buf
,
1115 int include_untracked
, int patch_mode
,
1116 struct stash_info
*info
, struct strbuf
*patch
,
1121 int untracked_commit_option
= 0;
1122 const char *head_short_sha1
= NULL
;
1123 const char *branch_ref
= NULL
;
1124 const char *branch_name
= "(no branch)";
1125 struct commit
*head_commit
= NULL
;
1126 struct commit_list
*parents
= NULL
;
1127 struct strbuf msg
= STRBUF_INIT
;
1128 struct strbuf commit_tree_label
= STRBUF_INIT
;
1129 struct strbuf untracked_files
= STRBUF_INIT
;
1131 prepare_fallback_ident("git stash", "git@stash");
1133 read_cache_preload(NULL
);
1134 refresh_cache(REFRESH_QUIET
);
1136 if (get_oid("HEAD", &info
->b_commit
)) {
1138 fprintf_ln(stderr
, _("You do not have "
1139 "the initial commit yet"));
1143 head_commit
= lookup_commit(the_repository
, &info
->b_commit
);
1146 if (!check_changes(ps
, include_untracked
, &untracked_files
)) {
1151 branch_ref
= resolve_ref_unsafe("HEAD", 0, NULL
, &flags
);
1152 if (flags
& REF_ISSYMREF
)
1153 branch_name
= strrchr(branch_ref
, '/') + 1;
1154 head_short_sha1
= find_unique_abbrev(&head_commit
->object
.oid
,
1156 strbuf_addf(&msg
, "%s: %s ", branch_name
, head_short_sha1
);
1157 pp_commit_easy(CMIT_FMT_ONELINE
, head_commit
, &msg
);
1159 strbuf_addf(&commit_tree_label
, "index on %s\n", msg
.buf
);
1160 commit_list_insert(head_commit
, &parents
);
1161 if (write_cache_as_tree(&info
->i_tree
, 0, NULL
) ||
1162 commit_tree(commit_tree_label
.buf
, commit_tree_label
.len
,
1163 &info
->i_tree
, parents
, &info
->i_commit
, NULL
, NULL
)) {
1165 fprintf_ln(stderr
, _("Cannot save the current "
1171 if (include_untracked
) {
1172 if (save_untracked_files(info
, &msg
, untracked_files
)) {
1174 fprintf_ln(stderr
, _("Cannot save "
1175 "the untracked files"));
1179 untracked_commit_option
= 1;
1182 ret
= stash_patch(info
, ps
, patch
, quiet
);
1185 fprintf_ln(stderr
, _("Cannot save the current "
1188 } else if (ret
> 0) {
1192 if (stash_working_tree(info
, ps
)) {
1194 fprintf_ln(stderr
, _("Cannot save the current "
1201 if (!stash_msg_buf
->len
)
1202 strbuf_addf(stash_msg_buf
, "WIP on %s", msg
.buf
);
1204 strbuf_insertf(stash_msg_buf
, 0, "On %s: ", branch_name
);
1207 * `parents` will be empty after calling `commit_tree()`, so there is
1208 * no need to call `free_commit_list()`
1211 if (untracked_commit_option
)
1212 commit_list_insert(lookup_commit(the_repository
,
1215 commit_list_insert(lookup_commit(the_repository
, &info
->i_commit
),
1217 commit_list_insert(head_commit
, &parents
);
1219 if (commit_tree(stash_msg_buf
->buf
, stash_msg_buf
->len
, &info
->w_tree
,
1220 parents
, &info
->w_commit
, NULL
, NULL
)) {
1222 fprintf_ln(stderr
, _("Cannot record "
1223 "working tree state"));
1229 strbuf_release(&commit_tree_label
);
1230 strbuf_release(&msg
);
1231 strbuf_release(&untracked_files
);
1235 static int create_stash(int argc
, const char **argv
, const char *prefix
)
1238 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1239 struct stash_info info
;
1242 /* Starting with argv[1], since argv[0] is "create" */
1243 strbuf_join_argv(&stash_msg_buf
, argc
- 1, ++argv
, ' ');
1245 memset(&ps
, 0, sizeof(ps
));
1246 if (!check_changes_tracked_files(&ps
))
1249 ret
= do_create_stash(&ps
, &stash_msg_buf
, 0, 0, &info
,
1252 printf_ln("%s", oid_to_hex(&info
.w_commit
));
1254 strbuf_release(&stash_msg_buf
);
1258 static int do_push_stash(const struct pathspec
*ps
, const char *stash_msg
, int quiet
,
1259 int keep_index
, int patch_mode
, int include_untracked
)
1262 struct stash_info info
;
1263 struct strbuf patch
= STRBUF_INIT
;
1264 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1265 struct strbuf untracked_files
= STRBUF_INIT
;
1267 if (patch_mode
&& keep_index
== -1)
1270 if (patch_mode
&& include_untracked
) {
1271 fprintf_ln(stderr
, _("Can't use --patch and --include-untracked"
1272 " or --all at the same time"));
1277 read_cache_preload(NULL
);
1278 if (!include_untracked
&& ps
->nr
) {
1280 char *ps_matched
= xcalloc(ps
->nr
, 1);
1282 for (i
= 0; i
< active_nr
; i
++)
1283 ce_path_match(&the_index
, active_cache
[i
], ps
,
1286 if (report_path_error(ps_matched
, ps
)) {
1287 fprintf_ln(stderr
, _("Did you forget to 'git add'?"));
1295 if (refresh_cache(REFRESH_QUIET
)) {
1300 if (!check_changes(ps
, include_untracked
, &untracked_files
)) {
1302 printf_ln(_("No local changes to save"));
1306 if (!reflog_exists(ref_stash
) && do_clear_stash()) {
1309 fprintf_ln(stderr
, _("Cannot initialize stash"));
1314 strbuf_addstr(&stash_msg_buf
, stash_msg
);
1315 if (do_create_stash(ps
, &stash_msg_buf
, include_untracked
, patch_mode
,
1316 &info
, &patch
, quiet
)) {
1321 if (do_store_stash(&info
.w_commit
, stash_msg_buf
.buf
, 1)) {
1324 fprintf_ln(stderr
, _("Cannot save the current status"));
1329 printf_ln(_("Saved working directory and index state %s"),
1333 if (include_untracked
&& !ps
->nr
) {
1334 struct child_process cp
= CHILD_PROCESS_INIT
;
1337 argv_array_pushl(&cp
.args
, "clean", "--force",
1338 "--quiet", "-d", NULL
);
1339 if (include_untracked
== INCLUDE_ALL_FILES
)
1340 argv_array_push(&cp
.args
, "-x");
1341 if (run_command(&cp
)) {
1348 struct child_process cp_add
= CHILD_PROCESS_INIT
;
1349 struct child_process cp_diff
= CHILD_PROCESS_INIT
;
1350 struct child_process cp_apply
= CHILD_PROCESS_INIT
;
1351 struct strbuf out
= STRBUF_INIT
;
1354 argv_array_push(&cp_add
.args
, "add");
1355 if (!include_untracked
)
1356 argv_array_push(&cp_add
.args
, "-u");
1357 if (include_untracked
== INCLUDE_ALL_FILES
)
1358 argv_array_push(&cp_add
.args
, "--force");
1359 argv_array_push(&cp_add
.args
, "--");
1360 add_pathspecs(&cp_add
.args
, ps
);
1361 if (run_command(&cp_add
)) {
1366 cp_diff
.git_cmd
= 1;
1367 argv_array_pushl(&cp_diff
.args
, "diff-index", "-p",
1368 "--cached", "--binary", "HEAD", "--",
1370 add_pathspecs(&cp_diff
.args
, ps
);
1371 if (pipe_command(&cp_diff
, NULL
, 0, &out
, 0, NULL
, 0)) {
1376 cp_apply
.git_cmd
= 1;
1377 argv_array_pushl(&cp_apply
.args
, "apply", "--index",
1379 if (pipe_command(&cp_apply
, out
.buf
, out
.len
, NULL
, 0,
1385 struct child_process cp
= CHILD_PROCESS_INIT
;
1387 argv_array_pushl(&cp
.args
, "reset", "--hard", "-q",
1389 if (run_command(&cp
)) {
1395 if (keep_index
== 1 && !is_null_oid(&info
.i_tree
)) {
1396 struct child_process cp
= CHILD_PROCESS_INIT
;
1399 argv_array_pushl(&cp
.args
, "checkout", "--no-overlay",
1400 oid_to_hex(&info
.i_tree
), "--", NULL
);
1402 argv_array_push(&cp
.args
, ":/");
1404 add_pathspecs(&cp
.args
, ps
);
1405 if (run_command(&cp
)) {
1412 struct child_process cp
= CHILD_PROCESS_INIT
;
1415 argv_array_pushl(&cp
.args
, "apply", "-R", NULL
);
1417 if (pipe_command(&cp
, patch
.buf
, patch
.len
, NULL
, 0, NULL
, 0)) {
1419 fprintf_ln(stderr
, _("Cannot remove "
1420 "worktree changes"));
1425 if (keep_index
< 1) {
1426 struct child_process cp
= CHILD_PROCESS_INIT
;
1429 argv_array_pushl(&cp
.args
, "reset", "-q", "--", NULL
);
1430 add_pathspecs(&cp
.args
, ps
);
1431 if (run_command(&cp
)) {
1440 strbuf_release(&stash_msg_buf
);
1444 static int push_stash(int argc
, const char **argv
, const char *prefix
)
1446 int keep_index
= -1;
1448 int include_untracked
= 0;
1450 const char *stash_msg
= NULL
;
1452 struct option options
[] = {
1453 OPT_BOOL('k', "keep-index", &keep_index
,
1455 OPT_BOOL('p', "patch", &patch_mode
,
1456 N_("stash in patch mode")),
1457 OPT__QUIET(&quiet
, N_("quiet mode")),
1458 OPT_BOOL('u', "include-untracked", &include_untracked
,
1459 N_("include untracked files in stash")),
1460 OPT_SET_INT('a', "all", &include_untracked
,
1461 N_("include ignore files"), 2),
1462 OPT_STRING('m', "message", &stash_msg
, N_("message"),
1463 N_("stash message")),
1468 argc
= parse_options(argc
, argv
, prefix
, options
,
1469 git_stash_push_usage
,
1472 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_FULL
| PATHSPEC_PREFIX_ORIGIN
,
1474 return do_push_stash(&ps
, stash_msg
, quiet
, keep_index
, patch_mode
,
1478 static int save_stash(int argc
, const char **argv
, const char *prefix
)
1480 int keep_index
= -1;
1482 int include_untracked
= 0;
1485 const char *stash_msg
= NULL
;
1487 struct strbuf stash_msg_buf
= STRBUF_INIT
;
1488 struct option options
[] = {
1489 OPT_BOOL('k', "keep-index", &keep_index
,
1491 OPT_BOOL('p', "patch", &patch_mode
,
1492 N_("stash in patch mode")),
1493 OPT__QUIET(&quiet
, N_("quiet mode")),
1494 OPT_BOOL('u', "include-untracked", &include_untracked
,
1495 N_("include untracked files in stash")),
1496 OPT_SET_INT('a', "all", &include_untracked
,
1497 N_("include ignore files"), 2),
1498 OPT_STRING('m', "message", &stash_msg
, "message",
1499 N_("stash message")),
1503 argc
= parse_options(argc
, argv
, prefix
, options
,
1504 git_stash_save_usage
,
1505 PARSE_OPT_KEEP_DASHDASH
);
1508 stash_msg
= strbuf_join_argv(&stash_msg_buf
, argc
, argv
, ' ');
1510 memset(&ps
, 0, sizeof(ps
));
1511 ret
= do_push_stash(&ps
, stash_msg
, quiet
, keep_index
,
1512 patch_mode
, include_untracked
);
1514 strbuf_release(&stash_msg_buf
);
1518 static int use_builtin_stash(void)
1520 struct child_process cp
= CHILD_PROCESS_INIT
;
1521 struct strbuf out
= STRBUF_INIT
;
1522 int ret
, env
= git_env_bool("GIT_TEST_STASH_USE_BUILTIN", -1);
1527 argv_array_pushl(&cp
.args
,
1528 "config", "--bool", "stash.usebuiltin", NULL
);
1530 if (capture_command(&cp
, &out
, 6)) {
1531 strbuf_release(&out
);
1536 ret
= !strcmp("true", out
.buf
);
1537 strbuf_release(&out
);
1541 int cmd_stash(int argc
, const char **argv
, const char *prefix
)
1544 pid_t pid
= getpid();
1545 const char *index_file
;
1546 struct argv_array args
= ARGV_ARRAY_INIT
;
1548 struct option options
[] = {
1552 if (!use_builtin_stash()) {
1553 const char *path
= mkpath("%s/git-legacy-stash",
1556 if (sane_execvp(path
, (char **)argv
) < 0)
1557 die_errno(_("could not exec %s"), path
);
1559 BUG("sane_execvp() returned???");
1562 prefix
= setup_git_directory();
1563 trace_repo_setup(prefix
);
1566 git_config(git_diff_basic_config
, NULL
);
1568 argc
= parse_options(argc
, argv
, prefix
, options
, git_stash_usage
,
1569 PARSE_OPT_KEEP_UNKNOWN
| PARSE_OPT_KEEP_DASHDASH
);
1571 index_file
= get_index_file();
1572 strbuf_addf(&stash_index_path
, "%s.stash.%" PRIuMAX
, index_file
,
1576 return !!push_stash(0, NULL
, prefix
);
1577 else if (!strcmp(argv
[0], "apply"))
1578 return !!apply_stash(argc
, argv
, prefix
);
1579 else if (!strcmp(argv
[0], "clear"))
1580 return !!clear_stash(argc
, argv
, prefix
);
1581 else if (!strcmp(argv
[0], "drop"))
1582 return !!drop_stash(argc
, argv
, prefix
);
1583 else if (!strcmp(argv
[0], "pop"))
1584 return !!pop_stash(argc
, argv
, prefix
);
1585 else if (!strcmp(argv
[0], "branch"))
1586 return !!branch_stash(argc
, argv
, prefix
);
1587 else if (!strcmp(argv
[0], "list"))
1588 return !!list_stash(argc
, argv
, prefix
);
1589 else if (!strcmp(argv
[0], "show"))
1590 return !!show_stash(argc
, argv
, prefix
);
1591 else if (!strcmp(argv
[0], "store"))
1592 return !!store_stash(argc
, argv
, prefix
);
1593 else if (!strcmp(argv
[0], "create"))
1594 return !!create_stash(argc
, argv
, prefix
);
1595 else if (!strcmp(argv
[0], "push"))
1596 return !!push_stash(argc
, argv
, prefix
);
1597 else if (!strcmp(argv
[0], "save"))
1598 return !!save_stash(argc
, argv
, prefix
);
1599 else if (*argv
[0] != '-')
1600 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv
[0]),
1601 git_stash_usage
, options
);
1603 if (strcmp(argv
[0], "-p")) {
1604 while (++i
< argc
&& strcmp(argv
[i
], "--")) {
1606 * `akpqu` is a string which contains all short options,
1607 * except `-m` which is verified separately.
1609 if ((strlen(argv
[i
]) == 2) && *argv
[i
] == '-' &&
1610 strchr("akpqu", argv
[i
][1]))
1613 if (!strcmp(argv
[i
], "--all") ||
1614 !strcmp(argv
[i
], "--keep-index") ||
1615 !strcmp(argv
[i
], "--no-keep-index") ||
1616 !strcmp(argv
[i
], "--patch") ||
1617 !strcmp(argv
[i
], "--quiet") ||
1618 !strcmp(argv
[i
], "--include-untracked"))
1622 * `-m` and `--message=` are verified separately because
1623 * they need to be immediately followed by a string
1624 * (i.e.`-m"foobar"` or `--message="foobar"`).
1626 if (starts_with(argv
[i
], "-m") ||
1627 starts_with(argv
[i
], "--message="))
1630 usage_with_options(git_stash_usage
, options
);
1634 argv_array_push(&args
, "push");
1635 argv_array_pushv(&args
, argv
);
1636 return !!push_stash(args
.argc
, args
.argv
, prefix
);