Skip to content

Commit 86ed00a

Browse files
Denton-Lgitster
authored andcommitted
rebase: use apply_autostash() from sequencer.c
The apply_autostash() function in builtin/rebase.c is similar enough to the apply_autostash() function in sequencer.c that they are almost interchangeable, except for the type of arg they accept. Make the sequencer.c version extern and use it in rebase. The rebase version was introduced in 6defce2 (builtin rebase: support `--autostash` option, 2018-09-04) as part of the shell to C conversion. It opted to duplicate the function because, at the time, there was another in-progress project converting interactive rebase from shell to C as well and they did not want to clash with them by refactoring sequencer.c version of apply_autostash(). Since both efforts are long done, we can freely combine them together now. Signed-off-by: Denton Liu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent facca7f commit 86ed00a

File tree

3 files changed

+5
-48
lines changed

3 files changed

+5
-48
lines changed

builtin/rebase.c

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -712,59 +712,14 @@ static int rebase_write_basic_state(struct rebase_options *opts)
712712
return 0;
713713
}
714714

715-
static int apply_autostash(struct rebase_options *opts)
716-
{
717-
const char *path = state_dir_path("autostash", opts);
718-
struct strbuf autostash = STRBUF_INIT;
719-
struct child_process stash_apply = CHILD_PROCESS_INIT;
720-
721-
if (!file_exists(path))
722-
return 0;
723-
724-
if (!read_oneliner(&autostash, path, READ_ONELINER_WARN_MISSING))
725-
return error(_("Could not read '%s'"), path);
726-
/* Ensure that the hash is not mistaken for a number */
727-
strbuf_addstr(&autostash, "^0");
728-
argv_array_pushl(&stash_apply.args,
729-
"stash", "apply", autostash.buf, NULL);
730-
stash_apply.git_cmd = 1;
731-
stash_apply.no_stderr = stash_apply.no_stdout =
732-
stash_apply.no_stdin = 1;
733-
if (!run_command(&stash_apply))
734-
printf(_("Applied autostash.\n"));
735-
else {
736-
struct argv_array args = ARGV_ARRAY_INIT;
737-
int res = 0;
738-
739-
argv_array_pushl(&args,
740-
"stash", "store", "-m", "autostash", "-q",
741-
autostash.buf, NULL);
742-
if (run_command_v_opt(args.argv, RUN_GIT_CMD))
743-
res = error(_("Cannot store %s"), autostash.buf);
744-
argv_array_clear(&args);
745-
strbuf_release(&autostash);
746-
if (res)
747-
return res;
748-
749-
fprintf(stderr,
750-
_("Applying autostash resulted in conflicts.\n"
751-
"Your changes are safe in the stash.\n"
752-
"You can run \"git stash pop\" or \"git stash drop\" "
753-
"at any time.\n"));
754-
}
755-
756-
strbuf_release(&autostash);
757-
return 0;
758-
}
759-
760715
static int finish_rebase(struct rebase_options *opts)
761716
{
762717
struct strbuf dir = STRBUF_INIT;
763718
const char *argv_gc_auto[] = { "gc", "--auto", NULL };
764719
int ret = 0;
765720

766721
delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
767-
apply_autostash(opts);
722+
apply_autostash(state_dir_path("autostash", opts));
768723
close_object_store(the_repository->objects);
769724
/*
770725
* We ignore errors in 'gc --auto', since the
@@ -1209,7 +1164,7 @@ static int run_specific_rebase(struct rebase_options *opts, enum action action)
12091164
} else if (status == 2) {
12101165
struct strbuf dir = STRBUF_INIT;
12111166

1212-
apply_autostash(opts);
1167+
apply_autostash(state_dir_path("autostash", opts));
12131168
strbuf_addstr(&dir, opts->state_dir);
12141169
remove_dir_recursively(&dir, 0);
12151170
strbuf_release(&dir);

sequencer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3657,7 +3657,7 @@ static enum todo_command peek_command(struct todo_list *todo_list, int offset)
36573657
return -1;
36583658
}
36593659

3660-
static int apply_autostash(const char *path)
3660+
int apply_autostash(const char *path)
36613661
{
36623662
struct strbuf stash_oid = STRBUF_INIT;
36633663
struct child_process child = CHILD_PROCESS_INIT;

sequencer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ void commit_post_rewrite(struct repository *r,
191191
const struct commit *current_head,
192192
const struct object_id *new_head);
193193

194+
int apply_autostash(const char *path);
195+
194196
#define SUMMARY_INITIAL_COMMIT (1 << 0)
195197
#define SUMMARY_SHOW_AUTHOR_DATE (1 << 1)
196198
void print_commit_summary(struct repository *repo,

0 commit comments

Comments
 (0)