-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_tag.go
79 lines (63 loc) · 1.71 KB
/
repo_tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"fmt"
"strings"
"github.com/mechmind/git-go/rawgit"
)
const TAG_PREFIX = "refs/tags/"
// IsTagExist returns true if given tag exists in the repository.
func IsTagExist(repoPath, name string) bool {
return IsReferenceExist(repoPath, TAG_PREFIX+name)
}
func (repo *Repository) IsTagExist(name string) bool {
oid, _ := repo.repo.ReadRef(TAG_PREFIX + name)
return oid != ""
}
func (repo *Repository) CreateTag(name, revision string) error {
return repo.repo.WriteRef(TAG_PREFIX+name, revision)
}
// GetTag returns a Git tag by given name.
func (repo *Repository) GetTag(name string) (*Tag, error) {
oid, err := repo.repo.ResolveRef(TAG_PREFIX + name)
if err != nil {
return nil, err
}
info, _, err := repo.repo.StatObject(oid)
if err != nil {
return nil, err
}
if info.GetOType() == rawgit.OTypeCommit {
return &Tag{
ID: sha1(*oid),
Object: sha1(*oid),
Type: string(OBJECT_COMMIT),
Name: name,
repo: repo,
}, nil
}
if info.GetOType() == rawgit.OTypeTag {
obj, err := repo.repo.OpenTag(oid)
if err != nil {
return nil, err
}
tag := raw2tag(repo, obj)
tag.Name = name
return tag, nil
}
return nil, fmt.Errorf("invalid tag target: %s", info.GetOType().String())
}
// GetTags returns all tags of the repository.
func (repo *Repository) GetTags() ([]string, error) {
rawRefs, err := repo.repo.ListRefs(TAG_PREFIX)
if err != nil {
return nil, err
}
refs := []string{}
for _, rawRef := range rawRefs {
refs = append(refs, strings.TrimPrefix(rawRef, TAG_PREFIX))
}
return refs, nil
}