Skip to content

Commit

Permalink
verify that Id::prefix() makes use of the git configuration (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Mar 1, 2022
1 parent af6326f commit 76e9110
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
27 changes: 21 additions & 6 deletions git-repository/src/id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//!
use std::convert::TryInto;
use std::ops::Deref;

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

/// Turn this object id into a shortened id with a length in hex as configured by `core.abbrev`.
pub fn prefix(&self) -> Result<git_hash::Prefix, prefix::Error> {
// let hex_len = self.handle.config.get_int("core.abbrev")?;
let hex_len = self.repo.config_int("core.abbrev", 8);
let hex_len = hex_len.try_into().map_err(|_| prefix::Error::ConfigValue {
actual: hex_len,
max_range: self.inner.kind().len_in_hex(),
err: None,
})?;
let prefix =
git_odb::find::PotentialPrefix::new(self.inner, hex_len).map_err(|err| prefix::Error::ConfigValue {
actual: hex_len as i64,
max_range: self.inner.kind().len_in_hex(),
err: Some(err),
})?;
Ok(self
.repo
.objects
.disambiguate_prefix(git_odb::find::PotentialPrefix::new(self.inner, 7)?)
.disambiguate_prefix(prefix)
.map_err(crate::object::find::existing::OdbError::Find)?
.ok_or(crate::object::find::existing::OdbError::NotFound { oid: self.inner })?)
}
Expand All @@ -46,10 +58,13 @@ mod prefix {
pub enum Error {
#[error(transparent)]
FindExisting(#[from] crate::object::find::existing::OdbError),
#[error(transparent)]
Config(#[from] crate::config::open::Error),
#[error(transparent)]
Prefix(#[from] git_hash::prefix::Error),
#[error("core.abbrev length was {}, but needs to be between 4 and {}", .actual, .max_range)]
ConfigValue {
#[source]
err: Option<git_hash::prefix::Error>,
actual: i64,
max_range: usize,
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion git-repository/src/repository/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ impl crate::Repository {
/// Return the integer value at `key` (like `core.abbrev`) or use the given `default` value if it isn't present.
// TODO: figure out how to identify sub-sections, or how to design such an API. This is really just a first test.
// TODO: tests
pub fn config_int(&self, key: &str, default: i64) -> i64 {
pub(crate) fn config_int(&self, key: &str, default: i64) -> i64 {
let (section, key) = key.split_once('.').expect("valid section.key format");
self.config
.value::<git_config::values::Integer>(section, None, key)
Expand Down
2 changes: 1 addition & 1 deletion git-repository/tests/easy/ext/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod set_namespace {
use git_repository::refs::transaction::PreviousValue;

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

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,26 @@ use std::cmp::Ordering;

#[test]
fn prefix() -> crate::Result {
let repo = crate::repo("make_repo_with_fork_and_dates.sh")?.to_thread_local();
let (repo, worktree_dir) = crate::repo_rw("make_repo_with_fork_and_dates.sh")?;
let id = hex_to_id("288e509293165cb5630d08f4185bdf2445bf6170").attach(&repo);
let prefix = id.prefix()?;
assert_eq!(prefix.cmp_oid(&id), Ordering::Equal);
assert_eq!(prefix.hex_len(), 7, "preconfigured via core.abbrev");
assert_eq!(prefix.hex_len(), 8, "preconfigured via core.abbrev default value");

assert!(
std::process::Command::new("git")
.current_dir(worktree_dir.path())
.args(["config", "--int", "core.abbrev", "5"])
.status()?
.success(),
"set core abbrev value successfully"
);

let repo = git_repository::open(worktree_dir.path()).unwrap();
let id = id.detach().attach(&repo);
let prefix = id.prefix()?;
assert_eq!(prefix.cmp_oid(&id), Ordering::Equal);
assert_eq!(prefix.hex_len(), 5, "preconfigured via core.abbrev in the repo file");
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion git-repository/tests/easy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod ext;
mod id;
mod object;
mod oid;
mod reference;
7 changes: 5 additions & 2 deletions git-repository/tests/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ fn repo(name: &str) -> crate::Result<ThreadSafeRepository> {
Ok(ThreadSafeRepository::open(repo_path)?)
}

fn repo_rw(name: &str) -> crate::Result<(ThreadSafeRepository, tempfile::TempDir)> {
fn repo_rw(name: &str) -> crate::Result<(git_repository::Repository, tempfile::TempDir)> {
let repo_path = git_testtools::scripted_fixture_repo_writable(name)?;
Ok((ThreadSafeRepository::discover(repo_path.path())?, repo_path))
Ok((
ThreadSafeRepository::discover(repo_path.path())?.to_thread_local(),
repo_path,
))
}

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

0 comments on commit 76e9110

Please sign in to comment.