Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(spm): git proxy config #3264

Merged
merged 2 commits into from
Nov 28, 2024
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
33 changes: 16 additions & 17 deletions src/backend/spm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ use crate::backend::Backend;
use crate::cli::args::BackendArg;
use crate::cmd::CmdLineRunner;
use crate::config::Settings;
use crate::git::Git;
use crate::install_context::InstallContext;
use crate::toolset::ToolVersion;
use crate::{file, github};
use eyre::WrapErr;
use git2::Repository;
use serde::de::{MapAccess, Visitor};
use serde::Deserializer;
use serde_derive::Deserialize;
use std::env::temp_dir;
use std::fmt::{self, Debug};
use std::path::PathBuf;
use url::Url;
Expand Down Expand Up @@ -62,7 +61,7 @@ impl Backend for SPMBackend {
} else {
tv.version.clone()
};
let repo_dir = self.clone_package_repo(&repo, &revision)?;
let repo_dir = self.clone_package_repo(ctx, &tv, &repo, &revision)?;

let executables = self.get_executable_names(&repo_dir)?;
if executables.is_empty() {
Expand All @@ -88,24 +87,24 @@ impl SPMBackend {

fn clone_package_repo(
&self,
ctx: &InstallContext,
tv: &ToolVersion,
package_repo: &SwiftPackageRepo,
revision: &str,
) -> Result<PathBuf, eyre::Error> {
let tmp_repo_dir = temp_dir().join("spm").join(package_repo.dir_name(revision));
file::remove_all(&tmp_repo_dir)?;
file::create_dir_all(tmp_repo_dir.parent().unwrap())?;
let repo = Git::new(tv.cache_path().join(package_repo.dir_name(revision)));
if !repo.exists() {
debug!(
"Cloning swift package repo {} to {}",
package_repo.url.as_str(),
repo.dir.display(),
);
repo.clone(package_repo.url.as_str(), Some(ctx.pr.as_ref()))?;
}
debug!("Checking out revision: {revision}");
repo.update(Some(revision.to_string()))?;

debug!(
"Cloning swift package repo: {}, revision: {} to path: {}",
package_repo.url.as_str(),
revision,
tmp_repo_dir.display()
);
let repo = Repository::clone(package_repo.url.as_str(), &tmp_repo_dir)?;
let (object, reference) = repo.revparse_ext(revision)?;
repo.checkout_tree(&object, None)?;
repo.set_head(reference.unwrap().name().unwrap())?;
Ok(tmp_repo_dir)
Ok(repo.dir)
}

fn get_executable_names(&self, repo_dir: &PathBuf) -> Result<Vec<String>, eyre::Error> {
Expand Down
17 changes: 17 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ impl Git {
pub fn update(&self, gitref: Option<String>) -> Result<(String, String)> {
let gitref = gitref.map_or_else(|| self.current_branch(), Ok)?;
debug!("updating {} to {}", self.dir.display(), gitref);
if SETTINGS.libgit2 {
if let Ok(repo) = self.repo() {
let mut fetch_options = get_fetch_options()?;
let mut remote = repo.find_remote("origin")?;
remote.fetch(&[&gitref], Some(&mut fetch_options), None)?;
let prev_rev = self.current_sha()?;
let (obj, reference) = repo.revparse_ext(&gitref)?;
repo.checkout_tree(&obj, None)?;
if let Some(reference) = reference.and_then(|r| r.name().map(|s| s.to_string())) {
repo.set_head(&reference)?;
}
let post_rev = self.current_sha()?;
touch_dir(&self.dir)?;
return Ok((prev_rev, post_rev));
}
}
let exec = |cmd: Expression| match cmd.stderr_to_stdout().stdout_capture().unchecked().run()
{
Ok(res) => {
Expand All @@ -81,6 +97,7 @@ impl Git {
}
Err(err) => Err(eyre!("git failed: {cmd:?} {err:#}")),
};
debug!("updating {} to {} with git", self.dir.display(), gitref);
exec(git_cmd!(
&self.dir,
"fetch",
Expand Down