aboutsummaryrefslogtreecommitdiffstats
path: root/example_test.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-01-31 23:09:28 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2017-01-31 23:09:52 +0100
commita6197bd8c459c7d10717bd9486c17f6c8b3f7f88 (patch)
tree63180ec210c66fda317c87197d2f6bab508406b6 /example_test.go
parent2308066c28d3cbbfb472fb634c3e10a1c7786cfc (diff)
downloadgo-git-a6197bd8c459c7d10717bd9486c17f6c8b3f7f88.tar.gz
documentation changes
Diffstat (limited to 'example_test.go')
-rw-r--r--example_test.go112
1 files changed, 112 insertions, 0 deletions
diff --git a/example_test.go b/example_test.go
new file mode 100644
index 0000000..a45225a
--- /dev/null
+++ b/example_test.go
@@ -0,0 +1,112 @@
+package git_test
+
+import (
+ "fmt"
+ "io"
+ "io/ioutil"
+ "log"
+ "os"
+ "path/filepath"
+
+ "srcd.works/go-billy.v1/memfs"
+ git "srcd.works/go-git.v4"
+ "srcd.works/go-git.v4/config"
+ "srcd.works/go-git.v4/plumbing"
+ "srcd.works/go-git.v4/storage/memory"
+)
+
+func ExampleClone() {
+ // Filesystem abstraction based on memory
+ fs := memfs.New()
+ // Git objects storer based on memory
+ storer := memory.NewStorage()
+
+ // Clones the repository into the worktree (fs) and storer all the .git
+ // content into the storer
+ _, _ = git.Clone(storer, fs, &git.CloneOptions{
+ URL: "https://github.com/git-fixtures/basic.git",
+ })
+
+ // Prints the content of the CHANGELOG file from the cloned repository
+ changelog, _ := fs.Open("CHANGELOG")
+
+ io.Copy(os.Stdout, changelog)
+ // Output: Initial changelog
+}
+
+func ExamplePlainClone() {
+ // Tempdir to clone the repository
+ dir, err := ioutil.TempDir("", "clone-example")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ defer os.RemoveAll(dir) // clean up
+
+ // Clones the repository into the given dir, just as a normal git clone does
+ _, err = git.PlainClone(dir, false, &git.CloneOptions{
+ URL: "https://github.com/git-fixtures/basic.git",
+ })
+
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ // Prints the content of the CHANGELOG file from the cloned repository
+ changelog, err := os.Open(filepath.Join(dir, "CHANGELOG"))
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ io.Copy(os.Stdout, changelog)
+ // Output: Initial changelog
+}
+
+func ExampleRepository_References() {
+ r, _ := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
+ URL: "https://github.com/git-fixtures/basic.git",
+ })
+
+ // simulating a git show-ref
+ refs, _ := r.References()
+ refs.ForEach(func(ref *plumbing.Reference) error {
+ if ref.Type() == plumbing.HashReference {
+ fmt.Println(ref)
+ }
+
+ return nil
+ })
+
+ // Example Output:
+ // 6ecf0ef2c2dffb796033e5a02219af86ec6584e5 refs/remotes/origin/master
+ // e8d3ffab552895c19b9fcf7aa264d277cde33881 refs/remotes/origin/branch
+ // 6ecf0ef2c2dffb796033e5a02219af86ec6584e5 refs/heads/master
+
+}
+
+func ExampleRepository_CreateRemote() {
+ r, _ := git.Init(memory.NewStorage(), nil)
+
+ // Add a new remote, with the default fetch refspec
+ _, err := r.CreateRemote(&config.RemoteConfig{
+ Name: "example",
+ URL: "https://github.com/git-fixtures/basic.git",
+ })
+
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ list, err := r.Remotes()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ for _, r := range list {
+ fmt.Println(r)
+ }
+
+ // Example Output:
+ // example https://github.com/git-fixtures/basic.git (fetch)
+ // example https://github.com/git-fixtures/basic.git (push)
+}