Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a78ae2

Browse files
authored
Merge pull request GitoxideLabs#2448 from cruessler/add-sha-256-to-gix-refspec
Run `gix-refspec` tests with both `sha1` and `sha256`
2 parents 470141c + 3cf53e2 commit 9a78ae2

9 files changed

Lines changed: 93 additions & 28 deletions

File tree

gix-refspec/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ smallvec = "1.15.1"
3131

3232
[dev-dependencies]
3333
gix-testtools = { path = "../tests/tools" }
34+
gix-hash = { path = "../gix-hash", features = ["sha1", "sha256"] }
3435
insta = "1.46.3"
3536

3637
[package.metadata.docs.rs]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/match_baseline.tar
2+
/match_baseline_sha256.tar
Binary file not shown.

gix-refspec/tests/fixtures/match_baseline.sh

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,24 @@ git clone --shared ./base clone
4343
baseline "main"
4444
baseline "v0.0-f1"
4545
baseline "tags/v0.0-f2"
46-
baseline "78b1c1be9421b33a49a7a8176d93eeeafa112da1"
47-
baseline "9d2fab1a0ba3585d0bc50922bfdd04ebb59361df"
48-
baseline "78b1c1be9421b33a49a7a8176d93eeeafa112da1:special"
49-
baseline "78b1c1be9421b33a49a7a8176d93eeeafa112da1:1111111111111111111111111111111111111111"
50-
baseline "9d2fab1a0ba3585d0bc50922bfdd04ebb59361df:tags/special"
51-
baseline "9d2fab1a0ba3585d0bc50922bfdd04ebb59361df:refs/tags/special"
46+
case ${GIX_TEST_FIXTURE_HASH:-sha1} in
47+
sha1)
48+
baseline "78b1c1be9421b33a49a7a8176d93eeeafa112da1"
49+
baseline "9d2fab1a0ba3585d0bc50922bfdd04ebb59361df"
50+
baseline "78b1c1be9421b33a49a7a8176d93eeeafa112da1:special"
51+
baseline "78b1c1be9421b33a49a7a8176d93eeeafa112da1:1111111111111111111111111111111111111111"
52+
baseline "9d2fab1a0ba3585d0bc50922bfdd04ebb59361df:tags/special"
53+
baseline "9d2fab1a0ba3585d0bc50922bfdd04ebb59361df:refs/tags/special" ;;
54+
sha256)
55+
baseline "b071221ea854da2958fba3a37527ca5cf32c4ebcd71ab0b68b6b8f10f04e93ad"
56+
baseline "ac050883b75422e0d03bfee760c591b292cbc10cee8ad934480ea5fb2ebc44fe"
57+
baseline "b071221ea854da2958fba3a37527ca5cf32c4ebcd71ab0b68b6b8f10f04e93ad:special"
58+
baseline "b071221ea854da2958fba3a37527ca5cf32c4ebcd71ab0b68b6b8f10f04e93ad:1111111111111111111111111111111111111111"
59+
baseline "ac050883b75422e0d03bfee760c591b292cbc10cee8ad934480ea5fb2ebc44fe:tags/special"
60+
baseline "ac050883b75422e0d03bfee760c591b292cbc10cee8ad934480ea5fb2ebc44fe:refs/tags/special" ;;
61+
*)
62+
exit 1 ;;
63+
esac
5264
baseline "f1:origin/f1"
5365
baseline "f1:remotes/origin/f1"
5466
baseline "f1:notes/f1"
@@ -87,4 +99,3 @@ git clone --shared ./base clone
8799
baseline '@:f1'
88100
baseline '@:HEAD'
89101
)
90-

