Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

Commit

Permalink
examples: checkout example update
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Apr 12, 2017
1 parent 63f2348 commit 5bcf802
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
26 changes: 19 additions & 7 deletions _examples/checkout/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion _examples/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

var examplesTest = flag.Bool("examples", false, "run the examples tests")

var defaultURL = "https://github.com/mcuadros/basic.git"
var defaultURL = "https://github.com/git-fixtures/basic.git"

var args = map[string][]string{
"checkout": []string{defaultURL, tempFolder(), "35e85108805c84807bc66a02d91535e1e24b38b9"},
Expand Down

0 comments on commit 5bcf802

Please sign in to comment.