[v4,19/23] sequencer: implement save_autostash()
diff mbox series

Message ID 75dc3f10a1aac24007b660a02ce829c8702ffd0e.1585962673.git.liu.denton@gmail.com
State New
Headers show
Series
  • merge: learn --autostash
Related show

Commit Message

Denton Liu April 4, 2020, 1:11 a.m. UTC
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 latter does not try to apply the stash. It skips that step and
just stores the created entry in the stash reflog.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 sequencer.c | 37 +++++++++++++++++++++++++++----------
 sequencer.h |  1 +
 2 files changed, 28 insertions(+), 10 deletions(-)

Comments

Phillip Wood April 6, 2020, 3:15 p.m. UTC | #1
Hi Denton

On 04/04/2020 02:11, Denton Liu wrote:
> 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 latter does not try to apply the stash. 

the latter is apply_autostash() - surely that tries to apply the stash?

> It skips that step and
> just stores the created entry in the stash reflog.

I think we want save_autostash() so we can call it to save the stash 
when running a reset - perhaps we should mention that in the commit message.

Best Wishes

Phillip

> Signed-off-by: Denton Liu <liu.denton@gmail.com>
> ---
>   sequencer.c | 37 +++++++++++++++++++++++++++----------
>   sequencer.h |  1 +
>   2 files changed, 28 insertions(+), 10 deletions(-)
> 
> diff --git a/sequencer.c b/sequencer.c
> index 5dd1c2438e..fb52583bc2 100644
> --- a/sequencer.c
> +++ b/sequencer.c
> @@ -3699,7 +3699,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;
> @@ -3712,13 +3712,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;
> @@ -3734,10 +3738,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);
> @@ -3745,6 +3752,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, ...)
>   {
> diff --git a/sequencer.h b/sequencer.h
> index cf1284f9ed..2d09c1ac0b 100644
> --- a/sequencer.h
> +++ b/sequencer.h
> @@ -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)
>

Patch
diff mbox series

diff --git a/sequencer.c b/sequencer.c
index 5dd1c2438e..fb52583bc2 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -3699,7 +3699,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;
@@ -3712,13 +3712,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;
@@ -3734,10 +3738,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);
@@ -3745,6 +3752,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, ...)
 {
diff --git a/sequencer.h b/sequencer.h
index cf1284f9ed..2d09c1ac0b 100644
--- a/sequencer.h
+++ b/sequencer.h
@@ -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)