Skip to content

Commit

Permalink
Group all the metadata statics in a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Nov 10, 2023
1 parent f577599 commit c60ef28
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 83 deletions.
33 changes: 15 additions & 18 deletions src/libcinnabar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ use crate::libgit::{
strbuf, FileMode, RawTree,
};
use crate::oid::{Abbrev, ObjectId};
use crate::store::{
store_git_commit, MetadataFlags, FILES_META, FILES_META_OID, GIT2HG, GIT2HG_OID, HG2GIT,
HG2GIT_OID, METADATA_FLAGS,
};
use crate::store::{store_git_commit, MetadataFlags, METADATA};

#[allow(non_camel_case_types)]
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -189,13 +186,13 @@ unsafe fn ensure_notes(t: *mut cinnabar_notes_tree) {
if !t.current.initialized() {
let oid;
let mut flags = 0;
if ptr::eq(t, &GIT2HG.0) {
oid = GIT2HG_OID;
} else if ptr::eq(t, &HG2GIT.0) {
oid = HG2GIT_OID;
} else if ptr::eq(t, &FILES_META.0) {
oid = FILES_META_OID;
if !METADATA_FLAGS.contains(MetadataFlags::FILES_META) {
if ptr::eq(t, &METADATA.git2hg.0) {
oid = METADATA.git2hg_cid;
} else if ptr::eq(t, &METADATA.hg2git.0) {
oid = METADATA.hg2git_cid;
} 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;
}
} else {
Expand Down Expand Up @@ -238,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 HG2GIT.0);
get_note_hg(&mut HG2GIT.0, oid)
ensure_notes(&mut METADATA.hg2git.0);
get_note_hg(&mut METADATA.hg2git.0, oid)
}

unsafe fn get_note_hg(
Expand All @@ -254,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 FILES_META.0);
get_note_hg(&mut FILES_META.0, oid)
ensure_notes(&mut METADATA.files_meta.0);
get_note_hg(&mut METADATA.files_meta.0, oid)
}

unsafe fn add_note_hg(
Expand All @@ -272,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 HG2GIT.0, oid, note_oid)
add_note_hg(&mut METADATA.hg2git.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 FILES_META.0, oid, note_oid)
add_note_hg(&mut METADATA.files_meta.0, oid, note_oid)
}

pub unsafe fn store_metadata_notes(
Expand All @@ -291,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, &HG2GIT.0) {
let mode = if ptr::eq(notes, &METADATA.hg2git.0) {
FileMode::GITLINK
} else {
FileMode::REGULAR | FileMode::RW
Expand Down
36 changes: 21 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ use store::{
raw_commit_for_changeset, reset_changeset_heads, reset_manifest_heads, store_git_blob,
store_manifest, ChangesetHeads, GeneratedGitChangesetMetadata, RawGitChangesetMetadata,
RawHgChangeset, RawHgFile, RawHgManifest, SetWhat, BROKEN_REF, CHANGESET_HEADS, CHECKED_REF,
FILES_META, GIT2HG, HG2GIT, METADATA_OID, METADATA_REF, NOTES_REF, REFS_PREFIX,
REPLACE_REFS_PREFIX,
METADATA, METADATA_REF, NOTES_REF, REFS_PREFIX, REPLACE_REFS_PREFIX,
};
use tree_util::{diff_by_path, RecurseTree};
use url::Url;
Expand Down Expand Up @@ -326,7 +325,10 @@ pub fn prepare_arg(arg: OsString) -> Vec<u16> {

fn do_one_hg2git(sha1: Abbrev<HgChangesetId>) -> String {
format!("{}", unsafe {
HG2GIT.get_note_abbrev(sha1).unwrap_or(GitObjectId::NULL)
METADATA
.hg2git
.get_note_abbrev(sha1)
.unwrap_or(GitObjectId::NULL)
})
}

Expand Down Expand Up @@ -389,7 +391,8 @@ where

fn do_data_changeset(rev: Abbrev<HgChangesetId>) -> Result<(), String> {
unsafe {
let commit_id = HG2GIT
let commit_id = METADATA
.hg2git
.get_note_abbrev(rev)
.ok_or_else(|| format!("Unknown changeset id: {}", rev))?;
let changeset = RawHgChangeset::read(GitChangesetId::from_unchecked(
Expand All @@ -402,7 +405,8 @@ fn do_data_changeset(rev: Abbrev<HgChangesetId>) -> Result<(), String> {

fn do_data_manifest(rev: Abbrev<HgManifestId>) -> Result<(), String> {
unsafe {
let commit_id = HG2GIT
let commit_id = METADATA
.hg2git
.get_note_abbrev(rev)
.ok_or_else(|| format!("Unknown manifest id: {}", rev))?;
let manifest = RawHgManifest::read(GitManifestId::from_unchecked(
Expand Down Expand Up @@ -951,9 +955,9 @@ fn do_reclone(rebase: bool) -> Result<(), String> {
}
}

let old_changesets_oid = unsafe { store::CHANGESETS_OID };
let old_changesets_oid = unsafe { METADATA.changesets_cid };
let mut old_git2hg = {
let git2hg_oid = unsafe { store::GIT2HG_OID };
let git2hg_oid = unsafe { METADATA.git2hg_cid };
if git2hg_oid.is_null() {
None
} else {
Expand All @@ -962,9 +966,9 @@ fn do_reclone(rebase: bool) -> Result<(), String> {
};

let current_metadata_oid = unsafe {
let current_metadata_oid = METADATA_OID;
let current_metadata_oid = METADATA.metadata_cid;
do_reload(Some(CommitId::NULL));
METADATA_OID = current_metadata_oid;
METADATA.metadata_cid = current_metadata_oid;
current_metadata_oid
};

Expand Down Expand Up @@ -1290,7 +1294,7 @@ fn do_reclone(rebase: bool) -> Result<(), String> {
}

unsafe {
METADATA_OID = current_metadata_oid;
METADATA.metadata_cid = current_metadata_oid;
}

do_done_and_check(&[])
Expand Down Expand Up @@ -1786,11 +1790,13 @@ fn do_setup() -> Result<(), String> {
fn do_data_file(rev: Abbrev<HgFileId>) -> Result<(), String> {
unsafe {
let mut stdout = stdout();
let blob_id = HG2GIT
let blob_id = METADATA
.hg2git
.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 = FILES_META
let metadata_id = METADATA
.files_meta
.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 @@ -2338,7 +2344,7 @@ fn do_fsck(force: bool, full: bool, commits: Vec<OsString>) -> Result<i32, Strin
);
return Ok(1);
}
let metadata_cid = unsafe { METADATA_OID };
let metadata_cid = unsafe { METADATA.metadata_cid };
let checked_cid = if force {
None
} else {
Expand Down Expand Up @@ -3131,7 +3137,7 @@ fn do_fsck_full(
}

if full_fsck && !broken.get() {
unsafe { &mut HG2GIT }.for_each(|h, _| {
unsafe { &mut METADATA.hg2git }.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 @@ -3146,7 +3152,7 @@ fn do_fsck_full(
do_set(SetWhat::File, h, GitObjectId::NULL);
do_set(SetWhat::FileMeta, h, GitObjectId::NULL);
});
unsafe { &mut GIT2HG }.for_each(|g, _| {
unsafe { &mut METADATA.git2hg }.for_each(|g, _| {
// TODO: this is gross.
let cid = GitChangesetId::from_unchecked(CommitId::from_unchecked(g));
if seen_git2hg.contains(&cid) {
Expand Down
Loading

0 comments on commit c60ef28

Please sign in to comment.