From 94555ba29a15682188a3e94aa4999daa50c6bdee Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Thu, 9 Nov 2023 18:12:14 +0900 Subject: [PATCH] Move store_notes to Rust --- src/cinnabar-fast-import.c | 10 +--------- src/cinnabar-notes.c | 5 +++++ src/cinnabar-notes.h | 5 +---- src/libcinnabar.rs | 22 ++++++++++++++++++++-- src/libgit.rs | 6 ++++++ 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/cinnabar-fast-import.c b/src/cinnabar-fast-import.c index c044cc9f7..904d2c831 100644 --- a/src/cinnabar-fast-import.c +++ b/src/cinnabar-fast-import.c @@ -294,15 +294,7 @@ int write_object_file_flags(const void *buf, size_t len, enum object_type type, return 0; } -static void store_notes(struct notes_tree *notes, struct object_id *result) -{ - oidclr(result); - if (notes_dirty(notes)) { - unsigned int mode = (notes == &hg2git) ? S_IFGITLINK - : S_IFREG | 0644; - write_notes_tree(notes, result, mode); - } -} +extern void store_notes(struct notes_tree *notes, struct object_id *result); void hg_file_store(struct hg_file *file, struct hg_file *reference) { diff --git a/src/cinnabar-notes.c b/src/cinnabar-notes.c index 0e7cdd137..e83703668 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_dirty(struct cinnabar_notes_tree *notes) +{ + return notes->current.dirty || notes->additions.dirty; +} + int cinnabar_add_note( struct cinnabar_notes_tree *t, const struct object_id *object_oid, const struct object_id *note_oid) diff --git a/src/cinnabar-notes.h b/src/cinnabar-notes.h index 1223bf6de..3c05b00b1 100644 --- a/src/cinnabar-notes.h +++ b/src/cinnabar-notes.h @@ -32,10 +32,7 @@ static inline int notes_initialized(struct notes_tree *notes) return notes->current.initialized; } -static inline int notes_dirty(struct notes_tree *notes) -{ - return notes->current.dirty || notes->additions.dirty; -} +int notes_dirty(struct notes_tree *notes); extern const struct object_id *get_abbrev_note( struct notes_tree *t, const struct object_id *object_oid, size_t len); diff --git a/src/libcinnabar.rs b/src/libcinnabar.rs index e9f47be85..0fa50e9b0 100644 --- a/src/libcinnabar.rs +++ b/src/libcinnabar.rs @@ -4,11 +4,12 @@ use std::marker::PhantomData; use std::mem::MaybeUninit; -use std::os::raw::{c_char, c_int, c_void}; +use std::os::raw::{c_char, c_int, c_uint, c_void}; +use std::ptr; use crate::git::GitObjectId; use crate::hg::HgObjectId; -use crate::libgit::{child_process, object_id}; +use crate::libgit::{child_process, object_id, FileMode}; use crate::oid::{Abbrev, ObjectId}; #[allow(non_camel_case_types)] @@ -132,6 +133,10 @@ extern "C" { ) -> c_int; fn cinnabar_remove_note(notes: *mut cinnabar_notes_tree, object_sha1: *const u8); + + fn notes_dirty(noted: *const cinnabar_notes_tree) -> c_int; + + fn cinnabar_write_notes_tree(notes: *mut cinnabar_notes_tree, result: *mut object_id, mode: c_uint) -> c_int; } fn for_each_note_in(notes: &mut cinnabar_notes_tree, mut f: F) { @@ -195,6 +200,19 @@ pub unsafe extern "C" fn add_files_meta( add_note_hg(&mut files_meta.0, oid, note_oid) } +#[no_mangle] +pub unsafe extern "C" fn store_notes(notes: *mut cinnabar_notes_tree, result: *mut object_id) { + *result = object_id::default(); + if notes_dirty(notes) != 0 { + let mode = if ptr::eq(notes, &hg2git.0) { + FileMode::GITLINK + } else { + FileMode::REGULAR | FileMode::RW + }; + cinnabar_write_notes_tree(notes, result, u16::from(mode).into()); + } +} + #[allow(non_camel_case_types)] #[repr(transparent)] pub struct git_notes_tree(cinnabar_notes_tree); diff --git a/src/libgit.rs b/src/libgit.rs index 5cd5cd2e9..e73eaa2b3 100644 --- a/src/libgit.rs +++ b/src/libgit.rs @@ -675,6 +675,12 @@ impl fmt::Debug for FileMode { } } +impl From for u16 { + fn from(value: FileMode) -> Self { + value.0 + } +} + impl FromBytes for FileMode { type Err = ParseIntError;