diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-04-06 22:32:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-06 22:32:08 +0200 |
commit | c34356d70ec011b8fe3a18bd60ce8234541358f7 (patch) | |
tree | 091972513babd4beca29d0318058facb678e5d1c /_examples/checkout/main.go | |
parent | 8ede7794a674fd890147f81201e25ecb517046f0 (diff) | |
parent | e0b296d0d0d235ad848cce7bc19130505c33d97b (diff) | |
download | go-git-c34356d70ec011b8fe3a18bd60ce8234541358f7.tar.gz |
Merge pull request #331 from marwan-at-work/examples
add git checkout example + housekeeping
Diffstat (limited to '_examples/checkout/main.go')
-rw-r--r-- | _examples/checkout/main.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/_examples/checkout/main.go b/_examples/checkout/main.go new file mode 100644 index 0000000..24f926a --- /dev/null +++ b/_examples/checkout/main.go @@ -0,0 +1,35 @@ +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", url, directory) + + r, err := git.PlainClone(directory, false, &git.CloneOptions{ + URL: url, + }) + + CheckIfError(err) + + Info("git checkout %s", commitRef) + + w, err := r.Worktree() + + CheckIfError(err) + + CheckIfError(w.Checkout(plumbing.NewHash(commitRef))) + + fmt.Println("voila") +} |