1package git
2
3import (
4 "bufio"
5 "bytes"
6 "io"
7 "io/fs"
8 "path/filepath"
9 "sort"
10
11 "github.com/gogs/git-module"
12)
13
14// Tree is a wrapper around git.Tree with helper methods.
15type Tree struct {
16 *git.Tree
17 Path string
18}
19
20// TreeEntry is a wrapper around git.TreeEntry with helper methods.
21type TreeEntry struct {
22 *git.TreeEntry
23 // path is the full path of the file
24 path string
25}
26
27// Entries is a wrapper around git.Entries
28type Entries []*TreeEntry
29
30var sorters = []func(t1, t2 *TreeEntry) bool{
31 func(t1, t2 *TreeEntry) bool {
32 return (t1.IsTree() || t1.IsCommit()) && !t2.IsTree() && !t2.IsCommit()
33 },
34 func(t1, t2 *TreeEntry) bool {
35 return t1.Name() < t2.Name()
36 },
37}
38
39// Len implements sort.Interface.
40func (es Entries) Len() int { return len(es) }
41
42// Swap implements sort.Interface.
43func (es Entries) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
44
45// Less implements sort.Interface.
46func (es Entries) Less(i, j int) bool {
47 t1, t2 := es[i], es[j]
48 var k int
49 for k = 0; k < len(sorters)-1; k++ {
50 sorter := sorters[k]
51 switch {
52 case sorter(t1, t2):
53 return true
54 case sorter(t2, t1):
55 return false
56 }
57 }
58 return sorters[k](t1, t2)
59}
60
61// Sort sorts the entries in the tree.
62func (es Entries) Sort() {
63 sort.Sort(es)
64}
65
66// File is a wrapper around git.Blob with helper methods.
67type File struct {
68 *git.Blob
69 Entry *TreeEntry
70}
71
72// Name returns the name of the file.
73func (f *File) Name() string {
74 return f.Entry.Name()
75}
76
77// Path returns the full path of the file.
78func (f *File) Path() string {
79 return f.Entry.path
80}
81
82// SubTree returns the sub-tree at the given path.
83func (t *Tree) SubTree(path string) (*Tree, error) {
84 tree, err := t.Subtree(path)
85 if err != nil {
86 return nil, err
87 }
88 return &Tree{
89 Tree: tree,
90 Path: path,
91 }, nil
92}
93
94// Entries returns the entries in the tree.
95func (t *Tree) Entries() (Entries, error) {
96 entries, err := t.Tree.Entries()
97 if err != nil {
98 return nil, err
99 }
100 ret := make(Entries, len(entries))
101 for i, e := range entries {
102 ret[i] = &TreeEntry{
103 TreeEntry: e,
104 path: filepath.Join(t.Path, e.Name()),
105 }
106 }
107 return ret, nil
108}
109
110func (t *Tree) TreeEntry(path string) (*TreeEntry, error) {
111 entry, err := t.Tree.TreeEntry(path)
112 if err != nil {
113 return nil, err
114 }
115 return &TreeEntry{
116 TreeEntry: entry,
117 path: filepath.Join(t.Path, entry.Name()),
118 }, nil
119}
120
121const sniffLen = 8000
122
123// IsBinary detects if data is a binary value based on:
124// http://git.kernel.org/cgit/git/git.git/tree/xdiff-interface.c?id=HEAD#n198
125func IsBinary(r io.Reader) (bool, error) {
126 reader := bufio.NewReader(r)
127 c := 0
128 for {
129 if c == sniffLen {
130 break
131 }
132
133 b, err := reader.ReadByte()
134 if err == io.EOF {
135 break
136 }
137 if err != nil {
138 return false, err
139 }
140
141 if b == byte(0) {
142 return true, nil
143 }
144
145 c++
146 }
147
148 return false, nil
149}
150
151// IsBinary returns true if the file is binary.
152func (f *File) IsBinary() (bool, error) {
153 stdout := new(bytes.Buffer)
154 stderr := new(bytes.Buffer)
155 err := f.Pipeline(stdout, stderr)
156 if err != nil {
157 return false, err
158 }
159 r := bufio.NewReader(stdout)
160 return IsBinary(r)
161}
162
163// Mode returns the mode of the file in fs.FileMode format.
164func (e *TreeEntry) Mode() fs.FileMode {
165 m := e.Blob().Mode()
166 switch m {
167 case git.EntryTree:
168 return fs.ModeDir | fs.ModePerm
169 default:
170 return fs.FileMode(m)
171 }
172}
173
174// File returns the file for the TreeEntry.
175func (e *TreeEntry) File() *File {
176 b := e.Blob()
177 return &File{
178 Blob: b,
179 Entry: e,
180 }
181}
182
183// Contents returns the contents of the file.
184func (e *TreeEntry) Contents() ([]byte, error) {
185 return e.File().Contents()
186}
187
188// Contents returns the contents of the file.
189func (f *File) Contents() ([]byte, error) {
190 return f.Blob.Bytes()
191}