Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Add example code for listing tags #484

Merged
merged 1 commit into from
Jul 17, 2017
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
3 changes: 2 additions & 1 deletion _examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions _examples/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
43 changes: 43 additions & 0 deletions _examples/tag/main.go
Original file line number Diff line number Diff line change
@@ -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)
}