| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"gopkg.in/src-d/go-git.v4" |
| 7 |
. "gopkg.in/src-d/go-git.v4/_examples" |
| 8 |
"gopkg.in/src-d/go-git.v4/config" |
| 9 |
"gopkg.in/src-d/go-git.v4/plumbing" |
| 10 |
"gopkg.in/src-d/go-git.v4/storage/memory" |
| 11 |
) |
| 12 |
|
| 13 |
// Example of how to: |
| 14 |
// - Create a new in-memory repository |
| 15 |
// - Create a new remote named "example" |
| 16 |
// - List remotes and print them |
| 17 |
// - Pull using the new remote "example" |
| 18 |
// - Iterate the references again, but only showing hash references, not symbolic ones |
| 19 |
// - Remove remote "example" |
| 20 |
func main() { |
| 21 |
// Create a new repository |
| 22 |
Info("git init") |
| 23 |
r, err := git.Init(memory.NewStorage(), nil) |
| 24 |
CheckIfError(err) |
| 25 |
|
| 26 |
// Add a new remote, with the default fetch refspec |
| 27 |
Info("git remote add example https://github.com/git-fixtures/basic.git") |
| 28 |
_, err = r.CreateRemote(&config.RemoteConfig{ |
| 29 |
Name: "example", |
| 30 |
URLs: []string{"https://github.com/git-fixtures/basic.git"}, |
| 31 |
}) |
| 32 |
|
| 33 |
CheckIfError(err) |
| 34 |
|
| 35 |
// List remotes from a repository |
| 36 |
Info("git remotes -v") |
| 37 |
|
| 38 |
list, err := r.Remotes() |
| 39 |
CheckIfError(err) |
| 40 |
|
| 41 |
for _, r := range list { |
| 42 |
fmt.Println(r) |
| 43 |
} |
| 44 |
|
| 45 |
// Fetch using the new remote |
| 46 |
Info("git fetch example") |
| 47 |
err = r.Fetch(&git.FetchOptions{ |
| 48 |
RemoteName: "example", |
| 49 |
}) |
| 50 |
|
| 51 |
CheckIfError(err) |
| 52 |
|
| 53 |
// List the branches |
| 54 |
// > git show-ref |
| 55 |
Info("git show-ref") |
| 56 |
|
| 57 |
refs, err := r.References() |
| 58 |
CheckIfError(err) |
| 59 |
|
| 60 |
err = refs.ForEach(func(ref *plumbing.Reference) error { |
| 61 |
// The HEAD is omitted in a `git show-ref` so we ignore the symbolic |
| 62 |
// references, the HEAD |
| 63 |
if ref.Type() == plumbing.SymbolicReference { |
| 64 |
return nil |
| 65 |
} |
| 66 |
|
| 67 |
fmt.Println(ref) |
| 68 |
return nil |
| 69 |
}) |
| 70 |
|
| 71 |
CheckIfError(err) |
| 72 |
|
| 73 |
// Delete the example remote |
| 74 |
Info("git remote rm example") |
| 75 |
|
| 76 |
err = r.DeleteRemote("example") |
| 77 |
CheckIfError(err) |
| 78 |
} |