Skip to content

Commit 12b6e13

Browse files
Denton-Lgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0dd562e commit 12b6e13

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

sequencer.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3707,7 +3707,7 @@ void create_autostash(struct repository *r, const char *path,
37073707
strbuf_release(&buf);
37083708
}
37093709

3710-
int apply_autostash(const char *path)
3710+
static int apply_save_autostash(const char *path, int attempt_apply)
37113711
{
37123712
struct strbuf stash_oid = STRBUF_INIT;
37133713
struct child_process child = CHILD_PROCESS_INIT;
@@ -3720,13 +3720,17 @@ int apply_autostash(const char *path)
37203720
}
37213721
strbuf_trim(&stash_oid);
37223722

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)
37303734
fprintf(stderr, _("Applied autostash.\n"));
37313735
else {
37323736
struct child_process store = CHILD_PROCESS_INIT;
@@ -3742,17 +3746,30 @@ int apply_autostash(const char *path)
37423746
ret = error(_("cannot store %s"), stash_oid.buf);
37433747
else
37443748
fprintf(stderr,
3745-
_("Applying autostash resulted in conflicts.\n"
3749+
_("%s\n"
37463750
"Your changes are safe in the stash.\n"
37473751
"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."));
37493756
}
37503757

37513758
unlink(path);
37523759
strbuf_release(&stash_oid);
37533760
return ret;
37543761
}
37553762

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+
37563773
static const char *reflog_message(struct replay_opts *opts,
37573774
const char *sub_action, const char *fmt, ...)
37583775
{

sequencer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ void commit_post_rewrite(struct repository *r,
193193

194194
void create_autostash(struct repository *r, const char *path,
195195
const char *default_reflog_action);
196+
int save_autostash(const char *path);
196197
int apply_autostash(const char *path);
197198

198199
#define SUMMARY_INITIAL_COMMIT (1 << 0)

0 commit comments

Comments
 (0)