From c04de26b358e2ff1e9d2afe81ffa0fc841298a52 Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Fri, 10 Nov 2023 05:24:42 +0900 Subject: [PATCH] Move ensure_notes to Rust --- src/cinnabar-helper.c | 21 ------------------ src/cinnabar-helper.h | 2 -- src/cinnabar-notes.c | 5 +++++ src/cinnabar-notes.h | 5 +---- src/libcinnabar.rs | 50 +++++++++++++++++++++++++++++++++++++++---- src/store.rs | 4 +++- 6 files changed, 55 insertions(+), 32 deletions(-) diff --git a/src/cinnabar-helper.c b/src/cinnabar-helper.c index f1cb981e2..34080158a 100644 --- a/src/cinnabar-helper.c +++ b/src/cinnabar-helper.c @@ -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) { diff --git a/src/cinnabar-helper.h b/src/cinnabar-helper.h index 493ac4bb3..d7a7a1847 100644 --- a/src/cinnabar-helper.h +++ b/src/cinnabar-helper.h @@ -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; diff --git a/src/cinnabar-notes.c b/src/cinnabar-notes.c index e83703668..56df3251b 100644 --- a/src/cinnabar-notes.c +++ b/src/cinnabar-notes.c @@ -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; diff --git a/src/cinnabar-notes.h b/src/cinnabar-notes.h index 3c05b00b1..b3b8ca330 100644 --- a/src/cinnabar-notes.h +++ b/src/cinnabar-notes.h @@ -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); diff --git a/src/libcinnabar.rs b/src/libcinnabar.rs index 7aa4bfa69..5859dc5f6 100644 --- a/src/libcinnabar.rs +++ b/src/libcinnabar.rs @@ -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; @@ -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)] @@ -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, @@ -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, @@ -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(notes: &mut cinnabar_notes_tree, mut f: F) { unsafe extern "C" fn each_note_cb( oid: *const object_id, diff --git a/src/store.rs b/src/store.rs index 76a94bff9..bef185ee5 100644 --- a/src/store.rs +++ b/src/store.rs @@ -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 } }