aboutsummaryrefslogtreecommitdiffstats
path: root/_examples
diff options
context:
space:
mode:
Diffstat (limited to '_examples')
-rw-r--r--_examples/README.md7
-rw-r--r--_examples/common.go2
-rw-r--r--_examples/common_test.go1
-rw-r--r--_examples/revision/main.go32
4 files changed, 38 insertions, 4 deletions
diff --git a/_examples/README.md b/_examples/README.md
index 10594ab..271379b 100644
--- a/_examples/README.md
+++ b/_examples/README.md
@@ -11,9 +11,10 @@ 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
-- [tag](tag/main.go) - list/print repository tags
-- [pull](pull/main.go) - pull changes from a remote repository
+- [checkout](checkout/main.go) - Check out a specific commit from a repository
+- [tag](tag/main.go) - List/print repository tags
+- [pull](pull/main.go) - Pull changes from a remote repository
+- [revision](revision/main.go) - Solve a revision into a commit
### 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/common.go b/_examples/common.go
index 2719c0e..81ed647 100644
--- a/_examples/common.go
+++ b/_examples/common.go
@@ -6,7 +6,7 @@ import (
"strings"
)
-// CheckArgs should be used to esnure the right command line arguments are
+// CheckArgs should be used to ensure the right command line arguments are
// passed before executing an example.
func CheckArgs(arg ...string) {
if len(os.Args) < len(arg)+1 {
diff --git a/_examples/common_test.go b/_examples/common_test.go
index d164297..6f8df45 100644
--- a/_examples/common_test.go
+++ b/_examples/common_test.go
@@ -23,6 +23,7 @@ var args = map[string][]string{
"open": {cloneRepository(defaultURL, tempFolder())},
"progress": {defaultURL, tempFolder()},
"push": {setEmptyRemote(cloneRepository(defaultURL, tempFolder()))},
+ "revision": {cloneRepository(defaultURL, tempFolder()), "master~2^"},
"showcase": {defaultURL, tempFolder()},
"tag": {cloneRepository(defaultURL, tempFolder())},
"pull": {createRepositoryWithRemote(tempFolder(), defaultURL)},
diff --git a/_examples/revision/main.go b/_examples/revision/main.go
new file mode 100644
index 0000000..a2389bd
--- /dev/null
+++ b/_examples/revision/main.go
@@ -0,0 +1,32 @@
+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"
+)
+
+// Example how to resolve a revision into its commit counterpart
+func main() {
+ CheckArgs("<path>", "<revision>")
+
+ path := os.Args[1]
+ revision := os.Args[2]
+
+ // We instantiate a new repository targeting the given path (the .git folder)
+ r, err := git.PlainOpen(path)
+ CheckIfError(err)
+
+ // Resolve revision into a sha1 commit, only some revisions are resolved
+ // look at the doc to get more details
+ Info("git rev-parse %s", revision)
+
+ h, err := r.ResolveRevision(plumbing.Revision(revision))
+
+ CheckIfError(err)
+
+ fmt.Println(h.String())
+}