go-git icon indicating copy to clipboard operation
go-git copied to clipboard

Unable to pull from branch

Open Raffo opened this issue 5 years ago • 0 comments

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?

Raffo avatar Jan 03 '20 18:01 Raffo