Skip to content

Commit

Permalink
Avoid resetting metadata history when recloning with cinnabarclone
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Nov 2, 2023
1 parent 7e3a25b commit a476f29
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
38 changes: 19 additions & 19 deletions src/cinnabar-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,19 +528,8 @@ static int count_refs(const char *refname, const struct object_id *oid,
return 0;
}

static void reset_metadata(void)
static void init_metadata(struct commit *c)
{
oidcpy(&metadata_oid, null_oid());
oidcpy(&changesets_oid, null_oid());
oidcpy(&manifests_oid, null_oid());
oidcpy(&hg2git_oid, null_oid());
oidcpy(&git2hg_oid, null_oid());
oidcpy(&files_meta_oid, null_oid());
}

static void init_metadata(void)
{
struct commit *c;
struct commit_list *cl;
const char *msg, *body;
struct strbuf **flags, **f;
Expand All @@ -550,9 +539,13 @@ static void init_metadata(void)
struct replace_object *replace;
size_t count = 0;

c = lookup_commit_reference_by_name(METADATA_REF);
if (!c) {
reset_metadata();
oidcpy(&metadata_oid, null_oid());
oidcpy(&changesets_oid, null_oid());
oidcpy(&manifests_oid, null_oid());
oidcpy(&hg2git_oid, null_oid());
oidcpy(&git2hg_oid, null_oid());
oidcpy(&files_meta_oid, null_oid());
return;
}
oidcpy(&metadata_oid, &c->object.oid);
Expand Down Expand Up @@ -657,8 +650,10 @@ void dump_ref_updates(void);

extern void reset_changeset_heads(void);

void do_reload(int reset)
void do_reload(struct object_id *oid)
{
struct commit *c = NULL;

done_cinnabar();
hashmap_init(&git_tree_cache, oid_map_entry_cmp, NULL, 0);

Expand All @@ -668,11 +663,14 @@ void do_reload(int reset)

metadata_flags = 0;
reset_replace_map();
if (reset) {
reset_metadata();
if (oid) {
if (!is_null_oid(oid)) {
c = lookup_commit_reference(the_repository, oid);
}
} else {
init_metadata();
c = lookup_commit_reference_by_name(METADATA_REF);
}
init_metadata(c);
reset_changeset_heads();
}

Expand Down Expand Up @@ -857,10 +855,12 @@ void init_cinnabar(const char *argv0)

int init_cinnabar_2(void)
{
struct commit *c;
if (nongit) {
return 0;
}
init_metadata();
c = lookup_commit_reference_by_name(METADATA_REF);
init_metadata(c);
hashmap_init(&git_tree_cache, oid_map_entry_cmp, NULL, 0);
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cinnabar-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void create_git_tree(const struct object_id *tree_id,
struct object_id *result);

void reset_manifest_heads(void);
void do_reload(int);
void do_reload(struct object_id *);
unsigned int replace_map_size(void);

const struct object_id *repo_lookup_replace_object(
Expand Down
17 changes: 11 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub const FULL_VERSION: &str = git_version!(

#[allow(improper_ctypes)]
extern "C" {
pub fn do_reload(reset: c_int);
pub fn do_reload(metadata: *const object_id);
fn do_cleanup(rollback: c_int);

fn do_store_metadata(result: *mut object_id);
Expand Down Expand Up @@ -273,7 +273,7 @@ fn do_done_and_check(args: &[&[u8]]) -> bool {
.unwrap();
transaction.commit().unwrap();
}
do_reload(0);
do_reload(std::ptr::null());
}
do_check_files()
}
Expand Down Expand Up @@ -839,11 +839,12 @@ extern "C" {
}

fn do_reclone() -> Result<(), String> {
unsafe {
let current_metadata_oid = unsafe {
let current_metadata_oid = metadata_oid.clone();
do_reload(/* reset */ 1);
metadata_oid = current_metadata_oid;
}
do_reload(&object_id::default());
metadata_oid = current_metadata_oid.clone();
current_metadata_oid
};

check_graft_refs();

Expand Down Expand Up @@ -942,6 +943,10 @@ fn do_reclone() -> Result<(), String> {
Ok(())
})
.and_then(|()| {
unsafe {
metadata_oid = current_metadata_oid;
}

do_done_and_check(&[])
.then_some(())
.ok_or_else(|| "Fatal error".to_string())?;
Expand Down
5 changes: 2 additions & 3 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use crate::progress::{progress_enabled, Progress};
use crate::tree_util::{diff_by_path, Empty, ParseTree, RecurseTree};
use crate::util::{FromBytes, ImmutBString, OsStrExt, ReadExt, SliceExt, ToBoxed, Transpose};
use crate::xdiff::{apply, textdiff, PatchInfo};
use crate::{check_enabled, do_reload, set_metadata_to, Checks, MetadataFlags};
use crate::{check_enabled, do_reload, Checks};

pub const REFS_PREFIX: &str = "refs/cinnabar/";
pub const REPLACE_REFS_PREFIX: &str = "refs/cinnabar/replace/";
Expand Down Expand Up @@ -1807,9 +1807,8 @@ pub fn merge_metadata(git_url: Url, hg_url: Option<Url>, branch: Option<&[u8]>)
}
}

set_metadata_to(Some(metadata_cid), MetadataFlags::FORCE, "cinnabarclone").unwrap();
unsafe {
do_reload(0);
do_reload(&object_id::from(metadata_cid));
}
true
}

0 comments on commit a476f29

Please sign in to comment.