Skip to content

Commit

Permalink
Move store_metadata_notes to Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Nov 9, 2023
1 parent 94555ba commit 92d7475
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
31 changes: 2 additions & 29 deletions src/cinnabar-fast-import.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,6 @@ int write_object_file_flags(const void *buf, size_t len, enum object_type type,
return 0;
}

extern void store_notes(struct notes_tree *notes, struct object_id *result);

void hg_file_store(struct hg_file *file, struct hg_file *reference)
{
struct object_id oid;
Expand Down Expand Up @@ -611,34 +609,9 @@ void store_manifest(struct rev_chunk *chunk,
extern void store_changesets_metadata(struct object_id *result);
extern void store_manifests_metadata(struct object_id *result);

void store_metadata_notes(
extern void store_metadata_notes(
struct notes_tree *notes, const struct object_id *reference,
struct object_id *result)
{
struct object_id tree;
oidcpy(result, null_oid());
store_notes(notes, &tree);

if (is_null_oid(&tree)) {
oidcpy(result, reference);
if (is_null_oid(result)) {
oidcpy(&tree, &empty_tree);
}
}
if (!is_null_oid(&tree)) {
struct strbuf buf = STRBUF_INIT;
strbuf_addf(
&buf, "tree %s\n",
oid_to_hex(&tree));
strbuf_addstr(
&buf,
"author <cinnabar@git> 0 +0000\n"
"committer <cinnabar@git> 0 +0000\n"
"\n");
store_git_commit(&buf, result);
strbuf_release(&buf);
}
}
struct object_id *result);

extern int config(const char *name, struct strbuf *result);

Expand Down
37 changes: 35 additions & 2 deletions src/libcinnabar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
* 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::io::Write;
use std::marker::PhantomData;
use std::mem::MaybeUninit;
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, FileMode};
use crate::libgit::{child_process, object_id, strbuf, FileMode, RawTree};
use crate::oid::{Abbrev, ObjectId};
use crate::store::store_git_commit;

#[allow(non_camel_case_types)]
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -136,7 +138,11 @@ extern "C" {

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 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 @@ -213,6 +219,33 @@ pub unsafe extern "C" fn store_notes(notes: *mut cinnabar_notes_tree, result: *m
}
}

#[no_mangle]
pub unsafe extern "C" fn store_metadata_notes(
notes: *mut cinnabar_notes_tree,
reference: *const object_id,
result: *mut object_id,
) {
*result = object_id::default();
let mut tree = object_id::default();
store_notes(notes, &mut tree);

if GitObjectId::from(tree.clone()).is_null() {
*result = reference.as_ref().unwrap().clone();
if GitObjectId::from(result.as_ref().unwrap().clone()).is_null() {
tree = RawTree::EMPTY_OID.into();
}
}
let tree = GitObjectId::from(tree);
if !tree.is_null() {
let mut buf = strbuf::new();
writeln!(buf, "tree {}", tree).ok();
buf.extend_from_slice(
b"author <cinnabar@git> 0 +0000\ncommitter <cinnabar@git> 0 +0000\n\n",
);
store_git_commit(&buf, result);
}
}

#[allow(non_camel_case_types)]
#[repr(transparent)]
pub struct git_notes_tree(cinnabar_notes_tree);
Expand Down

0 comments on commit 92d7475

Please sign in to comment.