diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-22 00:17:21 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-22 00:17:21 +0200 |
commit | a045606fc9c5cbf30b409384cbdad4804f01c61d (patch) | |
tree | 2b72458903060790280adaaf0f53c5aa4ba73a82 /examples | |
parent | b32e92d013f6fe0d97f094c4b1991100d568f980 (diff) | |
download | go-git-a045606fc9c5cbf30b409384cbdad4804f01c61d.tar.gz |
examples: improved basic and remote examples
Diffstat (limited to 'examples')
-rw-r--r-- | examples/basic/main.go | 58 | ||||
-rw-r--r-- | examples/remotes/main.go | 63 |
2 files changed, 98 insertions, 23 deletions
diff --git a/examples/basic/main.go b/examples/basic/main.go index 510b39d..4c65a70 100644 --- a/examples/basic/main.go +++ b/examples/basic/main.go @@ -2,37 +2,49 @@ package main import ( "fmt" - "os" + + "github.com/fatih/color" "gopkg.in/src-d/go-git.v4" ) func main() { - url := os.Args[1] - fmt.Printf("Retrieving %q ...\n", url) - r, err := git.NewMemoryRepository() - if err != nil { - panic(err) - } - - if err = r.Clone(&git.RepositoryCloneOptions{URL: url}); err != nil { - panic(err) - } - - iter, err := r.Commits() - if err != nil { - panic(err) - } + r := git.NewMemoryRepository() - defer iter.Close() + // Clone the given repository, creating the remote, the local branches + // and fetching the objects, exactly as: + // > git clone https://github.com/git-fixtures/basic.git + color.Blue("git clone https://github.com/git-fixtures/basic.git") - var count = 0 - iter.ForEach(func(commit *git.Commit) error { - count++ - fmt.Println(commit) + r.Clone(&git.RepositoryCloneOptions{ + URL: "https://github.com/git-fixtures/basic.git", + }) + // Getting the latest commit on the current branch + // > git log -1 + color.Blue("git log -1") + + // ... retrieving the branch being pointed by HEAD + ref, _ := r.Head() + // ... retrieving the commit object + commit, _ := r.Commit(ref.Hash()) + fmt.Println(commit) + + // List the tree from HEAD + // > git ls-tree -r HEAD + color.Blue("git ls-tree -r HEAD") + + // ... retrieve the tree from the commit + tree := commit.Tree() + // ... create a tree walker, allows to you intereste all nested trees + walker := git.NewTreeWalker(r, tree) + walker.ForEach(func(fullpath string, e git.TreeEntry) error { + // we ignore the tree + if e.Mode.Perm() == 0 { + return nil + } + + fmt.Printf("100644 blob %s %s\n", e.Hash, fullpath) return nil }) - - fmt.Println("total commits:", count) } 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") +} |