Skip to content

Commit

Permalink
Move store_file to Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Nov 12, 2023
1 parent 5bc786a commit b7d3580
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 223 deletions.
68 changes: 0 additions & 68 deletions src/cinnabar-fast-import.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,74 +285,6 @@ int write_object_file_flags(const void *buf, size_t len, enum object_type type,
return 0;
}

void hg_file_store(struct hg_file *file, struct hg_file *reference)
{
struct object_id oid;
struct strslice *last_blob = NULL;
struct object_entry *oe = NULL;

if (file->metadata.buf) {
store_git_object(OBJ_BLOB, file->metadata, &oid, NULL, NULL);
add_files_meta(&file->oid, &oid);
}

if (reference) {
last_blob = &reference->content;
oe = reference->content_oe;
}

store_git_object(OBJ_BLOB, file->content, &oid, last_blob, oe);
add_hg2git(&file->oid, &oid);

file->content_oe = get_object_entry(&oid);
}

void store_file(struct rev_chunk *chunk)
{
static struct hg_file last_file;
struct hg_file file;
struct strbuf data = STRBUF_INIT;
struct strslice diff;
struct rev_diff_part part;
size_t last_end = 0;

if (is_empty_hg_file(chunk->node))
return;

if (!hg_oideq(chunk->delta_node, &last_file.oid)) {
hg_file_release(&last_file);

if (!is_null_hg_oid(chunk->delta_node))
hg_file_load(&last_file, chunk->delta_node);

}

rev_diff_start_iter(&diff, chunk);
while (rev_diff_iter_next(&diff, &part)) {
if (part.start > last_file.file.len || part.start < last_end)
die("Malformed file chunk for %s",
hg_oid_to_hex(chunk->node));
strbuf_add(&data, last_file.file.buf + last_end,
part.start - last_end);
strbuf_addslice(&data, part.data);

last_end = part.end;
}

if (last_file.file.len < last_end)
die("Malformed file chunk for %s", hg_oid_to_hex(chunk->node));

strbuf_add(&data, last_file.file.buf + last_end,
last_file.file.len - last_end);

hg_file_init(&file);
hg_file_from_memory(&file, chunk->node, &data);

hg_file_store(&file, &last_file);
hg_file_swap(&file, &last_file);
hg_file_release(&file);
}

