简体   繁体   中英

delete a reference in pygit2

I am using the library pygit2 to implement a git module on my project. Right now i'm blocked by these 2 scenarios : - the deletion of a tag than a push - the deletion of a branch than a push

this is what i'm trying to do :

>>> branch = repo.lookup_branch('origin/1249something', pygit2.GIT_BRANCH_REMOTE)
>>> branch
<_pygit2.Branch object at 0xb7350270>
>>> branch.delete()
>>> branch_to_push = 'refs/remotes/origin/1249something'
>>> pusher = pygit2.Signature('test TESTSSSSSSSS', 'test-test1@site.com')
>>> message = "just test message"
>>> remote_repo = repo.remotes[0]
>>> remote_repo.push(branch_to_push, pusher, message)

and this is the result i get :

File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pygit2/remote.py", line 358, in push
    check_error(err)
  File "/usr/local/lib/python2.7/dist-packages/pygit2/errors.py", line 56, in check_error
    raise GitError(message)
_pygit2.GitError: src refspec 'refs/remotes/origin/1249something' does not match any existing object

The name refs/remotes/origin/1249something is not the branch's name, but your local idea of what the remote's branch looks like. Assuming a default fetch refspecs (ie you did git remote add origin <url> ) the branch on the remote is called refs/heads/1249something . When you look up origin/1249something , that's not a branch on the remote, but a remote-tracking branch, it serves as a mirror to the state of that branch the last time you ran a fetch.

Deleting your remote-tracking branch locally is not going to affect the remote repository in any way, and the next time you perform a fetch, it's going to show up again.

If you'd like to remove the branch from the remote, you'll need to specify a refspec which says this. This is done by providing en empty local part, in your case, you'd do it by providing :refs/heads/1249something as the refspec to push. The empty part left of the colon means that you want the reference on the right to be deleted.

remote = repo.remotes[0]
remote.push(':refs/heads/1249something', pusher, message)

will delete that branch from the remote.

I would suggest reading Working with Remotes to cement your understanding of how remotes and remote-tracking branches relate to the remote repository.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM