aboutsummaryrefslogtreecommitdiffstats
path: root/_examples/checkout/main.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-04-12 15:09:57 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2017-04-12 15:09:57 +0200
commit5bcf802213e801c4d52102612f007defa5d0397f (patch)
tree9b5dd9ad1665fad8424dfbdc5bd93b531f714b09 /_examples/checkout/main.go
parent63f234847bf613b621b3a714d77fc077d88717e6 (diff)
downloadgo-git-5bcf802213e801c4d52102612f007defa5d0397f.tar.gz
examples: checkout example update
Diffstat (limited to '_examples/checkout/main.go')
-rw-r--r--_examples/checkout/main.go26
1 files changed, 19 insertions, 7 deletions
diff --git a/_examples/checkout/main.go b/_examples/checkout/main.go
index 24f926a..2c54550 100644
--- a/_examples/checkout/main.go
+++ b/_examples/checkout/main.go
@@ -11,25 +11,37 @@ import (
// 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]
+ CheckArgs("<url>", "<directory>", "<commit>")
+ url, directory, commit := 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)
+ // ... retrieving the commit being pointed by HEAD
+ Info("git show-ref --head HEAD")
+ ref, err := r.Head()
+ CheckIfError(err)
+ fmt.Println(ref.Hash())
w, err := r.Worktree()
-
CheckIfError(err)
- CheckIfError(w.Checkout(plumbing.NewHash(commitRef)))
+ // ... checking out to commit
+ Info("git checkout %s", commit)
+ err = w.Checkout(&git.CheckoutOptions{
+ Hash: plumbing.NewHash(commit),
+ })
+ CheckIfError(err)
- fmt.Println("voila")
+ // ... retrieving the commit being pointed by HEAD, it's shows that the
+ // repository is poiting to the giving commit in detached mode
+ Info("git show-ref --head HEAD")
+ ref, err = r.Head()
+ CheckIfError(err)
+ fmt.Println(ref.Hash())
}