use std::path::Path;
use gitpoulpi_core::domain::REMOTE_TAG_MIRROR_PREFIX;
use gitpoulpi_core::domain::{
CloneOptions, ClonePhase, CloneProgress, CloneProgressSink, FetchOptions, FetchOutcome,
PullMode, PullOptions, PullOutcome, PushLease, PushOptions, PushOutcome, PushRejection,
RepoRef,
};
#[cfg(feature = "lfs")]
use gitpoulpi_core::domain::{LfsCloneMode, LfsDirection, LfsProgress};
use gitpoulpi_core::{Error, Result};
use crate::lfs::LfsProbe;
use crate::process::{run, run_async, run_full_async, run_status_async, run_streaming_stderr};
fn remote_tracking_refs(repo: &RepoRef, remote: &str) -> Result<Vec<(String, String)>> {
refs_under(repo, &format!("refs/remotes/{remote}"))
}
fn refs_under(repo: &RepoRef, prefix: &str) -> Result<Vec<(String, String)>> {
let raw = run(
&repo.root,
&["for-each-ref", "--format=%(refname) %(objectname)", prefix],
)?;
Ok(raw
.lines()
.filter_map(|line| {
let (name, oid) = line.split_once(' ')?;
Some((name.to_string(), oid.to_string()))
})
.collect())
}
fn fetch_porcelain_supported() -> bool {
crate::process::git_at_least(2, 41)
}
fn porcelain_updated_refs(stdout: &str, prefix: &str) -> Vec<String> {
stdout
.lines()
.filter_map(|line| {
let flag = line.chars().next()?;
if !matches!(flag, ' ' | '*' | '+') {
return None;
}
let name = line.split_whitespace().last()?;
name.starts_with(prefix).then(|| name.to_string())
})
.collect()
}
fn porcelain_pruned(stdout: &str, prefix: &str) -> Vec<(String, String)> {
stdout
.lines()
.filter_map(|line| {
let mut it = line.split_whitespace();
if it.next()? != "-" {
return None;
}
let old = it.next()?;
let name = it.last()?;
name.starts_with(prefix)
.then(|| (name.to_string(), old.to_string()))
})
.collect()
}
async fn fetch_once(repo: &RepoRef, target: &[&str], prune: bool) -> Result<Vec<String>> {
let all = target[0] == "--all";
let prefix = if all {
"refs/remotes/".to_string()
} else {
format!("refs/remotes/{}/", target[0])
};
let porcelain = fetch_porcelain_supported();
let scope = if all { "" } else { target[0] };
let before = if porcelain {
Vec::new()
} else {
remote_tracking_refs(repo, scope)?
};
let mut args: Vec<String> = Vec::new();
args.push("fetch".into());
if porcelain {
args.push("--porcelain".into());
}
args.extend(target.iter().map(|t| t.to_string()));
if prune {
args.push("--prune".into());
}
let args_ref: Vec<&str> = args.iter().map(String::as_str).collect();
let output = run_full_async(&repo.root, &args_ref).await?;
if !output.success {
return Err(crate::process::failed_with(
&args_ref,
output.exit_code,
&output.stderr,
));
}
if porcelain {
return Ok(porcelain_updated_refs(&output.stdout, &prefix));
}
let after = remote_tracking_refs(repo, scope)?;
Ok(after
.into_iter()
.filter(|pair| !before.contains(pair))
.map(|(name, _)| name)
.collect())
}
async fn fetch_mirror(
repo: &RepoRef,
remote: &str,
want_pruned: bool,
) -> Result<Vec<(String, String)>> {
let spec = crate::tags::mirror_refspec(remote);
let mut args = vec!["fetch"];
let porcelain = fetch_porcelain_supported();
let mirror_prefix = format!("{REMOTE_TAG_MIRROR_PREFIX}{remote}/");
let listed_before = if want_pruned && !porcelain {
refs_under(repo, &mirror_prefix)?
} else {
Vec::new()
};
if porcelain {
args.push("--porcelain");
}
args.extend(["--prune", "--no-tags", remote, spec.as_str()]);
let output = run_full_async(&repo.root, &args).await?;
if !output.success {
return Err(crate::process::failed_with(
&args,
output.exit_code,
&output.stderr,
));
}
Ok(if porcelain {
porcelain_pruned(&output.stdout, REMOTE_TAG_MIRROR_PREFIX)
} else if want_pruned {
let after = refs_under(repo, &mirror_prefix)?;
listed_before
.into_iter()
.filter(|(name, _)| !after.iter().any(|(kept, _)| kept == name))
.collect()
} else {
Vec::new()
})
}
async fn fetch_remotes(
repo: &RepoRef,
remotes: &[String],
target: &[&str],
opts: &FetchOptions,
) -> Result<FetchOutcome> {
let mirrors = async {
let mut pruned = Vec::new();
for remote in remotes {
if let Ok(p) = fetch_mirror(repo, remote, opts.prune_tags).await {
pruned.extend(p);
}
}
pruned
};
let (updated, pruned) = tokio::join!(fetch_once(repo, target, opts.prune), mirrors);
let updated_refs = updated?;
let pruned_tags = if opts.prune_tags {
crate::tags::delete_local_tags_lost_on_remote(repo, &pruned)?
} else {
Vec::new()
};
Ok(FetchOutcome {
updated_refs,
pruned_tags,
})
}
pub async fn fetch(repo: &RepoRef, remote: &str, opts: &FetchOptions) -> Result<FetchOutcome> {
gitpoulpi_core::guard::check_remote_arg("remote", remote)?;
fetch_remotes(repo, &[remote.to_string()], &[remote], opts).await
}
pub async fn fetch_all(repo: &RepoRef, opts: &FetchOptions) -> Result<FetchOutcome> {
let names: Vec<String> = crate::remotes::remotes(repo)?
.into_iter()
.map(|r| r.name)
.collect();
fetch_remotes(repo, &names, &["--all"], opts).await
}
pub async fn fetch_refspec(repo: &RepoRef, remote: &str, refspec: &str) -> Result<FetchOutcome> {
let (_, dst) = gitpoulpi_core::domain::parse_fetch_refspec(refspec).ok_or_else(|| {
Error::Backend(format!(
"`{refspec}` is not a fetchable spec (expected refs/<src>:{}<dst>)",
gitpoulpi_core::domain::FETCH_REFSPEC_NAMESPACE
))
})?;
gitpoulpi_core::guard::check_remote_arg("remote", remote)?;
let forced = if refspec.starts_with('+') {
refspec.to_string()
} else {
format!("+{refspec}")
};
run_async(
&repo.root,
&["fetch", "--quiet", "--no-tags", remote, &forced],
)
.await?;
Ok(FetchOutcome {
updated_refs: vec![dst.to_string()],
pruned_tags: Vec::new(),
})
}
pub async fn fetch_ref_as_branch(
repo: &RepoRef,
remote: &str,
remote_ref: &str,
branch: &str,
) -> Result<FetchOutcome> {
gitpoulpi_core::guard::check_remote_arg("remote", remote)?;
if !remote_ref.starts_with("refs/") || remote_ref.contains("..") || remote_ref.contains(' ') {
return Err(Error::Backend(format!(
"`{remote_ref}` is not a full ref name (expected refs/...)"
)));
}
run_async(&repo.root, &["check-ref-format", "--branch", branch])
.await
.map_err(|_| Error::Backend(format!("`{branch}` is not a valid branch name")))?;
let tracking = format!("refs/remotes/{remote}/{branch}");
run_async(
&repo.root,
&[
"fetch",
"--quiet",
remote,
&format!("+{remote_ref}:{tracking}"),
],
)
.await?;
let local = format!("refs/heads/{branch}");
let exists = run_async(&repo.root, &["rev-parse", "--verify", "--quiet", &local])
.await
.is_ok();
if !exists {
run_async(&repo.root, &["branch", "--track", branch, &tracking]).await?;
} else {
let ff = run_async(
&repo.root,
&["merge-base", "--is-ancestor", &local, &tracking],
)
.await
.is_ok();
if ff {
if current_branch(repo).await.ok().as_deref() == Some(branch) {
run_async(&repo.root, &["merge", "--ff-only", "--quiet", &tracking]).await?;
} else {
run_async(&repo.root, &["branch", "-f", branch, &tracking]).await?;
}
}
}
Ok(FetchOutcome {
updated_refs: vec![tracking],
pruned_tags: Vec::new(),
})
}
async fn current_branch(repo: &RepoRef) -> Result<String> {
let branch = run_async(&repo.root, &["rev-parse", "--abbrev-ref", "HEAD"]).await?;
let branch = branch.trim().to_string();
if branch == "HEAD" {
return Err(Error::Backend(
"HEAD is detached: check a branch out first".into(),
));
}
Ok(branch)
}
fn pull_mode_args(mode: PullMode) -> &'static [&'static str] {
match mode {
PullMode::Merge => &["--no-rebase", "--ff", "--no-edit"],
PullMode::FastForwardOnly => &["--ff-only"],
PullMode::Rebase => &["--rebase"],
}
}
pub async fn pull(repo: &RepoRef, opts: &PullOptions) -> Result<PullOutcome> {
if let Some(remote) = &opts.remote {
gitpoulpi_core::guard::check_remote_arg("remote", remote)?;
}
let before = run_async(&repo.root, &["rev-parse", "HEAD"]).await?;
let mut args: Vec<String> = vec!["pull".into()];
args.extend(pull_mode_args(opts.mode).iter().map(|a| a.to_string()));
if let Some(remote) = &opts.remote {
let branch = match &opts.remote_branch {
Some(b) => b.clone(),
None => current_branch(repo).await?,
};
args.push(remote.clone());
args.push(branch);
}
finish_pull(repo, opts, before, args).await
}
struct PullSource {
remote: String,
branch: Option<String>,
tracking: Option<String>,
}
async fn pull_source(repo: &RepoRef, opts: &PullOptions) -> Result<Option<PullSource>> {
if let Some(remote) = &opts.remote {
gitpoulpi_core::guard::check_remote_arg("remote", remote)?;
}
if let Some(remote) = &opts.remote {
let branch = match &opts.remote_branch {
Some(b) => b.clone(),
None => current_branch(repo).await?,
};
return Ok(Some(PullSource {
remote: remote.clone(),
tracking: Some(format!("refs/remotes/{remote}/{branch}")),
branch: Some(branch),
}));
}
let Ok(branch) = current_branch(repo).await else {
return Ok(None);
};
let config = |key: &str| -> Result<Option<String>> {
let (ok, text) = crate::process::run_lenient(
&repo.root,
&["config", "--get", &format!("branch.{branch}.{key}")],
)?;
let text = text.trim().to_string();
Ok((ok && !text.is_empty()).then_some(text))
};
let (Some(remote), Some(_merge)) = (config("remote")?, config("merge")?) else {
return Ok(None);
};
let (ok, tracking) = crate::process::run_lenient(
&repo.root,
&["rev-parse", "--symbolic-full-name", "@{upstream}"],
)?;
Ok(Some(PullSource {
remote,
branch: None,
tracking: ok.then(|| tracking.trim().to_string()),
}))
}
pub async fn pull_fetch(repo: &RepoRef, opts: &PullOptions) -> Result<()> {
let Some(source) = pull_source(repo, opts).await? else {
return Ok(());
};
let mut args = vec!["fetch".to_string(), source.remote];
args.extend(source.branch);
let args_ref: Vec<&str> = args.iter().map(String::as_str).collect();
let output = run_status_async(&repo.root, &args_ref).await?;
if output.success {
Ok(())
} else {
Err(crate::process::failed_with(
&args_ref,
output.exit_code,
&output.stderr,
))
}
}
fn merge_heads(repo: &RepoRef) -> Vec<String> {
let Ok((true, path)) =
crate::process::run_lenient(&repo.root, &["rev-parse", "--git-path", "FETCH_HEAD"])
else {
return Vec::new();
};
let path = repo.root.join(path.trim());
let Ok(text) = std::fs::read_to_string(path) else {
return Vec::new();
};
text.lines()
.filter_map(|line| {
let mut fields = line.split('\t');
let oid = fields.next()?.trim();
let marker = fields.next().unwrap_or("");
(marker.is_empty() && !oid.is_empty()).then(|| oid.to_string())
})
.collect()
}
pub async fn pull_integrate(repo: &RepoRef, opts: &PullOptions) -> Result<PullOutcome> {
let source = pull_source(repo, opts).await.ok().flatten();
let heads = merge_heads(repo);
let (Some(source), Some(head)) = (source, heads.first()) else {
return pull(repo, opts).await;
};
let before = run_async(&repo.root, &["rev-parse", "HEAD"]).await?;
let args: Vec<String> = match opts.mode {
PullMode::Merge => ["merge", "--ff", "--no-edit", "FETCH_HEAD"]
.map(String::from)
.to_vec(),
PullMode::FastForwardOnly => ["merge", "--ff-only", "FETCH_HEAD"]
.map(String::from)
.to_vec(),
PullMode::Rebase => {
let fork = source.tracking.as_deref().and_then(|tracking| {
let (ok, out) = crate::process::run_lenient(
&repo.root,
&["merge-base", "--fork-point", tracking, "HEAD"],
)
.ok()?;
let out = out.trim().to_string();
(ok && !out.is_empty()).then_some(out)
});
vec![
"rebase".into(),
"--onto".into(),
head.clone(),
fork.unwrap_or_else(|| head.clone()),
]
}
};
finish_pull(repo, opts, before, args).await
}
async fn finish_pull(
repo: &RepoRef,
opts: &PullOptions,
before: String,
args: Vec<String>,
) -> Result<PullOutcome> {
let args_ref: Vec<&str> = args.iter().map(String::as_str).collect();
let output = run_status_async(&repo.root, &args_ref).await?;
if !output.success {
let conflicts = run_async(&repo.root, &["diff", "--name-only", "--diff-filter=U"]).await;
if matches!(&conflicts, Ok(s) if !s.trim().is_empty()) {
return Ok(PullOutcome::Conflicted);
}
if opts.mode == PullMode::FastForwardOnly {
let ancestor = run_status_async(
&repo.root,
&["merge-base", "--is-ancestor", "HEAD", "FETCH_HEAD"],
)
.await;
if matches!(ancestor, Ok(out) if !out.success) {
return Err(Error::NotFastForward);
}
}
return Err(crate::process::failed_with(
&args_ref,
output.exit_code,
&output.stderr,
));
}
let after = run_async(&repo.root, &["rev-parse", "HEAD"]).await?;
if after.trim() == before.trim() {
return Ok(PullOutcome::UpToDate);
}
let oid = gitpoulpi_core::domain::Oid::from_hex(after.trim())
.ok_or_else(|| Error::Backend("HEAD was not a valid oid".into()))?;
let parents = run_async(
&repo.root,
&["rev-list", "--parents", "-n", "1", after.trim()],
)
.await?;
let parents: Vec<&str> = parents.split_whitespace().skip(1).collect();
let is_new_merge = parents.len() >= 2 && parents[0] == before.trim();
let before_is_ancestor = run_status_async(
&repo.root,
&["merge-base", "--is-ancestor", before.trim(), after.trim()],
)
.await?
.success;
if before_is_ancestor && !is_new_merge {
Ok(PullOutcome::FastForwarded(oid))
} else {
Ok(PullOutcome::Merged(oid))
}
}
pub async fn push(repo: &RepoRef, remote: &str, opts: &PushOptions) -> Result<PushOutcome> {
gitpoulpi_core::guard::check_remote_arg("remote", remote)?;
let branch = match &opts.branch {
Some(b) => b.clone(),
None => current_branch(repo).await?,
};
let mut args = vec!["push".to_string(), "--porcelain".to_string()];
if opts.set_upstream {
args.push("--set-upstream".to_string());
}
let dst = opts.remote_branch.clone().unwrap_or_else(|| branch.clone());
if let Some(dst) = &opts.remote_branch {
gitpoulpi_core::guard::check_remote_arg("remote branch", dst)?;
}
let lease = opts
.lease
.or(opts.force_with_lease.then_some(PushLease::Tracking));
match lease {
None => {}
Some(PushLease::Tracking) => args.push("--force-with-lease".to_string()),
Some(PushLease::Expect(oid)) => {
args.push(format!(
"--force-with-lease=refs/heads/{dst}:{}",
oid.to_hex()
));
}
Some(PushLease::Absent) => args.push(format!("--force-with-lease=refs/heads/{dst}:")),
}
args.push(remote.to_string());
args.push(format!("refs/heads/{branch}:refs/heads/{dst}"));
let args_ref: Vec<&str> = args.iter().map(String::as_str).collect();
let output = run_full_async(&repo.root, &args_ref).await?;
if !output.success {
if let Some(reason) = crate::history::push::rejection_reason(&output.stdout, &output.stderr)
{
let detail =
match crate::process::failed_with(&args_ref, output.exit_code, &output.stderr) {
Error::Git { detail, .. } => Some(detail),
_ => None,
};
return Err(Error::PushRejected(PushRejection {
remote: remote.to_string(),
branch,
reason,
detail,
}));
}
return Err(crate::process::failed_with(
&args_ref,
output.exit_code,
output.stderr.trim(),
));
}
Ok(PushOutcome {
pushed_refs: vec![branch],
})
}
pub async fn push_for_review(
repo: &RepoRef,
remote: &str,
branch: &str,
topic: Option<&str>,
) -> Result<PushOutcome> {
fn check(what: &str, value: &str) -> Result<()> {
let bad = value.is_empty()
|| value.starts_with('-')
|| value.starts_with('/')
|| value.ends_with('/')
|| value.contains("..")
|| value
.chars()
.any(|c| c.is_whitespace() || c.is_control() || "%:?*[\\^~@{}".contains(c));
if bad {
return Err(Error::Backend(format!(
"`{value}` is not a usable {what} for a review push"
)));
}
Ok(())
}
let branch = branch.trim();
let branch = branch.strip_prefix("refs/heads/").unwrap_or(branch);
check("branch", branch)?;
check("remote", remote)?;
gitpoulpi_core::guard::check_remote_arg("remote", remote)?;
let mut target = format!("refs/for/{branch}");
if let Some(topic) = topic.map(str::trim).filter(|t| !t.is_empty()) {
check("topic", topic)?;
target.push_str(&format!("%topic={topic}"));
}
let refspec = format!("HEAD:{target}");
let args = ["push", "--porcelain", remote, refspec.as_str()];
let output = run_full_async(&repo.root, &args).await?;
if !output.success {
return Err(crate::process::failed_with(
&args,
output.exit_code,
output.stderr.trim(),
));
}
Ok(PushOutcome {
pushed_refs: vec![target],
})
}
fn clone_args(url: &str, dest: &Path, opts: &CloneOptions, progress: bool) -> Vec<String> {
let mut args = vec!["clone".to_string()];
if progress {
args.push("--progress".to_string());
}
if let Some(depth) = opts.shallow_depth {
args.push("--depth".to_string());
args.push(depth.to_string());
}
if opts.recurse_submodules {
args.push("--recurse-submodules".to_string());
}
if opts.sparse.is_some() {
args.push("--sparse".to_string());
}
if let Some(branch) = &opts.branch {
args.push("--branch".to_string());
args.push(branch.clone());
}
if let Some(remote) = &opts.remote_name {
args.push("--origin".to_string());
args.push(remote.clone());
}
args.push("--".to_string());
args.push(url.to_string());
args.push(dest.to_string_lossy().to_string());
args
}
fn clone_cwd(dest: &Path) -> Result<std::path::PathBuf> {
let parent = dest
.parent()
.filter(|p| !p.as_os_str().is_empty())
.map(Path::to_path_buf)
.unwrap_or_else(|| std::path::PathBuf::from("."));
std::fs::create_dir_all(&parent)
.map_err(|e| Error::Backend(format!("could not create {}: {e}", parent.display())))?;
Ok(parent)
}
#[cfg(feature = "lfs")]
const CLONE_WITHOUT_LFS_FILTERS: [&str; 6] = [
"-c",
"filter.lfs.smudge=",
"-c",
"filter.lfs.process=",
"-c",
"filter.lfs.required=false",
];
#[cfg(feature = "lfs")]
fn clone_command_args(url: &str, dest: &Path, opts: &CloneOptions, progress: bool) -> Vec<String> {
let mut args: Vec<String> = CLONE_WITHOUT_LFS_FILTERS
.iter()
.map(|s| (*s).to_string())
.collect();
args.extend(clone_args(url, dest, opts, progress));
args
}
#[cfg(not(feature = "lfs"))]
fn clone_command_args(url: &str, dest: &Path, opts: &CloneOptions, progress: bool) -> Vec<String> {
clone_args(url, dest, opts, progress)
}
fn sparse_dirs_of(opts: &CloneOptions) -> Result<Option<Vec<String>>> {
opts.sparse
.as_deref()
.map(gitpoulpi_core::domain::normalize_sparse_dirs)
.transpose()
}
fn sparse_after_clone(repo: &RepoRef, dirs: Option<&[String]>) -> Result<()> {
match dirs {
Some(dirs) => crate::sparse::set(repo, dirs),
None => Ok(()),
}
}
#[cfg(feature = "lfs")]
async fn lfs_after_clone(
repo: &RepoRef,
opts: &CloneOptions,
probe: &LfsProbe,
progress: CloneProgressSink<'_>,
) {
if opts.lfs != LfsCloneMode::Auto || !probe.get(&repo.root).is_usable() {
return;
}
if !crate::lfs::repo_uses_lfs(repo) {
return;
}
progress(CloneProgress::new(ClonePhase::LfsObjects, 0));
let highest = std::sync::atomic::AtomicU8::new(0);
let sink = |p: LfsProgress| {
if p.direction != LfsDirection::Download {
return;
}
let percent = p.percent();
if percent < highest.fetch_max(percent, std::sync::atomic::Ordering::Relaxed) {
return;
}
progress(
CloneProgress::new(ClonePhase::LfsObjects, percent)
.with_counts(p.files_done, p.files_total),
);
};
let remote = opts.remote_name.as_deref().unwrap_or("origin");
let _ = crate::lfs_network::lfs_pull(repo, probe, remote, &sink).await;
}
#[cfg(not(feature = "lfs"))]
async fn lfs_after_clone(
_repo: &RepoRef,
_opts: &CloneOptions,
_probe: &LfsProbe,
_progress: CloneProgressSink<'_>,
) {
}
pub async fn clone(
url: &str,
dest: &Path,
opts: &CloneOptions,
probe: &LfsProbe,
) -> Result<RepoRef> {
gitpoulpi_core::guard::check_remote_arg("clone address", url)?;
let sparse = sparse_dirs_of(opts)?;
let args = clone_command_args(url, dest, opts, false);
let args_ref: Vec<&str> = args.iter().map(String::as_str).collect();
let cwd = clone_cwd(dest)?;
let mut undo = crate::cancel_cleanup::CloneCleanup::new(dest);
let cloned = run_async(&cwd, &args_ref).await;
undo.disarm();
cloned?;
let repo = cloned_ref(dest);
sparse_after_clone(&repo, sparse.as_deref())?;
lfs_after_clone(&repo, opts, probe, &|_| {}).await;
Ok(repo)
}
pub async fn clone_with_progress(
url: &str,
dest: &Path,
opts: &CloneOptions,
probe: &LfsProbe,
progress: CloneProgressSink<'_>,
) -> Result<RepoRef> {
gitpoulpi_core::guard::check_remote_arg("clone address", url)?;
let sparse = sparse_dirs_of(opts)?;
let args = clone_command_args(url, dest, opts, true);
let args_ref: Vec<&str> = args.iter().map(String::as_str).collect();
let cwd = clone_cwd(dest)?;
progress(CloneProgress::new(ClonePhase::Connecting, 0));
let mut last: Option<CloneProgress> = None;
let mut undo = crate::cancel_cleanup::CloneCleanup::new(dest);
let cloned = run_streaming_stderr(&cwd, &args_ref, |chunk| {
for report in parse_clone_progress(chunk) {
let keep = match last {
Some(prev) if prev.phase == report.phase => report.percent >= prev.percent,
_ => true,
};
if keep {
last = Some(report);
progress(report);
}
}
})
.await;
undo.disarm();
cloned?;
let repo = cloned_ref(dest);
sparse_after_clone(&repo, sparse.as_deref())?;
lfs_after_clone(&repo, opts, probe, progress).await;
progress(CloneProgress::done());
Ok(repo)
}
fn cloned_ref(dest: &Path) -> RepoRef {
RepoRef::from_root(dest)
}
fn parse_clone_progress(chunk: &str) -> Vec<CloneProgress> {
const PHASES: &[(&str, ClonePhase)] = &[
("remote: Counting objects:", ClonePhase::Counting),
("Counting objects:", ClonePhase::Counting),
("remote: Compressing objects:", ClonePhase::Compressing),
("Compressing objects:", ClonePhase::Compressing),
("Receiving objects:", ClonePhase::Receiving),
("Resolving deltas:", ClonePhase::Resolving),
("Updating files:", ClonePhase::CheckingOut),
("Checking out files:", ClonePhase::CheckingOut),
];
let mut out = Vec::new();
for raw in chunk.split(['\r', '\n']) {
let line = raw.trim();
if line.is_empty() {
continue;
}
let Some((prefix, phase)) = PHASES.iter().find(|(prefix, _)| line.starts_with(prefix))
else {
continue;
};
let rest = &line[prefix.len()..];
let Some(percent) = rest
.split('%')
.next()
.map(str::trim)
.and_then(|n| n.parse::<u32>().ok())
else {
continue;
};
let mut report = CloneProgress::new(*phase, percent.min(100) as u8);
if let Some(counts) = rest
.split_once('(')
.and_then(|(_, r)| r.split_once(')'))
.map(|(c, _)| c)
{
if let Some((cur, total)) = counts.split_once('/') {
if let (Ok(cur), Ok(total)) = (cur.trim().parse(), total.trim().parse()) {
report = report.with_counts(cur, total);
}
}
}
out.push(report);
}
out
}
pub fn remote_add(repo: &RepoRef, name: &str, url: &str) -> Result<()> {
gitpoulpi_core::guard::check_remote_arg("remote name", name)?;
gitpoulpi_core::guard::check_remote_arg("remote address", url)?;
run(&repo.root, &["remote", "add", "--", name, url])?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use gitpoulpi_core::domain::PushRejectReason;
#[test]
fn porcelain_rejections_are_recognized_and_nothing_else_is() {
let line = |summary: &str| {
format!("To ../origin.git\n!\trefs/heads/main:refs/heads/main\t{summary}\nDone\n")
};
assert_eq!(
crate::history::push::rejection_reason(&line("[rejected] (non-fast-forward)"), ""),
Some(PushRejectReason::NonFastForward)
);
assert_eq!(
crate::history::push::rejection_reason(&line("[rejected] (fetch first)"), ""),
Some(PushRejectReason::NonFastForward)
);
assert_eq!(
crate::history::push::rejection_reason(&line("[rejected] (stale info)"), ""),
Some(PushRejectReason::StaleLease)
);
assert_eq!(
crate::history::push::rejection_reason(
&line("[remote rejected] (pre-receive hook declined)"),
""
),
Some(PushRejectReason::RemoteRefused)
);
assert_eq!(
crate::history::push::rejection_reason(
"To ../o.git\n \trefs/heads/main:refs/heads/main\t1a2b..3c4d\nDone\n",
""
),
None
);
}
#[test]
fn a_protected_branch_refusal_is_typed_from_the_hosts_words() {
let porcelain = "!\trefs/heads/main:refs/heads/main\t[remote rejected] (protected branch hook declined)\n";
assert_eq!(
crate::history::push::rejection_reason(porcelain, ""),
Some(PushRejectReason::ProtectedByHost)
);
}
#[test]
fn a_chunk_of_real_git_progress_becomes_reports() {
let chunk = "Receiving objects: 45% (450/1000), 1.20 MiB | 3.00 MiB/s\r\
Receiving objects: 100% (1000/1000), 2.40 MiB | 3.00 MiB/s, done.\r\n\
Resolving deltas: 10% (12/120)\r";
let got = parse_clone_progress(chunk);
assert_eq!(got.len(), 3);
assert_eq!(got[0].phase, ClonePhase::Receiving);
assert_eq!(got[0].percent, 45);
assert_eq!(got[0].current, Some(450));
assert_eq!(got[0].total, Some(1000));
assert_eq!(got[1].percent, 100);
assert_eq!(got[2].phase, ClonePhase::Resolving);
assert_eq!(got[2].current, Some(12));
}
#[test]
fn the_remote_prefixed_phases_are_recognized() {
let got = parse_clone_progress(
"remote: Counting objects: 50% (5/10)\rremote: Compressing objects: 100% (10/10), done.\r",
);
assert_eq!(got[0].phase, ClonePhase::Counting);
assert_eq!(got[1].phase, ClonePhase::Compressing);
}
#[test]
fn non_progress_output_produces_no_reports() {
for noise in [
"Cloning into 'thing'...\n",
"warning: redirecting to https://example.invalid/thing.git\n",
"Receiving objec", "",
] {
assert!(
parse_clone_progress(noise).is_empty(),
"{noise:?} must not parse as progress"
);
}
}
#[test]
fn the_option_mapping_is_the_one_git_understands() {
let opts = CloneOptions {
shallow_depth: Some(1),
recurse_submodules: true,
branch: Some("side".into()),
remote_name: Some("upstream".into()),
..CloneOptions::default()
};
let args = clone_args("url", Path::new("dest"), &opts, true);
assert!(args.windows(2).any(|w| w == ["--depth", "1"]));
assert!(args.windows(2).any(|w| w == ["--branch", "side"]));
assert!(args.windows(2).any(|w| w == ["--origin", "upstream"]));
assert!(args.iter().any(|a| a == "--recurse-submodules"));
assert!(args.iter().any(|a| a == "--progress"));
let plain = clone_args("url", Path::new("dest"), &CloneOptions::default(), false);
assert!(!plain.iter().any(|a| a == "--progress"));
assert_eq!(plain, vec!["clone", "--", "url", "dest"]);
}
#[test]
#[cfg(feature = "lfs")]
fn the_lfs_filters_are_switched_off_for_the_clone_itself() {
let args = clone_command_args("url", Path::new("dest"), &CloneOptions::default(), false);
let clone_at = args.iter().position(|a| a == "clone").unwrap();
for setting in [
"filter.lfs.smudge=",
"filter.lfs.process=",
"filter.lfs.required=false",
] {
let at = args.iter().position(|a| a == setting).unwrap();
assert!(at < clone_at, "{setting} must precede `clone`");
assert_eq!(args[at - 1], "-c");
}
assert_eq!(&args[clone_at..], ["clone", "--", "url", "dest"]);
}
#[cfg(not(feature = "lfs"))]
#[test]
fn a_clone_without_lfs_passes_no_lfs_filter_config() {
let args = clone_command_args("url", Path::new("dest"), &CloneOptions::default(), false);
assert_eq!(args, ["clone", "--", "url", "dest"]);
assert!(!args.iter().any(|a| a.contains("filter.lfs")));
}
}