From a476f298eaed5cda5b8efc951a6742aac9c519ea Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Thu, 2 Nov 2023 20:05:06 +0900 Subject: [PATCH] Avoid resetting metadata history when recloning with cinnabarclone --- src/cinnabar-helper.c | 38 +++++++++++++++++++------------------- src/cinnabar-helper.h | 2 +- src/main.rs | 17 +++++++++++------ src/store.rs | 5 ++--- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/cinnabar-helper.c b/src/cinnabar-helper.c index 131e69668..8212594e4 100644 --- a/src/cinnabar-helper.c +++ b/src/cinnabar-helper.c @@ -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; @@ -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); @@ -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); @@ -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(); } @@ -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; } diff --git a/src/cinnabar-helper.h b/src/cinnabar-helper.h index 8cf2c9ca4..d1eddcfac 100644 --- a/src/cinnabar-helper.h +++ b/src/cinnabar-helper.h @@ -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( diff --git a/src/main.rs b/src/main.rs index 6a56cd1a5..10f00e0d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); @@ -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() } @@ -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(); @@ -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())?; diff --git a/src/store.rs b/src/store.rs index 5847f9719..a0f9573a6 100644 --- a/src/store.rs +++ b/src/store.rs @@ -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/"; @@ -1807,9 +1807,8 @@ pub fn merge_metadata(git_url: Url, hg_url: Option, 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 }