feat(setup-repo): Use URI to generate target directory if not provided #2

Merged
noah.knegt merged 10 commits from optional-target-dir into main 2025-07-03 23:53:23 +02:00
2 changed files with 26 additions and 8 deletions
Showing only changes of commit c0fe91f916 - Show all commits

View File

@@ -14,7 +14,7 @@ struct Args {
/// Target directory for the repository setup /// Target directory for the repository setup
#[arg(short, long)] #[arg(short, long)]
target_dir: String, target_dir: Option<String>,
/// Enable verbose output /// Enable verbose output
#[arg(short, long)] #[arg(short, long)]
@@ -32,28 +32,45 @@ fn main() -> Result<()> {
// Enable or disable colored output // Enable or disable colored output
colored::control::set_override(!args.no_color); colored::control::set_override(!args.no_color);
let target_dir = match args.target_dir {
Some(dir) => dir,
// If a directory is not provided construct one from the last part of the URI.
// This behavior follows git itself
None => {
let start = args.repo_url.rfind("/").unwrap() + 1;
let end = match args.repo_url.rfind(".git")
{
Some(index) => index,
None => args.repo_url.len()
};
args.repo_url[start..end].to_owned()
}
};
// Print verbose information if enabled // Print verbose information if enabled
if args.verbose { if args.verbose {
println!("{}", "Verbose mode enabled".dimmed()); println!("{}", "Verbose mode enabled".dimmed());
println!("{}", format!("Repository URL: {}", args.repo_url).dimmed()); println!("{}", format!("Repository URL: {}", args.repo_url).dimmed());
println!("{}", format!("Target directory: {}", args.target_dir).dimmed()); println!("{}", format!("Target directory: {}", target_dir).dimmed());
} }
println!("{}", "Setting up repository for worktree development".blue()); println!("{}", "Setting up repository for worktree development".blue());
// Clone the repository as a bare clone // Clone the repository as a bare clone
Git::clone_bare_repo(&args.repo_url, &args.target_dir)?; Git::clone_bare_repo(&args.repo_url, &target_dir)?;
// Set up the .git file to point to the .bare directory // Set up the .git file to point to the .bare directory
Git::setup_git_pointer(&args.target_dir)?; Git::setup_git_pointer(&target_dir)?;
// Configure the remote.origin.fetch setting // Configure the remote.origin.fetch setting
Git::configure_remote_fetch(&args.target_dir)?; Git::configure_remote_fetch(&target_dir)?;
// Fetch all remotes // Fetch all remotes
Git::fetch_remotes(&args.target_dir)?; Git::fetch_remotes(&target_dir)?;
println!("{}", "Repository setup complete.".green()); println!("{}", "Repository setup complete.".green());
println!("{}", format!("You can now create worktrees in '{}'.", args.target_dir).green()); println!("{}", format!("You can now create worktrees in '{}'.", target_dir).green());
Ok(()) Ok(())
} }