Skip to content

Commit

Permalink
Move ensure_notes to Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Nov 9, 2023
1 parent f81c67b commit c04de26
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 32 deletions.
21 changes: 0 additions & 21 deletions src/cinnabar-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,27 +158,6 @@ void diff_tree_(int argc, const char **argv, void (*cb)(void *, struct diff_tree
release_revisions(&revs);
}

void ensure_notes(struct notes_tree *notes)
{
if (!notes_initialized(notes)) {
const struct object_id *oid;
int flags = 0;
if (notes == &git2hg)
oid = &git2hg_oid;
else if (notes == &hg2git)
oid = &hg2git_oid;
else if (notes == &files_meta) {
oid = &files_meta_oid;
if (!(metadata_flags & FILES_META))
flags = NOTES_INIT_EMPTY;
} else
die("Unknown notes tree");
if (is_null_oid(oid))
flags = NOTES_INIT_EMPTY;
init_notes(notes, oid_to_hex(oid), combine_notes_ignore, flags);
}
}

const struct object_id *repo_lookup_replace_object(
struct repository *r, const struct object_id *oid)
{
Expand Down
2 changes: 0 additions & 2 deletions src/cinnabar-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ extern int cinnabar_check(int);

extern struct notes_tree git2hg, hg2git, files_meta;

extern void ensure_notes(struct notes_tree *notes);

int check_manifest(const struct object_id *oid);

struct remote;
Expand Down
5 changes: 5 additions & 0 deletions src/cinnabar-notes.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ void cinnabar_free_notes(struct cinnabar_notes_tree *t)
free_notes(&t->additions);
}

int notes_initialized(struct cinnabar_notes_tree *notes)
{
return notes->current.initialized;
}

int notes_dirty(struct cinnabar_notes_tree *notes)
{
return notes->current.dirty || notes->additions.dirty;
Expand Down
5 changes: 1 addition & 4 deletions src/cinnabar-notes.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ struct cinnabar_notes_tree {
#define for_each_note cinnabar_for_each_note
#define write_notes_tree cinnabar_write_notes_tree

static inline int notes_initialized(struct notes_tree *notes)
{
return notes->current.initialized;
}
int notes_initialized(struct notes_tree *notes);

int notes_dirty(struct notes_tree *notes);

Expand Down
50 changes: 46 additions & 4 deletions src/libcinnabar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::ffi::CString;
use std::io::Write;
use std::marker::PhantomData;
use std::mem::MaybeUninit;
Expand All @@ -12,9 +13,12 @@ use derive_more::{Deref, DerefMut};

use crate::git::{GitObjectId, TreeId};
use crate::hg::HgObjectId;
use crate::libgit::{child_process, object_id, strbuf, FileMode, RawTree};
use crate::libgit::{
child_process, die, files_meta_oid, git2hg_oid, hg2git_oid, object_id, strbuf, FileMode,
RawTree,
};
use crate::oid::{Abbrev, ObjectId};
use crate::store::store_git_commit;
use crate::store::{metadata_flags, store_git_commit, FILES_META};

#[allow(non_camel_case_types)]
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -105,7 +109,17 @@ extern "C" {
pub static mut hg2git: hg_notes_tree;
pub static mut files_meta: hg_notes_tree;

fn ensure_notes(t: *mut cinnabar_notes_tree);
fn combine_notes_ignore(cur_oid: *mut object_id, new_oid: *const object_id) -> c_int;

fn init_notes(
notes: *mut cinnabar_notes_tree,
notes_ref: *const c_char,
combine_notes_fn: unsafe extern "C" fn(
cur_oid: *mut object_id,
new_oid: *const object_id,
) -> c_int,
flags: c_int,
);

fn cinnabar_get_note(
notes: *mut cinnabar_notes_tree,
Expand Down Expand Up @@ -138,7 +152,8 @@ extern "C" {

fn cinnabar_remove_note(notes: *mut cinnabar_notes_tree, object_sha1: *const u8);

fn notes_dirty(noted: *const cinnabar_notes_tree) -> c_int;
fn notes_initialized(notes: *const cinnabar_notes_tree) -> c_int;
fn notes_dirty(notes: *const cinnabar_notes_tree) -> c_int;

fn cinnabar_write_notes_tree(
notes: *mut cinnabar_notes_tree,
Expand All @@ -147,6 +162,33 @@ extern "C" {
) -> c_int;
}

const NOTES_INIT_EMPTY: c_int = 1;

unsafe fn ensure_notes(t: *mut cinnabar_notes_tree) {
if notes_initialized(t) == 0 {
let oid;
let mut flags = 0;
if ptr::eq(t, &git2hg.0) {
oid = git2hg_oid.clone();
} else if ptr::eq(t, &hg2git.0) {
oid = hg2git_oid.clone();
} else if ptr::eq(t, &files_meta.0) {
oid = files_meta_oid.clone();
if metadata_flags & FILES_META == 0 {
flags = NOTES_INIT_EMPTY;
}
} else {
die!("Unknown notes tree");
}
let oid = GitObjectId::from(oid);
if oid.is_null() {
flags = NOTES_INIT_EMPTY;
}
let oid = CString::new(oid.to_string()).unwrap();
init_notes(t, oid.as_ptr(), combine_notes_ignore, flags);
}
}

fn for_each_note_in<F: FnMut(GitObjectId, GitObjectId)>(notes: &mut cinnabar_notes_tree, mut f: F) {
unsafe extern "C" fn each_note_cb<F: FnMut(GitObjectId, GitObjectId)>(
oid: *const object_id,
Expand Down
4 changes: 3 additions & 1 deletion src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ pub const BROKEN_REF: &str = "refs/cinnabar/broken";
pub const NOTES_REF: &str = "refs/notes/cinnabar";

extern "C" {
static metadata_flags: c_int;
pub static metadata_flags: c_int;
}

pub const FILES_META: c_int = 0x1;

pub fn has_metadata() -> bool {
unsafe { metadata_flags != 0 }
}
Expand Down

0 comments on commit c04de26

Please sign in to comment.