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 <liu.denton@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Denton Liu committed
Apr 7, 2020 at 10:28 UTC
0816f1dff8715e80b5fa0b73b48dd36ef9e1e381
3 files changed
+52
-49
builtin/rebase.c
-49
@@ -1274,55 +1274,6 @@ static int check_exec_cmd(const char *cmd)
1274
return 0;
1275
}
1276
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
-
1277
int cmd_rebase(int argc, const char **argv, const char *prefix)
1278
{
1279
struct rebase_options options = REBASE_OPTIONS_INIT;
sequencer.c
+50
@@ -32,6 +32,7 @@
32
#include "alias.h"
33
#include "commit-reach.h"
34
#include "rebase-interactive.h"
35
+#include "reset.h"
36
37
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
38
@@ -3657,6 +3658,55 @@ static enum todo_command peek_command(struct todo_list *todo_list, int offset)
3658
return -1;
3659
}
3660
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
+
3710
int apply_autostash(const char *path)
3711
{
3712
struct strbuf stash_oid = STRBUF_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
+void create_autostash(struct repository *r, const char *path,
195
+ const char *default_reflog_action);
196
int apply_autostash(const char *path);
197
198
#define SUMMARY_INITIAL_COMMIT (1 << 0)