-
Notifications
You must be signed in to change notification settings - Fork 38
fix: preserve branch names with "/" in switch_branch #4486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v3
Are you sure you want to change the base?
Changes from all commits
f0dfac2
86ad073
5f2f717
1e7d532
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -268,8 +268,12 @@ def switch_branch(path, branch): | |
| except CalledProcessError: | ||
| cur_branch = check_output('git rev-parse HEAD'.split(), cwd=path).decode().strip() | ||
| if cur_branch != branch: | ||
| branches = check_output('git ls-remote --heads origin'.split(), cwd=path) | ||
| branches = [line.split("/")[-1] for line in branches.decode().strip().split("\n")] | ||
| raw_branches = check_output('git ls-remote --heads origin'.split(), cwd=path) | ||
| branches = [] | ||
| for line in raw_branches.decode().strip().split("\n"): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The whitespace check on the next line and the value stored two lines down don't match: the check looks at >>> line = "def456\trefs/heads/main\r"
>>> bool(line.strip()) # passes the check below
True
>>> line.split("refs/heads/", 1)[-1]
'main\r' # stored like this, and 'main\r' != 'main'So the branch is reported as missing even though it's right there — the same wrong outcome this PR is fixing, except a stray The If stray whitespace is worth guarding against, the stored value needs cleaning too — |
||
| if not line.strip(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flip side of the comment above: if stray whitespace can't happen, this check isn't needed at all.
So it's one or the other: either clean the stored value (comment above), or drop this check plus the branches = [line.split("refs/heads/", 1)[-1] for line in check_output(...).decode().strip().split("\n")]Right now we pay for the check but don't get the protection. |
||
| continue | ||
| branches.append(line.split("refs/heads/", 1)[-1]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
>>> "some unexpected output".split("refs/heads/", 1)[-1]
'some unexpected output'That goes into Skipping such lines outright makes it obvious: if "refs/heads/" not in line:
continue
branches.append(line.split("refs/heads/", 1)[1])Worth doing because the output isn't fixed forever: git 2.46 renamed |
||
|
|
||
| if branch in branches: | ||
| print(f'Switch to branch: {branch} (from {cur_branch})') | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stepping back: git can answer "does this branch exist?" on its own, so none of this listing and splitting is really needed.
--exit-codereturns 0 if the branch exists and 2 if it doesn't (I checked). That replaces the list, the loop and thein branchestest, and there's no text to parse, so the bug this PR fixes can't happen in the first place.It's also a lot less work: right now we ask the server for the full list of branches once per dependency (13 in
.gitmodules), and for a repo likepercona/grafanathat's thousands of branch names downloaded to check one.Not blocking — just worth knowing the whole block could go away.