英文:
Unable to git clone using go language
问题
我正在尝试使用以下Go语言代码片段克隆git/bitbucket仓库,但它不起作用,也没有显示任何错误。
dir, err := ioutil.TempDir("", "clone-example")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir) // 清理
// 克隆仓库到给定的目录,就像普通的git clone一样
_, 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)
}
请注意,这是一个代码片段,你需要确保导入了正确的包并设置了正确的URL、用户名和密码。
英文:
I'm trying to clone the git/bitbucket repository using the below go-lang code snippet, but it's not working , I can't see any errors either.
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)
}
答案1
得分: 1
代码可以正常工作,只是在函数结束后立即删除了文件夹!(还要注意克隆的项目会放在/tmp/<project-name>
中)
要防止删除操作,请注释掉这一行。
//defer os.RemoveAll(dir) // clean up
英文:
The code works, It just deletes the folder right after the function ends! (Also beware that the cloned project goes to /tmp/<project-name>
)
comment this line to prevent it.
//defer os.RemoveAll(dir) // clean up
答案2
得分: 0
如果你不想处理磁盘清理(这会让克隆失败,因为克隆的文件夹被删除),你可以进行内存克隆,就像这个go-git
示例中所示:
// 在内存中克隆给定的存储库,创建远程仓库、本地分支并获取对象,就像执行以下命令一样:
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)
// 获取HEAD的历史记录,就像执行以下命令一样:
Info("git log")
// ... 获取HEAD指向的分支
ref, err := r.Head()
CheckIfError(err)
// ... 获取提交历史记录
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
CheckIfError(err)
// ... 遍历提交记录并打印
err = cIter.ForEach(func(c *object.Commit) error {
fmt.Println(c)
return nil
})
CheckIfError(err)
英文:
If you don't want to deal with disk cleanup (which gives the illusion the clone has failed, since the cloned folder is deleted), you can do an in-memory clone, as in this go-git
example:
// 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论