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

Commit bebcb4f

Browse files
authored
Merge pull request #484 from orirawlings/tagsExample
Add example code for listing tags
2 parents d3c7400 + 484ccbf commit bebcb4f

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

_examples/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ Here you can find a list of annotated _go-git_ examples:
1010
- [remotes](remotes/main.go) - Working with remotes: adding, removing, etc
1111
- [progress](progress/main.go) - Printing the progress information from the sideband
1212
- [push](push/main.go) - Push repository to default remote (origin)
13-
- [checkout](checkout/main.go) - check out a specific commit from a repository.
13+
- [checkout](checkout/main.go) - check out a specific commit from a repository
14+
- [tag](tag/main.go) - list/print repository tags
1415
### Advanced
1516
- [custom_http](custom_http/main.go) - Replacing the HTTP client using a custom one
1617
- [storage](storage/README.md) - Implementing a custom storage system

_examples/common_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var args = map[string][]string{
2323
"progress": []string{defaultURL, tempFolder()},
2424
"push": []string{setEmptyRemote(cloneRepository(defaultURL, tempFolder()))},
2525
"showcase": []string{defaultURL, tempFolder()},
26+
"tag": []string{cloneRepository(defaultURL, tempFolder())},
2627
}
2728

2829
var ignored = map[string]bool{}

_examples/tag/main.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"gopkg.in/src-d/go-git.v4"
8+
. "gopkg.in/src-d/go-git.v4/_examples"
9+
"gopkg.in/src-d/go-git.v4/plumbing"
10+
"gopkg.in/src-d/go-git.v4/plumbing/object"
11+
)
12+
13+
// Basic example of how to list tags.
14+
func main() {
15+
CheckArgs("<path>")
16+
path := os.Args[1]
17+
18+
// We instance a new repository targeting the given path (the .git folder)
19+
r, err := git.PlainOpen(path)
20+
CheckIfError(err)
21+
22+
// List all tag references, both lightweight tags and annotated tags
23+
Info("git show-ref --tag")
24+
25+
tagrefs, err := r.Tags()
26+
CheckIfError(err)
27+
err = tagrefs.ForEach(func(t *plumbing.Reference) error {
28+
fmt.Println(t)
29+
return nil
30+
})
31+
CheckIfError(err)
32+
33+
// Print each annotated tag object (lightweight tags are not included)
34+
Info("for t in $(git show-ref --tag); do if [ \"$(git cat-file -t $t)\" = \"tag\" ]; then git cat-file -p $t ; fi; done")
35+
36+
tags, err := r.TagObjects()
37+
CheckIfError(err)
38+
err = tags.ForEach(func(t *object.Tag) error {
39+
fmt.Println(t)
40+
return nil
41+
})
42+
CheckIfError(err)
43+
}

0 commit comments

Comments
 (0)