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 541 forks source link

Unable to pull from branch #1271

Open Raffo opened 4 years ago

Raffo commented 4 years ago

I'm trying to open a local repo and pull it. I want to checkout a specific branch identified by the var branch. I'm doing the following:

dir, err := os.Getwd()
    if err != nil {
        logrus.Errorf("error getting current directory: %v", err)
    }

    currentUser, err := user.Current()
    if err != nil {
        logrus.Errorf("error getting current user: %v", err)
    }

    sshAuth, err := ssh.NewPublicKeysFromFile("git", currentUser.HomeDir+"/.ssh/id_rsa", "")
    if err != nil {
        logrus.Errorf("error getting public key: %v", err)
    }

    r, err := git.PlainOpenWithOptions(dir, &git.PlainOpenOptions{DetectDotGit: true})
    if err != nil {
        logrus.Errorf("error opening repo: %v", err)
    }
    w, err := r.Worktree()
    if err != nil {
        logrus.Errorf("error getting worktree: %v", err)
    }
    err = w.Checkout(&git.CheckoutOptions{
        Branch: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branch)),
    })
    if err != nil {
        logrus.Errorf("error checking out repo: %v", err)
    }

    err = w.Pull(&git.PullOptions{ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branch)), Auth: sshAuth})
    if err != nil {
        logrus.Errorf("error pulling repo: %v", err)
    }

I keep getting errornon-fast-forward update when trying to pull. From a debug session, I saw that in https://github.com/src-d/go-git/blob/master/worktree.go#L89 master is actually returned and this causes issues when determining if we are in a fast forward situation.

Can someone help debug this issue and/or suggest how to proceed?