Skip to content

Commit

Permalink
Avoid using the global METADATA for store_manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Nov 13, 2023
1 parent 5147f82 commit a2bef64
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 24 deletions.
26 changes: 17 additions & 9 deletions src/cinnabar-fast-import.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ static int split_manifest_line(struct strslice *slice,
return 0;
}

static int add_parent(struct strbuf *data,
static int add_parent(struct Metadata *metadata, struct strbuf *data,
const struct hg_object_id *last_manifest_oid,
const struct branch *last_manifest,
const struct hg_object_id *parent_oid)
Expand All @@ -330,7 +330,7 @@ static int add_parent(struct strbuf *data,
if (hg_oideq(parent_oid, last_manifest_oid))
note = &last_manifest->oid;
else {
note = resolve_hg2git(parent_oid);
note = resolve_hg2git(metadata, parent_oid);
}
if (!note)
return -1;
Expand All @@ -355,9 +355,17 @@ static void manifest_metadata_path(struct strbuf *out, struct strslice *in)
strbuf_addslice(out, *in);
}

extern void add_manifest_head(const struct object_id *manifest);
extern void add_hg2git(struct Metadata *metadata,
const struct hg_object_id *oid,
const struct object_id *note_oid);

void store_manifest(struct rev_chunk *chunk,
extern void add_manifest_head(struct Metadata *metadata,
const struct object_id *manifest);

extern int check_manifest(const struct object_id *oid);


void store_manifest(struct Metadata *metadata, struct rev_chunk *chunk,
const struct strslice last_manifest_content,
struct strslice_mut stored_manifest)
{
Expand Down Expand Up @@ -388,7 +396,7 @@ void store_manifest(struct rev_chunk *chunk,
assert(last_manifest_content.len == 0);
} else if (!hg_oideq(chunk->delta_node, &last_manifest_oid)) {
const struct object_id *note;
note = resolve_hg2git(chunk->delta_node);
note = resolve_hg2git(metadata, chunk->delta_node);
if (!note)
die("Cannot find delta node %s for %s",
hg_oid_to_hex(chunk->delta_node),
Expand Down Expand Up @@ -499,9 +507,9 @@ void store_manifest(struct rev_chunk *chunk,
strbuf_addf(&data, "tree %s\n",
oid_to_hex(&last_manifest->branch_tree.versions[1].oid));

if ((add_parent(&data, &last_manifest_oid, last_manifest,
if ((add_parent(metadata, &data, &last_manifest_oid, last_manifest,
chunk->parent1) == -1) ||
(add_parent(&data, &last_manifest_oid, last_manifest,
(add_parent(metadata, &data, &last_manifest_oid, last_manifest,
chunk->parent2) == -1))
goto malformed;

Expand All @@ -513,8 +521,8 @@ void store_manifest(struct rev_chunk *chunk,
store_git_object(OBJ_COMMIT, strbuf_as_slice(&data),
&last_manifest->oid, NULL, NULL);
strbuf_release(&data);
add_hg2git(&last_manifest_oid, &last_manifest->oid);
add_manifest_head(&last_manifest->oid);
add_hg2git(metadata, &last_manifest_oid, &last_manifest->oid);
add_manifest_head(metadata, &last_manifest->oid);
if ((cinnabar_check(CHECK_MANIFESTS)) &&
!check_manifest(&last_manifest->oid))
die("sha1 mismatch for node %s", hg_oid_to_hex(chunk->node));
Expand Down
3 changes: 2 additions & 1 deletion src/cinnabar-fast-import.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct hg_object_id;
struct cinnabar_notes_tree;
struct rev_chunk;
struct object_entry;
struct Metadata;

int maybe_handle_command(struct reader *helper_input, int helper_output,
const char *command, struct string_list *args);
Expand All @@ -38,7 +39,7 @@ void do_cleanup(int rollback);
void do_set_replace(const struct object_id *replaced,
const struct object_id *replace_with);

void store_manifest(struct rev_chunk *chunk,
void store_manifest(struct Metadata *metadata, struct rev_chunk *chunk,
const struct strslice last_manifest_content,
struct strslice_mut data);
void store_metadata_notes(
Expand Down
7 changes: 4 additions & 3 deletions src/cinnabar-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "hg-data.h"
#include "cinnabar-notes.h"

struct Metadata;

#define METADATA_REF "refs/cinnabar/metadata"

extern struct object_id metadata_oid, changesets_oid, manifests_oid, git2hg_oid,
Expand All @@ -20,8 +22,6 @@ extern int cinnabar_check(int);

extern struct notes_tree git2hg, hg2git, files_meta;

int check_manifest(const struct object_id *oid);

struct remote;

const char *remote_get_name(const struct remote *remote);
Expand All @@ -40,7 +40,8 @@ unsigned int replace_map_tablesize(void);

const struct object_id *repo_lookup_replace_object(
struct repository *r, const struct object_id *oid);
const struct object_id *resolve_hg2git(const struct hg_object_id *oid);
const struct object_id *resolve_hg2git(struct Metadata *metadata,
const struct hg_object_id *oid);

struct commit;

Expand Down
2 changes: 0 additions & 2 deletions src/hg-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,4 @@ int is_null_hg_oid(const struct hg_object_id *oid);

int is_empty_hg_file(const struct hg_object_id *oid);

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

#endif
17 changes: 12 additions & 5 deletions src/libcinnabar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::libgit::{
FileMode, RawTree,
};
use crate::oid::{Abbrev, ObjectId};
use crate::store::{store_git_commit, METADATA};
use crate::store::{store_git_commit, Metadata, METADATA};

#[allow(non_camel_case_types)]
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -203,16 +203,23 @@ 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 {
pub unsafe extern "C" fn resolve_hg2git(
metadata: &mut Metadata,
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 METADATA.hg2git_mut().0, &git_oid.into())
cinnabar_get_note(&mut metadata.hg2git_mut().0, &git_oid.into())
}

#[no_mangle]
pub unsafe extern "C" fn add_hg2git(oid: *const hg_object_id, note_oid: *const object_id) {
METADATA.hg2git_mut().add_note(
pub unsafe extern "C" fn add_hg2git(
metadata: &mut Metadata,
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(),
);
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1949,7 +1949,12 @@ fn create_manifest(content: &mut [u8], parents: &[HgManifestId]) -> HgManifestId
manifest_chunk.extend_from_slice(b"\0\0\0\0");
for chunk in RevChunkIter::new(2, manifest_chunk.as_bytes()) {
unsafe {
store_manifest(&chunk.into(), (&parent_manifest).into(), content.into());
store_manifest(
&mut METADATA,
&chunk.into(),
(&parent_manifest).into(),
content.into(),
);
}
}
mid
Expand Down
12 changes: 9 additions & 3 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,8 +1190,8 @@ fn store_manifests_metadata(metadata: &Metadata) -> CommitId {
}

#[no_mangle]
pub unsafe extern "C" fn add_manifest_head(mn: *const object_id) {
let heads = METADATA.manifest_heads_mut();
pub unsafe extern "C" fn add_manifest_head(metadata: &mut Metadata, mn: *const object_id) {
let heads = metadata.manifest_heads_mut();
heads.add(GitManifestId::from_unchecked(CommitId::from_unchecked(
mn.as_ref().unwrap().clone().into(),
)));
Expand Down Expand Up @@ -1695,7 +1695,12 @@ pub fn create_changeset(
// The rev_chunk has a non-FFI-safe field that is not exposed to C.
#[allow(improper_ctypes)]
extern "C" {
pub fn store_manifest(chunk: *const rev_chunk, reference_mn: strslice, stored_mn: strslice_mut);
pub fn store_manifest(
metadata: &mut Metadata,
chunk: *const rev_chunk,
reference_mn: strslice,
stored_mn: strslice_mut,
);
}

#[no_mangle]
Expand Down Expand Up @@ -1830,6 +1835,7 @@ pub fn store_changegroup<R: Read>(metadata: &mut Metadata, input: R, version: u8
let mut stored_manifest = Rc::builder_with_capacity(mn_size);
unsafe {
store_manifest(
metadata,
&manifest.into(),
(&reference_mn).into(),
(&mut stored_manifest.spare_capacity_mut()[..mn_size]).into(),
Expand Down

0 comments on commit a2bef64

Please sign in to comment.