forked from speedata/gogit
-
Notifications
You must be signed in to change notification settings - Fork 32
/
tree_entry.go
133 lines (106 loc) · 2.03 KB
/
tree_entry.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
package git
import (
"os"
"sort"
"time"
)
// There are only a few file modes in Git. They look like unix file modes, but they can only be
// one of these.
const (
ModeBlob EntryMode = 0100644
ModeExec EntryMode = 0100755
ModeSymlink EntryMode = 0120000
ModeCommit EntryMode = 0160000
ModeTree EntryMode = 0040000
)
type EntryMode int
type Entries []*TreeEntry
var sorter = []func(t1, t2 *TreeEntry) bool{
func(t1, t2 *TreeEntry) bool {
return t1.IsDir() && !t2.IsDir()
},
func(t1, t2 *TreeEntry) bool {
return t1.name < t2.name
},
}
func (bs Entries) Len() int { return len(bs) }
func (bs Entries) Swap(i, j int) { bs[i], bs[j] = bs[j], bs[i] }
func (bs Entries) Less(i, j int) bool {
t1, t2 := bs[i], bs[j]
var k int
for k = 0; k < len(sorter)-1; k++ {
sort := sorter[k]
switch {
case sort(t1, t2):
return true
case sort(t2, t1):
return false
}
}
return sorter[k](t1, t2)
}
func (bs Entries) Sort() {
sort.Sort(bs)
}
type TreeEntry struct {
Id sha1
Type ObjectType
mode EntryMode
name string
ptree *Tree
// commit *Commit
commited bool
size int64
sized bool
// modTime time.Time
}
func (te *TreeEntry) Name() string {
return te.name
}
func (te *TreeEntry) Size() int64 {
if te.IsDir() {
return 0
}
if te.sized {
return te.size
}
size, err := te.ptree.repo.objectSize(te.Id)
if err != nil {
return 0
}
te.sized = true
te.size = size
return te.size
}
func (te *TreeEntry) Mode() (mode os.FileMode) {
switch te.mode {
case ModeBlob, ModeSymlink:
mode = mode | 0644
case ModeExec:
fallthrough
default:
mode = mode | 0755
}
switch te.mode {
case ModeTree:
mode = mode | os.ModeDir
case ModeSymlink:
mode = mode | os.ModeSymlink
}
return
}
func (te *TreeEntry) ModTime() time.Time {
return time.Now()
}
func (te *TreeEntry) IsDir() bool {
return te.mode == ModeTree
}
func (te *TreeEntry) Sys() interface{} {
return nil
}
func (te *TreeEntry) EntryMode() EntryMode {
return te.mode
}
func (te *TreeEntry) Blob() *Blob {
return &Blob{TreeEntry: te}
}