Skip to content

Commit

Permalink
Initialize old_git2hg in Rust rather than C
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Nov 10, 2023
1 parent 389bb8e commit 2839520
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 51 deletions.
12 changes: 0 additions & 12 deletions src/cinnabar-notes.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,3 @@ void consolidate_notes(struct cinnabar_notes_tree *t) {
}
free(notes_ref);
}

struct cinnabar_notes_tree *new_notes_tree(const char *notes_ref)
{
struct cinnabar_notes_tree *result = calloc(1, sizeof(*result));
cinnabar_init_notes(result, notes_ref, combine_notes_ignore, 0);
return result;
}

void destroy_notes_tree(struct cinnabar_notes_tree *t) {
cinnabar_free_notes(t);
free(t);
}
4 changes: 0 additions & 4 deletions src/cinnabar-notes.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,4 @@ int write_notes_tree(struct notes_tree *t, struct object_id *result,

void consolidate_notes(struct notes_tree *t);

struct notes_tree *new_notes_tree(const char *notes_ref);

void destroy_notes_tree(struct notes_tree *t);

#endif
42 changes: 38 additions & 4 deletions src/libcinnabar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,36 @@ pub struct cinnabar_notes_tree {
init_flags: c_int,
}

impl Drop for cinnabar_notes_tree {
fn drop(&mut self) {
unsafe {
cinnabar_free_notes(self);
}
}
}

impl cinnabar_notes_tree {
const fn new() -> Self {
pub const fn new() -> Self {
cinnabar_notes_tree {
current: notes_tree::new(),
additions: notes_tree::new(),
init_flags: 0,
}
}

pub fn new_with(c: CommitId) -> Self {
let mut result = Self::new();
let oid = CString::new(c.to_string()).unwrap();
unsafe {
cinnabar_init_notes(&mut result, oid.as_ptr(), combine_notes_ignore, 0);
}
result
}
}

pub static mut GIT2HG: git_notes_tree = git_notes_tree(cinnabar_notes_tree::new());
pub static mut HG2GIT: hg_notes_tree = hg_notes_tree(cinnabar_notes_tree::new());
pub static mut FILES_META: hg_notes_tree = hg_notes_tree(cinnabar_notes_tree::new());
pub static mut GIT2HG: git_notes_tree = git_notes_tree::new();
pub static mut HG2GIT: hg_notes_tree = hg_notes_tree::new();
pub static mut FILES_META: hg_notes_tree = hg_notes_tree::new();

extern "C" {
fn combine_notes_ignore(cur_oid: *mut object_id, new_oid: *const object_id) -> c_int;
Expand Down Expand Up @@ -303,6 +320,14 @@ pub unsafe fn store_metadata_notes(
pub struct git_notes_tree(cinnabar_notes_tree);

impl git_notes_tree {
pub const fn new() -> Self {
git_notes_tree(cinnabar_notes_tree::new())
}

pub fn new_with(c: CommitId) -> Self {
git_notes_tree(cinnabar_notes_tree::new_with(c))
}

pub fn get_note(&mut self, oid: GitObjectId) -> Option<GitObjectId> {
unsafe {
ensure_notes(&mut self.0);
Expand Down Expand Up @@ -349,6 +374,15 @@ impl git_notes_tree {
pub struct hg_notes_tree(cinnabar_notes_tree);

impl hg_notes_tree {
pub const fn new() -> Self {
hg_notes_tree(cinnabar_notes_tree::new())
}

#[allow(dead_code)]
pub fn new_with(c: CommitId) -> Self {
hg_notes_tree(cinnabar_notes_tree::new_with(c))
}

pub fn get_note(&mut self, oid: HgObjectId) -> Option<GitObjectId> {
unsafe {
ensure_notes(&mut self.0);
Expand Down
32 changes: 1 addition & 31 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ use std::fs::File;
use std::hash::Hash;
use std::io::{stderr, stdin, stdout, BufRead, BufWriter, IsTerminal, Write};
use std::iter::repeat;
use std::ops::{Deref, DerefMut};
use std::os::raw::{c_char, c_int, c_void};
#[cfg(windows)]
use std::os::windows::ffi::OsStrExt as WinOsStrExt;
Expand Down Expand Up @@ -854,34 +853,6 @@ extern "C" {
fn get_worktree_head_oid(wt: *const worktree) -> *const object_id;

fn get_worktree_ref_store(wr: *const worktree) -> *const libgit::ref_store;

fn new_notes_tree(notes_ref: *const c_char) -> *mut git_notes_tree;

fn destroy_notes_tree(t: *mut git_notes_tree);
}

struct NotesBox(*mut git_notes_tree);

impl Deref for NotesBox {
type Target = git_notes_tree;

fn deref(&self) -> &Self::Target {
unsafe { self.0.as_ref().unwrap() }
}
}

impl DerefMut for NotesBox {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.0.as_mut().unwrap() }
}
}

impl Drop for NotesBox {
fn drop(&mut self) {
unsafe {
destroy_notes_tree(self.0);
}
}
}

fn do_reclone(rebase: bool) -> Result<(), String> {
Expand Down Expand Up @@ -949,8 +920,7 @@ fn do_reclone(rebase: bool) -> Result<(), String> {
if git2hg_oid.is_null() {
None
} else {
let git2hg_oid = CString::new(git2hg_oid.to_string()).unwrap();
Some(NotesBox(unsafe { new_notes_tree(git2hg_oid.as_ptr()) }))
Some(git_notes_tree::new_with(git2hg_oid))
}
};

Expand Down

0 comments on commit 2839520

Please sign in to comment.