Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang git pulling a repo

Tags:

git

go

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 ?

like image 701
Sajith Silva Avatar asked Jul 27 '17 06:07

Sajith Silva


People also ask

How do I pull a git repository?

PULL Request Or also called a target repository. The simple command to PULL from a branch is: git pull 'remote_name' 'branch_name' .

Is go get the same as git clone?

The git clone command will clone a repo into a newly created directory, while go get downloads and installs the packages named by the import paths, along with their dependencies. Plus, neither the named packages nor the dependencies need to be Git repositories. Other VCS are supported as well.


2 Answers

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.

like image 60
Alex Efimov Avatar answered Sep 28 '22 04:09

Alex Efimov


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,
})
like image 32
Dinu Mathai Avatar answered Sep 28 '22 03:09

Dinu Mathai