From ca1f649ecff3ef5ac201cc28f4db88fe389360b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Sun, 21 Nov 2021 22:04:27 +0100 Subject: [PATCH] Linting & formatting --- src/cmd.rs | 7 +--- src/lib.rs | 112 +++++++++++++++++++++++++++++----------------------- src/repo.rs | 6 ++- 3 files changed, 68 insertions(+), 57 deletions(-) diff --git a/src/cmd.rs b/src/cmd.rs index bb5c9de..a79ec91 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -28,12 +28,8 @@ pub enum SubCommand { Find(Find), #[clap(about = "Show status of configured repositories")] Status(OptionalConfig), - #[clap( - visible_alias = "wt", - about = "Manage worktrees" - )] + #[clap(visible_alias = "wt", about = "Manage worktrees")] Worktree(Worktree), - } #[derive(Parser)] @@ -81,7 +77,6 @@ pub struct WorktreeActionArgs { pub name: String, } - pub fn parse() -> Opts { Opts::parse() } diff --git a/src/lib.rs b/src/lib.rs index bbeb8c0..dc847a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,7 +75,7 @@ fn sync_trees(config: Config) { if repo.worktree_setup && !actual_git_directory.exists() { print_repo_error( &repo.name, - &format!("Repo already exists, but is not using a worktree setup"), + "Repo already exists, but is not using a worktree setup", ); process::exit(1); } @@ -88,43 +88,39 @@ fn sync_trees(config: Config) { process::exit(1); }, )); - } else { - if matches!(&repo.remotes, None) - || repo.remotes.as_ref().unwrap().len().clone() == 0 - { - print_repo_action( - &repo.name, - "Repository does not have remotes configured, initializing new", - ); - repo_handle = match init_repo(&repo_path, repo.worktree_setup) { - Ok(r) => { - print_repo_success(&repo.name, "Repository created"); - Some(r) - } - Err(e) => { - print_repo_error( - &repo.name, - &format!("Repository failed during init: {}", e), - ); - None - } + } else if matches!(&repo.remotes, None) || repo.remotes.as_ref().unwrap().is_empty() { + print_repo_action( + &repo.name, + "Repository does not have remotes configured, initializing new", + ); + repo_handle = match init_repo(&repo_path, repo.worktree_setup) { + Ok(r) => { + print_repo_success(&repo.name, "Repository created"); + Some(r) + } + Err(e) => { + print_repo_error( + &repo.name, + &format!("Repository failed during init: {}", e), + ); + None } - } else { - let first = repo.remotes.as_ref().unwrap().first().unwrap(); - - match clone_repo(first, &repo_path, repo.worktree_setup) { - Ok(_) => { - print_repo_success(&repo.name, "Repository successfully cloned"); - } - Err(e) => { - print_repo_error( - &repo.name, - &format!("Repository failed during clone: {}", e), - ); - continue; - } - }; } + } else { + let first = repo.remotes.as_ref().unwrap().first().unwrap(); + + match clone_repo(first, &repo_path, repo.worktree_setup) { + Ok(_) => { + print_repo_success(&repo.name, "Repository successfully cloned"); + } + Err(e) => { + print_repo_error( + &repo.name, + &format!("Repository failed during clone: {}", e), + ); + continue; + } + }; } if let Some(remotes) = &repo.remotes { let repo_handle = repo_handle.unwrap_or_else(|| { @@ -602,7 +598,7 @@ pub fn run() { let toml = toml::to_string(&config).unwrap(); print!("{}", toml); - }, + } cmd::SubCommand::Worktree(args) => { let dir = match std::env::current_dir() { Ok(d) => d, @@ -618,14 +614,21 @@ pub fn run() { Ok(r) => r, Err(e) => { match e.kind { - RepoErrorKind::NotFound => print_error(&"Current directory does not contain a worktree setup"), + RepoErrorKind::NotFound => print_error( + "Current directory does not contain a worktree setup", + ), _ => print_error(&format!("Error opening repo: {}", e)), } process::exit(1); } }; - let worktrees = repo.worktrees().unwrap().iter().map(|e| e.unwrap()).collect::(); + let worktrees = repo + .worktrees() + .unwrap() + .iter() + .map(|e| e.unwrap()) + .collect::(); if worktrees.contains(&action_args.name) { print_error("Worktree directory already exists"); process::exit(1); @@ -638,7 +641,7 @@ pub fn run() { process::exit(1); } }; - }, + } cmd::WorktreeAction::Delete(action_args) => { let worktree_dir = dir.join(&action_args.name); if !worktree_dir.exists() { @@ -650,15 +653,17 @@ pub fn run() { Err(e) => { print_error(&format!("Error opening repo: {}", e)); process::exit(1); - }, + } }; let status = get_repo_status(&repo); - if let Some(_) = status.changes { + if status.changes.is_some() { print_error("Changes found in worktree, refusing to delete!"); process::exit(1); } - let mut branch = repo.find_branch(&action_args.name, git2::BranchType::Local).unwrap(); + let mut branch = repo + .find_branch(&action_args.name, git2::BranchType::Local) + .unwrap(); match branch.upstream() { Ok(remote_branch) => { let (ahead, behind) = repo @@ -672,24 +677,31 @@ pub fn run() { print_error(&format!("Branch {} is not in line with remote branch, refusing to delete worktree!", &action_args.name)); process::exit(1); } - }, + } Err(_) => { print_error(&format!("No remote tracking branch for branch {} found, refusing to delete worktree!", &action_args.name)); process::exit(1); - }, + } } match std::fs::remove_dir_all(&worktree_dir) { Ok(_) => print_success(&format!("Worktree {} deleted", &action_args.name)), Err(e) => { - print_error(&format!("Error deleting {}: {}", &worktree_dir.display(), e)); + print_error(&format!( + "Error deleting {}: {}", + &worktree_dir.display(), + e + )); process::exit(1); - }, + } } - repo.find_worktree(&action_args.name).unwrap().prune(None).unwrap(); + repo.find_worktree(&action_args.name) + .unwrap() + .prune(None) + .unwrap(); branch.delete().unwrap(); - }, + } } - }, + } } } diff --git a/src/repo.rs b/src/repo.rs index 7e5484d..4933fe1 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -274,7 +274,11 @@ pub fn get_repo_status(repo: &git2::Repository) -> RepoStatus { }; let statuses = repo - .statuses(Some(git2::StatusOptions::new().include_ignored(false).include_untracked(true))) + .statuses(Some( + git2::StatusOptions::new() + .include_ignored(false) + .include_untracked(true), + )) .unwrap(); let changes = match statuses.is_empty() {