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