-
Notifications
You must be signed in to change notification settings - Fork 2
/
ref.go
94 lines (73 loc) · 1.58 KB
/
ref.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package go4git
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
)
func refContent(path string) (string, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return strings.Trim(string(b), "\r\n"), nil
}
func (r *Repository) References() ([]Reference, error) {
refs := make([]Reference, 0)
headsDir := filepath.Join(r.gitDir, "refs", "heads")
tagsDir := filepath.Join(r.gitDir, "refs", "tags")
heads, err := ioutil.ReadDir(headsDir)
if err != nil {
return refs, err
}
for _, h := range heads {
n := filepath.Join("refs", "heads", h.Name())
rpath := filepath.Join(r.gitDir, n)
targetSha, err := refContent(rpath)
if err != nil {
return refs, err
}
ref := Reference{name: n, targetId: targetSha}
refs = append(refs, ref)
}
tags, err := ioutil.ReadDir(tagsDir)
if err != nil {
return refs, err
}
for _, t := range tags {
n := filepath.Join("refs", "tags", t.Name())
rpath := filepath.Join(r.gitDir, n)
targetSha, err := refContent(rpath)
if err != nil {
return refs, err
}
ref := Reference{name: n, targetId: targetSha}
refs = append(refs, ref)
}
return refs, nil
}
type Reference struct {
name string
targetId string
}
func (r Reference) IsBranch() bool {
return false
}
func (r Reference) IsTag() bool {
return false
}
func (r Reference) IsRemote() bool {
return false
}
func (r Reference) HasLog() bool {
return false
}
func (r Reference) String() string {
return fmt.Sprintf("%s %s", r.targetId, r.name)
}
func (r Reference) Type() string {
return ""
}
func (r Reference) Name() string {
return ""
}