无法使用Go语言进行git克隆。

huangapple go评论79阅读模式
英文:

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(&quot;&quot;, &quot;clone-example&quot;)
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, &amp;git.CloneOptions{
	URL: &quot;&lt;https://git repository url***&gt;&quot;,
	Auth: &amp;http.BasicAuth{
		Username: &quot;*****&quot;,
		Password: &quot;***&quot;,
	},
})
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/&lt;project-name&gt;)

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(&quot;git clone https://github.com/src-d/go-siva&quot;)

r, err := git.Clone(memory.NewStorage(), nil, &amp;git.CloneOptions{
    URL: &quot;https://github.com/src-d/go-siva&quot;,
})

CheckIfError(err)

// Gets the HEAD history from HEAD, just like does:
Info(&quot;git log&quot;)

// ... retrieves the branch pointed by HEAD
ref, err := r.Head()
CheckIfError(err)


// ... retrieves the commit history
cIter, err := r.Log(&amp;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)

huangapple
  • 本文由 发表于 2022年6月1日 20:21:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/72461816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定