Skip to content

Commit

Permalink
Include org in local repo checkout
Browse files Browse the repository at this point in the history
This addresses an issue where certain fonts exist in multiple locations
(e.g. under the original author as well as the googlefotns org)
  • Loading branch information
cmyr committed Sep 19, 2024
1 parent 7d78d71 commit 26647ba
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
23 changes: 2 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,8 @@ fn config_files_and_rev_for_repo(
repo_url: &str,
checkout_font_dir: &Path,
) -> Result<(Vec<PathBuf>, GitRev), ConfigFetchIssue> {
let repo_name = repo_name_from_url(repo_url)
.ok_or_else(|| ConfigFetchIssue::BadRepoUrl(repo_url.into()))?;

let local_repo_dir = checkout_font_dir.join(repo_name);
let local_repo_dir = repo_info::repo_path_for_url(repo_url, checkout_font_dir)
.ok_or_else(|| ConfigFetchIssue::BadRepoUrl(repo_url.to_owned()))?;
// - if local repo already exists, then look there
// - otherwise try naive http requests first,
// - and then finally clone the repo and look
Expand Down Expand Up @@ -528,11 +526,6 @@ fn clone_repo(url: &str, to_dir: &Path) -> Result<(), GitFail> {
Ok(())
}

fn repo_name_from_url(url: &str) -> Option<&str> {
let url = url.trim_end_matches('/');
url.rsplit_once('/').map(|(_, tail)| tail)
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -548,18 +541,6 @@ mod tests {
));
}

#[test]
fn name_from_url() {
assert_eq!(
repo_name_from_url("https://github.com/hyper-type/hahmlet/"),
Some("hahmlet"),
);
assert_eq!(
repo_name_from_url("https://github.com/hyper-type/Advent"),
Some("Advent"),
);
}

#[test]
fn remote_sha() {
let rev = get_git_rev_remote("https://github.com/googlefonts/fontations").unwrap();
Expand Down
39 changes: 35 additions & 4 deletions src/repo_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct RepoInfo {
/// The repository's url
pub repo_url: String,
/// The commit rev of the repository's main branch, at discovery time.
//NOTE: this isn't private because we want to force the use of `new` for
//NOTE: this is private because we want to force the use of `new` for
//construction, so we can ensure urls are well formed
rev: String,
/// The names of config files that exist in this repository's source directory
Expand Down Expand Up @@ -55,13 +55,20 @@ impl RepoInfo {
&self.rev
}

/// Given a root cache directory, return the local path this repo.
///
/// This is in the format, `{cache_dir}/{repo_org}/{repo_name}`
pub fn repo_path(&self, cache_dir: &Path) -> PathBuf {
// unwrap is okay because we already know the url is well formed
repo_path_for_url(&self.repo_url, cache_dir).unwrap()
}

/// Return the a `Vec` of source files in this respository.
///
/// If necessary, this will create a new checkout of this repo at
/// '{git_cache_dir}/{repo_name}'.
/// '{git_cache_dir}/{repo_org}/{repo_name}'.
pub fn get_sources(&self, git_cache_dir: &Path) -> Result<Vec<PathBuf>, LoadRepoError> {
let font_dir = git_cache_dir.join(self.repo_name());

let font_dir = self.repo_path(git_cache_dir);
if !font_dir.exists() {
std::fs::create_dir_all(&font_dir)?;
super::clone_repo(&self.repo_url, &font_dir)?;
Expand Down Expand Up @@ -107,3 +114,27 @@ fn repo_name_and_org_from_url(url: &str) -> Option<(&str, &str)> {
let (_, org) = rest.rsplit_once('/')?;
Some((org, name))
}

pub(super) fn repo_path_for_url(url: &str, base_cache_dir: &Path) -> Option<PathBuf> {
let (org, name) = repo_name_and_org_from_url(url)?;
let mut path = base_cache_dir.join(org);
path.push(name);
Some(path)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn org_and_name_from_url() {
assert_eq!(
repo_name_and_org_from_url("https://github.com/hyper-type/hahmlet/"),
Some(("hyper-type", "hahmlet")),
);
assert_eq!(
repo_name_and_org_from_url("https://github.com/hyper-type/Advent"),
Some(("hyper-type", "Advent")),
);
}
}

0 comments on commit 26647ba

Please sign in to comment.