Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Here you can find a list of annotated _go-git_ examples:
- [remotes](remotes/main.go) - Working with remotes: adding, removing, etc
- [progress](progress/main.go) - Printing the progress information from the sideband
- [push](push/main.go) - Push repository to default remote (origin)
- [checkout](checkout/main.go) - check out a specific commit from a repository.
### Advanced
- [custom_http](custom_http/main.go) - Replacing the HTTP client using a custom one
- [storage](storage/README.md) - Implementing a custom storage system
35 changes: 35 additions & 0 deletions _examples/checkout/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"os"

"gopkg.in/src-d/go-git.v4"
. "gopkg.in/src-d/go-git.v4/_examples"
"gopkg.in/src-d/go-git.v4/plumbing"
)

// 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]

// 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)

w, err := r.Worktree()

CheckIfError(err)

CheckIfError(w.Checkout(plumbing.NewHash(commitRef)))

fmt.Println("voila")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can print the HEAD commit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcuadros, the problem as i stated in the pr description, is that the head commit will still be the old one, because go-git does not checkout the commit in a detached state. So you will actually still get the old HEAD commit, but the difference is that you will have the changes from the target-ref applied directly to your current branch . Therefore, printing the HEAD commit would be misleading. Let me know if there's a more proper way to do this. Thanks!

}
5 changes: 5 additions & 0 deletions _examples/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
"strings"
)

// CheckArgs should be used to esnure the right command line arguments are
// passed before executing an example.
func CheckArgs(arg ...string) {
if len(os.Args) < len(arg)+1 {
Warning("Usage: %s %s", os.Args[0], strings.Join(arg, " "))
os.Exit(1)
}
}

// CheckIfError should be used to naively panics if an error is not nil.
func CheckIfError(err error) {
if err == nil {
return
Expand All @@ -22,10 +25,12 @@ func CheckIfError(err error) {
os.Exit(1)
}

// Info should be used to describe the example commands that are about to run.
func Info(format string, args ...interface{}) {
fmt.Printf("\x1b[34;1m%s\x1b[0m\n", fmt.Sprintf(format, args...))
}

// Warning should be used to display a warning
func Warning(format string, args ...interface{}) {
fmt.Printf("\x1b[36;1m%s\x1b[0m\n", fmt.Sprintf(format, args...))
}
7 changes: 4 additions & 3 deletions _examples/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ var examplesTest = flag.Bool("examples", false, "run the examples tests")
var defaultURL = "https://github.com/mcuadros/basic.git"

var args = map[string][]string{
"showcase": []string{defaultURL, tempFolder()},
"custom_http": []string{defaultURL},
"checkout": []string{defaultURL, tempFolder(), "35e85108805c84807bc66a02d91535e1e24b38b9"},
"clone": []string{defaultURL, tempFolder()},
"progress": []string{defaultURL, tempFolder()},
"custom_http": []string{defaultURL},
"open": []string{cloneRepository(defaultURL, tempFolder())},
"progress": []string{defaultURL, tempFolder()},
"push": []string{setEmptyRemote(cloneRepository(defaultURL, tempFolder()))},
"showcase": []string{defaultURL, tempFolder()},
}

var ignored = map[string]bool{}
Expand Down