git-co: batch Levenshtein calculations into a single python3 call#12
Merged
git-co: batch Levenshtein calculations into a single python3 call#12
Conversation
Co-authored-by: mlutonsky <423848+mlutonsky@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Refactor Levenshtein distance calculations for branches
git-co: batch Levenshtein calculations into a single python3 call
Mar 14, 2026
mlutonsky
approved these changes
Mar 14, 2026
There was a problem hiding this comment.
Pull request overview
Optimizes git-co’s fuzzy branch matching by batching Levenshtein distance computations into a single Python invocation instead of spawning one python3 process per branch.
Changes:
- Removed the per-branch
_levenshtein_distance()helper. - Rewrote
_find_branches_by_levenshtein()to compute/sort distances in onepython3 -ccall fed by stdin.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+25
to
30
| echo "$all_branches" | python3 -c ' | ||
| import sys | ||
|
|
||
| def levenshtein(s1, s2): | ||
| s1, s2 = s1.lower(), s2.lower() | ||
| if len(s1) < len(s2): |
Comment on lines
+53
to
+55
| for dist, branch in results: | ||
| if dist <= max_dist: | ||
| print(branch) |
| for dist, branch in results: | ||
| if dist <= max_dist: | ||
| print(branch) | ||
| ' "$input" "$max_distance" |
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.
_find_branches_by_levenshtein()spawned onepython3process per branch — O(n) process forks for n branches.Changes
_levenshtein_distance()entirely_find_branches_by_levenshtein()to pipe all branch names to a singlepython3 -c '...'invocation that computes, sorts, and filters all distances at onceNote: The
python3 - <<'PYEOF'pattern from the issue spec is broken — the heredoc wins the stdin race and the piped branch data is silently discarded. Usingpython3 -c '...'keeps the script inline and leaves stdin available for the pipe.Original prompt
git-co: batch Levenshtein calculations into a single python3 call
File to modify:
git-coProblem
_find_branches_by_levenshtein()calls_levenshtein_distance()once per branch in a while loop. Each call spawns a newpython3process, which is expensive — on a repo with 50 branches this spawns 50 Python processes.Fix
Rewrite
_levenshtein_distance()and_find_branches_by_levenshtein()so that all branch distances are computed in a singlepython3invocation.Replace both functions with the following:
Also remove the now-unused
_levenshtein_distance()function entirely.Update the call site in
_find_similar_branches()— the call_find_branches_by_levenshtein "$input" "$all_branches" "$max_distance"remains the same, no changes needed there.No other changes.
The following is the prior conversation context from the user's chat exploration (may be truncated):
User: @copilot Accepted Confirmation: Are you sure?
User: @copilot Accepted Confirmation: Are you sure?
Assistant: The Copilot coding agent is now working on all the pull requests! Here's the full tracking list:
All 10 are now running. You can track progress at github.com/mlutonsky/utils/pulls — PRs will appear as the agent completes each one!
User: also re-create pull request for the "Fix git fetch --prune error handling in git-branch-clean (#10)"
Assistant: It seems the agent didn't return a task URL this time. Let me retry:
Assistant: I'm waiting on a tool call confirmation to proceed — please accept it and I'll get that PR queued up right away.
User: @copilot Accepted Confirmation: Are you sure?
Assistant: The agent is on it!
You can track it at github.com/mlutonsky/utils/pulls once the agent opens the PR.
User: @copilot Accepted Con...
This pull request was created from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.