aboutsummaryrefslogtreecommitdiffstats
path: root/examples/basic/main.go
blob: d92002a32ba89389a7d6b4c824a96fdcf94ee677 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main

import (
	"fmt"
	"strings"

	"github.com/fatih/color"

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

func main() {
	r, _ := git.NewFilesystemRepository(".git")

	// 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")

	r.Clone(&git.CloneOptions{
		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, err := r.Commit(ref.Hash())
	fmt.Println(commit, err)

	// 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()
	// ... get the files iterator and print the file
	tree.Files().ForEach(func(f *git.File) error {
		fmt.Printf("100644 blob %s    %s\n", f.Hash, f.Name)
		return nil
	})

	// List the history of the repository
	// > git log --oneline
	color.Blue("git log --oneline")

	commits, _ := commit.History()
	for _, c := range commits {
		hash := c.Hash.String()
		line := strings.Split(c.Message, "\n")
		fmt.Println(hash[:7], line[0])
	}

}