Skip to content

Commit

Permalink
Make hg2git, git2hg and files_meta private
Browse files Browse the repository at this point in the history
Most uses of the corresponding methods, though, need a mut reference at
the moment, but shouldn't. We'll fix that later.
  • Loading branch information
glandium committed Nov 11, 2023
1 parent eaf1f3d commit c26772b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 36 deletions.
20 changes: 10 additions & 10 deletions src/libcinnabar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ unsafe fn ensure_notes(t: *mut cinnabar_notes_tree) {
if !t.current.initialized() {
let oid;
let mut flags = 0;
if ptr::eq(t, &METADATA.git2hg.0) {
if ptr::eq(t, &METADATA.git2hg().0) {
oid = METADATA.git2hg_cid;
} else if ptr::eq(t, &METADATA.hg2git.0) {
} else if ptr::eq(t, &METADATA.hg2git().0) {
oid = METADATA.hg2git_cid;
} else if ptr::eq(t, &METADATA.files_meta.0) {
} else if ptr::eq(t, &METADATA.files_meta().0) {
oid = METADATA.files_meta_cid;
if !METADATA.flags.contains(MetadataFlags::FILES_META) {
flags = NOTES_INIT_EMPTY;
Expand Down Expand Up @@ -235,8 +235,8 @@ fn for_each_note_in<F: FnMut(GitObjectId, GitObjectId)>(notes: &mut cinnabar_not

#[no_mangle]
pub unsafe extern "C" fn resolve_hg2git(oid: *const hg_object_id) -> *const object_id {
ensure_notes(&mut METADATA.hg2git.0);
get_note_hg(&mut METADATA.hg2git.0, oid)
ensure_notes(&mut METADATA.hg2git_mut().0);
get_note_hg(&mut METADATA.hg2git_mut().0, oid)
}

unsafe fn get_note_hg(
Expand All @@ -251,8 +251,8 @@ unsafe fn get_note_hg(

#[no_mangle]
pub unsafe extern "C" fn get_files_meta(oid: *const hg_object_id) -> *const object_id {
ensure_notes(&mut METADATA.files_meta.0);
get_note_hg(&mut METADATA.files_meta.0, oid)
ensure_notes(&mut METADATA.files_meta_mut().0);
get_note_hg(&mut METADATA.files_meta_mut().0, oid)
}

unsafe fn add_note_hg(
Expand All @@ -269,15 +269,15 @@ unsafe fn add_note_hg(

#[no_mangle]
pub unsafe extern "C" fn add_hg2git(oid: *const hg_object_id, note_oid: *const object_id) -> c_int {
add_note_hg(&mut METADATA.hg2git.0, oid, note_oid)
add_note_hg(&mut METADATA.hg2git_mut().0, oid, note_oid)
}

#[no_mangle]
pub unsafe extern "C" fn add_files_meta(
oid: *const hg_object_id,
note_oid: *const object_id,
) -> c_int {
add_note_hg(&mut METADATA.files_meta.0, oid, note_oid)
add_note_hg(&mut METADATA.files_meta_mut().0, oid, note_oid)
}

pub unsafe fn store_metadata_notes(
Expand All @@ -288,7 +288,7 @@ pub unsafe fn store_metadata_notes(
let mut tree = object_id::default();
let notes = notes.as_mut().unwrap();
if notes.current.dirty() || notes.additions.dirty() {
let mode = if ptr::eq(notes, &METADATA.hg2git.0) {
let mode = if ptr::eq(notes, &METADATA.hg2git().0) {
FileMode::GITLINK
} else {
FileMode::REGULAR | FileMode::RW
Expand Down
14 changes: 7 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ pub fn prepare_arg(arg: OsString) -> Vec<u16> {
fn do_one_hg2git(sha1: Abbrev<HgChangesetId>) -> String {
format!("{}", unsafe {
METADATA
.hg2git
.hg2git_mut()
.get_note_abbrev(sha1)
.unwrap_or(GitObjectId::NULL)
})
Expand Down Expand Up @@ -389,7 +389,7 @@ where
fn do_data_changeset(rev: Abbrev<HgChangesetId>) -> Result<(), String> {
unsafe {
let commit_id = METADATA
.hg2git
.hg2git_mut()
.get_note_abbrev(rev)
.ok_or_else(|| format!("Unknown changeset id: {}", rev))?;
let changeset = RawHgChangeset::read(GitChangesetId::from_unchecked(
Expand All @@ -403,7 +403,7 @@ fn do_data_changeset(rev: Abbrev<HgChangesetId>) -> Result<(), String> {
fn do_data_manifest(rev: Abbrev<HgManifestId>) -> Result<(), String> {
unsafe {
let commit_id = METADATA
.hg2git
.hg2git_mut()
.get_note_abbrev(rev)
.ok_or_else(|| format!("Unknown manifest id: {}", rev))?;
let manifest = RawHgManifest::read(GitManifestId::from_unchecked(
Expand Down Expand Up @@ -1788,12 +1788,12 @@ fn do_data_file(rev: Abbrev<HgFileId>) -> Result<(), String> {
unsafe {
let mut stdout = stdout();
let blob_id = METADATA
.hg2git
.hg2git_mut()
.get_note_abbrev(rev)
.ok_or_else(|| format!("Unknown file id: {}", rev))?;
let file_id = GitFileId::from_unchecked(BlobId::from_unchecked(blob_id));
let metadata_id = METADATA
.files_meta
.files_meta_mut()
.get_note_abbrev(rev)
.map(|oid| GitFileMetadataId::from_unchecked(BlobId::from_unchecked(oid)));
let file = RawHgFile::read(file_id, metadata_id).unwrap();
Expand Down Expand Up @@ -3134,7 +3134,7 @@ fn do_fsck_full(
}

if full_fsck && !broken.get() {
unsafe { &mut METADATA.hg2git }.for_each(|h, _| {
unsafe { &mut METADATA }.hg2git_mut().for_each(|h, _| {
if seen_changesets.contains(&HgChangesetId::from_unchecked(h))
|| seen_manifests.contains(&HgManifestId::from_unchecked(h))
|| seen_files.contains(&HgFileId::from_unchecked(h))
Expand All @@ -3149,7 +3149,7 @@ fn do_fsck_full(
unsafe { &mut METADATA }.set(SetWhat::File, h, GitObjectId::NULL);
unsafe { &mut METADATA }.set(SetWhat::FileMeta, h, GitObjectId::NULL);
});
unsafe { &mut METADATA.git2hg }.for_each(|g, _| {
unsafe { &mut METADATA }.git2hg_mut().for_each(|g, _| {
// TODO: this is gross.
let cid = GitChangesetId::from_unchecked(CommitId::from_unchecked(g));
if seen_git2hg.contains(&cid) {
Expand Down
67 changes: 48 additions & 19 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ pub struct Metadata {
pub hg2git_cid: CommitId,
pub git2hg_cid: CommitId,
pub files_meta_cid: CommitId,
pub hg2git: hg_notes_tree,
pub git2hg: git_notes_tree,
pub files_meta: hg_notes_tree,
pub hg2git_: hg_notes_tree,
pub git2hg_: git_notes_tree,
pub files_meta_: hg_notes_tree,
pub flags: MetadataFlags,
changeset_heads_: OnceCell<ChangesetHeads>,
manifest_heads_: OnceCell<ManifestHeads>,
Expand All @@ -97,9 +97,9 @@ impl Metadata {
git2hg_cid: CommitId::NULL,
hg2git_cid: CommitId::NULL,
files_meta_cid: CommitId::NULL,
git2hg: git_notes_tree::new(),
hg2git: hg_notes_tree::new(),
files_meta: hg_notes_tree::new(),
git2hg_: git_notes_tree::new(),
hg2git_: hg_notes_tree::new(),
files_meta_: hg_notes_tree::new(),
flags: MetadataFlags::empty(),
changeset_heads_: OnceCell::new(),
manifest_heads_: OnceCell::new(),
Expand Down Expand Up @@ -137,6 +137,30 @@ impl Metadata {
self.manifest_heads();
self.manifest_heads_.get_mut().unwrap()
}

pub fn hg2git(&self) -> &hg_notes_tree {
&self.hg2git_
}

pub fn hg2git_mut(&mut self) -> &mut hg_notes_tree {
&mut self.hg2git_
}

pub fn git2hg(&self) -> &git_notes_tree {
&self.git2hg_
}

pub fn git2hg_mut(&mut self) -> &mut git_notes_tree {
&mut self.git2hg_
}

pub fn files_meta(&self) -> &hg_notes_tree {
&self.files_meta_
}

pub fn files_meta_mut(&mut self) -> &mut hg_notes_tree {
&mut self.files_meta_
}
}

pub fn has_metadata() -> bool {
Expand All @@ -149,7 +173,7 @@ macro_rules! hg2git {
pub fn to_git(self) -> Option<$g> {
unsafe {
METADATA
.hg2git
.hg2git_mut()
.get_note(self.into())
.map(|o| $g::from_raw_bytes(o.as_raw_bytes()).unwrap())
}
Expand Down Expand Up @@ -178,7 +202,7 @@ pub struct RawGitChangesetMetadata(RawBlob);

impl RawGitChangesetMetadata {
pub fn read(changeset_id: GitChangesetId) -> Option<Self> {
Self::read_from_notes_tree(unsafe { &mut METADATA.git2hg }, changeset_id)
Self::read_from_notes_tree(unsafe { &mut METADATA }.git2hg_mut(), changeset_id)
}

pub fn read_from_notes_tree(
Expand Down Expand Up @@ -755,7 +779,9 @@ impl RawHgFile {
if oid == Self::EMPTY_OID {
Some(Self(vec![].into()))
} else {
let metadata = unsafe { METADATA.files_meta.get_note(oid.into()) }
let metadata = unsafe { &mut METADATA }
.files_meta_mut()
.get_note(oid.into())
.map(BlobId::from_unchecked)
.map(GitFileMetadataId::from_unchecked);
Self::read(oid.to_git().unwrap(), metadata)
Expand Down Expand Up @@ -1210,10 +1236,10 @@ impl Metadata {
match what {
SetWhat::Changeset => {
if git_id.is_null() {
self.hg2git.remove_note(hg_id);
self.hg2git_mut().remove_note(hg_id);
} else if let Ok(ref mut commit) = CommitId::try_from(git_id) {
handle_changeset_conflict(HgChangesetId::from_unchecked(hg_id), commit);
self.hg2git.add_note(hg_id, (*commit).into());
self.hg2git_mut().add_note(hg_id, (*commit).into());
} else {
die!("Invalid object");
}
Expand All @@ -1222,11 +1248,11 @@ impl Metadata {
let csid = HgChangesetId::from_unchecked(hg_id);
if let Some(cid) = csid.to_git() {
if git_id.is_null() {
self.git2hg.remove_note(cid.into());
self.git2hg_mut().remove_note(cid.into());
} else if BlobId::try_from(git_id).is_err() {
die!("Invalid object");
} else {
self.git2hg.add_note(cid.into(), git_id);
self.git2hg_mut().add_note(cid.into(), git_id);
}
} else if !git_id.is_null() {
die!("Invalid sha1");
Expand All @@ -1238,13 +1264,13 @@ impl Metadata {
CommitId::from_unchecked(git_id),
));
}
set::<CommitId>(&mut self.hg2git, hg_id, git_id);
set::<CommitId>(self.hg2git_mut(), hg_id, git_id);
}
SetWhat::File => {
set::<BlobId>(&mut self.hg2git, hg_id, git_id);
set::<BlobId>(self.hg2git_mut(), hg_id, git_id);
}
SetWhat::FileMeta => {
set::<BlobId>(&mut self.files_meta, hg_id, git_id);
set::<BlobId>(self.files_meta_mut(), hg_id, git_id);
}
}
}
Expand Down Expand Up @@ -2218,9 +2244,12 @@ pub fn do_store_metadata(metadata: &mut Metadata) -> CommitId {
let mut tree = object_id::default();
let mut previous = None;
unsafe {
hg2git_ = metadata.hg2git.store(metadata.hg2git_cid);
git2hg_ = metadata.git2hg.store(metadata.git2hg_cid);
files_meta_ = metadata.files_meta.store(metadata.files_meta_cid);
let hg2git_cid = metadata.hg2git_cid;
hg2git_ = metadata.hg2git_mut().store(hg2git_cid);
let git2hg_cid = metadata.git2hg_cid;
git2hg_ = metadata.git2hg_mut().store(git2hg_cid);
let files_meta_cid = metadata.files_meta_cid;
files_meta_ = metadata.files_meta_mut().store(files_meta_cid);
manifests = store_manifests_metadata(metadata);
changesets = store_changesets_metadata(metadata);
if !metadata.metadata_cid.is_null() {
Expand Down

0 comments on commit c26772b

Please sign in to comment.