aboutsummaryrefslogtreecommitdiffstats
path: root/_examples/checkout/main.go
diff options
context:
space:
mode:
Diffstat (limited to '_examples/checkout/main.go')
-rw-r--r--_examples/checkout/main.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/_examples/checkout/main.go b/_examples/checkout/main.go
new file mode 100644
index 0000000..3282f14
--- /dev/null
+++ b/_examples/checkout/main.go
@@ -0,0 +1,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")
+}