From 2b503ad6cd32fa8e265e36dd09ea76d53bc26950 Mon Sep 17 00:00:00 2001 From: marwan-at-work Date: Thu, 6 Apr 2017 15:57:48 -0400 Subject: add git checkout example + housekeeping --- _examples/checkout/main.go | 36 ++++++++++++++++++++++++++++++++++++ _examples/common.go | 5 +++++ _examples/common_test.go | 7 ++++--- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 _examples/checkout/main.go diff --git a/_examples/checkout/main.go b/_examples/checkout/main.go new file mode 100644 index 0000000..3282f14 --- /dev/null +++ b/_examples/checkout/main.go @@ -0,0 +1,36 @@ +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, commitRef := os.Args[1], os.Args[2], os.Args[3] + + // Clone the given repository to the given directory + Info("git clone %s %s --recursive", url, directory) + + r, err := git.PlainClone(directory, false, &git.CloneOptions{ + URL: url, + RecurseSubmodules: git.DefaultSubmoduleRecursionDepth, + }) + + CheckIfError(err) + + Info("git checkout %s", commitRef) + + w, err := r.Worktree() + + CheckIfError(err) + + CheckIfError(w.Checkout(plumbing.NewHash(commitRef))) + + fmt.Println("voila") +} diff --git a/_examples/common.go b/_examples/common.go index 913cb18..c4618ae 100644 --- a/_examples/common.go +++ b/_examples/common.go @@ -6,6 +6,8 @@ import ( "strings" ) +// CheckArgs should be uesed 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, " ")) @@ -13,6 +15,7 @@ func CheckArgs(arg ...string) { } } +// CheckIfError should be used to naiivly panics if an error is not nil. func CheckIfError(err error) { if err == nil { return @@ -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...)) } diff --git a/_examples/common_test.go b/_examples/common_test.go index cb101e2..d812f2b 100644 --- a/_examples/common_test.go +++ b/_examples/common_test.go @@ -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{} -- cgit From e0b296d0d0d235ad848cce7bc19130505c33d97b Mon Sep 17 00:00:00 2001 From: marwan-at-work Date: Thu, 6 Apr 2017 16:25:29 -0400 Subject: pr-fixes --- _examples/README.md | 1 + _examples/checkout/main.go | 5 ++--- _examples/common.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/_examples/README.md b/_examples/README.md index d6f3e55..9473a06 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -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 diff --git a/_examples/checkout/main.go b/_examples/checkout/main.go index 3282f14..24f926a 100644 --- a/_examples/checkout/main.go +++ b/_examples/checkout/main.go @@ -15,11 +15,10 @@ func main() { 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 --recursive", url, directory) + Info("git clone %s %s", url, directory) r, err := git.PlainClone(directory, false, &git.CloneOptions{ - URL: url, - RecurseSubmodules: git.DefaultSubmoduleRecursionDepth, + URL: url, }) CheckIfError(err) diff --git a/_examples/common.go b/_examples/common.go index c4618ae..2719c0e 100644 --- a/_examples/common.go +++ b/_examples/common.go @@ -6,7 +6,7 @@ import ( "strings" ) -// CheckArgs should be uesed to esnure the right command line arguments are +// 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 { @@ -15,7 +15,7 @@ func CheckArgs(arg ...string) { } } -// CheckIfError should be used to naiivly panics if an error is not nil. +// CheckIfError should be used to naively panics if an error is not nil. func CheckIfError(err error) { if err == nil { return -- cgit