]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sequencer: implement save_autostash()
authorDenton Liu <liu.denton@gmail.com>
Tue, 7 Apr 2020 14:28:05 +0000 (10:28 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 10 Apr 2020 16:28:02 +0000 (09:28 -0700)
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>
sequencer.c
sequencer.h

index 645bc47ce3c0e9cc080c6bd0aa56e3cd314e9508..084ea4117e74ab850e066c091cf08a028818e049 100644 (file)
@@ -3707,7 +3707,7 @@ void create_autostash(struct repository *r, const char *path,
        strbuf_release(&buf);
 }
 
-int apply_autostash(const char *path)
+static int apply_save_autostash(const char *path, int attempt_apply)
 {
        struct strbuf stash_oid = STRBUF_INIT;
        struct child_process child = CHILD_PROCESS_INIT;
@@ -3720,13 +3720,17 @@ int apply_autostash(const char *path)
        }
        strbuf_trim(&stash_oid);
 
-       child.git_cmd = 1;
-       child.no_stdout = 1;
-       child.no_stderr = 1;
-       argv_array_push(&child.args, "stash");
-       argv_array_push(&child.args, "apply");
-       argv_array_push(&child.args, stash_oid.buf);
-       if (!run_command(&child))
+       if (attempt_apply) {
+               child.git_cmd = 1;
+               child.no_stdout = 1;
+               child.no_stderr = 1;
+               argv_array_push(&child.args, "stash");
+               argv_array_push(&child.args, "apply");
+               argv_array_push(&child.args, stash_oid.buf);
+               ret = run_command(&child);
+       }
+
+       if (attempt_apply && !ret)
                fprintf(stderr, _("Applied autostash.\n"));
        else {
                struct child_process store = CHILD_PROCESS_INIT;
@@ -3742,10 +3746,13 @@ int apply_autostash(const char *path)
                        ret = error(_("cannot store %s"), stash_oid.buf);
                else
                        fprintf(stderr,
-                               _("Applying autostash resulted in conflicts.\n"
+                               _("%s\n"
                                  "Your changes are safe in the stash.\n"
                                  "You can run \"git stash pop\" or"
-                                 " \"git stash drop\" at any time.\n"));
+                                 " \"git stash drop\" at any time.\n"),
+                               attempt_apply ?
+                               _("Applying autostash resulted in conflicts.") :
+                               _("Autostash exists; creating a new stash entry."));
        }
 
        unlink(path);
@@ -3753,6 +3760,16 @@ int apply_autostash(const char *path)
        return ret;
 }
 
+int save_autostash(const char *path)
+{
+       return apply_save_autostash(path, 0);
+}
+
+int apply_autostash(const char *path)
+{
+       return apply_save_autostash(path, 1);
+}
+
 static const char *reflog_message(struct replay_opts *opts,
        const char *sub_action, const char *fmt, ...)
 {
index 9d1fe0ccfba78c341cf2ddc76b6316fb862f4648..306d4cecfb2c17cce56dde1fb0349a6db3d259d0 100644 (file)
@@ -193,6 +193,7 @@ void commit_post_rewrite(struct repository *r,
 
 void create_autostash(struct repository *r, const char *path,
                      const char *default_reflog_action);
+int save_autostash(const char *path);
 int apply_autostash(const char *path);
 
 #define SUMMARY_INITIAL_COMMIT   (1 << 0)