src-d / go-git

Project has been moved to: https://github.com/go-git/go-git
https://github.com/go-git/go-git
Apache License 2.0
4.91k stars 542 forks source link

Can't delete a local branch: branch not found #1135

Open iyalang opened 5 years ago

iyalang commented 5 years ago

Hi! I'm trying to delete a local branch that is not present on the remote. I get an error branch not found when I run:

branch := "refs/heads/template/test"
err = repo.DeleteBranch(branch)

(using just the name template/test doesn't work either) The branch I'm trying to delete is included in the list of branches.

refs, err := repo.Branches()
err = refs.ForEach(func(ref *plumbing.Reference) error {
    fmt.Println(ref)
    return nil
})

provides the following output:

f2d93bb67ced13936dbbbbfb44502abd42e7df13 refs/heads/global
df46ab083f17051afd6ca20e3ea4bfe01aedbb37 refs/heads/template/test
141f45305380aa0dc9f6802512ea76c5d48a87a1 refs/heads/template/test2

I created cfg := repo.Storer.Config() to check what cfg.Branches contains. Surprisingly, this map has only the following element: &{global origin refs/heads/global 0xc0007fbf80}. So other branches can't be deleted because they are not found in this config. Is there any way to delete the branch? Why is it not added to the config?

sauravhaloi commented 5 years ago

I encountered the same issue with DeleteBranch function. In fact, the same applies to CreateBranch function as well. It throws an error "branch not found".

Here is another way you can delete a branch, which is mentioned in the example programs as well:

    branch := fmt.Sprintf("refs/heads/%s", branchName)
    b := plumbing.ReferenceName(branch)

    err = repo.Storer.RemoveReference(b)
    if err != nil {
        err = fmt.Errorf("error deleting branch: %s", err)
        log.Println(err)
        return
    }