refactor(mcp): reduce complexity of find_merge_base in src/mcp.rs#1115
Draft
github-actions[bot] wants to merge 1 commit into
Draft
refactor(mcp): reduce complexity of find_merge_base in src/mcp.rs#1115github-actions[bot] wants to merge 1 commit into
github-actions[bot] wants to merge 1 commit into
Conversation
…pers Extract three focused helper functions to break up the monolithic find_merge_base method (cognitive complexity: 18 → ~4): - build_merge_base_candidates: discovers candidate remote refs by running 'git symbolic-ref refs/remotes/origin/HEAD' and appending common defaults (origin/main, origin/master) as fallbacks. - try_candidates_for_merge_base: iterates the candidates, tries 'git merge-base HEAD <ref>' for each, and tracks whether any ref exists in the repo even when merge-base itself fails. - try_root_commit_fallback: handles the single-commit-repo edge case — runs 'git rev-list --max-parents=0 HEAD' and 'git rev-list --count HEAD' and returns the root SHA only when the repo has exactly one commit. find_merge_base itself is now a concise four-step sequencing function that delegates each phase to a named helper, making the overall strategy immediately readable. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was complex
find_merge_baseinsrc/mcp.rshad a cognitive complexity of 18 (against a threshold of 10). The function handled three distinct concerns in a single deeply-nested body:git symbolic-ref refs/remotes/origin/HEADand building the candidate listgit merge-base, tracking whether any ref existedgit rev-list --max-parents=0and--count HEADwith extra nesting to validate the "truly single-commit repo" conditionThe four nested control-flow levels (loop → if-let → if-success → if-non-empty) plus the two-level fallback block pushed the complexity well above threshold.
What changed
Extracted three focused module-level async helper functions, placed in a new
// Git merge-base helperssection:build_merge_base_candidatesgit symbolic-ref, parses the result, appends common defaultstry_candidates_for_merge_basegit merge-base, tracksfound_remote_reftry_root_commit_fallbackfind_merge_baseitself is now a four-line sequencing function:Before / After complexity
find_merge_basebuild_merge_base_candidatestry_candidates_for_merge_basetry_root_commit_fallbackVerification
cargo test)cargo clippy --all-targets --all-features— clean, no new warningsfind_merge_baseno longer appears in the cognitive-complexity report at threshold 10found_remote_reftracking, and both error messages are preserved verbatim in the extracted helpersWarning
Firewall blocked 2 domains
The following domains were blocked by the firewall during workflow execution:
spsprodeus21.vssps.visualstudio.comspsprodweu4.vssps.visualstudio.comSee Network Configuration for more information.