feat(worktree): add managed workspace cloning (#30117)

This commit is contained in:
Dax 2026-05-31 11:57:44 -04:00 committed by GitHub
commit 5661af2034
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 2374 additions and 28 deletions

View file

@ -0,0 +1,13 @@
[package]
name = "worktree-cli"
version.workspace = true
edition.workspace = true
license.workspace = true
[[bin]]
name = "worktree"
path = "src/main.rs"
[dependencies]
clap.workspace = true
worktree = { path = "../core" }

View file

@ -0,0 +1,79 @@
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use worktree::{Create, Link, Manager};
#[derive(Parser)]
#[command(name = "worktree")]
struct Cli {
#[arg(long, hide = true)]
database: Option<PathBuf>,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
Create {
from: Option<PathBuf>,
#[arg(long)]
name: Option<String>,
#[arg(long)]
into: Option<PathBuf>,
},
Remove {
at: PathBuf,
},
Link {
at: PathBuf,
#[arg(long)]
to: Option<PathBuf>,
},
Children {
of: PathBuf,
},
Ancestors {
of: PathBuf,
},
}
fn main() {
if let Err(error) = run() {
eprintln!("worktree: {error}");
std::process::exit(1);
}
}
fn run() -> worktree::Result<()> {
let cli = Cli::parse();
let mut manager = match cli.database {
Some(path) => Manager::open(path)?,
None => Manager::open_default()?,
};
match cli.command {
Command::Create { from, name, into } => {
println!(
"{}",
manager
.create(Create {
from: from.unwrap_or(std::env::current_dir()?),
name,
into,
})?
.display()
);
}
Command::Remove { at } => manager.remove(at)?,
Command::Link { at, to } => manager.link(Link { at, to })?,
Command::Children { of } => {
for path in manager.children(of)? {
println!("{}", path.display());
}
}
Command::Ancestors { of } => {
for path in manager.ancestors(of)? {
println!("{}", path.display());
}
}
}
Ok(())
}