From a045606fc9c5cbf30b409384cbdad4804f01c61d Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Mon, 22 Aug 2016 00:17:21 +0200 Subject: examples: improved basic and remote examples --- examples/remotes/main.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 examples/remotes/main.go (limited to 'examples/remotes/main.go') diff --git a/examples/remotes/main.go b/examples/remotes/main.go new file mode 100644 index 0000000..488986a --- /dev/null +++ b/examples/remotes/main.go @@ -0,0 +1,63 @@ +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.RepositoryPullOptions{ + RemoteName: "example", + }) + + // List the branches + // > git show-ref + color.Blue("git show-ref") + + r.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") +} -- cgit