Skip to content

Commit 4d4bc15

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

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

builtin/rebase.c

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,55 @@ static int check_exec_cmd(const char *cmd)
12741274
return 0;
12751275
}
12761276

1277+
static void create_autostash(struct rebase_options *options)
1278+
{
1279+
struct strbuf buf = STRBUF_INIT;
1280+
struct lock_file lock_file = LOCK_INIT;
1281+
int fd;
1282+
1283+
fd = hold_locked_index(&lock_file, 0);
1284+
refresh_cache(REFRESH_QUIET);
1285+
if (0 <= fd)
1286+
repo_update_index_if_able(the_repository, &lock_file);
1287+
rollback_lock_file(&lock_file);
1288+
1289+
if (has_unstaged_changes(the_repository, 1) ||
1290+
has_uncommitted_changes(the_repository, 1)) {
1291+
const char *autostash =
1292+
state_dir_path("autostash", options);
1293+
struct child_process stash = CHILD_PROCESS_INIT;
1294+
struct object_id oid;
1295+
1296+
argv_array_pushl(&stash.args,
1297+
"stash", "create", "autostash", NULL);
1298+
stash.git_cmd = 1;
1299+
stash.no_stdin = 1;
1300+
strbuf_reset(&buf);
1301+
if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
1302+
die(_("Cannot autostash"));
1303+
strbuf_trim_trailing_newline(&buf);
1304+
if (get_oid(buf.buf, &oid))
1305+
die(_("Unexpected stash response: '%s'"),
1306+
buf.buf);
1307+
strbuf_reset(&buf);
1308+
strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
1309+
1310+
if (safe_create_leading_directories_const(autostash))
1311+
die(_("Could not create directory for '%s'"),
1312+
options->state_dir);
1313+
write_file(autostash, "%s", oid_to_hex(&oid));
1314+
printf(_("Created autostash: %s\n"), buf.buf);
1315+
if (reset_head(the_repository, NULL, "reset --hard",
1316+
NULL, RESET_HEAD_HARD, NULL, NULL,
1317+
DEFAULT_REFLOG_ACTION) < 0)
1318+
die(_("could not reset --hard"));
1319+
1320+
if (discard_index(the_repository->index) < 0 ||
1321+
repo_read_index(the_repository) < 0)
1322+
die(_("could not read index"));
1323+
}
1324+
strbuf_release(&buf);
1325+
}
12771326

12781327
int cmd_rebase(int argc, const char **argv, const char *prefix)
12791328
{
@@ -1907,50 +1956,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
19071956
die(_("could not read index"));
19081957

19091958
if (options.autostash) {
1910-
struct lock_file lock_file = LOCK_INIT;
1911-
int fd;
1912-
1913-
fd = hold_locked_index(&lock_file, 0);
1914-
refresh_cache(REFRESH_QUIET);
1915-
if (0 <= fd)
1916-
repo_update_index_if_able(the_repository, &lock_file);
1917-
rollback_lock_file(&lock_file);
1918-
1919-
if (has_unstaged_changes(the_repository, 1) ||
1920-
has_uncommitted_changes(the_repository, 1)) {
1921-
const char *autostash =
1922-
state_dir_path("autostash", &options);
1923-
struct child_process stash = CHILD_PROCESS_INIT;
1924-
struct object_id oid;
1925-
1926-
argv_array_pushl(&stash.args,
1927-
"stash", "create", "autostash", NULL);
1928-
stash.git_cmd = 1;
1929-
stash.no_stdin = 1;
1930-
strbuf_reset(&buf);
1931-
if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
1932-
die(_("Cannot autostash"));
1933-
strbuf_trim_trailing_newline(&buf);
1934-
if (get_oid(buf.buf, &oid))
1935-
die(_("Unexpected stash response: '%s'"),
1936-
buf.buf);
1937-
strbuf_reset(&buf);
1938-
strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
1939-
1940-
if (safe_create_leading_directories_const(autostash))
1941-
die(_("Could not create directory for '%s'"),
1942-
options.state_dir);
1943-
write_file(autostash, "%s", oid_to_hex(&oid));
1944-
printf(_("Created autostash: %s\n"), buf.buf);
1945-
if (reset_head(the_repository, NULL, "reset --hard",
1946-
NULL, RESET_HEAD_HARD, NULL, NULL,
1947-
DEFAULT_REFLOG_ACTION) < 0)
1948-
die(_("could not reset --hard"));
1949-
1950-
if (discard_index(the_repository->index) < 0 ||
1951-
repo_read_index(the_repository) < 0)
1952-
die(_("could not read index"));
1953-
}
1959+
create_autostash(&options);
19541960
}
19551961

19561962
if (require_clean_work_tree(the_repository, "rebase",

0 commit comments

Comments
 (0)