| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
|
| 7 |
"gopkg.in/src-d/go-git.v4" |
| 8 |
. "gopkg.in/src-d/go-git.v4/_examples" |
| 9 |
"gopkg.in/src-d/go-git.v4/plumbing" |
| 10 |
) |
| 11 |
|
| 12 |
// Basic example of how to checkout a specific commit. |
| 13 |
func main() { |
| 14 |
CheckArgs("<url>", "<directory>", "<commit>") |
| 15 |
url, directory, commit := os.Args[1], os.Args[2], os.Args[3] |
| 16 |
|
| 17 |
// Clone the given repository to the given directory |
| 18 |
Info("git clone %s %s", url, directory) |
| 19 |
r, err := git.PlainClone(directory, false, &git.CloneOptions{ |
| 20 |
URL: url, |
| 21 |
}) |
| 22 |
|
| 23 |
CheckIfError(err) |
| 24 |
|
| 25 |
// ... retrieving the commit being pointed by HEAD |
| 26 |
Info("git show-ref --head HEAD") |
| 27 |
ref, err := r.Head() |
| 28 |
CheckIfError(err) |
| 29 |
fmt.Println(ref.Hash()) |
| 30 |
|
| 31 |
w, err := r.Worktree() |
| 32 |
CheckIfError(err) |
| 33 |
|
| 34 |
// ... checking out to commit |
| 35 |
Info("git checkout %s", commit) |
| 36 |
err = w.Checkout(&git.CheckoutOptions{ |
| 37 |
Hash: plumbing.NewHash(commit), |
| 38 |
}) |
| 39 |
CheckIfError(err) |
| 40 |
|
| 41 |
// ... retrieving the commit being pointed by HEAD, it shows that the |
| 42 |
// repository is pointing to the giving commit in detached mode |
| 43 |
Info("git show-ref --head HEAD") |
| 44 |
ref, err = r.Head() |
| 45 |
CheckIfError(err) |
| 46 |
fmt.Println(ref.Hash()) |
| 47 |
} |