golang git pulling a repo

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

golang git pulling a repo

问题

我对golang非常陌生,我正在尝试从go程序中进行git pull操作。我查看了一些原生库,并找到了https://github.com/src-d/go-git/。

它具有克隆等功能,但没有pull操作。从源代码来看,似乎有一个用于pull的函数:

func (r *Repository) Pull(o *PullOptions) 

然而,编译器警告说它未定义。有人可以告诉我如何做到这一点,或者推荐一个同时支持克隆和pull操作的替代库吗?

英文:

I'm very new to golang
Im trying to do a git pull from go program. I have looked in to native libraries and found https://github.com/src-d/go-git/.

I has features to of cloning ect. but not pulling. Looking at the source it seems there is a function for pulling as well

func (r *Repository) Pull(o *PullOptions) 

However compiler warns that its undefined. Can anyone point me how can I do this or to an alternative library which supports both clone and pull ?

答案1

得分: 14

你应该通过克隆一个仓库来创建一个 Repository 结构体:

import (
  git "gopkg.in/src-d/go-git.v4"
)

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

然后在 repo 结构体上调用 Pull 方法:

err := repo.Pull(&git.PullOptions{
    RemoteName: "origin",
})

你不能直接调用 git.Pull

英文:

You should create a Repository struct by cloning a repo:

import {
  git "gopkg.in/src-d/go-git.v4"
}

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

And then on the repo struct call Pull.

err := repo.Pull(&git.PullOptions{
    RemoteName: "origin"
})

You cannot call git.Pull directly.

答案2

得分: 4

gopkg.in/src-d/go-git.v4 不再维护,建议使用 github.com/go-git/go-git 替代。请参考 - https://pkg.go.dev/github.com/go-git/go-git

示例代码

import "github.com/go-git/go-git/v5"

_, err := git.PlainClone("/tmp/foo", false, &git.CloneOptions{
    URL:      "https://github.com/go-git/go-git",
    Progress: os.Stdout,
})
英文:

gopkg.in/src-d/go-git.v4 is no longer maintained recommended to use github.com/go-git/go-git instead. Refer - https://pkg.go.dev/github.com/go-git/go-git

Sample code

import "github.com/go-git/go-git/v5"

_, err := git.PlainClone("/tmp/foo", false, &git.CloneOptions{
    URL:      "https://github.com/go-git/go-git",
    Progress: os.Stdout,
})

huangapple
  • 本文由 发表于 2017年7月27日 14:11:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/45342640.html
匿名

发表评论

匿名网友

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

确定