aboutsummaryrefslogtreecommitdiffstats
path: root/examples/remotes/main.go
blob: 552d6a50e5508f43143f2f2bdc19a2f52b08802a (plain) (blame)
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
package main

import (
	"fmt"

	"github.com/fatih/color"

	"gopkg.in/src-d/go-git.v4"
	"gopkg.in/src-d/go-git.v4/config"
	"gopkg.in/src-d/go-git.v4/core"
)

func main() {
	// Create a new repository
	color.Blue("git init")
	r := git.NewMemoryRepository()

	// Add a new remote, with the default fetch refspec
	// > git remote add example https://github.com/git-fixtures/basic.git
	color.Blue("git remote add example https://github.com/git-fixtures/basic.git")

	r.CreateRemote(&config.RemoteConfig{
		Name: "example",
		URL:  "https://github.com/git-fixtures/basic.git",
	})

	// List remotes from a repository
	// > git remotes -v
	color.Blue("git remotes -v")

	list, _ := r.Remotes()
	for _, r := range list {
		fmt.Println(r)
	}

	// Pull using the create repository
	// > git pull example
	color.Blue("git pull example")

	r.Pull(&git.PullOptions{
		RemoteName: "example",
	})

	// List the branches
	// > git show-ref
	color.Blue("git show-ref")

	refs, _ := r.Refs()
	refs.ForEach(func(ref *core.Reference) error {
		// The HEAD is ommitted in a `git show-ref` so we ignore the symbolic
		// references, the HEAD
		if ref.Type() == core.SymbolicReference {
			return nil
		}

		fmt.Println(ref)
		return nil
	})

	// Delete the example remote
	// > git remote rm example
	color.Blue("git remote rm example")
	r.DeleteRemote("example")
}