Skip to content

Commit 0816f1d

Browse files
Denton-Lgitster
authored andcommitted
sequencer: extract perform_autostash() from rebase
Lib-ify the autostash code by extracting perform_autostash() from rebase into sequencer. In a future commit, this will be used to implement `--autostash` in other builtins. This patch is best viewed with `--color-moved`. Signed-off-by: Denton Liu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9bb3dea commit 0816f1d

File tree

3 files changed

+52
-49
lines changed

3 files changed

+52
-49
lines changed

builtin/rebase.c

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

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

sequencer.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "alias.h"
3333
#include "commit-reach.h"
3434
#include "rebase-interactive.h"
35+
#include "reset.h"
3536

3637
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
3738

@@ -3657,6 +3658,55 @@ static enum todo_command peek_command(struct todo_list *todo_list, int offset)
36573658
return -1;
36583659
}
36593660

3661+
void create_autostash(struct repository *r, const char *path,
3662+
const char *default_reflog_action)
3663+
{
3664+
struct strbuf buf = STRBUF_INIT;
3665+
struct lock_file lock_file = LOCK_INIT;
3666+
int fd;
3667+
3668+
fd = repo_hold_locked_index(r, &lock_file, 0);
3669+
refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
3670+
if (0 <= fd)
3671+
repo_update_index_if_able(r, &lock_file);
3672+
rollback_lock_file(&lock_file);
3673+
3674+
if (has_unstaged_changes(r, 1) ||
3675+
has_uncommitted_changes(r, 1)) {
3676+
struct child_process stash = CHILD_PROCESS_INIT;
3677+
struct object_id oid;
3678+
3679+
argv_array_pushl(&stash.args,
3680+
"stash", "create", "autostash", NULL);
3681+
stash.git_cmd = 1;
3682+
stash.no_stdin = 1;
3683+
strbuf_reset(&buf);
3684+
if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
3685+
die(_("Cannot autostash"));
3686+
strbuf_trim_trailing_newline(&buf);
3687+
if (get_oid(buf.buf, &oid))
3688+
die(_("Unexpected stash response: '%s'"),
3689+
buf.buf);
3690+
strbuf_reset(&buf);
3691+
strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
3692+
3693+
if (safe_create_leading_directories_const(path))
3694+
die(_("Could not create directory for '%s'"),
3695+
path);
3696+
write_file(path, "%s", oid_to_hex(&oid));
3697+
printf(_("Created autostash: %s\n"), buf.buf);
3698+
if (reset_head(r, NULL, "reset --hard",
3699+
NULL, RESET_HEAD_HARD, NULL, NULL,
3700+
default_reflog_action) < 0)
3701+
die(_("could not reset --hard"));
3702+
3703+
if (discard_index(r->index) < 0 ||
3704+
repo_read_index(r) < 0)
3705+
die(_("could not read index"));
3706+
}
3707+
strbuf_release(&buf);
3708+
}
3709+
36603710
int apply_autostash(const char *path)
36613711
{
36623712
struct strbuf stash_oid = STRBUF_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+
void create_autostash(struct repository *r, const char *path,
195+
const char *default_reflog_action);
194196
int apply_autostash(const char *path);
195197

196198
#define SUMMARY_INITIAL_COMMIT (1 << 0)

0 commit comments

Comments
 (0)