-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
256 lines (224 loc) · 6.63 KB
/
index.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package model
import (
"bytes"
"encoding/hex"
"fmt"
"math"
"os"
"path"
"slices"
"sort"
"strconv"
"github.com/ignorantshr/mgit/util"
)
type TimePair struct {
S int64
NS int64
}
type IndexEntry struct {
Ctime TimePair
Mtime TimePair
Device int64 // the ID of device containing this file
Inode int64 // inode
ModeType int // The object type, either b1000 (regular), b1010 (symlink), b1110 (gitlink)
ModePerms int // The object permissions, an integer
Uid int
Gid int
Fsize int64
Sha string
FlagAssumValid bool
FlagStage int64
Name string // full path
}
type Index struct {
Version int
Entries []*IndexEntry
}
func NewIndex(ver int, entries []*IndexEntry) *Index {
if ver < 2 {
ver = 2
}
i := &Index{Version: ver}
if len(entries) == 0 {
i.Entries = make([]*IndexEntry, 0)
} else {
i.Entries = entries
}
return i
}
func ReadIndex(repo *Repository) *Index {
indexFile, err := repo.RepoFile(false, "index")
util.PanicErr(err)
index := NewIndex(2, nil)
// new repository have no index
if !util.IsFile(indexFile) {
return index
}
raw, err := os.ReadFile(indexFile)
util.PanicErr(err)
header := raw[:12]
signature := header[:4]
if string(signature) != "DIRC" {
util.PanicErr(fmt.Errorf("index file header error: %v", signature))
}
version := util.BytesToInt(header[4:8])
if version != 2 {
util.PanicErr(fmt.Errorf("index file version error: %v", version))
}
count := util.BytesToInt(header[8:])
content := raw[12:]
idx := int64(0)
for i := 0; i < count; i++ {
ctime_s := util.BytesToInt64(content[idx : idx+4])
ctime_ns := util.BytesToInt64(content[idx+4 : idx+8])
mtime_s := util.BytesToInt64(content[idx+8 : idx+12])
mtime_ns := util.BytesToInt64(content[idx+12 : idx+16])
device := util.BytesToInt64(content[idx+16 : idx+20])
ino := util.BytesToInt64(content[idx+20 : idx+24])
// unused := util.BytesToInt(content[idx+24:idx+26]) // assert
mode := util.BytesToInt(content[idx+26 : idx+28])
modeType := mode >> 12
modePerms := mode & 0b0000000111111111
uid := util.BytesToInt(content[idx+28 : idx+32])
gid := util.BytesToInt(content[idx+32 : idx+36])
fsize := util.BytesToInt64(content[idx+36 : idx+40])
sha := hex.EncodeToString(content[idx+40 : idx+60]) // fmt.Sprintf("%40x", new(big.Int).SetBytes(content[idx+40:idx+60])) // 将字节序列表示的整数转换为一个 40 字节长度的十六进制字符串
util.PanicErr(err)
flags := util.BytesToInt64(content[idx+60 : idx+62])
flagAssumValid := (flags & 0b1000000000000000) != 0
// flagExtend := (flags & 0b0100000000000000) != 0 // assert
flagStage := flags & 0b0011000000000000
nameLength := flags & 0b0000111111111111 // 12bit 存储,最大 0xFFF,可能会溢出,所以继续向后寻找直到 0x00
idx += 62
var rawName []byte
if nameLength < 0xFFF {
if content[idx+nameLength] != 0x00 {
util.PanicErr(fmt.Errorf("name length parse fail"))
}
rawName = content[idx : idx+nameLength]
idx += nameLength + 1
} else {
nullIdx := bytes.IndexByte(content[idx+0xFFF:], '\x00')
rawName = content[idx : idx+0xFFF+int64(nullIdx)]
idx = int64(nullIdx) + 1
}
name := string(rawName)
idx = 8 * int64(math.Ceil(float64(idx)/8))
index.Entries = append(index.Entries, &IndexEntry{
Ctime: TimePair{int64(ctime_s), ctime_ns},
Mtime: TimePair{mtime_s, mtime_ns},
Device: device,
Inode: ino,
ModeType: int(modeType),
ModePerms: int(modePerms),
Uid: int(uid),
Gid: int(gid),
Fsize: fsize,
Sha: sha,
FlagAssumValid: flagAssumValid,
FlagStage: flagStage,
Name: name,
})
}
return index
}
func WriteIndex(repo *Repository, index *Index) {
p, err := repo.RepoFile(false, "index")
util.PanicErr(err)
f, err := os.OpenFile(p, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
util.PanicErr(err)
defer f.Close()
// 字节宽度,int 值
wrinteger := func(width, value int) {
f.Write(util.IntToBytes(value, width))
}
// HEADER
f.WriteString("DIRC")
wrinteger(4, index.Version)
wrinteger(4, len(index.Entries))
// ENTRIES
idx := 0
for _, e := range index.Entries {
wrinteger(4, int(e.Ctime.S))
wrinteger(4, int(e.Ctime.NS))
wrinteger(4, int(e.Mtime.S))
wrinteger(4, int(e.Mtime.NS))
wrinteger(4, int(e.Device))
wrinteger(4, int(e.Inode))
wrinteger(4, int(e.ModeType<<12|e.ModePerms))
wrinteger(4, int(e.Uid))
wrinteger(4, int(e.Gid))
wrinteger(4, int(e.Fsize))
sha, _ := hex.DecodeString(e.Sha)
f.Write(sha)
flagAssumValid := 0
if e.FlagAssumValid {
flagAssumValid = 0x1 << 15
}
nameLen := len(e.Name)
if nameLen >= 0xFFF {
nameLen = 0xFFF
}
wrinteger(2, flagAssumValid|int(e.FlagStage)|nameLen)
f.WriteString(e.Name)
// 0x00
wrinteger(1, 0)
idx += 62 + nameLen + 1
if idx%8 != 0 {
pad := 8 - idx%8
buf := make([]byte, pad)
f.Write(buf)
idx += pad
}
}
}
func Index2Tree(repo *Repository, index *Index) string {
// 遍历 index 文件,根据文件路径由深到浅逐层构建 tree 结构
contents := make(map[string][]any)
sortpaths := make([]string, 0)
for _, e := range index.Entries {
dir := path.Dir(e.Name)
contents[dir] = append(contents[dir], e)
sortpaths = append(sortpaths, dir)
key := dir
// 对每一级目录都建立一个列表项
for key != "." {
if _, ok := contents[key]; !ok {
contents[key] = []any{}
}
key = path.Dir(key)
}
}
// 路径长度倒序
sort.Slice(sortpaths, func(i, j int) bool {
return len(sortpaths[i]) > len(sortpaths[j])
})
// 去重
sortpaths = slices.Compact[[]string](sortpaths)
sha := "" // 记录 tree 根的 sha
for _, p := range sortpaths {
tree := NewTreeObj()
// 每个文件路径构建一棵树
for _, e := range contents[p] {
// An entry can be a normal GitIndexEntry read from the index,
// or a tree we've created.
var leaf *treeLeaf
switch v := e.(type) {
case *IndexEntry:
mode, _ := strconv.Atoi(fmt.Sprintf("%02o%04o", v.ModeType, v.ModePerms))
leafMode := string(util.IntToBytes(mode, 6))
leaf = &treeLeaf{Mode: leafMode, Path: path.Base(v.Name), Sha: v.Sha}
case [2]string:
mode, _ := strconv.Atoi(fmt.Sprintf("%02o%04o", 4, 0))
leafMode := string(util.IntToBytes(mode, 6))
leaf = &treeLeaf{Mode: leafMode, Path: path.Base(v[0]), Sha: v[1]}
}
tree.items = append(tree.items, leaf)
}
sha = WriteObject(repo, tree) // 写子树到磁盘
parent := path.Dir(p)
base := path.Base(p)
contents[parent] = append(contents[parent], [2]string{base, sha}) // 加到父目录项中
}
return sha
}