summaryrefslogtreecommitdiff
path: root/builtin/rebase.c
diff options
context:
space:
mode:
authorDenton Liu <liu.denton@gmail.com>2020-04-07 14:28:01 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-04-10 16:28:02 (GMT)
commit4d4bc157f85316576d9f9bb90657d455fe76e281 (patch)
tree4b22bd7ff49ee16cc57e769569f3dbd7f80f8a6e /builtin/rebase.c
parentb309a9710899dab2f76455526f573a90fad5d13c (diff)
downloadgit-4d4bc157f85316576d9f9bb90657d455fe76e281.zip
git-4d4bc157f85316576d9f9bb90657d455fe76e281.tar.gz
git-4d4bc157f85316576d9f9bb90657d455fe76e281.tar.bz2
rebase: extract create_autostash()
In a future commit, we will lib-ify this code. In preparation for this, extract the code into the create_autostash() function so that it can be cleaned up before it is finally lib-ified. This patch is best viewed with `--color-moved` and `--color-moved-ws=allow-indentation-change`. Signed-off-by: Denton Liu <liu.denton@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/rebase.c')
-rw-r--r--builtin/rebase.c94
1 files changed, 50 insertions, 44 deletions
diff --git a/builtin/rebase.c b/builtin/rebase.c
index 48ef36b..581ad3a 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -1274,6 +1274,55 @@ static int check_exec_cmd(const char *cmd)
return 0;
}
+static void create_autostash(struct rebase_options *options)
+{
+ struct strbuf buf = STRBUF_INIT;
+ struct lock_file lock_file = LOCK_INIT;
+ int fd;
+
+ fd = hold_locked_index(&lock_file, 0);
+ refresh_cache(REFRESH_QUIET);
+ if (0 <= fd)
+ repo_update_index_if_able(the_repository, &lock_file);
+ rollback_lock_file(&lock_file);
+
+ if (has_unstaged_changes(the_repository, 1) ||
+ has_uncommitted_changes(the_repository, 1)) {
+ const char *autostash =
+ state_dir_path("autostash", options);
+ struct child_process stash = CHILD_PROCESS_INIT;
+ struct object_id oid;
+
+ argv_array_pushl(&stash.args,
+ "stash", "create", "autostash", NULL);
+ stash.git_cmd = 1;
+ stash.no_stdin = 1;
+ strbuf_reset(&buf);
+ if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
+ die(_("Cannot autostash"));
+ strbuf_trim_trailing_newline(&buf);
+ if (get_oid(buf.buf, &oid))
+ die(_("Unexpected stash response: '%s'"),
+ buf.buf);
+ strbuf_reset(&buf);
+ strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
+
+ if (safe_create_leading_directories_const(autostash))
+ die(_("Could not create directory for '%s'"),
+ options->state_dir);
+ write_file(autostash, "%s", oid_to_hex(&oid));
+ printf(_("Created autostash: %s\n"), buf.buf);
+ if (reset_head(the_repository, NULL, "reset --hard",
+ NULL, RESET_HEAD_HARD, NULL, NULL,
+ DEFAULT_REFLOG_ACTION) < 0)
+ die(_("could not reset --hard"));
+
+ if (discard_index(the_repository->index) < 0 ||
+ repo_read_index(the_repository) < 0)
+ die(_("could not read index"));
+ }
+ strbuf_release(&buf);
+}
int cmd_rebase(int argc, const char **argv, const char *prefix)
{
@@ -1907,50 +1956,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
die(_("could not read index"));
if (options.autostash) {
- struct lock_file lock_file = LOCK_INIT;
- int fd;
-
- fd = hold_locked_index(&lock_file, 0);
- refresh_cache(REFRESH_QUIET);
- if (0 <= fd)
- repo_update_index_if_able(the_repository, &lock_file);
- rollback_lock_file(&lock_file);
-
- if (has_unstaged_changes(the_repository, 1) ||
- has_uncommitted_changes(the_repository, 1)) {
- const char *autostash =
- state_dir_path("autostash", &options);
- struct child_process stash = CHILD_PROCESS_INIT;
- struct object_id oid;
-
- argv_array_pushl(&stash.args,
- "stash", "create", "autostash", NULL);
- stash.git_cmd = 1;
- stash.no_stdin = 1;
- strbuf_reset(&buf);
- if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
- die(_("Cannot autostash"));
- strbuf_trim_trailing_newline(&buf);
- if (get_oid(buf.buf, &oid))
- die(_("Unexpected stash response: '%s'"),
- buf.buf);
- strbuf_reset(&buf);
- strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
-
- if (safe_create_leading_directories_const(autostash))
- die(_("Could not create directory for '%s'"),
- options.state_dir);
- write_file(autostash, "%s", oid_to_hex(&oid));
- printf(_("Created autostash: %s\n"), buf.buf);
- if (reset_head(the_repository, NULL, "reset --hard",
- NULL, RESET_HEAD_HARD, NULL, NULL,
- DEFAULT_REFLOG_ACTION) < 0)
- die(_("could not reset --hard"));
-
- if (discard_index(the_repository->index) < 0 ||
- repo_read_index(the_repository) < 0)
- die(_("could not read index"));
- }
+ create_autostash(&options);
}
if (require_clean_work_tree(the_repository, "rebase",