Skip to content

Commit

Permalink
Move metadata oids to Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Nov 9, 2023
1 parent f9cbec0 commit ee14c9d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 43 deletions.
2 changes: 0 additions & 2 deletions src/cinnabar-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/libcinnabar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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,
child_process, die, FILES_META_OID, GIT2HG_OID, HG2GIT_OID, object_id, strbuf, FileMode,
RawTree,
};
use crate::oid::{Abbrev, ObjectId};
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 6 additions & 8 deletions src/libgit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ impl From<object_id> 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)]
Expand Down
14 changes: 7 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ 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,
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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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
};

Expand Down Expand Up @@ -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(&[])
Expand Down Expand Up @@ -2330,7 +2330,7 @@ fn do_fsck(force: bool, full: bool, commits: Vec<OsString>) -> Result<i32, Strin
);
return Ok(1);
}
let metadata_cid = unsafe { CommitId::from_unchecked(GitObjectId::from(metadata_oid.clone())) };
let metadata_cid = unsafe { CommitId::from_unchecked(GitObjectId::from(METADATA_OID.clone())) };
let checked_cid = if force {
None
} else {
Expand Down
44 changes: 22 additions & 22 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ use crate::libcinnabar::{
strslice_mut,
};
use crate::libgit::{
changesets_oid, commit, commit_oid, die, files_meta_oid, for_each_ref_in, get_oid_blob,
git2hg_oid, hg2git_oid, manifests_oid, metadata_oid, object_id, strbuf, Commit, RawBlob,
CHANGESETS_OID, commit, commit_oid, die, FILES_META_OID, for_each_ref_in, get_oid_blob,
GIT2HG_OID, HG2GIT_OID, MANIFESTS_OID, METADATA_OID, object_id, strbuf, Commit, RawBlob,
RawCommit, RawTree, RefTransaction,
};
use crate::oid::ObjectId;
Expand Down Expand Up @@ -858,7 +858,7 @@ impl ChangesetHeads {

fn from_stored_metadata() -> 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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ee14c9d

Please sign in to comment.