Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

error_chain! {
foreign_links {
Io(::std::io::Error) #[doc = "An error from the std::io module"];
Expand Down Expand Up @@ -75,5 +77,15 @@ error_chain! {
description("Failed to parse a version for a dependency")
display("The version `{}` for the dependency `{}` couldn't be parsed", version, dep)
}
/// Missing registrary checkout in the cargo registrary
MissingRegistraryCheckout(path: PathBuf) {
description("Missing registrary checkout in the cargo registrary")
display("Looks like ({}) is empty", path.display())
}
/// Non Unicode git path
NonUnicodeGitPath {
// this is because git2 function takes &str insted of something like AsRef<Path>
description("Path to cargos registrary contains non unicode characters")
}
}
}
26 changes: 22 additions & 4 deletions src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ pub fn update_registry_index(registry: &Url) -> Result<()> {
output.reset()?;
writeln!(output, " '{}' index", registry)?;

let refspec = "refs/heads/master:refs/remotes/origin/master";
fetch_with_cli(&repo, registry.as_str(), refspec)?;
let refspec = format!("refs/heads/{0}:refs/remotes/origin/{0}", get_checkout_name(registry_path)?);
fetch_with_cli(&repo, registry.as_str(), &refspec)?;

Ok(())
}
Expand Down Expand Up @@ -260,15 +260,33 @@ fn get_no_latest_version_from_json_when_all_are_yanked() {
assert!(read_latest_version(&versions, false).is_err());
}

/// Gets the checkedout branch name of .cargo/registry/index/git.832008.xyz-*/.git/refs
fn get_checkout_name(registry_path: impl AsRef<Path>) -> Result<String> {
let checkout_dir = registry_path.as_ref().join(".git").join("refs/remotes/origin/");
Ok(checkout_dir
.read_dir()?
.next() //Is there always only one branch? (expecting either master og HEAD)
.ok_or_else(|| ErrorKind::MissingRegistraryCheckout(checkout_dir))??
.file_name()
.into_string()
.map_err(|_| ErrorKind::NonUnicodeGitPath)?)
}

/// Fuzzy query crate from registry index
fn fuzzy_query_registry_index(
crate_name: impl Into<String>,
registry_path: impl AsRef<Path>,
) -> Result<Vec<CrateVersion>> {
let crate_name = crate_name.into();
let repo = git2::Repository::open(registry_path)?;
let remotes = PathBuf::from("refs/remotes/origin/");
let repo = git2::Repository::open(&registry_path)?;
let tree = repo
.find_reference("refs/remotes/origin/master")?
.find_reference(
remotes
.join(get_checkout_name(&registry_path)?)
.to_str()
.ok_or_else(|| ErrorKind::NonUnicodeGitPath)?,
)?
.peel_to_tree()?;

let mut names = gen_fuzzy_crate_names(crate_name.clone())?;
Expand Down