@matheusbsbs / matheus-bagliano / commits / 86ed00aff4
Fork 0

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 6defce2b02 (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 <liu.denton@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Denton Liu committed Apr 7, 2020 at 10:27 UTC 86ed00aff4b86eed6ecc606e9d5f7828a1dca353
3 files changed +5 -48
builtin/rebase.c
+2 -47
@@ -712,51 +712,6 @@ static int rebase_write_basic_state(struct rebase_options *opts)
712 return 0;
713 }
714
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 -
715 static int finish_rebase(struct rebase_options *opts)
716 {
717 struct strbuf dir = STRBUF_INIT;
@@ -764,7 +719,7 @@ static int finish_rebase(struct rebase_options *opts)
719 int ret = 0;
720
721 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
767 - apply_autostash(opts);
722 + apply_autostash(state_dir_path("autostash", opts));
723 close_object_store(the_repository->objects);
724 /*
725 * We ignore errors in 'gc --auto', since the
@@ -1209,7 +1164,7 @@ finished_rebase:
1164 } else if (status == 2) {
1165 struct strbuf dir = STRBUF_INIT;
1166
1212 - apply_autostash(opts);
1167 + apply_autostash(state_dir_path("autostash", opts));
1168 strbuf_addstr(&dir, opts->state_dir);
1169 remove_dir_recursively(&dir, 0);
1170 strbuf_release(&dir);
sequencer.c
+1 -1
@@ -3657,7 +3657,7 @@ static enum todo_command peek_command(struct todo_list *todo_list, int offset)
3657 return -1;
3658 }
3659
3660 -static int apply_autostash(const char *path)
3660 +int apply_autostash(const char *path)
3661 {
3662 struct strbuf stash_oid = STRBUF_INIT;
3663 struct child_process child = CHILD_PROCESS_INIT;
sequencer.h
+2
@@ -191,6 +191,8 @@ void commit_post_rewrite(struct repository *r,
191 const struct commit *current_head,
192 const struct object_id *new_head);
193
194 +int apply_autostash(const char *path);
195 +
196 #define SUMMARY_INITIAL_COMMIT (1 << 0)
197 #define SUMMARY_SHOW_AUTHOR_DATE (1 << 1)
198 void print_commit_summary(struct repository *repo,