Files
repo-utils/setup-repo/src/main.rs

77 lines
2.1 KiB
Rust

use anyhow::Result;
use clap::Parser;
use colored::*;
use git::Git;
/// Tool to set up Git repositories for worktree development
#[derive(Parser, Debug)]
#[command(author, version, about)]
struct Args {
/// Repository URL to clone
#[arg(short, long)]
repo_url: String,
/// Target directory for the repository setup
#[arg(short, long)]
target_dir: Option<String>,
/// Enable verbose output
#[arg(short, long)]
verbose: bool,
/// Disable colored output
#[arg(long)]
no_color: bool,
}
fn main() -> Result<()> {
// Parse arguments
let args = Args::parse();
// Enable or disable colored output
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
if args.verbose {
println!("{}", "Verbose mode enabled".dimmed());
println!("{}", format!("Repository URL: {}", args.repo_url).dimmed());
println!("{}", format!("Target directory: {}", target_dir).dimmed());
}
println!("{}", "Setting up repository for worktree development".blue());
// Clone the repository as a bare clone
Git::clone_bare_repo(&args.repo_url, &target_dir)?;
// Set up the .git file to point to the .bare directory
Git::setup_git_pointer(&target_dir)?;
// Configure the remote.origin.fetch setting
Git::configure_remote_fetch(&target_dir)?;
// Fetch all remotes
Git::fetch_remotes(&target_dir)?;
println!("{}", "Repository setup complete.".green());
println!("{}", format!("You can now create worktrees in '{}'.", target_dir).green());
Ok(())
}