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