Skip to content

Commit

Permalink
Merge pull request #119 from SierraSoftworks/feat/refspecs
Browse files Browse the repository at this point in the history
feat: Add support for fetching specific refspecs
  • Loading branch information
notheotherben authored Dec 10, 2024
2 parents ffe90f9 + 70ccab5 commit 33fb3b6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ mod tests {
}

#[test]
#[cfg_attr(feature = "pure_tests", ignore)]
fn deserialize_example_config() {
let args = Args::parse_from(&[
let args = Args::parse_from([
"github-backup",
"--config",
&format!(
Expand Down
10 changes: 9 additions & 1 deletion src/engines/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ impl GitEngine {

let original_head = repository.head_id().ok();

let default_refspecs = vec!["+refs/heads/*:refs/remotes/origin/*".to_string()];

trace!(
"Configuring fetch operation for repository {}",
target.display()
Expand All @@ -150,7 +152,12 @@ impl GitEngine {
)
})?
.with_fetch_tags(Tags::All)
.with_refspecs(["+refs/heads/*:refs/remotes/origin/*"], gix::remote::Direction::Fetch)
.with_refspecs(
repo.refspecs.as_ref().unwrap_or(&default_refspecs)
.iter()
.map(|s| gix::bstr::BString::from(s.as_str()))
.collect::<Vec<gix::bstr::BString>>(),
gix::remote::Direction::Fetch)
.map_err(|e| {
errors::user_with_internal(
&format!(
Expand Down Expand Up @@ -319,6 +326,7 @@ mod tests {
let repo = GitRepo::new(
"SierraSoftworks/grey",
"https://github.com/sierrasoftworks/grey.git",
None,
);

let state1 = agent
Expand Down
2 changes: 1 addition & 1 deletion src/entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ entity!(HttpFile(url: U => String) {
with_content_type => content_type: Option<String>,
});

entity!(GitRepo(clone_url: U => String) {
entity!(GitRepo(clone_url: U => String, refspecs: R => Option<Vec<String>>) {
with_credentials => credentials: Credentials,
});
2 changes: 1 addition & 1 deletion src/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ mod tests {
async_stream::stream! {
let repos: Vec<crate::helpers::github::GitHubRepo> = load_test_file("github.repos.0.json").unwrap();
for repo in repos {
yield Ok(GitRepo::new(repo.full_name.as_str(), repo.clone_url.as_str())
yield Ok(GitRepo::new(repo.full_name.as_str(), repo.clone_url.as_str(), None)
.with_metadata_source(&repo));
}
}
Expand Down
19 changes: 15 additions & 4 deletions src/sources/github_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ impl BackupSource<GitRepo> for GitHubRepoSource {
let target: GitHubRepoSourceKind = policy.from.as_str().parse()?;

match target {
GitHubRepoSourceKind::Org(_) if self.artifact_kind == GitHubArtifactKind::Star => return Err(errors::user(
GitHubRepoSourceKind::Org(_) if self.artifact_kind == GitHubArtifactKind::Star => Err(errors::user(
"You cannot use an organization as the source for a starred repository backup.",
"Either use `from: user` or `from: users/<name>` when using a github/stars source kind.",
)),
GitHubRepoSourceKind::Repo(_) if self.artifact_kind == GitHubArtifactKind::Star => return Err(errors::user(
GitHubRepoSourceKind::Repo(_) if self.artifact_kind == GitHubArtifactKind::Star => Err(errors::user(
"You cannot use a repository as the source for a starred repository backup.",
"Either use `from: user` or `from: users/<name>` when using a github/stars source kind.",
)),
Expand Down Expand Up @@ -83,16 +83,27 @@ impl BackupSource<GitRepo> for GitHubRepoSource {

tracing_batteries::prelude::debug!("Calling {} to fetch repos", &url);

let refspecs = policy
.properties
.get("refspecs")
.map(|r| r.split(',').map(|r| r.to_string()).collect::<Vec<String>>());

async_stream::try_stream! {
if matches!(target, GitHubRepoSourceKind::Repo(_)) {
let repo = self.client.get::<GitHubRepo>(url, &policy.credentials, cancel).await?;
yield GitRepo::new(repo.full_name.as_str(), repo.clone_url.as_str())
yield GitRepo::new(
repo.full_name.as_str(),
repo.clone_url.as_str(),
refspecs.clone())
.with_credentials(policy.credentials.clone())
.with_metadata_source(&repo);
} else {
for await repo in self.client.get_paginated::<GitHubRepo>(url, &policy.credentials, cancel) {
let repo = repo?;
yield GitRepo::new(repo.full_name.as_str(), repo.clone_url.as_str())
yield GitRepo::new(
repo.full_name.as_str(),
repo.clone_url.as_str(),
refspecs.clone())
.with_credentials(policy.credentials.clone())
.with_metadata_source(&repo);
}
Expand Down

0 comments on commit 33fb3b6

Please sign in to comment.