Skip to content

Commit

Permalink
Move store_notes to Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Nov 9, 2023
1 parent 3b08214 commit 94555ba
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
10 changes: 1 addition & 9 deletions src/cinnabar-fast-import.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
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_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)
Expand Down
5 changes: 1 addition & 4 deletions src/cinnabar-notes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 20 additions & 2 deletions src/libcinnabar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<F: FnMut(GitObjectId, GitObjectId)>(notes: &mut cinnabar_notes_tree, mut f: F) {
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/libgit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,12 @@ impl fmt::Debug for FileMode {
}
}

impl From<FileMode> for u16 {
fn from(value: FileMode) -> Self {
value.0
}
}

impl FromBytes for FileMode {
type Err = ParseIntError;

Expand Down

0 comments on commit 94555ba

Please sign in to comment.