aboutsummaryrefslogtreecommitdiffstats
path: root/_examples/checkout/main.go
blob: 3282f144d54a4998904d41867b2e8f119c50c187 (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
package main

import (
	"fmt"
	"os"

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

// Basic example of how to checkout a specific commit.
func main() {
	CheckArgs("<url>", "<directory>", "<commit-ref>")
	url, directory, commitRef := os.Args[1], os.Args[2], os.Args[3]

	// Clone the given repository to the given directory
	Info("git clone %s %s --recursive", url, directory)

	r, err := git.PlainClone(directory, false, &git.CloneOptions{
		URL:               url,
		RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
	})

	CheckIfError(err)

	Info("git checkout %s", commitRef)

	w, err := r.Worktree()

	CheckIfError(err)

	CheckIfError(w.Checkout(plumbing.NewHash(commitRef)))

	fmt.Println("voila")
}