Skip to content
This repository was archived by the owner on Nov 3, 2026. It is now read-only.
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
50 changes: 42 additions & 8 deletions src/clone_queue.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use log::{debug, warn};
use meta_core::config;
use meta_core::display::escape_path;
use std::collections::{BTreeSet, HashSet};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
Expand Down Expand Up @@ -94,16 +95,16 @@ impl CloneQueue {
}
}

let Some((meta_path, _format)) = config::find_meta_config_in(base_dir) else {
debug!("No .meta config found in {}", base_dir.display());
let Some((meta_path, _format)) = config::find_meta_config_in(base_dir)? else {
debug!("No .meta config found in {}", escape_path(base_dir));
return Ok(0);
};

let (projects, _) = config::parse_meta_config(&meta_path)?;
debug!(
"Discovered {} projects in {} at depth {}",
projects.len(),
base_dir.display(),
escape_path(base_dir),
depth_level
);

Expand All @@ -113,11 +114,9 @@ impl CloneQueue {

// Skip if already exists
if target_path.exists() {
// But still check if it has a config file for nested discovery
if config::find_meta_config_in(&target_path).is_some() {
// Queue it for discovery even though it's already cloned
added += self.push_from_meta(&target_path, depth_level + 1)?;
}
// But still look in it for nested discovery (push_from_meta
// checks the depth limit before it looks for a config)
added += self.push_from_meta(&target_path, depth_level + 1)?;
continue;
}

Expand Down Expand Up @@ -261,6 +260,41 @@ mod tests {

// ── push / dedup ──────────────────────────────────────────

#[cfg(unix)]
#[test]
fn push_from_meta_does_not_look_past_the_depth_limit() {
// child exists with a dangling .meta; at depth limit 0 it is never
// read, so seeding still queues the missing sibling
let dir = tempfile::tempdir().unwrap();
std::fs::create_dir(dir.path().join("child")).unwrap();
std::fs::write(
dir.path().join(".meta"),
r#"{"projects": {"child": {"repo": "u", "meta": true}, "sib": "u2"}}"#,
)
.unwrap();
std::os::unix::fs::symlink("missing.json", dir.path().join("child/.meta")).unwrap();
let queue = CloneQueue::new(None, Some(0));
assert_eq!(queue.push_from_meta(dir.path(), 0).unwrap(), 1);
}

#[cfg(unix)]
#[test]
fn push_from_meta_fails_on_a_nested_config_it_cannot_read() {
// child/.meta is a link to nowhere: its repos are unknown, which is
// an error, not "no nested repos"
let dir = tempfile::tempdir().unwrap();
std::fs::create_dir(dir.path().join("child")).unwrap();
std::fs::write(
dir.path().join(".meta"),
r#"{"projects": {"child": {"repo": "u", "meta": true}}}"#,
)
.unwrap();
std::os::unix::fs::symlink("missing.json", dir.path().join("child/.meta")).unwrap();
let queue = CloneQueue::new(None, None);
let err = queue.push_from_meta(dir.path(), 0).unwrap_err();
assert!(format!("{err:#}").contains("Cannot read"), "{err:#}");
}

#[test]
fn push_adds_task_and_increments_count() {
let queue = CloneQueue::new(None, None);
Expand Down
17 changes: 9 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use indicatif::ProgressBar;
use meta_core::display::escape_path;
use std::path::Path;
use std::process::{Command, Stdio};
pub mod clone_queue;
Expand All @@ -23,17 +24,17 @@ pub fn clone_repo_with_progress(
if let Some(pb) = pb {
pb.finish_with_message(format!(
"{}: already exists, skipping",
target_dir.display()
escape_path(target_dir)
));
} else {
println!("{}: already exists, skipping", target_dir.display());
println!("{}: already exists, skipping", escape_path(target_dir));
}
return Ok(());
}
if let Some(pb) = pb {
pb.set_message(format!("Cloning {url}"));
} else {
println!("Cloning {} into {}", url, target_dir.display());
println!("Cloning {} into {}", url, escape_path(target_dir));
}
let mut child = Command::new("git")
.arg("clone")
Expand All @@ -45,22 +46,22 @@ pub fn clone_repo_with_progress(
let status = child.wait()?;
if let Some(pb) = pb {
if status.success() {
pb.finish_with_message(format!("{} ✓", style(target_dir.display()).green()));
pb.finish_with_message(format!("{} ✓", style(escape_path(target_dir)).green()));
} else {
pb.finish_with_message(format!(
"Failed to clone {} into {}",
url,
target_dir.display()
escape_path(target_dir)
));
}
} else if status.success() {
println!("{} ✓", style(target_dir.display()).green());
println!("{} ✓", style(escape_path(target_dir)).green());
} else {
println!("Failed to clone {} into {}", url, target_dir.display());
println!("Failed to clone {} into {}", url, escape_path(target_dir));
}
if status.success() {
Ok(())
} else {
anyhow::bail!("Failed to clone {} into {}", url, target_dir.display())
anyhow::bail!("Failed to clone {} into {}", url, escape_path(target_dir))
}
}
37 changes: 19 additions & 18 deletions src/worktree/git_ops.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Git operations for worktree management.

use anyhow::Result;
use meta_core::display::{escape, escape_path};
use std::path::Path;
use std::process::{Command, Stdio};

Expand Down Expand Up @@ -58,15 +59,15 @@ fn ensure_checkout_config_is_not_bare(repo_path: &Path) -> Result<()> {
anyhow::bail!(
"Repository '{}' is a checked-out work tree but its shared Git config has \
core.bare=true.\nRepair it with:\n git -C {} config --local core.bare false",
repo_path.display(),
shell_quote_path(repo_path)
escape_path(repo_path),
escape(&shell_quote_path(repo_path))
);
}
} else if output.status.code() != Some(1) {
let stderr = String::from_utf8_lossy(&output.stderr);
anyhow::bail!(
"Failed to inspect core.bare for '{}': {}",
repo_path.display(),
escape_path(repo_path),
stderr.trim()
);
}
Expand All @@ -90,7 +91,7 @@ pub fn git_worktree_add(
anyhow::anyhow!(
"Ref '{}' not found in repo '{}'",
ref_name,
repo_path.display()
escape_path(repo_path)
)
})?;
// Pass the name, not the commit: a remote-tracking ref still sets the
Expand All @@ -104,7 +105,7 @@ pub fn git_worktree_add(
let stderr = String::from_utf8_lossy(&output.stderr);
anyhow::bail!(
"git worktree add failed for '{}' (branch: {}, ref: {}): {}",
repo_path.display(),
escape_path(repo_path),
branch,
ref_name,
stderr.trim()
Expand Down Expand Up @@ -139,7 +140,7 @@ pub fn git_worktree_add(
} else {
// Create new branch from HEAD
let start = git_rev_parse(repo_path, "HEAD")?.ok_or_else(|| {
anyhow::anyhow!("{} has no commit to branch from", repo_path.display())
anyhow::anyhow!("{} has no commit to branch from", escape_path(repo_path))
})?;
(
vec!["worktree", "add", "-b", branch, &dest_str],
Expand All @@ -153,7 +154,7 @@ pub fn git_worktree_add(
let stderr = String::from_utf8_lossy(&output.stderr);
anyhow::bail!(
"git worktree add failed for '{}' (branch: {}): {}",
repo_path.display(),
escape_path(repo_path),
branch,
stderr.trim()
);
Expand Down Expand Up @@ -192,7 +193,7 @@ pub fn git_status_summary(repo_path: &Path) -> Result<GitStatusSummary> {
let stderr = String::from_utf8_lossy(&output.stderr);
anyhow::bail!(
"git status failed for '{}': {}",
repo_path.display(),
escape_path(repo_path),
stderr.trim()
);
}
Expand Down Expand Up @@ -311,10 +312,10 @@ pub fn remove_worktree_repos(

for m in ordered {
if verbose {
eprintln!("Removing worktree at {}", m.path.display());
eprintln!("Removing worktree at {}", escape_path(&m.path));
}
git_worktree_remove(&m.repo_git_dir, &m.path, force)
.map_err(|e| e.context(format!("Cannot remove {}", m.path.display())))?;
.map_err(|e| e.context(format!("Cannot remove {}", escape_path(&m.path))))?;
}
Ok(())
}
Expand All @@ -337,7 +338,7 @@ pub fn git_rev_parse(repo_path: &Path, rev: &str) -> Result<Option<String>> {
}
anyhow::bail!(
"git rev-parse {rev} failed in {}: {}",
repo_path.display(),
escape_path(repo_path),
stderr.trim()
)
}
Expand Down Expand Up @@ -373,7 +374,7 @@ pub fn git_branch_is_free(repo_path: &Path, branch: &str) -> Result<()> {
anyhow::ensure!(
list.status.success(),
"Cannot list the worktrees of {}: {}",
repo_path.display(),
escape_path(repo_path),
String::from_utf8_lossy(&list.stderr).trim()
);
let wanted = format!("branch {full}");
Expand All @@ -390,7 +391,7 @@ pub fn git_branch_is_free(repo_path: &Path, branch: &str) -> Result<()> {
anyhow::ensure!(
output.status.success(),
"Cannot find the git directory of {}: {}",
repo_path.display(),
escape_path(repo_path),
String::from_utf8_lossy(&output.stderr).trim()
);
// Relative output is relative to the repository, where git ran.
Expand All @@ -399,8 +400,8 @@ pub fn git_branch_is_free(repo_path: &Path, branch: &str) -> Result<()> {
anyhow::ensure!(
common.is_dir(),
"Cannot find the git directory of {} (got {})",
repo_path.display(),
common.display()
escape_path(repo_path),
escape_path(&common)
);
let mut admin_dirs = vec![common.clone()];
match std::fs::read_dir(common.join("worktrees")) {
Expand All @@ -418,7 +419,7 @@ pub fn git_branch_is_free(repo_path: &Path, branch: &str) -> Result<()> {
Ok(text) => Ok(text),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(String::new()),
Err(e) => {
Err(anyhow::Error::from(e).context(format!("Cannot read {}", path.display())))
Err(anyhow::Error::from(e).context(format!("Cannot read {}", escape_path(&path))))
}
}
};
Expand All @@ -433,14 +434,14 @@ pub fn git_branch_is_free(repo_path: &Path, branch: &str) -> Result<()> {
{
return Err(in_use(&format!(
"being rebased by the worktree whose git dir is {}",
dir.display()
escape_path(&dir)
)));
}
let bisect = read(dir.join("BISECT_START"))?;
if [branch, full.as_str()].contains(&bisect.trim()) {
return Err(in_use(&format!(
"being bisected by the worktree whose git dir is {}",
dir.display()
escape_path(&dir)
)));
}
}
Expand Down
Loading
Loading