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 b0ef042
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 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
37 changes: 34 additions & 3 deletions src/repo_info.rs
Original file line number Diff line number Diff line change
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 b0ef042

Please sign in to comment.