Skip to content

Commit 48ce1dc

Browse files
Merge pull request #78 from omnibor/abrinker/improvements
Miscellaneous improvements
2 parents 38a7c81 + 16f0028 commit 48ce1dc

File tree

10 files changed

+44
-181
lines changed

10 files changed

+44
-181
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[workspace]
22

33
members = ["omnibor", "gitoid"]
4+
resolver = "2"
45

gitoid/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ crate-type = ["rlib", "cdylib"]
1515

1616
[dependencies]
1717
hex = "0.4.3"
18-
sha1 = "0.10.4"
19-
sha2 = "0.10.5"
20-
url = "2.2.2"
18+
sha1 = "0.10.6"
19+
sha2 = "0.10.8"
20+
url = "2.4.1"

gitoid/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# It is intended to be easy for newcomers to the project to understand.
55

66
# Variables for cbindgen.
7-
CBINDGEN_CONFIG="../cbindgen.toml"
7+
CBINDGEN_CONFIG="./cbindgen.toml"
88

99
# Variables for the header file.
1010
INCLUDE_DIR="./include"

cbindgen.toml renamed to gitoid/cbindgen.toml

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
language = "C"
33

44
# Wrap generated file in an include guard.
5-
include_guard = "gitbom_h"
5+
include_guard = "gitoid_h"
66

77
# Note the version of cbindgen used to generate the header.
88
include_version = true
@@ -20,7 +20,7 @@ style = "both"
2020
header = """
2121
/**
2222
* @file
23-
* @brief "GitBom"
23+
* @brief "GitOID"
2424
*/
2525
"""
2626

@@ -40,23 +40,3 @@ autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't mod
4040

4141
# Prefix enum variants with the name of the overall type.
4242
prefix_with_name = true
43-
44-
45-
#==============================================================================
46-
# Parsing Rules
47-
#------------------------------------------------------------------------------
48-
49-
[parse]
50-
51-
# Parse dependencies.
52-
#parse_deps = true
53-
#include = []
54-
55-
#==============================================================================
56-
# Macro Expansion Rules
57-
#------------------------------------------------------------------------------
58-
59-
#[parse.expand]
60-
61-
# Expand macros for `gitoid`.
62-
#crates = ["gitoid"]

