3 ms·
This isn't asking someone else to make this work, it's more of a caution to convince folks like me to just use "import git" rather than pygit2: So something li
by bloopernova 3y ago
This isn't asking someone else to make this work, it's more of a caution to convince folks like me to just use "import git" rather than pygit2:
So something like this was what I expected to work, but leaves the repo in detached head state:
import pygit2
def checkout_branch(path, branch_name):
repo = pygit2.Repository(path)
branch_ref = repo.lookup_reference(f"refs/remotes/origin/{branch_name}")
print(f"{branch_ref.name}")
repo.checkout(branch_ref)
The branch_ref.name prints "refs/remotes/origin/test" but git status says "HEAD detached at origin/test"
So I'm probably feeding the wrong thing into repo.checkout, but I'm honestly not sure what else it should be.
Funnily enough, git itself tries to do the right thing if pulled in a detached head state:
From https://github.com/testorg/example
* [new branch] test -> origin/test
You are not currently on a branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
- deleted 3y ago[deleted]
- bloopernova 3y agoHa, and of course just messing around gets me something that actually works. There always seems to be just one more stackoverflow thread to read that has the real answer: https://stackoverflow.com/questions/68435607/how-to-clone-master-and-checkout-another-branch-in-pygit2-without-detached-head https://stackoverflow.com/questions/68435607/how-to-clone-ma... (found via Kagi which I wasn't using before, and the search "pygit2 detached head") def checkout_branch(path, branch_name): repo = pygit2.Repository(path) main_branch = repo.lookup_branch("main") print(f"Main branch upstream: {main_branch.upstream_name}") if branch_name not in repo.branches.local: print(f"Branch {branch_name} not found in local branches") remote_branch = "origin/" + branch_name if remote_branch not in repo.branches.remote: raise SystemExit(f"Branch {remote_branch} not found in remote branches") (commit, remote_ref) = repo.resolve_refish(remote_branch) repo.create_reference("refs/heads/" + branch_name, commit.hex) branch = repo.lookup_branch(branch_name) print(f"Branch name: {branch.name}") repo.checkout(branch) print(f"Is branch head? {branch.is_head()}") (commit, branch_remote) = repo.resolve_refish("origin/" + branch_name) print(f"Remote branch: {branch_remote.name}") branch.upstream = branch_remote With git reflog telling me the right thing: d44aedc (HEAD -> test, origin/test) HEAD@{0}: checkout: moving from main to test And git push has the remote branch already set. I wish there was a pair programmer AI that you had to explain stuff to. That would enable the "by explaining it, I solved it" phenomenon.
- Izkata 3y ago> I wish there was a pair programmer AI that you had to explain stuff to. That would enable the "by explaining it, I solved it" phenomenon. It's called rubber duck debugging, named for having an actual rubber duck at your desk you'd talk to.