-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefs.go
173 lines (157 loc) · 3.87 KB
/
refs.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package g
import (
"bufio"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
)
// UpdateHead writes branch name as a reference in the Git HEAD file
func UpdateHead(branch string) error {
return os.WriteFile(GitHeadPath(), []byte(fmt.Sprintf("ref: refs/heads/%s\n", branch)), 0655)
}
// UpdateBranchHead updates the sha hash pointed to by a branch
func UpdateBranchHead(branch string, sha Sha) error {
path := filepath.Join(RefsHeadsDirectory(), branch)
return os.WriteFile(path, []byte(sha.AsHexString()+"\n"), 0755)
}
// HeadSHA returns the hash pointed to by a branch
func HeadSHA(currentBranch string) (Sha, error) {
path := filepath.Join(RefsHeadsDirectory(), currentBranch)
bytes, err := os.ReadFile(path)
if err != nil && errors.Is(err, fs.ErrNotExist) {
// the branch does not exist in refs/heads when there are no commits
// lets check packed-refs
branchMap, err := packedrefs()
if err != nil {
return Sha{}, err
}
if v, ok := branchMap[currentBranch]; ok {
return v, nil
}
return Sha{}, nil
} else if err != nil {
return Sha{}, err
} else if bytes == nil {
return Sha{}, fmt.Errorf("fatal: not a valid object name: '%s'", currentBranch)
}
sha, err := NewSha(bytes[0:40])
return sha, err
}
// CurrentBranch returns the name of the current branch
func CurrentBranch() (string, error) {
f, err := os.Open(GitHeadPath())
if err != nil {
return "", err
}
defer func() { _ = f.Close() }()
r := bufio.NewReader(f)
b, err := r.ReadBytes('\n')
if err != nil {
return "", err
}
if len(b) < 12 {
return "", errors.New("invalid HEAD file, expected > 12 bytes")
}
return string(b[16 : len(b)-1]), nil
}
// CurrentCommit return the current commit SHA
// @todo this probably breaks in detached head...
func CurrentCommit() (Sha, error) {
currentBranch, err := CurrentBranch()
if err != nil {
return Sha{}, err
}
sha, err := HeadSHA(currentBranch)
if err != nil {
return Sha{}, err
}
return sha, nil
}
func PreviousCommits() ([]Sha, error) {
previousCommit, err := CurrentCommit()
if err != nil {
return nil, err
}
if previousCommit.IsSet() {
return []Sha{previousCommit}, nil
}
return nil, nil
}
// ListBranches lists Git branches from refs/heads and info/refs
// It does not currently allow listing remote tracking branches
func ListBranches() ([]string, error) {
var branches []string
branchMap := make(map[string]struct{})
// check for packed refs
packed, err := packedrefs()
if err != nil {
return nil, err
}
for k := range packed {
branchMap[k] = struct{}{}
}
f, err := os.ReadDir(RefsHeadsDirectory())
if err != nil {
return branches, err
}
for _, v := range f {
if v.IsDir() {
continue
}
branchMap[v.Name()] = struct{}{}
}
// return branches sorted alphabetically
for k := range branchMap {
branches = append(branches, k)
}
sort.Strings(branches)
return branches, nil
}
func CreateBranch(name string) error {
currentBranch, err := CurrentBranch()
if err != nil {
return err
}
head, err := HeadSHA(currentBranch)
if err != nil {
return err
}
return UpdateBranchHead(name, head)
}
func DeleteBranch(name string) error {
return os.Remove(filepath.Join(RefsHeadsDirectory(), name))
}
func packedrefs() (map[string]Sha, error) {
branchMap := make(map[string]Sha)
fh, err := os.Open(PackedRefsFile())
defer func() {
if fh != nil {
_ = fh.Close()
}
}()
if err == nil {
// we have a packed refs file to parse into a list of branches
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
line := scanner.Bytes()
hash := line[0:40]
path := string(line[41:])
// path can have multiple prefixes
// refs/heads/
// refs (for stash)
// refs/remotes/.../
// for now just use refs/heads/
if path, ok := strings.CutPrefix(path, RefsHeadPrefix()); ok {
branchMap[path], err = NewSha(hash)
if err != nil {
return nil, err
}
}
}
}
return branchMap, nil
}