gitoid/src/ffi/gitoid.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ use std::ffi::CStr;
1515
use std::ffi::CString;
1616
use std::fs::File;
1717
use std::io::BufReader;
18+
#[cfg(target_family = "unix")]
1819
use std::os::unix::prelude::FromRawFd;
20+
#[cfg(target_family = "unix")]
1921
use std::os::unix::prelude::RawFd;
22+
#[cfg(target_family = "windows")]
23+
use std::os::windows::io::FromRawHandle;
24+
#[cfg(target_family = "windows")]
25+
use std::os::windows::prelude::RawHandle;
2026
use std::ptr::null;
2127
use std::ptr::null_mut;
2228
use std::slice;
@@ -64,6 +70,8 @@ pub extern "C" fn gitoid_invalid(gitoid: *const GitOid) -> c_int {
6470

6571
/// Construct a new `GitOid` from a buffer of bytes.
6672
///
73+
/// `content_len` is the number of elements, not the number of bytes.
74+
///
6775
/// `content_len` times 8 (byte size) must be less than or equal to the
6876
/// maximum size representable with an unsigned integer at the size used by
6977
/// the ISA (32-bit or 64-bit usually).
@@ -74,7 +82,7 @@ pub extern "C" fn gitoid_invalid(gitoid: *const GitOid) -> c_int {
7482
pub extern "C" fn gitoid_new_from_bytes(
7583
hash_algorithm: HashAlgorithm,
7684
object_type: ObjectType,
77-
content: *const u8,
85+
content: *mut u8,
7886
content_len: usize,
7987
) -> GitOid {
8088
let output = catch_panic(|| {
@@ -277,5 +285,5 @@ pub extern "C" fn gitoid_str_free(s: *mut c_char) {
277285
return;
278286
}
279287

280-
unsafe { CString::from_raw(s) };
288+
let _ = unsafe { CString::from_raw(s) };
281289
}

gitoid/src/gitoid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl GitOid {
5555
///
5656
/// This construction should _only_ be used for error-handling purposes
5757
/// when using the `gitoid` crate over FFI.
58-
pub fn new_invalid() -> Self {
58+
pub(crate) fn new_invalid() -> Self {
5959
GitOid {
6060
hash_algorithm: HashAlgorithm::Sha1,
6161
object_type: ObjectType::Blob,

gitoid/test/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
c_test.exe
2+
c_test

gitoid/test/c/test.c

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,76 +5,70 @@
55
#include "gitoid.h"
66

77
#define LEN(arr) (sizeof(arr) / sizeof(arr[0]));
8+
#define TEST(NAME) {.name = #NAME, .fn = NAME}
89

910
void test_gitoid_new_from_str() {
1011
GitOid gitoid = gitoid_new_from_str(HashAlgorithm_Sha1, ObjectType_Blob, "hello world");
12+
assert(!gitoid_invalid(&gitoid));
1113
assert(gitoid.len == 20);
1214
assert(gitoid.value[0] == 149);
1315
}
1416

1517
void test_gitoid_new_from_bytes() {
16-
// Section that creates the byte array was heavily inspired by [1].
17-
//
18-
// Does not do error checking, and is intended solely for test purposes.
19-
// The length of `byte_array` is equal to the length of `string` plus one,
20-
// to make space for the nul-terminator.
21-
//
22-
// [1]: https://stackoverflow.com/a/3409211/2308264
23-
const char *string = "hello_world";
24-
const char *position = string;
25-
unsigned char byte_array[12];
26-
size_t size = LEN(byte_array);
27-
for (size_t count = 0; count < size; ++count) {
28-
sscanf(position, "%2hhx", &byte_array[count]);
29-
position += 2;
30-
}
31-
uint8_t byte_array_length = sizeof byte_array;
18+
unsigned char bytes[] = {0x00, 0x01, 0x02, 0x03,
19+
0x04, 0x05, 0x06, 0x07,
20+
0x08, 0x09, 0x0A, 0x0B,
21+
0x0C, 0x0D, 0x0E, 0x0F};
22+
uint8_t bytes_len = LEN(bytes);
3223

3324
GitOid gitoid = gitoid_new_from_bytes(
3425
HashAlgorithm_Sha1,
3526
ObjectType_Blob,
36-
&byte_array_length,
37-
*byte_array
27+
bytes,
28+
bytes_len
3829
);
3930

31+
assert(!gitoid_invalid(&gitoid));
4032
assert(gitoid.len == 20);
41-
assert(gitoid.value[0] == 130);
33+
assert(gitoid.value[0] == 182);
4234
}
4335

4436
void test_gitoid_new_from_url() {
4537
char *url = "gitoid:blob:sha256:fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03";
4638
GitOid gitoid = gitoid_new_from_url(url);
39+
assert(!gitoid_invalid(&gitoid));
4740
assert(gitoid.len == 32);
4841
assert(gitoid.value[0] == 254);
4942
}
5043

5144
void test_gitoid_get_url() {
5245
char *url_in = "gitoid:blob:sha256:fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03";
5346
GitOid gitoid = gitoid_new_from_url(url_in);
47+
assert(!gitoid_invalid(&gitoid));
5448
char *url_out = gitoid_get_url(&gitoid);
5549
assert(strncmp(url_in, url_out, 83) == 0);
5650
gitoid_str_free(url_out);
5751
}
5852

5953
void test_gitoid_hash_algorithm_name() {
6054
GitOid gitoid = gitoid_new_from_str(HashAlgorithm_Sha1, ObjectType_Blob, "hello world");
55+
assert(!gitoid_invalid(&gitoid));
6156
const char *hash_algorithm = gitoid_hash_algorithm_name(gitoid.hash_algorithm);
6257
assert(strncmp(hash_algorithm, "sha1", 4) == 0);
6358
}
6459

6560
void test_gitoid_object_type_name() {
6661
GitOid gitoid = gitoid_new_from_str(HashAlgorithm_Sha1, ObjectType_Blob, "hello world");
62+
assert(!gitoid_invalid(&gitoid));
6763
const char *object_type = gitoid_object_type_name(gitoid.object_type);
6864
assert(strncmp(object_type, "blob", 4) == 0);
6965
}
7066

7167
void test_gitoid_validity() {
72-
// Notice the SHA type is invalid.
7368
char *validity_url = "gitoid:blob:sha000:fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03";
7469
GitOid gitoid = gitoid_new_from_url(validity_url);
7570
assert(gitoid_invalid(&gitoid));
7671

77-
// Also test the error message reporting.
7872
char *expected_msg = "string is not a valid GitOID URL";
7973
char error_msg[256];
8074
gitoid_get_error_message(error_msg, 256);
@@ -91,14 +85,14 @@ typedef struct test {
9185
int main() {
9286
setvbuf(stdout, NULL, _IONBF, 0);
9387

94-
test_t tests[7] = {
95-
{.name = "gitoid_new_from_str", .fn = test_gitoid_new_from_str},
96-
{.name = "gitoid_new_from_bytes", .fn = test_gitoid_new_from_bytes},
97-
{.name = "gitoid_new_from_url", .fn = test_gitoid_new_from_url},
98-
{.name = "gitoid_get_url", .fn = test_gitoid_get_url},
99-
{.name = "gitoid_hash_algorithm_name", .fn = test_gitoid_hash_algorithm_name},
100-
{.name = "gitoid_object_type_name", .fn = test_gitoid_object_type_name},
101-
{.name = "gitoid_validity", .fn = test_gitoid_validity},
88+
test_t tests[] = {
89+
TEST(test_gitoid_new_from_str),
90+
TEST(test_gitoid_new_from_bytes),
91+
TEST(test_gitoid_new_from_url),
92+
TEST(test_gitoid_get_url),
93+
TEST(test_gitoid_hash_algorithm_name),
94+
TEST(test_gitoid_object_type_name),
95+
TEST(test_gitoid_validity),
10296
};
10397

10498
size_t n_tests = LEN(tests);

omnibor/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,3 @@ version = "0.1.7"
1212

1313
[dependencies]
1414
gitoid = "0.1.5"
15-
hex = "0.4.3"
16-
im = "15.1.0"
17-
sha1 = "0.10.4"
18-
sha2 = "0.10.5"

omnibor/src/lib.rs

Lines changed: 1 addition & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,2 @@
1-
use gitoid::GitOid;
2-
use im::{HashSet, Vector};
31

4-
/// A [persistent][wiki] collection of [git oids][git_scm].
5-
///
6-
/// Why persistent? While Rust and the borrow checker is great about ownership and
7-
/// mutation, always knowing that a Ref will not change if passed as a parameter
8-
/// to a function eliminates a class of errors.
9-
///
10-
/// [wiki]: https://en.wikipedia.org/wiki/Persistent_data_structure
11-
/// [git_scm]: https://git-scm.com/book/en/v2/Git-Internals-Git-Objects
12-
#[derive(Clone, PartialOrd, Eq, Ord, Debug, Hash, PartialEq)]
13-
pub struct OmniBor {
14-
git_oids: HashSet<GitOid>,
15-
}
16-
17-
impl FromIterator<GitOid> for OmniBor {
18-
/// Create an OmniBor from many GitOids
19-
fn from_iter<T>(gitoids: T) -> Self
20-
where
21-
T: IntoIterator<Item = GitOid>,
22-
{
23-
let me = OmniBor::new();
24-
me.add_many(gitoids)
25-
}
26-
}
27-
28-
impl OmniBor {
29-
/// Create a new instance
30-
#[allow(clippy::new_without_default)]
31-
pub fn new() -> Self {
32-
Self {
33-
git_oids: HashSet::new(),
34-
}
35-
}
36-
37-
/// Create a OmniBor from many GitOids
38-
pub fn new_from_iterator<I>(gitoids: I) -> Self
39-
where
40-
I: IntoIterator<Item = GitOid>,
41-
{
42-
let me = OmniBor::new();
43-
me.add_many(gitoids)
44-
}
45-
46-
/// Add a `GitOid` hash to the `OmniBor`.
47-
///
48-
/// Note that this creates a new persistent data structure under the hood.
49-
pub fn add(&self, gitoid: GitOid) -> Self {
50-
self.add_many([gitoid])
51-
}
52-
53-
/// Append many `GitOid`s and return a new `OmniBor`
54-
pub fn add_many<I>(&self, gitoids: I) -> Self
55-
where
56-
I: IntoIterator<Item = GitOid>,
57-
{
58-
let mut updated = self.git_oids.clone(); // im::HashSet has O(1) cloning
59-
for gitoid in gitoids {
60-
updated = updated.update(gitoid);
61-
}
62-
Self { git_oids: updated }
63-
}
64-
65-
/// Return the `Vector` of `GitOid`s.
66-
pub fn get_oids(&self) -> HashSet<GitOid> {
67-
self.git_oids.clone() // im::HashSet as O(1) cloning.
68-
}
69-
70-
/// Get a sorted `Vector` of `GitOid`s.
71-
///
72-
/// In some cases, getting a sorted `Vector` of oids is desirable.
73-
/// This function (cost O(n log n)) returns a `Vector` of sorted oids
74-
pub fn get_sorted_oids(&self) -> Vector<GitOid> {
75-
let mut ret = self.git_oids.clone().into_iter().collect::<Vector<_>>();
76-
ret.sort();
77-
ret
78-
}
79-
}
80-
81-
#[cfg(test)]
82-
mod tests {
83-
use gitoid::{GitOid, HashAlgorithm, ObjectType::Blob};
84-
use im::vector;
85-
86-
use super::*;
87-
88-
#[test]
89-
fn test_add() {
90-
let oid = GitOid::new_from_str(HashAlgorithm::Sha256, Blob, "Hello");
91-
assert_eq!(OmniBor::new().add(oid).get_sorted_oids(), vector![oid])
92-
}
93-
94-
#[test]
95-
fn test_add_many() {
96-
let mut oids: Vector<GitOid> = vec!["eee", "Hello", "Cat", "Dog"]
97-
.into_iter()
98-
.map(|s| GitOid::new_from_str(HashAlgorithm::Sha256, Blob, s))
99-
.collect();
100-
101-
let da_bom = OmniBor::new().add_many(oids.clone());
102-
oids.sort();
103-
assert_eq!(da_bom.get_sorted_oids(), oids);
104-
}
105-
106-
#[test]
107-
fn test_add_gitoid_to_gitbom() {
108-
let input = "hello world".as_bytes();
109-
110-
let generated_gitoid = GitOid::new_from_bytes(HashAlgorithm::Sha256, Blob, input);
111-
112-
let new_gitbom = OmniBor::new();
113-
let new_gitbom = new_gitbom.add(generated_gitoid);
114-
115-
assert_eq!(
116-
"fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03",
117-
new_gitbom.get_sorted_oids()[0].hash().as_hex()
118-
)
119-
}
120-
}
2+
// TODO(abrinker): Complete first draft of the rewrite of this crate.

0 commit comments

Comments
 (0)