Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: go-git clone is incomplete and Why do I need to pull twice to pull the latest code? #1083

Open
SunYonge opened this issue Apr 25, 2024 · 0 comments

Comments

@SunYonge
Copy link

SunYonge commented Apr 25, 2024

In addition, sometimes my network is relatively poor, which will cause errors when I use go-git (incomplete warehouse pull), prompting error: reference not found, or some files indicate that they have not been cloned, only if I completely delete the local warehouse generated by using go-git, and execute go-git clone again, the clone can proceed normally. How can I solve this problem?
Our project has to use go-git/v5, but these two problems have been bothering me for a long time, help me, homie.


}
if err != nil {
	runLog.Error("Clone project failed")
	return -1, err
} else {
	runLog.Info("git clone done.")
}

// 获取远程分支的引用
remoteRef, err := repo.Reference(plumbing.ReferenceName("refs/remotes/origin/"+r.Branch), true)
if err != nil {
	runLog.Errorf("get repo remote ref error, err: %v", err)
	return -1, err
} else {
	runLog.Info("get remoteRef done.")
}

// 创建本地分支引用
localRef := plumbing.NewHashReference(plumbing.ReferenceName("refs/heads/"+r.Branch), remoteRef.Hash())
runLog.Info("create localRef done.")

w, err := repo.Worktree()
if err != nil {
	runLog.Errorf("worktree error: %v", err)
	return -1, err
}

// 拉取最新的代码
err = w.Pull(&git.PullOptions{
	ReferenceName: localRef.Name(),
	Auth:          pk,
})
if err != nil && err != git.NoErrAlreadyUpToDate {
	runLog.Errorf("Failed to pull latest code: %v", err)
} else {
	runLog.Info("git pull done.")
}

// 更新本地分支引用
err = repo.Storer.SetReference(localRef)
if err != nil {
	runLog.Errorf("Failed to set local reference: %v", err)
} else {
	runLog.Info("update localRef done.")
}

// 检出本地分支
err = w.Checkout(&git.CheckoutOptions{
	Branch: plumbing.ReferenceName("refs/heads/" + r.Branch),
	Force:  true,
})
if err != nil {
	runLog.Errorf("Failed to checkout branch: %v", err)
	//return -1, err
}
runLog.Infof("Fetch and checkout complete.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant