diff options
author | Ori Rawlings <orirawlings@gmail.com> | 2017-07-15 10:17:50 -0500 |
---|---|---|
committer | Ori Rawlings <orirawlings@gmail.com> | 2017-07-17 08:55:25 -0500 |
commit | 484ccbfa5f9233a8e0b234c4cb14a05eb2d81aee (patch) | |
tree | 74488f8d59e3eddef0d4d3ea7c33ace08a922ae7 /_examples | |
parent | d3c7400c39f86a4c59340c7a9cda8497186e00fc (diff) | |
download | go-git-484ccbfa5f9233a8e0b234c4cb14a05eb2d81aee.tar.gz |
Add example code for listing tags
Diffstat (limited to '_examples')
-rw-r--r-- | _examples/README.md | 3 | ||||
-rw-r--r-- | _examples/common_test.go | 1 | ||||
-rw-r--r-- | _examples/tag/main.go | 43 |
3 files changed, 46 insertions, 1 deletions
diff --git a/_examples/README.md b/_examples/README.md index 9473a06..2e7d514 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -10,7 +10,8 @@ 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. +- [checkout](checkout/main.go) - check out a specific commit from a repository +- [tag](tag/main.go) - list/print repository tags ### 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_test.go b/_examples/common_test.go index 0a00006..86205fe 100644 --- a/_examples/common_test.go +++ b/_examples/common_test.go @@ -23,6 +23,7 @@ var args = map[string][]string{ "progress": []string{defaultURL, tempFolder()}, "push": []string{setEmptyRemote(cloneRepository(defaultURL, tempFolder()))}, "showcase": []string{defaultURL, tempFolder()}, + "tag": []string{cloneRepository(defaultURL, tempFolder())}, } var ignored = map[string]bool{} 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) +} |