gix-refspec/tests/refspec/match_group.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,53 @@
11
mod single {
22
use crate::matching::baseline;
33

4+
fn test_hashes() -> (String, String) {
5+
let annotated_tag = match gix_testtools::hash_kind_from_env().unwrap_or_default() {
6+
gix_hash::Kind::Sha1 => "78b1c1be9421b33a49a7a8176d93eeeafa112da1",
7+
gix_hash::Kind::Sha256 => "b071221ea854da2958fba3a37527ca5cf32c4ebcd71ab0b68b6b8f10f04e93ad",
8+
_ => unimplemented!(),
9+
};
10+
let initial_commit = match gix_testtools::hash_kind_from_env().unwrap_or_default() {
11+
gix_hash::Kind::Sha1 => "9d2fab1a0ba3585d0bc50922bfdd04ebb59361df",
12+
gix_hash::Kind::Sha256 => "ac050883b75422e0d03bfee760c591b292cbc10cee8ad934480ea5fb2ebc44fe",
13+
_ => unimplemented!(),
14+
};
15+
16+
(annotated_tag.into(), initial_commit.into())
17+
}
18+
419
#[test]
520
fn fetch_only() {
21+
let (annotated_tag, initial_commit) = test_hashes();
22+
623
baseline::agrees_with_fetch_specs(Some("refs/heads/main"));
724
baseline::agrees_with_fetch_specs(Some("heads/main"));
825
baseline::agrees_with_fetch_specs(Some("main"));
926
baseline::agrees_with_fetch_specs(Some("v0.0-f1"));
1027
baseline::agrees_with_fetch_specs(Some("tags/v0.0-f2"));
11-
baseline::of_objects_always_matches_if_the_server_has_the_object(Some(
12-
"78b1c1be9421b33a49a7a8176d93eeeafa112da1",
13-
));
14-
baseline::of_objects_always_matches_if_the_server_has_the_object(Some(
15-
"9d2fab1a0ba3585d0bc50922bfdd04ebb59361df",
16-
));
28+
baseline::of_objects_always_matches_if_the_server_has_the_object(Some(annotated_tag.as_ref()));
29+
baseline::of_objects_always_matches_if_the_server_has_the_object(Some(initial_commit.as_ref()));
1730
}
1831

1932
#[test]
2033
fn fetch_and_update() {
34+
let (annotated_tag, initial_commit) = test_hashes();
35+
2136
baseline::of_objects_with_destinations_are_written_into_given_local_branches(
22-
Some("78b1c1be9421b33a49a7a8176d93eeeafa112da1:special"),
23-
["78b1c1be9421b33a49a7a8176d93eeeafa112da1:refs/heads/special"],
37+
Some(format!("{annotated_tag}:special").as_ref()),
38+
[format!("{annotated_tag}:refs/heads/special").as_ref()],
2439
);
2540
baseline::of_objects_with_destinations_are_written_into_given_local_branches(
26-
Some("78b1c1be9421b33a49a7a8176d93eeeafa112da1:1111111111111111111111111111111111111111"),
27-
["78b1c1be9421b33a49a7a8176d93eeeafa112da1:refs/heads/1111111111111111111111111111111111111111"],
41+
Some(format!("{annotated_tag}:1111111111111111111111111111111111111111").as_ref()),
42+
[format!("{annotated_tag}:refs/heads/1111111111111111111111111111111111111111").as_ref()],
2843
);
2944
baseline::of_objects_with_destinations_are_written_into_given_local_branches(
30-
Some("9d2fab1a0ba3585d0bc50922bfdd04ebb59361df:tags/special"),
31-
["9d2fab1a0ba3585d0bc50922bfdd04ebb59361df:refs/tags/special"],
45+
Some(format!("{initial_commit}:tags/special").as_ref()),
46+
[format!("{initial_commit}:refs/tags/special").as_ref()],
3247
);
3348
baseline::of_objects_with_destinations_are_written_into_given_local_branches(
34-
Some("9d2fab1a0ba3585d0bc50922bfdd04ebb59361df:refs/tags/special"),
35-
["9d2fab1a0ba3585d0bc50922bfdd04ebb59361df:refs/tags/special"],
49+
Some(format!("{initial_commit}:refs/tags/special").as_ref()),
50+
[format!("{initial_commit}:refs/tags/special").as_ref()],
3651
);
3752

3853
baseline::agrees_but_observable_refs_are_vague(Some("f1:origin/f1"), ["refs/heads/f1:refs/heads/origin/f1"]);

gix-refspec/tests/refspec/spec.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ mod prefix {
5353
parse("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391").to_ref().prefix(),
5454
None
5555
);
56+
assert_eq!(
57+
parse("b071221ea854da2958fba3a37527ca5cf32c4ebcd71ab0b68b6b8f10f04e93ad")
58+
.to_ref()
59+
.prefix(),
60+
None
61+
);
5662
}
5763

5864
fn parse(spec: &str) -> RefSpec {
@@ -117,6 +123,10 @@ mod expand_prefixes {
117123
#[test]
118124
fn object_names_expand_to_nothing() {
119125
assert_eq!(parse("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391").len(), 0);
126+
assert_eq!(
127+
parse("b071221ea854da2958fba3a37527ca5cf32c4ebcd71ab0b68b6b8f10f04e93ad").len(),
128+
0
129+
);
120130
}
121131

122132
fn parse(spec: &str) -> Vec<String> {

gix/Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@ comfort = [
8585
#! Support for various hash algorithms is controlled here. The idea is to allow applications to
8686
#! compile in only the ones they need, which is why within the entire `gix` stack there is no
8787
#! crate that sets an algorithm, except for the top-level `gix` crate for convenience.
88-
#!
88+
#!
8989
#! At least one hash algorithm must be enabled for compilation to succeed.
9090

9191
## Enable support for the SHA-1 hash throughout the `gix` stack.
9292
sha1 = [
93+
"gix-hash/sha1",
9394
"gix-archive?/sha1",
9495
"gix-blame?/sha1",
9596
"gix-commitgraph/sha1",
@@ -98,7 +99,6 @@ sha1 = [
9899
"gix-dir?/sha1",
99100
"gix-discover/sha1",
100101
"gix-filter?/sha1",
101-
"gix-hash/sha1",
102102
"gix-hashtable/sha1",
103103
"gix-index?/sha1",
104104
"gix-merge?/sha1",
@@ -120,6 +120,11 @@ sha1 = [
120120
"gix-worktree-stream?/sha1",
121121
]
122122

123+
## Enable support for the SHA-256 hash throughout the `gix` stack.
124+
sha256 = [
125+
"gix-hash/sha256",
126+
]
127+
123128
#! #### Components
124129
#!
125130
#! A component is a distinct feature which may be comprised of one or more methods around a particular topic.

justfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ check:
182182
cargo check -p gix --no-default-features --features interrupt --tests
183183
cargo check -p gix --no-default-features --features blame --tests
184184
cargo check -p gix --no-default-features --features sha1
185+
cargo check -p gix --no-default-features --features sha1,sha256
186+
cargo check -p gix --no-default-features 2>&1 >/dev/null | grep 'Please set either the `sha1` or the `sha256` feature flag'
185187
cargo check -p gix-odb --features serde 2>&1 >/dev/null | grep 'Please set either the `sha1` or the `sha256` feature flag'
186188
cargo check -p gix-odb --features sha1,serde
187189
cargo check --no-default-features --features max-control,sha1
@@ -230,6 +232,8 @@ unit-tests:
230232
cargo nextest run -p gix-protocol --features blocking-client --no-fail-fast
231233
cargo nextest run -p gix-protocol --features async-client --no-fail-fast
232234
cargo nextest run -p gix-blame --no-fail-fast
235+
env GIX_TEST_FIXTURE_HASH=sha1 cargo nextest run -p gix-refspec --no-fail-fast
236+
env GIX_TEST_FIXTURE_HASH=sha256 cargo nextest run -p gix-refspec --no-fail-fast
233237
cargo nextest run -p gix --no-default-features --no-fail-fast
234238
cargo nextest run -p gix --no-default-features --features basic,comfort,max-performance-safe --no-fail-fast
235239
cargo nextest run -p gix --no-default-features --features basic,extras,comfort,need-more-recent-msrv --no-fail-fast

tests/tools/src/lib.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,11 @@ fn force_and_dir(
10461046
let dir = fixture_path_inner(
10471047
Path::new("generated-do-not-edit")
10481048
.join(archive_name)
1049-
.join(hash_kind.unwrap_or_else(hash_kind_from_env).to_string())
1049+
.join(
1050+
hash_kind
1051+
.unwrap_or_else(|| hash_kind_from_env().unwrap_or_default())
1052+
.to_string(),
1053+
)
10501054
.join(format!("{}-{}", script_identity, family_name())),
10511055
root,
10521056
);
@@ -1141,7 +1145,7 @@ where
11411145
gix_tempfile::signal::handler::Mode::DeleteTempfilesOnTerminationAndRestoreDefaultBehaviour,
11421146
);
11431147

1144-
let hash_kind = hash_kind_from_env();
1148+
let hash_kind = hash_kind_from_env().unwrap_or_default();
11451149
eprintln!("Using hash '{hash_kind}' when determining which fixture to use or recreate");
11461150

11471151
let script_location = script_name.as_ref();
@@ -1266,15 +1270,29 @@ where
12661270
Ok((script_result_directory, res))
12671271
}
12681272

1269-
fn hash_kind_from_env() -> gix_hash::Kind {
1270-
env::var_os("GIX_TEST_FIXTURE_HASH").and_then(|value| value.into_string().ok()).map(|object_kind| {
1273+
/// Returns the hash function that is used when creating or loading test fixtures.
1274+
///
1275+
/// The value returned is derived from the environment variable `GIX_TEST_FIXTURE_HASH`.
1276+
/// Use this, e. g., when you need to run different assertions depending on the hash
1277+
/// function used in a specific fixture.
1278+
///
1279+
/// Returns `None` if the environment variable isn't set.
1280+
///
1281+
/// # Panics
1282+
///
1283+
/// If the value set in `GIX_TEST_FIXTURE_HASH` is not valid.
1284+
pub fn hash_kind_from_env() -> Option<gix_hash::Kind> {
1285+
static FIXTURE_HASH: LazyLock<Option<gix_hash::Kind>> = LazyLock::new(|| {
1286+
env::var_os("GIX_TEST_FIXTURE_HASH").and_then(|value| value.into_string().ok()).map(|object_kind| {
12711287
gix_hash::Kind::from_str(&object_kind).unwrap_or_else(|_| {
12721288
panic!(
12731289
"GIX_TEST_FIXTURE_HASH was set to {object_kind} which is an invalid value. Valid values are {}. Exiting.",
12741290
gix_hash::Kind::all().iter().map(std::string::ToString::to_string).collect::<Vec<_>>().join(", ")
12751291
)
12761292
})
1277-
}).unwrap_or_default()
1293+
})
1294+
});
1295+
*FIXTURE_HASH
12781296
}
12791297

12801298
#[cfg(windows)]

0 commit comments

Comments
 (0)