简体   繁体   English

无法使用 go 语言克隆 git

[英]Unable to git clone using go language

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

The code works, It just deletes the folder right after the function ends (Also beware that the cloned project goes to /tmp/<project-name> )代码有效,它只是在 function 结束后立即删除文件夹(还要注意克隆的项目转到/tmp/<project-name>

comment this line to prevent it.评论此行以防止它。

 //defer os.RemoveAll(dir) // clean up

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 :如果您不想处理磁盘清理(这会产生克隆失败的错觉,因为克隆的文件夹已被删除),您可以执行内存中的克隆,如以下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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM