From 92ebd453076015993d25102d262a4821fe416e06 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 16 Jul 2024 19:59:11 +0100 Subject: [PATCH] refactor: Reduce cognitive complexity of 'validate_position' Closes kemitix/git-next#83 --- crates/git/src/validation/positions.rs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/crates/git/src/validation/positions.rs b/crates/git/src/validation/positions.rs index 84dd5a1..689a56d 100644 --- a/crates/git/src/validation/positions.rs +++ b/crates/git/src/validation/positions.rs @@ -12,7 +12,6 @@ pub struct Positions { pub dev_commit_history: Vec, } -#[allow(clippy::cognitive_complexity)] // TODO: (#83) reduce complexity pub fn validate_positions( open_repository: &dyn git::repository::OpenRepositoryLike, repo_details: &git::RepoDetails, @@ -21,39 +20,31 @@ pub fn validate_positions( let main_branch = repo_config.branches().main(); let next_branch = repo_config.branches().next(); let dev_branch = repo_config.branches().dev(); - tracing::debug!(%main_branch, %next_branch, %dev_branch, "branches"); // Collect Commit Histories for `main`, `next` and `dev` branches open_repository.fetch()?; - tracing::debug!("fetch okay"); let commit_histories = get_commit_histories(open_repository, &repo_config)?; - tracing::debug!(?commit_histories, "get commit histories okay"); // branch tips let main = commit_histories.main.first().cloned().ok_or_else(|| { Error::NonRetryable(format!("Branch has no commits: {}", main_branch)) })?; - tracing::debug!("main branch okay"); let next = commit_histories.next.first().cloned().ok_or_else(|| { Error::NonRetryable(format!("Branch has no commits: {}", next_branch)) })?; - tracing::debug!("next branch okay"); let dev = commit_histories .dev .first() .cloned() .ok_or_else(|| Error::NonRetryable(format!("Branch has no commits: {}", dev_branch)))?; - tracing::debug!("dev branch okay"); // Validations: // Dev must be on main branch, else the USER must rebase it if is_not_based_on(&commit_histories.dev, &main) { - tracing::warn!("Dev '{dev_branch}' not based on main '{main_branch}' - user must rebase",); return Err(Error::NonRetryable(format!( "Branch '{}' not based on '{}'", dev_branch, main_branch ))); } - tracing::debug!("dev based on main okay"); // verify that next is on main or at most one commit on top of main, else reset it back to main if is_not_based_on( commit_histories @@ -68,14 +59,11 @@ pub fn validate_positions( tracing::info!("Main not on same commit as next, or it's parent - resetting next to main",); return reset_next_to_main(open_repository, repo_details, &main, &next, &next_branch); } - tracing::debug!("next on or near main okay"); // verify that next is an ancestor of dev, else reset it back to main if is_not_based_on(&commit_histories.dev, &next) { tracing::info!("Next is not an ancestor of dev - resetting next to main"); return reset_next_to_main(open_repository, repo_details, &main, &next, &next_branch); } - tracing::debug!("dev based on next okay"); - Ok(git::validation::positions::Positions { main, next,