Skip to content

Commit 35122da

Browse files
pks-tgitster
authored andcommitted
sequencer: introduce functions to handle autostashes via refs
We're about to convert the MERGE_AUTOSTASH ref to become non-special, using the refs API instead of direct filesystem access to both read and write the ref. The current interfaces to write autostashes is entirely path-based though, so we need to extend them to also support writes via the refs API instead. Ideally, we would be able to fully replace the old set of path-based interfaces. But the sequencer will continue to write state into "rebase-merge/autostash". This path is not considered to be a ref at all and will thus stay is-is for now, which requires us to keep both path- and refs-based interfaces to handle autostashes. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fd7c6ff commit 35122da

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

sequencer.c

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4463,12 +4463,17 @@ static enum todo_command peek_command(struct todo_list *todo_list, int offset)
44634463
return -1;
44644464
}
44654465

4466-
void create_autostash(struct repository *r, const char *path)
4466+
static void create_autostash_internal(struct repository *r,
4467+
const char *path,
4468+
const char *refname)
44674469
{
44684470
struct strbuf buf = STRBUF_INIT;
44694471
struct lock_file lock_file = LOCK_INIT;
44704472
int fd;
44714473

4474+
if (path && refname)
4475+
BUG("can only pass path or refname");
4476+
44724477
fd = repo_hold_locked_index(r, &lock_file, 0);
44734478
refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
44744479
if (0 <= fd)
@@ -4495,10 +4500,16 @@ void create_autostash(struct repository *r, const char *path)
44954500
strbuf_reset(&buf);
44964501
strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
44974502

4498-
if (safe_create_leading_directories_const(path))
4499-
die(_("Could not create directory for '%s'"),
4500-
path);
4501-
write_file(path, "%s", oid_to_hex(&oid));
4503+
if (path) {
4504+
if (safe_create_leading_directories_const(path))
4505+
die(_("Could not create directory for '%s'"),
4506+
path);
4507+
write_file(path, "%s", oid_to_hex(&oid));
4508+
} else {
4509+
refs_update_ref(get_main_ref_store(r), "", refname,
4510+
&oid, null_oid(), 0, UPDATE_REFS_DIE_ON_ERR);
4511+
}
4512+
45024513
printf(_("Created autostash: %s\n"), buf.buf);
45034514
if (reset_head(r, &ropts) < 0)
45044515
die(_("could not reset --hard"));
@@ -4509,6 +4520,16 @@ void create_autostash(struct repository *r, const char *path)
45094520
strbuf_release(&buf);
45104521
}
45114522

4523+
void create_autostash(struct repository *r, const char *path)
4524+
{
4525+
create_autostash_internal(r, path, NULL);
4526+
}
4527+
4528+
void create_autostash_ref(struct repository *r, const char *refname)
4529+
{
4530+
create_autostash_internal(r, NULL, refname);
4531+
}
4532+
45124533
static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply)
45134534
{
45144535
struct child_process child = CHILD_PROCESS_INIT;
@@ -4586,6 +4607,41 @@ int apply_autostash_oid(const char *stash_oid)
45864607
return apply_save_autostash_oid(stash_oid, 1);
45874608
}
45884609

4610+
static int apply_save_autostash_ref(struct repository *r, const char *refname,
4611+
int attempt_apply)
4612+
{
4613+
struct object_id stash_oid;
4614+
char stash_oid_hex[GIT_MAX_HEXSZ + 1];
4615+
int flag, ret;
4616+
4617+
if (!refs_ref_exists(get_main_ref_store(r), refname))
4618+
return 0;
4619+
4620+
if (!refs_resolve_ref_unsafe(get_main_ref_store(r), refname,
4621+
RESOLVE_REF_READING, &stash_oid, &flag))
4622+
return -1;
4623+
if (flag & REF_ISSYMREF)
4624+
return error(_("autostash reference is a symref"));
4625+
4626+
oid_to_hex_r(stash_oid_hex, &stash_oid);
4627+
ret = apply_save_autostash_oid(stash_oid_hex, attempt_apply);
4628+
4629+
refs_delete_ref(get_main_ref_store(r), "", refname,
4630+
&stash_oid, REF_NO_DEREF);
4631+
4632+
return ret;
4633+
}
4634+
4635+
int save_autostash_ref(struct repository *r, const char *refname)
4636+
{
4637+
return apply_save_autostash_ref(r, refname, 0);
4638+
}
4639+
4640+
int apply_autostash_ref(struct repository *r, const char *refname)
4641+
{
4642+
return apply_save_autostash_ref(r, refname, 1);
4643+
}
4644+
45894645
static int checkout_onto(struct repository *r, struct replay_opts *opts,
45904646
const char *onto_name, const struct object_id *onto,
45914647
const struct object_id *orig_head)

sequencer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,12 @@ void commit_post_rewrite(struct repository *r,
225225
const struct object_id *new_head);
226226

227227
void create_autostash(struct repository *r, const char *path);
228+
void create_autostash_ref(struct repository *r, const char *refname);
228229
int save_autostash(const char *path);
230+
int save_autostash_ref(struct repository *r, const char *refname);
229231
int apply_autostash(const char *path);
230232
int apply_autostash_oid(const char *stash_oid);
233+
int apply_autostash_ref(struct repository *r, const char *refname);
231234

232235
#define SUMMARY_INITIAL_COMMIT (1 << 0)
233236
#define SUMMARY_SHOW_AUTHOR_DATE (1 << 1)

0 commit comments

Comments
 (0)