struct manifest_line {
struct strslice path;
struct hg_object_id oid;
Expand Down
1 change: 0 additions & 1 deletion src/cinnabar-fast-import.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ void do_set_replace(const struct object_id *replaced,
void do_set_(const char *what, const struct hg_object_id *hg_id,
const struct object_id *git_id);

void store_file(struct rev_chunk *chunk);
void store_manifest(struct rev_chunk *chunk,
const struct strslice last_manifest_content,
struct strslice_mut data);
Expand Down
97 changes: 0 additions & 97 deletions src/hg-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "git-compat-util.h"
#include "object-store.h"
#include "cinnabar-helper.h"
#include "cinnabar-notes.h"
#include "cinnabar-fast-import.h"
#include "hg-data.h"

static const struct hg_object_id empty_hg_file = {{
Expand All @@ -25,96 +21,3 @@ int is_empty_hg_file(const struct hg_object_id *oid)
{
return hg_oideq(&empty_hg_file, oid);
}

static void _hg_file_split(struct hg_file *result, size_t metadata_len)
{
result->metadata.buf = metadata_len ? result->file.buf + 2 : NULL;
result->metadata.len = metadata_len - 4;
result->content.buf = result->file.buf + metadata_len;
result->content.len = result->file.len - metadata_len;
}

void hg_file_load(struct hg_file *result, const struct hg_object_id *oid)
{
const struct object_id *note;
struct object_info oi = OBJECT_INFO_INIT;
char *content;
enum object_type type;
unsigned long len;
size_t metadata_len;

oi.typep = &type;
oi.sizep = &len;
oi.contentp = (void **) &content;

strbuf_release(&result->file);
hg_oidcpy(&result->oid, oid);

if (is_empty_hg_file(oid))
return;

note = get_files_meta(oid);
if (note) {
if (oid_object_info_extended(
the_repository, note, &oi,
OBJECT_INFO_DIE_IF_CORRUPT) != 0)
die("Missing data");
strbuf_add(&result->file, "\1\n", 2);
strbuf_add(&result->file, content, len);
strbuf_add(&result->file, "\1\n", 2);
free(content);
}

metadata_len = result->file.len;

note = resolve_hg2git(oid);
if (!note)
die("Missing data");

if (oid_object_info_extended(
the_repository, note, &oi,
OBJECT_INFO_DIE_IF_CORRUPT) != 0)
die("Missing data");

strbuf_add(&result->file, content, len);
free(content);

// Note this duplicates work read_object_file already did.
result->content_oe = get_object_entry(note);

_hg_file_split(result, metadata_len);
}

void hg_file_from_memory(struct hg_file *result,
const struct hg_object_id *oid, struct strbuf *buf)
{
size_t metadata_len = 0;

strbuf_swap(&result->file, buf);
hg_oidcpy(&result->oid, oid);
result->content_oe = NULL;

if (result->file.len > 4 && memcmp(result->file.buf, "\1\n", 2) == 0) {
char *metadata_end = strstr(result->file.buf + 2, "\1\n");
if (metadata_end)
metadata_len = metadata_end + 2 - result->file.buf;
}

_hg_file_split(result, metadata_len);
}

void hg_file_init(struct hg_file *file)
{
hg_oidclr(&file->oid);
strbuf_init(&file->file, 0);
file->metadata.buf = NULL;
file->metadata.len = 0;
file->content = strbuf_as_slice(&file->file);
file->content_oe = NULL;
}

void hg_file_release(struct hg_file *file)
{
strbuf_release(&file->file);
hg_file_init(file);
}
28 changes: 0 additions & 28 deletions src/hg-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,6 @@ int is_null_hg_oid(const struct hg_object_id *oid);

int is_empty_hg_file(const struct hg_object_id *oid);

struct hg_file {
struct hg_object_id oid;

struct strbuf file;
struct strslice metadata;
struct strslice content;
struct object_entry *content_oe;
};

void hg_file_load(struct hg_file *result, const struct hg_object_id *oid);

void hg_file_from_memory(struct hg_file *result,
const struct hg_object_id *oid, struct strbuf *buf);

static inline void hg_file_swap(struct hg_file *a, struct hg_file *b)
{
SWAP(*a, *b);
}

void hg_file_init(struct hg_file *file);

void hg_file_release(struct hg_file *file);

void hg_file_store(struct hg_file *file, struct hg_file *reference);

void add_hg2git(const struct hg_object_id *oid, const struct object_id *note_oid);
void add_files_meta(const struct hg_object_id *oid, const struct object_id *note_oid);

const struct object_id *get_files_meta(const struct hg_object_id *oid);

#endif
28 changes: 3 additions & 25 deletions src/libcinnabar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,42 +204,20 @@ fn for_each_note_in<F: FnMut(GitObjectId, GitObjectId)>(notes: &mut cinnabar_not

#[no_mangle]
pub unsafe extern "C" fn resolve_hg2git(oid: *const hg_object_id) -> *const object_id {
get_note_hg(METADATA.hg2git_mut(), oid)
}

unsafe fn get_note_hg(notes: &mut hg_notes_tree, oid: *const hg_object_id) -> *const object_id {
let git_oid =
GitObjectId::from_raw_bytes(HgObjectId::from(oid.as_ref().unwrap().clone()).as_raw_bytes())
.unwrap();
cinnabar_get_note(&mut notes.0, &git_oid.into())
cinnabar_get_note(&mut METADATA.hg2git_mut().0, &git_oid.into())
}

#[no_mangle]
pub unsafe extern "C" fn get_files_meta(oid: *const hg_object_id) -> *const object_id {
get_note_hg(METADATA.files_meta_mut(), oid)
}

unsafe fn add_note_hg(
notes: &mut hg_notes_tree,
oid: *const hg_object_id,
note_oid: *const object_id,
) {
notes.add_note(
pub unsafe extern "C" fn add_hg2git(oid: *const hg_object_id, note_oid: *const object_id) {
METADATA.hg2git_mut().add_note(
HgObjectId::from(oid.as_ref().unwrap().clone()),
note_oid.as_ref().unwrap().clone().into(),
);
}

#[no_mangle]
pub unsafe extern "C" fn add_hg2git(oid: *const hg_object_id, note_oid: *const object_id) {
add_note_hg(METADATA.hg2git_mut(), oid, note_oid);
}

#[no_mangle]
pub unsafe extern "C" fn add_files_meta(oid: *const hg_object_id, note_oid: *const object_id) {
add_note_hg(METADATA.files_meta_mut(), oid, note_oid);
}

pub fn store_metadata_notes(notes: &mut cinnabar_notes_tree, reference: CommitId) -> CommitId {
let mut result = object_id::default();
let mut tree = object_id::default();
Expand Down
5 changes: 5 additions & 0 deletions src/libgit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ pub struct remote(c_void);
#[repr(transparent)]
pub struct child_process(c_void);


#[allow(non_camel_case_types)]
#[repr(transparent)]
pub struct object_entry(c_void);

#[allow(non_camel_case_types)]
#[repr(C)]
pub struct active_request_slot {
Expand Down
Loading

0 comments on commit b7d3580

Please sign in to comment.