1package main
2
3import (
4 "fmt"
5
6 "gopkg.in/src-d/go-git.v4"
7 . "gopkg.in/src-d/go-git.v4/_examples"
8 "gopkg.in/src-d/go-git.v4/plumbing/object"
9 "gopkg.in/src-d/go-git.v4/storage/memory"
10)
11
12// Example of how to:
13// - Clone a repository into memory
14// - Get the HEAD reference
15// - Using the HEAD reference, obtain the commit this reference is pointing to
16// - Using the commit, obtain its history and print it
17func main() {
18 // Clones the given repository, creating the remote, the local branches
19 // and fetching the objects, everything in memory:
20 Info("git clone https://github.com/src-d/go-siva")
21 r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
22 URL: "https://github.com/src-d/go-siva",
23 })
24 CheckIfError(err)
25
26 // Gets the HEAD history from HEAD, just like does:
27 Info("git log")
28
29 // ... retrieves the branch pointed by HEAD
30 ref, err := r.Head()
31 CheckIfError(err)
32
33 // ... retrieves the commit history
34 cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
35 CheckIfError(err)
36
37 // ... just iterates over the commits, printing it
38 err = cIter.ForEach(func(c *object.Commit) error {
39 fmt.Println(c)
40
41 return nil
42 })
43 CheckIfError(err)
44}