diff options
Diffstat (limited to '_examples/checkout/main.go')
-rw-r--r-- | _examples/checkout/main.go | 26 |
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()) } |