Skip to content

Commit

Permalink
Move do_set_("changeset-metadata") to Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Nov 6, 2023
1 parent dcc6592 commit c7bc5ff
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
22 changes: 0 additions & 22 deletions src/cinnabar-fast-import.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,35 +297,13 @@ void do_set_(const char *what, const struct hg_object_id *hg_id,
type = OBJ_COMMIT;
if (what[0] != 'm')
is_changeset = 1;
} else if (!strcmp(what, "changeset-metadata")) {
type = OBJ_BLOB;
notes = &git2hg;
} else if (!strcmp(what, "file-meta")) {
type = OBJ_BLOB;
notes = &files_meta;
} else {
die("Unknown kind of object: %s", what);
}

if (notes == &git2hg) {
const struct object_id *note;
ensure_notes(&hg2git);
note = get_note_hg(&hg2git, hg_id);
if (note) {
ensure_notes(&git2hg);
if (is_null_oid(git_id)) {
remove_note(notes, note->hash);
} else if (oid_object_info(the_repository, git_id,
NULL) != OBJ_BLOB) {
die("Invalid object");
} else {
add_note(notes, note, git_id);
}
} else if (!is_null_oid(git_id))
die("Invalid sha1");
return;
}

ensure_notes(notes);
if (is_null_oid(git_id)) {
remove_note_hg(notes, hg_id);
Expand Down
22 changes: 22 additions & 0 deletions src/libcinnabar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ extern "C" {
) -> c_int,
cb_data: *mut c_void,
) -> c_int;

fn cinnabar_add_note(
notes: *mut cinnabar_notes_tree,
object_oid: *const object_id,
note_oid: *const object_id,
);

fn cinnabar_remove_note(notes: *mut cinnabar_notes_tree, object_sha1: *const u8);
}

fn for_each_note_in<F: FnMut(GitObjectId, GitObjectId)>(notes: &mut cinnabar_notes_tree, mut f: F) {
Expand Down Expand Up @@ -165,6 +173,20 @@ impl git_notes_tree {
pub fn for_each<F: FnMut(GitObjectId, GitObjectId)>(&mut self, f: F) {
for_each_note_in(&mut self.0, f);
}

pub fn add_note(&mut self, oid: GitObjectId, note_oid: GitObjectId) {
unsafe {
ensure_notes(&mut self.0);
cinnabar_add_note(&mut self.0, &oid.into(), &note_oid.into());
}
}

pub fn remove_note(&mut self, oid: GitObjectId) {
unsafe {
ensure_notes(&mut self.0);
cinnabar_remove_note(&mut self.0, oid.as_raw_bytes().as_ptr());
}
}
}

#[allow(non_camel_case_types)]
Expand Down
20 changes: 19 additions & 1 deletion src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,25 @@ pub enum SetWhat {
pub fn do_set(what: SetWhat, hg_id: HgObjectId, git_id: GitObjectId) {
let what = match what {
SetWhat::Changeset => cstr!("changeset"),
SetWhat::ChangesetMeta => cstr!("changeset-metadata"),
SetWhat::ChangesetMeta => {
let csid = HgChangesetId::from_unchecked(hg_id);
if let Some(cid) = csid.to_git() {
if git_id.is_null() {
unsafe {
git2hg.remove_note(cid.into());
}
} else if BlobId::try_from(git_id).is_err() {
die!("Invalid object");
} else {
unsafe {
git2hg.add_note(cid.into(), git_id);
}
}
} else if !git_id.is_null() {
die!("Invalid sha1");
}
return;
}
SetWhat::Manifest => {
if !git_id.is_null() {
MANIFEST_HEADS
Expand Down

0 comments on commit c7bc5ff

Please sign in to comment.