From b327f441036d09bdff3bf0f61770ac9accea1fbb Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Fri, 10 Nov 2023 08:01:51 +0900 Subject: [PATCH] Move metadata oids to Rust --- src/cinnabar-helper.c | 2 -- src/libcinnabar.rs | 10 +++++----- src/libgit.rs | 14 ++++++------- src/main.rs | 20 +++++++++---------- src/store.rs | 46 +++++++++++++++++++++---------------------- 5 files changed, 44 insertions(+), 48 deletions(-) diff --git a/src/cinnabar-helper.c b/src/cinnabar-helper.c index 853eabf34..74da70882 100644 --- a/src/cinnabar-helper.c +++ b/src/cinnabar-helper.c @@ -44,8 +44,6 @@ #define STRINGIFY(s) _STRINGIFY(s) struct notes_tree git2hg, hg2git, files_meta; -struct object_id metadata_oid, changesets_oid, manifests_oid, git2hg_oid, - hg2git_oid, files_meta_oid; struct object_id *commit_oid(struct commit *c) { return &c->object.oid; diff --git a/src/libcinnabar.rs b/src/libcinnabar.rs index fd337b877..a8d602f4c 100644 --- a/src/libcinnabar.rs +++ b/src/libcinnabar.rs @@ -14,8 +14,8 @@ use derive_more::{Deref, DerefMut}; use crate::git::{GitObjectId, TreeId}; use crate::hg::HgObjectId; use crate::libgit::{ - child_process, die, files_meta_oid, git2hg_oid, hg2git_oid, object_id, strbuf, FileMode, - RawTree, + child_process, die, object_id, strbuf, FileMode, RawTree, FILES_META_OID, GIT2HG_OID, + HG2GIT_OID, }; use crate::oid::{Abbrev, ObjectId}; use crate::store::{store_git_commit, FILES_META, METADATA_FLAGS}; @@ -171,11 +171,11 @@ unsafe fn ensure_notes(t: *mut cinnabar_notes_tree) { let oid; let mut flags = 0; if ptr::eq(t, &git2hg.0) { - oid = git2hg_oid.clone(); + oid = GIT2HG_OID.clone(); } else if ptr::eq(t, &hg2git.0) { - oid = hg2git_oid.clone(); + oid = HG2GIT_OID.clone(); } else if ptr::eq(t, &files_meta.0) { - oid = files_meta_oid.clone(); + oid = FILES_META_OID.clone(); if METADATA_FLAGS & FILES_META == 0 { flags = NOTES_INIT_EMPTY; } diff --git a/src/libgit.rs b/src/libgit.rs index e0ab4ae2c..35b8df791 100644 --- a/src/libgit.rs +++ b/src/libgit.rs @@ -62,14 +62,12 @@ impl From for GitObjectId { } } -extern "C" { - pub static mut metadata_oid: object_id; - pub static mut changesets_oid: object_id; - pub static mut manifests_oid: object_id; - pub static mut git2hg_oid: object_id; - pub static mut hg2git_oid: object_id; - pub static mut files_meta_oid: object_id; -} +pub static mut METADATA_OID: object_id = object_id([0; GIT_MAX_RAWSZ], GIT_HASH_SHA1); +pub static mut CHANGESETS_OID: object_id = object_id([0; GIT_MAX_RAWSZ], GIT_HASH_SHA1); +pub static mut MANIFESTS_OID: object_id = object_id([0; GIT_MAX_RAWSZ], GIT_HASH_SHA1); +pub static mut GIT2HG_OID: object_id = object_id([0; GIT_MAX_RAWSZ], GIT_HASH_SHA1); +pub static mut HG2GIT_OID: object_id = object_id([0; GIT_MAX_RAWSZ], GIT_HASH_SHA1); +pub static mut FILES_META_OID: object_id = object_id([0; GIT_MAX_RAWSZ], GIT_HASH_SHA1); #[allow(non_camel_case_types)] #[repr(C)] diff --git a/src/main.rs b/src/main.rs index 479b343c9..59fdefb5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -109,10 +109,10 @@ use itertools::{EitherOrBoth, Itertools}; use libcinnabar::{files_meta, git2hg, git_notes_tree, hg2git}; use libgit::{ commit, config_get_value, die, diff_tree_with_copies, for_each_ref_in, for_each_remote, - get_oid_committish, get_unique_abbrev, lookup_commit, lookup_replace_commit, metadata_oid, - object_id, reachable_subset, remote, repository, resolve_ref, rev_list, - rev_list_with_boundaries, strbuf, the_repository, DiffTreeItem, MaybeBoundary, RawBlob, - RawCommit, RawTree, RefTransaction, + get_oid_committish, get_unique_abbrev, lookup_commit, lookup_replace_commit, object_id, + reachable_subset, remote, repository, resolve_ref, rev_list, rev_list_with_boundaries, strbuf, + the_repository, DiffTreeItem, MaybeBoundary, RawBlob, RawCommit, RawTree, RefTransaction, + METADATA_OID, }; use logging::{LoggingReader, LoggingWriter}; use oid::{Abbrev, ObjectId}; @@ -941,9 +941,9 @@ fn do_reclone(rebase: bool) -> Result<(), String> { } } - let old_changesets_oid = GitObjectId::from(unsafe { libgit::changesets_oid.clone() }); + let old_changesets_oid = GitObjectId::from(unsafe { libgit::CHANGESETS_OID.clone() }); let mut old_git2hg = { - let git2hg_oid = GitObjectId::from(unsafe { libgit::git2hg_oid.clone() }); + let git2hg_oid = GitObjectId::from(unsafe { libgit::GIT2HG_OID.clone() }); if git2hg_oid.is_null() { None } else { @@ -953,9 +953,9 @@ fn do_reclone(rebase: bool) -> Result<(), String> { }; let current_metadata_oid = unsafe { - let current_metadata_oid = metadata_oid.clone(); + let current_metadata_oid = METADATA_OID.clone(); do_reload(&object_id::default()); - metadata_oid = current_metadata_oid.clone(); + METADATA_OID = current_metadata_oid.clone(); current_metadata_oid }; @@ -1282,7 +1282,7 @@ fn do_reclone(rebase: bool) -> Result<(), String> { } unsafe { - metadata_oid = current_metadata_oid; + METADATA_OID = current_metadata_oid; } do_done_and_check(&[]) @@ -2330,7 +2330,7 @@ fn do_fsck(force: bool, full: bool, commits: Vec) -> Result Self { let changesets_cid = - CommitId::from_raw_bytes(unsafe { changesets_oid.as_raw_bytes() }).unwrap(); + CommitId::from_raw_bytes(unsafe { CHANGESETS_OID.as_raw_bytes() }).unwrap(); if changesets_cid.is_null() { ChangesetHeads::new() } else { @@ -931,7 +931,7 @@ impl ManifestHeads { fn from_stored_metadata() -> Self { let manifests_cid = - CommitId::from_raw_bytes(unsafe { manifests_oid.as_raw_bytes() }).unwrap(); + CommitId::from_raw_bytes(unsafe { MANIFESTS_OID.as_raw_bytes() }).unwrap(); if manifests_cid.is_null() { ManifestHeads::new() } else { @@ -1555,7 +1555,7 @@ pub fn do_check_files() -> bool { .update( BROKEN_REF, unsafe { - CommitId::from_unchecked(GitObjectId::from(crate::libgit::metadata_oid.clone())) + CommitId::from_unchecked(GitObjectId::from(crate::libgit::METADATA_OID.clone())) }, None, "post-pull check", @@ -2064,12 +2064,12 @@ pub unsafe extern "C" fn init_metadata(c: *const commit) { let cid = if let Some(c) = c.as_ref() { CommitId::from_unchecked(GitObjectId::from(commit_oid(c).as_ref().unwrap().clone())) } else { - metadata_oid = object_id::default(); - changesets_oid = object_id::default(); - manifests_oid = object_id::default(); - hg2git_oid = object_id::default(); - git2hg_oid = object_id::default(); - files_meta_oid = object_id::default(); + METADATA_OID = object_id::default(); + CHANGESETS_OID = object_id::default(); + MANIFESTS_OID = object_id::default(); + HG2GIT_OID = object_id::default(); + GIT2HG_OID = object_id::default(); + FILES_META_OID = object_id::default(); return; }; let c = RawCommit::read(cid).unwrap(); @@ -2078,12 +2078,12 @@ pub unsafe extern "C" fn init_metadata(c: *const commit) { die!("Invalid metadata?"); } for (cid, field) in Some(cid).iter().chain(c.parents()[..5].iter()).zip([ - &mut metadata_oid, - &mut changesets_oid, - &mut manifests_oid, - &mut hg2git_oid, - &mut git2hg_oid, - &mut files_meta_oid, + &mut METADATA_OID, + &mut CHANGESETS_OID, + &mut MANIFESTS_OID, + &mut HG2GIT_OID, + &mut GIT2HG_OID, + &mut FILES_META_OID, ]) { *field = (*cid).into(); } @@ -2171,14 +2171,14 @@ pub fn do_store_metadata() -> CommitId { let mut tree = object_id::default(); let mut previous = None; unsafe { - store_metadata_notes(&mut *hg2git, &hg2git_oid, &mut hg2git_); - store_metadata_notes(&mut *git2hg, &git2hg_oid, &mut git2hg_); - store_metadata_notes(&mut *files_meta, &files_meta_oid, &mut files_meta_); + store_metadata_notes(&mut *hg2git, &HG2GIT_OID, &mut hg2git_); + store_metadata_notes(&mut *git2hg, &GIT2HG_OID, &mut git2hg_); + store_metadata_notes(&mut *files_meta, &FILES_META_OID, &mut files_meta_); store_manifests_metadata(&mut manifests); store_changesets_metadata(&mut changesets); - if !GitObjectId::from(metadata_oid.clone()).is_null() { + if !GitObjectId::from(METADATA_OID.clone()).is_null() { previous = Some(CommitId::from_unchecked(GitObjectId::from( - metadata_oid.clone(), + METADATA_OID.clone(), ))); } store_replace_map(&mut tree);