Skip to content

Commit

Permalink
Use bitflags for METADATA_FLAGS
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Nov 10, 2023
1 parent 403d3b4 commit 8e2cba8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/libcinnabar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::libgit::{
HG2GIT_OID,
};
use crate::oid::{Abbrev, ObjectId};
use crate::store::{store_git_commit, FILES_META, METADATA_FLAGS};
use crate::store::{store_git_commit, MetadataFlags, METADATA_FLAGS};

#[allow(non_camel_case_types)]
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -174,7 +174,7 @@ unsafe fn ensure_notes(t: *mut cinnabar_notes_tree) {
oid = HG2GIT_OID;
} else if ptr::eq(t, &files_meta.0) {
oid = FILES_META_OID;
if METADATA_FLAGS & FILES_META == 0 {
if METADATA_FLAGS.contains(MetadataFlags::FILES_META) {
flags = NOTES_INIT_EMPTY;
}
} else {
Expand Down
14 changes: 7 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ fn do_done_and_check(args: &[&[u8]]) -> bool {
do_cleanup(0);
set_metadata_to(
Some(new_metadata),
MetadataFlags::FORCE | MetadataFlags::KEEP_REFS,
SetMetadataFlags::FORCE | SetMetadataFlags::KEEP_REFS,
"update",
)
.unwrap();
Expand Down Expand Up @@ -652,20 +652,20 @@ fn get_previous_metadata(metadata: CommitId) -> Option<CommitId> {

bitflags! {
#[derive(Debug)]
pub struct MetadataFlags: i32 {
pub struct SetMetadataFlags: i32 {
const FORCE = 0x1;
const KEEP_REFS = 0x2;
}
}

fn set_metadata_to(
new_metadata: Option<CommitId>,
flags: MetadataFlags,
flags: SetMetadataFlags,
msg: &str,
) -> Result<Option<CommitId>, String> {
let mut refs = HashMap::new();
for_each_ref_in(REFS_PREFIX, |r, oid| {
if flags.contains(MetadataFlags::KEEP_REFS)
if flags.contains(SetMetadataFlags::KEEP_REFS)
&& (r.as_bytes().starts_with_str("refs/")
|| r.as_bytes().starts_with_str("hg/")
|| r == "HEAD")
Expand Down Expand Up @@ -724,7 +724,7 @@ fn set_metadata_to(

let mut m = metadata;
let found = flags
.contains(MetadataFlags::FORCE)
.contains(SetMetadataFlags::FORCE)
.then(|| {
state = MetadataState::Unknown;
new
Expand Down Expand Up @@ -1526,9 +1526,9 @@ fn do_rollback(
return Err("Nothing to rollback.".to_string());
};
let flags = if force {
MetadataFlags::FORCE
SetMetadataFlags::FORCE
} else {
MetadataFlags::empty()
SetMetadataFlags::empty()
};
set_metadata_to(wanted_metadata, flags, "rollback").map(|_| ())
}
Expand Down
23 changes: 16 additions & 7 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::rc::Rc;
use std::sync::Mutex;

use bit_vec::BitVec;
use bitflags::bitflags;
use bstr::{BStr, BString, ByteSlice};
use derive_more::Deref;
use either::Either;
Expand Down Expand Up @@ -65,13 +66,18 @@ pub const CHECKED_REF: &str = "refs/cinnabar/checked";
pub const BROKEN_REF: &str = "refs/cinnabar/broken";
pub const NOTES_REF: &str = "refs/notes/cinnabar";

pub static mut METADATA_FLAGS: c_int = 0;
bitflags! {
#[derive(Debug, Copy, Clone)]
pub struct MetadataFlags: i32 {
const FILES_META = 0x1;
const UNIFIED_MANIFESTS_V2 = 0x2;

pub const FILES_META: c_int = 0x1;
pub const UNIFIED_MANIFESTS_V2: c_int = 0x2;
}
}
pub static mut METADATA_FLAGS: MetadataFlags = MetadataFlags::empty();

pub fn has_metadata() -> bool {
unsafe { METADATA_FLAGS != 0 }
unsafe { !METADATA_FLAGS.is_empty() }
}

macro_rules! hg2git {
Expand Down Expand Up @@ -2098,16 +2104,19 @@ pub unsafe extern "C" fn init_metadata(c: *const commit) {
for flag in c.body().split(|&b| b == b' ') {
match flag {
b"files-meta" => {
METADATA_FLAGS |= FILES_META;
METADATA_FLAGS.insert(MetadataFlags::FILES_META);
}
b"unified-manifests" => old_metadata(),
b"unified-manifests-v2" => {
METADATA_FLAGS |= UNIFIED_MANIFESTS_V2;
METADATA_FLAGS.insert(MetadataFlags::UNIFIED_MANIFESTS_V2);
}
_ => new_metadata(),
}
}
if METADATA_FLAGS != FILES_META | UNIFIED_MANIFESTS_V2 {
if !METADATA_FLAGS
.difference(MetadataFlags::FILES_META | MetadataFlags::UNIFIED_MANIFESTS_V2)
.is_empty()
{
old_metadata();
}
let mut count = 0;
Expand Down

0 comments on commit 8e2cba8

Please sign in to comment.