diff --git a/setup-repo/src/main.rs b/setup-repo/src/main.rs index 02269a6..d7def01 100644 --- a/setup-repo/src/main.rs +++ b/setup-repo/src/main.rs @@ -14,7 +14,7 @@ struct Args { /// Target directory for the repository setup #[arg(short, long)] - target_dir: String, + target_dir: Option, /// Enable verbose output #[arg(short, long)] @@ -32,28 +32,45 @@ fn main() -> Result<()> { // 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: {}", args.target_dir).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, &args.target_dir)?; + Git::clone_bare_repo(&args.repo_url, &target_dir)?; // 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 - Git::configure_remote_fetch(&args.target_dir)?; + Git::configure_remote_fetch(&target_dir)?; // Fetch all remotes - Git::fetch_remotes(&args.target_dir)?; + Git::fetch_remotes(&target_dir)?; 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(()) }