[英]Unable to git clone using go language
我正在尝试使用下面的 go-lang 代码片段克隆 git/bitbucket 存储库,但它不起作用,我也看不到任何错误。
dir, err := ioutil.TempDir("", "clone-example")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
// Clones the repository into the given dir, just as a normal git clone does
_, err = git.PlainClone(dir, false, &git.CloneOptions{
URL: "<https://git repository url***>",
Auth: &http.BasicAuth{
Username: "*****",
Password: "***",
},
})
fmt.Println(err)
if err != nil {
log.Fatal(err)
}
代码有效,它只是在 function 结束后立即删除文件夹(还要注意克隆的项目转到/tmp/<project-name>
)
评论此行以防止它。
//defer os.RemoveAll(dir) // clean up
如果您不想处理磁盘清理(这会产生克隆失败的错觉,因为克隆的文件夹已被删除),您可以执行内存中的克隆,如以下go-git
示例:
// Clones the given repository in memory, creating the remote, the local
// branches and fetching the objects, exactly as:
Info("git clone https://github.com/src-d/go-siva")
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: "https://github.com/src-d/go-siva",
})
CheckIfError(err)
// Gets the HEAD history from HEAD, just like does:
Info("git log")
// ... retrieves the branch pointed by HEAD
ref, err := r.Head()
CheckIfError(err)
// ... retrieves the commit history
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
CheckIfError(err)
// ... just iterates over the commits, printing it
err = cIter.ForEach(func(c *object.Commit) error {
fmt.Println(c)
return nil
})
CheckIfError(err)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.