@matheusbsbs / matheus-bagliano / commits / 12b6e1367a
Fork 0

sequencer: implement save_autostash()

Extract common functionality of apply_autostash() into apply_save_autostash() and use it to implement save_autostash(). This function will be used in a future commit. The difference between save_autostash() and apply_autostash() is that the former does not try to apply the stash. It skips that step and just stores the created entry in the stash reflog. This is useful in the case where we abort an operation when an autostash is present but we don't want to dirty the worktree with the application of the stash. For example, in a future commit, we will implement `git merge --autostash`. Since merges can be aborted using `git reset --hard`, we'd make use of save_autostash() to save the autostash entry instead of applying it to the worktree thus keeping the worktree undirtied. Signed-off-by: Denton Liu <liu.denton@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Denton Liu committed Apr 7, 2020 at 10:28 UTC 12b6e1367af429628dbaeaa0c7518cacc620d786
2 files changed +28 -10
sequencer.c
+27 -10
@@ -3707,7 +3707,7 @@ void create_autostash(struct repository *r, const char *path,
3707 strbuf_release(&buf);
3708 }
3709
3710 -int apply_autostash(const char *path)
3710 +static int apply_save_autostash(const char *path, int attempt_apply)
3711 {
3712 struct strbuf stash_oid = STRBUF_INIT;
3713 struct child_process child = CHILD_PROCESS_INIT;
@@ -3720,13 +3720,17 @@ int apply_autostash(const char *path)
3720 }
3721 strbuf_trim(&stash_oid);
3722
3723 - child.git_cmd = 1;
3724 - child.no_stdout = 1;
3725 - child.no_stderr = 1;
3726 - argv_array_push(&child.args, "stash");
3727 - argv_array_push(&child.args, "apply");
3728 - argv_array_push(&child.args, stash_oid.buf);
3729 - if (!run_command(&child))
3723 + if (attempt_apply) {
3724 + child.git_cmd = 1;
3725 + child.no_stdout = 1;
3726 + child.no_stderr = 1;
3727 + argv_array_push(&child.args, "stash");
3728 + argv_array_push(&child.args, "apply");
3729 + argv_array_push(&child.args, stash_oid.buf);
3730 + ret = run_command(&child);
3731 + }
3732 +
3733 + if (attempt_apply && !ret)
3734 fprintf(stderr, _("Applied autostash.\n"));
3735 else {
3736 struct child_process store = CHILD_PROCESS_INIT;
@@ -3742,10 +3746,13 @@ int apply_autostash(const char *path)
3746 ret = error(_("cannot store %s"), stash_oid.buf);
3747 else
3748 fprintf(stderr,
3745 - _("Applying autostash resulted in conflicts.\n"
3749 + _("%s\n"
3750 "Your changes are safe in the stash.\n"
3751 "You can run \"git stash pop\" or"
3748 - " \"git stash drop\" at any time.\n"));
3752 + " \"git stash drop\" at any time.\n"),
3753 + attempt_apply ?
3754 + _("Applying autostash resulted in conflicts.") :
3755 + _("Autostash exists; creating a new stash entry."));
3756 }
3757
3758 unlink(path);
@@ -3753,6 +3760,16 @@ int apply_autostash(const char *path)
3760 return ret;
3761 }
3762
3763 +int save_autostash(const char *path)
3764 +{
3765 + return apply_save_autostash(path, 0);
3766 +}
3767 +
3768 +int apply_autostash(const char *path)
3769 +{
3770 + return apply_save_autostash(path, 1);
3771 +}
3772 +
3773 static const char *reflog_message(struct replay_opts *opts,
3774 const char *sub_action, const char *fmt, ...)
3775 {
sequencer.h
+1
@@ -193,6 +193,7 @@ void commit_post_rewrite(struct repository *r,
193
194 void create_autostash(struct repository *r, const char *path,
195 const char *default_reflog_action);
196 +int save_autostash(const char *path);
197 int apply_autostash(const char *path);
198
199 #define SUMMARY_INITIAL_COMMIT (1 << 0)