Linting & formatting

This commit is contained in:
2021-11-21 22:04:27 +01:00
parent bbedc9d8a8
commit ca1f649ecf
3 changed files with 68 additions and 57 deletions

View File

@@ -28,12 +28,8 @@ pub enum SubCommand {
Find(Find), Find(Find),
#[clap(about = "Show status of configured repositories")] #[clap(about = "Show status of configured repositories")]
Status(OptionalConfig), Status(OptionalConfig),
#[clap( #[clap(visible_alias = "wt", about = "Manage worktrees")]
visible_alias = "wt",
about = "Manage worktrees"
)]
Worktree(Worktree), Worktree(Worktree),
} }
#[derive(Parser)] #[derive(Parser)]
@@ -81,7 +77,6 @@ pub struct WorktreeActionArgs {
pub name: String, pub name: String,
} }
pub fn parse() -> Opts { pub fn parse() -> Opts {
Opts::parse() Opts::parse()
} }

View File

@@ -75,7 +75,7 @@ fn sync_trees(config: Config) {
if repo.worktree_setup && !actual_git_directory.exists() { if repo.worktree_setup && !actual_git_directory.exists() {
print_repo_error( print_repo_error(
&repo.name, &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); process::exit(1);
} }
@@ -88,43 +88,39 @@ fn sync_trees(config: Config) {
process::exit(1); process::exit(1);
}, },
)); ));
} else { } else if matches!(&repo.remotes, None) || repo.remotes.as_ref().unwrap().is_empty() {
if matches!(&repo.remotes, None) print_repo_action(
|| repo.remotes.as_ref().unwrap().len().clone() == 0 &repo.name,
{ "Repository does not have remotes configured, initializing new",
print_repo_action( );
&repo.name, repo_handle = match init_repo(&repo_path, repo.worktree_setup) {
"Repository does not have remotes configured, initializing new", Ok(r) => {
); print_repo_success(&repo.name, "Repository created");
repo_handle = match init_repo(&repo_path, repo.worktree_setup) { Some(r)
Ok(r) => { }
print_repo_success(&repo.name, "Repository created"); Err(e) => {
Some(r) print_repo_error(
} &repo.name,
Err(e) => { &format!("Repository failed during init: {}", e),
print_repo_error( );
&repo.name, None
&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 { if let Some(remotes) = &repo.remotes {
let repo_handle = repo_handle.unwrap_or_else(|| { let repo_handle = repo_handle.unwrap_or_else(|| {
@@ -602,7 +598,7 @@ pub fn run() {
let toml = toml::to_string(&config).unwrap(); let toml = toml::to_string(&config).unwrap();
print!("{}", toml); print!("{}", toml);
}, }
cmd::SubCommand::Worktree(args) => { cmd::SubCommand::Worktree(args) => {
let dir = match std::env::current_dir() { let dir = match std::env::current_dir() {
Ok(d) => d, Ok(d) => d,
@@ -618,14 +614,21 @@ pub fn run() {
Ok(r) => r, Ok(r) => r,
Err(e) => { Err(e) => {
match e.kind { 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)), _ => print_error(&format!("Error opening repo: {}", e)),
} }
process::exit(1); process::exit(1);
} }
}; };
let worktrees = repo.worktrees().unwrap().iter().map(|e| e.unwrap()).collect::<String>(); let worktrees = repo
.worktrees()
.unwrap()
.iter()
.map(|e| e.unwrap())
.collect::<String>();
if worktrees.contains(&action_args.name) { if worktrees.contains(&action_args.name) {
print_error("Worktree directory already exists"); print_error("Worktree directory already exists");
process::exit(1); process::exit(1);
@@ -638,7 +641,7 @@ pub fn run() {
process::exit(1); process::exit(1);
} }
}; };
}, }
cmd::WorktreeAction::Delete(action_args) => { cmd::WorktreeAction::Delete(action_args) => {
let worktree_dir = dir.join(&action_args.name); let worktree_dir = dir.join(&action_args.name);
if !worktree_dir.exists() { if !worktree_dir.exists() {
@@ -650,15 +653,17 @@ pub fn run() {
Err(e) => { Err(e) => {
print_error(&format!("Error opening repo: {}", e)); print_error(&format!("Error opening repo: {}", e));
process::exit(1); process::exit(1);
}, }
}; };
let status = get_repo_status(&repo); 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!"); print_error("Changes found in worktree, refusing to delete!");
process::exit(1); 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() { match branch.upstream() {
Ok(remote_branch) => { Ok(remote_branch) => {
let (ahead, behind) = repo 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)); print_error(&format!("Branch {} is not in line with remote branch, refusing to delete worktree!", &action_args.name));
process::exit(1); process::exit(1);
} }
}, }
Err(_) => { Err(_) => {
print_error(&format!("No remote tracking branch for branch {} found, refusing to delete worktree!", &action_args.name)); print_error(&format!("No remote tracking branch for branch {} found, refusing to delete worktree!", &action_args.name));
process::exit(1); process::exit(1);
}, }
} }
match std::fs::remove_dir_all(&worktree_dir) { match std::fs::remove_dir_all(&worktree_dir) {
Ok(_) => print_success(&format!("Worktree {} deleted", &action_args.name)), Ok(_) => print_success(&format!("Worktree {} deleted", &action_args.name)),
Err(e) => { Err(e) => {
print_error(&format!("Error deleting {}: {}", &worktree_dir.display(), e)); print_error(&format!(
"Error deleting {}: {}",
&worktree_dir.display(),
e
));
process::exit(1); 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(); branch.delete().unwrap();
}, }
} }
}, }
} }
} }

View File

@@ -274,7 +274,11 @@ pub fn get_repo_status(repo: &git2::Repository) -> RepoStatus {
}; };
let statuses = repo 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(); .unwrap();
let changes = match statuses.is_empty() { let changes = match statuses.is_empty() {