Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

repo.Reference(plumbing.ReferenceName("refs/remotes/origin/branch"), true) results in "reference not found" error #676

Closed
dbriesz opened this issue Dec 4, 2017 · 1 comment

Comments

@dbriesz
Copy link

dbriesz commented Dec 4, 2017

Steps to reproduce:

• Create new repository (git.Init)
• Add new remote (repo.CreateRemote)
• Fetch https://github.com/src-d/go-git.git/fix/checkout-empty-repo -> works
• Pull https://github.com/src-d/go-git.git/fix/checkout-empty-repo -> results in "reference not found error", but can print reference hash, type, etc.

Example code:

    // Initialize non bare repo
    r, err := git.PlainInit(path, false)
    if err != git.ErrRepositoryAlreadyExists && err != nil {
        log.Fatal(err)
    }

    // Open repo, if initialized
    r, err = git.PlainOpen(path)
    if err != nil {
        log.Println(err)
    }

    // Add a upstream remote
    _, err = r.CreateRemote(&config.RemoteConfig{
        Name: "upstream",
        URLs: []string{"https://github.com/src-d/go-git.git"},
    })
    if err != nil {
        switch err {
        case git.ErrRemoteNotFound:
            log.Fatal("remote not found")
        default:
            log.Println(err)
        }
    }

    // Get the working directory for the repository
    w, err := r.Worktree()
    if err != nil {
        log.Fatal(err)
    }

    // Pull using default options
    log.Println("Pulling https://github.com/src-d/go-git.git")

    err = w.Pull(&git.PullOptions{
        RemoteName:    "upstream",
        ReferenceName: plumbing.ReferenceName("refs/remotes/fix/checkout-empty-repo"),
    })
    if err != nil {
        switch err {
        case transport.ErrEmptyRemoteRepository:
            log.Fatal("upstream repository is empty")
        default:
            log.Println(err)
        }
    }
@orirawlings orirawlings changed the title repo.Reference(plumbing.ReferenceName("refs/remotes/origin/branch", true)) results in "reference not found" error repo.Reference(plumbing.ReferenceName("refs/remotes/origin/branch"), true) results in "reference not found" error Dec 5, 2017
@orirawlings
Copy link
Contributor

Hey @dbriesz,

When specifying your PullOptions.ReferenceName you need to use the name of the reference as it is stored in the remote (ex. something under refs/heads/*). So to fix this code you would do:

err = w.Pull(&git.PullOptions{
    RemoteName:    "upstream",
    ReferenceName: plumbing.ReferenceName("refs/heads/fix/checkout-empty-repo"),
})

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants