aboutsummaryrefslogtreecommitdiffstats
path: root/examples/log/main.go
blob: 6ab0bb70bd5210209b32d4b9b84ed7701ed8f80e (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
package main

import (
	"fmt"

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

func main() {
	// Instances an in-memory git repository
	r := git.NewMemoryRepository()

	// Clones the given repository, creating the remote, the local branches
	// and fetching the objects, exactly as:
	Info("git clone https://github.com/src-d/go-siva")

	err := r.Clone(&git.CloneOptions{URL: "https://github.com/src-d/go-siva"})
	CheckIfError(err)

	// Gets the HEAD history from HEAD, just like does:
	Info("git log")

	// ... retrieves the branch pointed by HEAD
	ref, err := r.Head()
	CheckIfError(err)

	// ... retrieves the commit object
	commit, err := r.Commit(ref.Hash())
	CheckIfError(err)

	// ... retrieves the commit history
	history, err := commit.History()
	CheckIfError(err)

	// ... just iterates over the commits, printing it
	for _, c := range history {
		fmt.Println(c)
	}
}