aboutsummaryrefslogtreecommitdiffstats
path: root/_examples/tag
diff options
context:
space:
mode:
authorOri Rawlings <orirawlings@gmail.com>2017-07-15 10:17:50 -0500
committerOri Rawlings <orirawlings@gmail.com>2017-07-17 08:55:25 -0500
commit484ccbfa5f9233a8e0b234c4cb14a05eb2d81aee (patch)
tree74488f8d59e3eddef0d4d3ea7c33ace08a922ae7 /_examples/tag
parentd3c7400c39f86a4c59340c7a9cda8497186e00fc (diff)
downloadgo-git-484ccbfa5f9233a8e0b234c4cb14a05eb2d81aee.tar.gz
Add example code for listing tags
Diffstat (limited to '_examples/tag')
-rw-r--r--_examples/tag/main.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/_examples/tag/main.go b/_examples/tag/main.go
new file mode 100644
index 0000000..190c3ad
--- /dev/null
+++ b/_examples/tag/main.go
@@ -0,0 +1,43 @@
+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"
+ "gopkg.in/src-d/go-git.v4/plumbing/object"
+)
+
+// Basic example of how to list tags.
+func main() {
+ CheckArgs("<path>")
+ path := os.Args[1]
+
+ // We instance a new repository targeting the given path (the .git folder)
+ r, err := git.PlainOpen(path)
+ CheckIfError(err)
+
+ // List all tag references, both lightweight tags and annotated tags
+ Info("git show-ref --tag")
+
+ tagrefs, err := r.Tags()
+ CheckIfError(err)
+ err = tagrefs.ForEach(func(t *plumbing.Reference) error {
+ fmt.Println(t)
+ return nil
+ })
+ CheckIfError(err)
+
+ // Print each annotated tag object (lightweight tags are not included)
+ Info("for t in $(git show-ref --tag); do if [ \"$(git cat-file -t $t)\" = \"tag\" ]; then git cat-file -p $t ; fi; done")
+
+ tags, err := r.TagObjects()
+ CheckIfError(err)
+ err = tags.ForEach(func(t *object.Tag) error {
+ fmt.Println(t)
+ return nil
+ })
+ CheckIfError(err)
+}