无法使用GO语言git克隆

发布于 2025-02-03 12:00:43 字数 545 浏览 2 评论 0原文

我正在尝试使用以下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)
}

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)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光飞翔i 2025-02-10 12:00:43

代码有效,它只是在功能结束后立即删除文件夹! (还要提防克隆项目转到/tmp/&lt; project-name&gt;

发表此行以防止它。

 //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
悲念泪 2025-02-10 12:00:43

如果您不想处理磁盘清理(因为删除了克隆的文件夹,这给了克隆失败的错觉),则可以执行内存中的克隆,例如

// 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文