rebase: generify create_autostash()
In the future, we plan on lib-ifying create_autostash() so we need it to be more generic. Make it more generic by making it accept a `struct repository` argument instead of implicitly using the non-repo functions and `the_repository`. Also, make it accept a `path` argument so that we no longer rely have to rely on `struct rebase_options`. Finally, make it accept a `default_reflog_action` argument so we no longer have to rely on `DEFAULT_REFLOG_ACTION`. 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
9bb3dea45d69921578b1729c7dc7b12bfbe6f69e
1 file changed
+16
-16
builtin/rebase.c
+16
-16
@@ -1274,22 +1274,21 @@ static int check_exec_cmd(const char *cmd)
1274
return 0;
1275
}
1276
1277
-static void create_autostash(struct rebase_options *options)
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
1283
- fd = hold_locked_index(&lock_file, 0);
1284
- refresh_cache(REFRESH_QUIET);
1284
+ fd = repo_hold_locked_index(r, &lock_file, 0);
1285
+ refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
1286
if (0 <= fd)
1286
- repo_update_index_if_able(the_repository, &lock_file);
1287
+ repo_update_index_if_able(r, &lock_file);
1288
rollback_lock_file(&lock_file);
1289
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);
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
@@ -1307,18 +1306,18 @@ static void create_autostash(struct rebase_options *options)
1306
strbuf_reset(&buf);
1307
strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
1308
1310
- if (safe_create_leading_directories_const(autostash))
1309
+ if (safe_create_leading_directories_const(path))
1310
die(_("Could not create directory for '%s'"),
1312
- options->state_dir);
1313
- write_file(autostash, "%s", oid_to_hex(&oid));
1311
+ path);
1312
+ write_file(path, "%s", oid_to_hex(&oid));
1313
printf(_("Created autostash: %s\n"), buf.buf);
1315
- if (reset_head(the_repository, NULL, "reset --hard",
1314
+ if (reset_head(r, NULL, "reset --hard",
1315
NULL, RESET_HEAD_HARD, NULL, NULL,
1317
- DEFAULT_REFLOG_ACTION) < 0)
1316
+ default_reflog_action) < 0)
1317
die(_("could not reset --hard"));
1318
1320
- if (discard_index(the_repository->index) < 0 ||
1321
- repo_read_index(the_repository) < 0)
1319
+ if (discard_index(r->index) < 0 ||
1320
+ repo_read_index(r) < 0)
1321
die(_("could not read index"));
1322
}
1323
strbuf_release(&buf);
@@ -1956,7 +1955,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1955
die(_("could not read index"));
1956
1957
if (options.autostash) {
1959
- create_autostash(&options);
1958
+ create_autostash(the_repository, state_dir_path("autostash", &options),
1959
+ DEFAULT_REFLOG_ACTION);
1960
}
1961
1962
if (require_clean_work_tree(the_repository, "rebase",