Skip to content

Commit 74813a0

Browse files
committed
verify that Id::prefix() makes use of the git configuration (#301)
1 parent f290030 commit 74813a0

File tree

6 files changed

+46
-13
lines changed

6 files changed

+46
-13
lines changed

git-repository/src/id.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//!
2+
use std::convert::TryInto;
23
use std::ops::Deref;
34

45
use git_hash::{oid, ObjectId};
@@ -28,11 +29,22 @@ impl<'repo> Id<'repo> {
2829

2930
/// Turn this object id into a shortened id with a length in hex as configured by `core.abbrev`.
3031
pub fn prefix(&self) -> Result<git_hash::Prefix, prefix::Error> {
31-
// let hex_len = self.handle.config.get_int("core.abbrev")?;
32+
let hex_len = self.repo.config_int("core.abbrev", 8);
33+
let hex_len = hex_len.try_into().map_err(|_| prefix::Error::ConfigValue {
34+
actual: hex_len,
35+
max_range: self.inner.kind().len_in_hex(),
36+
err: None,
37+
})?;
38+
let prefix =
39+
git_odb::find::PotentialPrefix::new(self.inner, hex_len).map_err(|err| prefix::Error::ConfigValue {
40+
actual: hex_len as i64,
41+
max_range: self.inner.kind().len_in_hex(),
42+
err: Some(err),
43+
})?;
3244
Ok(self
3345
.repo
3446
.objects
35-
.disambiguate_prefix(git_odb::find::PotentialPrefix::new(self.inner, 7)?)
47+
.disambiguate_prefix(prefix)
3648
.map_err(crate::object::find::existing::OdbError::Find)?
3749
.ok_or(crate::object::find::existing::OdbError::NotFound { oid: self.inner })?)
3850
}
@@ -46,10 +58,13 @@ mod prefix {
4658
pub enum Error {
4759
#[error(transparent)]
4860
FindExisting(#[from] crate::object::find::existing::OdbError),
49-
#[error(transparent)]
50-
Config(#[from] crate::config::open::Error),
51-
#[error(transparent)]
52-
Prefix(#[from] git_hash::prefix::Error),
61+
#[error("core.abbrev length was {}, but needs to be between 4 and {}", .actual, .max_range)]
62+
ConfigValue {
63+
#[source]
64+
err: Option<git_hash::prefix::Error>,
65+
actual: i64,
66+
max_range: usize,
67+
},
5368
}
5469
}
5570

git-repository/src/repository/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ impl crate::Repository {
33
/// Return the integer value at `key` (like `core.abbrev`) or use the given `default` value if it isn't present.
44
// TODO: figure out how to identify sub-sections, or how to design such an API. This is really just a first test.
55
// TODO: tests
6-
pub fn config_int(&self, key: &str, default: i64) -> i64 {
6+
pub(crate) fn config_int(&self, key: &str, default: i64) -> i64 {
77
let (section, key) = key.split_once('.').expect("valid section.key format");
88
self.config
99
.value::<git_config::values::Integer>(section, None, key)

git-repository/tests/easy/ext/reference.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod set_namespace {
33
use git_repository::refs::transaction::PreviousValue;
44

55
fn easy_repo_rw() -> crate::Result<(git::Repository, tempfile::TempDir)> {
6-
crate::repo_rw("make_references_repo.sh").map(|(r, d)| (r.to_thread_local(), d))
6+
crate::repo_rw("make_references_repo.sh")
77
}
88

99
#[test]

git-repository/tests/easy/oid.rs renamed to git-repository/tests/easy/id.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,26 @@ use std::cmp::Ordering;
44

55
#[test]
66
fn prefix() -> crate::Result {
7-
let repo = crate::repo("make_repo_with_fork_and_dates.sh")?.to_thread_local();
7+
let (repo, worktree_dir) = crate::repo_rw("make_repo_with_fork_and_dates.sh")?;
88
let id = hex_to_id("288e509293165cb5630d08f4185bdf2445bf6170").attach(&repo);
99
let prefix = id.prefix()?;
1010
assert_eq!(prefix.cmp_oid(&id), Ordering::Equal);
11-
assert_eq!(prefix.hex_len(), 7, "preconfigured via core.abbrev");
11+
assert_eq!(prefix.hex_len(), 8, "preconfigured via core.abbrev default value");
12+
13+
assert!(
14+
std::process::Command::new("git")
15+
.current_dir(worktree_dir.path())
16+
.args(["config", "--int", "core.abbrev", "5"])
17+
.status()?
18+
.success(),
19+
"set core abbrev value successfully"
20+
);
21+
22+
let repo = git_repository::open(worktree_dir.path()).unwrap();
23+
let id = id.detach().attach(&repo);
24+
let prefix = id.prefix()?;
25+
assert_eq!(prefix.cmp_oid(&id), Ordering::Equal);
26+
assert_eq!(prefix.hex_len(), 5, "preconfigured via core.abbrev in the repo file");
1227
Ok(())
1328
}
1429

git-repository/tests/easy/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
mod ext;
2+
mod id;
23
mod object;
3-
mod oid;
44
mod reference;

git-repository/tests/repo.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ fn repo(name: &str) -> crate::Result<ThreadSafeRepository> {
88
Ok(ThreadSafeRepository::open(repo_path)?)
99
}
1010

11-
fn repo_rw(name: &str) -> crate::Result<(ThreadSafeRepository, tempfile::TempDir)> {
11+
fn repo_rw(name: &str) -> crate::Result<(git_repository::Repository, tempfile::TempDir)> {
1212
let repo_path = git_testtools::scripted_fixture_repo_writable(name)?;
13-
Ok((ThreadSafeRepository::discover(repo_path.path())?, repo_path))
13+
Ok((
14+
ThreadSafeRepository::discover(repo_path.path())?.to_thread_local(),
15+
repo_path,
16+
))
1417
}
1518

1619
fn easy_repo_rw(name: &str) -> crate::Result<(Repository, tempfile::TempDir)> {

0 commit comments

Comments
 (0)