git/git.go (view raw)
1package git
2
3import (
4 "archive/tar"
5 "fmt"
6 "io"
7 "io/fs"
8 "path"
9 "sort"
10 "time"
11
12 "github.com/go-git/go-git/v5"
13 "github.com/go-git/go-git/v5/plumbing"
14 "github.com/go-git/go-git/v5/plumbing/object"
15)
16
17type GitRepo struct {
18 r *git.Repository
19 h plumbing.Hash
20}
21
22type TagList struct {
23 refs []*TagReference
24 r *git.Repository
25}
26
27// TagReference is used to list both tag and non-annotated tags.
28// Non-annotated tags should only contains a reference.
29// Annotated tags should contain its reference and its tag information.
30type TagReference struct {
31 ref *plumbing.Reference
32 tag *object.Tag
33}
34
35// infoWrapper wraps the property of a TreeEntry so it can export fs.FileInfo
36// to tar WriteHeader
37type infoWrapper struct {
38 name string
39 size int64
40 mode fs.FileMode
41 modTime time.Time
42 isDir bool
43}
44
45func (self *TagList) Len() int {
46 return len(self.refs)
47}
48
49func (self *TagList) Swap(i, j int) {
50 self.refs[i], self.refs[j] = self.refs[j], self.refs[i]
51}
52
53// sorting tags in reverse chronological order
54func (self *TagList) Less(i, j int) bool {
55 var dateI time.Time
56 var dateJ time.Time
57
58 if self.refs[i].tag != nil {
59 dateI = self.refs[i].tag.Tagger.When
60 } else {
61 c, err := self.r.CommitObject(self.refs[i].ref.Hash())
62 if err != nil {
63 dateI = time.Now()
64 } else {
65 dateI = c.Committer.When
66 }
67 }
68
69 if self.refs[j].tag != nil {
70 dateJ = self.refs[j].tag.Tagger.When
71 } else {
72 c, err := self.r.CommitObject(self.refs[j].ref.Hash())
73 if err != nil {
74 dateJ = time.Now()
75 } else {
76 dateJ = c.Committer.When
77 }
78 }
79
80 return dateI.After(dateJ)
81}
82
83func Open(path string, ref string) (*GitRepo, error) {
84 var err error
85 g := GitRepo{}
86 g.r, err = git.PlainOpen(path)
87 if err != nil {
88 return nil, fmt.Errorf("opening %s: %w", path, err)
89 }
90
91 if ref == "" {
92 head, err := g.r.Head()
93 if err != nil {
94 return nil, fmt.Errorf("getting head of %s: %w", path, err)
95 }
96 g.h = head.Hash()
97 } else {
98 hash, err := g.r.ResolveRevision(plumbing.Revision(ref))
99 if err != nil {
100 return nil, fmt.Errorf("resolving rev %s for %s: %w", ref, path, err)
101 }
102 g.h = *hash
103 }
104 return &g, nil
105}
106
107func (g *GitRepo) Commits() ([]*object.Commit, error) {
108 ci, err := g.r.Log(&git.LogOptions{From: g.h})
109 if err != nil {
110 return nil, fmt.Errorf("commits from ref: %w", err)
111 }
112
113 commits := []*object.Commit{}
114 ci.ForEach(func(c *object.Commit) error {
115 commits = append(commits, c)
116 return nil
117 })
118
119 return commits, nil
120}
121
122func (g *GitRepo) LastCommit() (*object.Commit, error) {
123 c, err := g.r.CommitObject(g.h)
124 if err != nil {
125 return nil, fmt.Errorf("last commit: %w", err)
126 }
127 return c, nil
128}
129
130func (g *GitRepo) FileContent(path string) (string, error) {
131 c, err := g.r.CommitObject(g.h)
132 if err != nil {
133 return "", fmt.Errorf("commit object: %w", err)
134 }
135
136 tree, err := c.Tree()
137 if err != nil {
138 return "", fmt.Errorf("file tree: %w", err)
139 }
140
141 file, err := tree.File(path)
142 if err != nil {
143 return "", err
144 }
145
146 isbin, _ := file.IsBinary()
147
148 if !isbin {
149 return file.Contents()
150 } else {
151 return "Not displaying binary file", nil
152 }
153}
154
155func (g *GitRepo) Tags() ([]*TagReference, error) {
156 iter, err := g.r.Tags()
157 if err != nil {
158 return nil, fmt.Errorf("tag objects: %w", err)
159 }
160
161 tags := make([]*TagReference, 0)
162
163 if err := iter.ForEach(func(ref *plumbing.Reference) error {
164 obj, err := g.r.TagObject(ref.Hash())
165 switch err {
166 case nil:
167 tags = append(tags, &TagReference{
168 ref: ref,
169 tag: obj,
170 })
171 case plumbing.ErrObjectNotFound:
172 tags = append(tags, &TagReference{
173 ref: ref,
174 })
175 default:
176 return err
177 }
178 return nil
179 }); err != nil {
180 return nil, err
181 }
182
183 tagList := &TagList{r: g.r, refs: tags}
184 sort.Sort(tagList)
185 return tags, nil
186}
187
188func (g *GitRepo) Branches() ([]*plumbing.Reference, error) {
189 bi, err := g.r.Branches()
190 if err != nil {
191 return nil, fmt.Errorf("branchs: %w", err)
192 }
193
194 branches := []*plumbing.Reference{}
195
196 _ = bi.ForEach(func(ref *plumbing.Reference) error {
197 branches = append(branches, ref)
198 return nil
199 })
200
201 return branches, nil
202}
203
204func (g *GitRepo) FindMainBranch(branches []string) (string, error) {
205 for _, b := range branches {
206 _, err := g.r.ResolveRevision(plumbing.Revision(b))
207 if err == nil {
208 return b, nil
209 }
210 }
211 return "", fmt.Errorf("unable to find main branch")
212}
213
214// WriteTar writes itself from a tree into a binary tar file format.
215// prefix is root folder to be appended.
216func (g *GitRepo) WriteTar(w io.Writer, prefix string) error {
217 tw := tar.NewWriter(w)
218 defer tw.Close()
219
220 c, err := g.r.CommitObject(g.h)
221 if err != nil {
222 return fmt.Errorf("commit object: %w", err)
223 }
224
225 tree, err := c.Tree()
226 if err != nil {
227 return err
228 }
229
230 walker := object.NewTreeWalker(tree, true, nil)
231 defer walker.Close()
232
233 name, entry, err := walker.Next()
234 for ; err == nil; name, entry, err = walker.Next() {
235 info, err := newInfoWrapper(name, prefix, &entry, tree)
236 if err != nil {
237 return err
238 }
239
240 header, err := tar.FileInfoHeader(info, "")
241 if err != nil {
242 return err
243 }
244
245 err = tw.WriteHeader(header)
246 if err != nil {
247 return err
248 }
249
250 if !info.IsDir() {
251 file, err := tree.File(name)
252 if err != nil {
253 return err
254 }
255
256 reader, err := file.Blob.Reader()
257 if err != nil {
258 return err
259 }
260
261 _, err = io.Copy(tw, reader)
262 if err != nil {
263 reader.Close()
264 return err
265 }
266 reader.Close()
267 }
268 }
269
270 return nil
271}
272
273func newInfoWrapper(
274 name string,
275 prefix string,
276 entry *object.TreeEntry,
277 tree *object.Tree,
278) (*infoWrapper, error) {
279 var (
280 size int64
281 mode fs.FileMode
282 isDir bool
283 )
284
285 if entry.Mode.IsFile() {
286 file, err := tree.TreeEntryFile(entry)
287 if err != nil {
288 return nil, err
289 }
290 mode = fs.FileMode(file.Mode)
291
292 size, err = tree.Size(name)
293 if err != nil {
294 return nil, err
295 }
296 } else {
297 isDir = true
298 mode = fs.ModeDir | fs.ModePerm
299 }
300
301 fullname := path.Join(prefix, name)
302 return &infoWrapper{
303 name: fullname,
304 size: size,
305 mode: mode,
306 modTime: time.Unix(0, 0),
307 isDir: isDir,
308 }, nil
309}
310
311func (i *infoWrapper) Name() string {
312 return i.name
313}
314
315func (i *infoWrapper) Size() int64 {
316 return i.size
317}
318
319func (i *infoWrapper) Mode() fs.FileMode {
320 return i.mode
321}
322
323func (i *infoWrapper) ModTime() time.Time {
324 return i.modTime
325}
326
327func (i *infoWrapper) IsDir() bool {
328 return i.isDir
329}
330
331func (i *infoWrapper) Sys() any {
332 return nil
333}
334
335func (t *TagReference) Name() string {
336 return t.ref.Name().Short()
337}
338
339func (t *TagReference) Message() string {
340 if t.tag != nil {
341 return t.tag.Message
342 }
343 return ""
344}