【问题标题】:Unable to git clone using go language无法使用 go 语言进行 git clone
【发布时间】:2022-07-03 14:42:20
【问题描述】:

我正在尝试使用下面的 go-lang 代码 sn-p 克隆 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)
}

【问题讨论】:

  • 在您的代码中 url 错误:URL: "ttps://github.com/git-fixtures/basic.git", 应该是 https 或 ssh
  • 您可以提供任何网址并查看

标签: git go repository bitbucket git-clone


【解决方案1】:

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

评论此行以防止它发生。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      • 2015-11-23
      • 2020-01-10
      • 1970-01-01
      • 2011-11-01
      • 2013-11-28
      相关资源
      最近更新 更多