From 5bcf802213e801c4d52102612f007defa5d0397f Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Wed, 12 Apr 2017 15:09:57 +0200 Subject: examples: checkout example update --- _examples/checkout/main.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to '_examples/checkout/main.go') 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, commitRef := os.Args[1], os.Args[2], os.Args[3] + CheckArgs("", "", "") + 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()) } -- cgit