-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
319 lines (290 loc) · 7.22 KB
/
fs.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package g
import (
"errors"
"os"
"path/filepath"
"strings"
"time"
)
const (
// NotUpdated means that the modification time of the file in the
// commit is the same as the one in the index.
NotUpdated IndexStatus = iota
// UpdatedInIndex means that the modification time of the file in the
// index is newer than that of the commit
UpdatedInIndex
// TypeChangedInIndex mentioned in the Git docs but not implemented
// here - file type changed (regular file, symbolic link or submodule)
TypeChangedInIndex
// AddedInIndex means that the file is in the index but not in the
// commit.
AddedInIndex
// DeletedInIndex means that the file has been removed from the index
DeletedInIndex
// RenamedInIndex - @todo I do not understand how to implement this
RenamedInIndex
// CopiedInIndex - @todo not sure about this one either
// - copied (if config option status.renames is set to "copies")
CopiedInIndex
// UntrackedInIndex means that the file is not in the Index
UntrackedInIndex
)
const (
// IndexAndWorkingTreeMatch means the file modification time in the
// working directory is the same as in the index
IndexAndWorkingTreeMatch WDStatus = iota
// WorktreeChangedSinceIndex means the file in the working directory has
// a newer modification time than the file in the index
WorktreeChangedSinceIndex
// TypeChangedInWorktreeSinceIndex is not implemented
TypeChangedInWorktreeSinceIndex
// DeletedInWorktree means that the file has been removed from the working
// directory but exists in the commit
DeletedInWorktree
// RenamedInWorktree not implemented
RenamedInWorktree
// CopiedInWorktree not implemented
CopiedInWorktree
// Untracked means that the file is in the working directory but not in
// the index or commit
Untracked
)
type (
FileStatus struct {
path string
idxStatus IndexStatus
wdStatus WDStatus
index *fileInfo
wd *fileInfo
commit *fileInfo
}
fileInfo struct {
Sha Sha
Finfo os.FileInfo
}
FfileSet struct {
files []*FileStatus
idx map[string]*FileStatus
}
IndexStatus uint8
WDStatus uint8
Finfo struct {
CTimeS uint32
CTimeN uint32
MTimeS uint32
MTimeN uint32
Dev uint32
Ino uint32
MMode uint32
Uid uint32
Gid uint32
SSize uint32
Sha [20]byte
NName string
}
Mtime struct {
Sec uint32
Nsec uint32
}
)
func (is IndexStatus) StatusString() string {
switch is {
case NotUpdated:
return " "
case UpdatedInIndex:
return "M"
case TypeChangedInIndex:
return "T"
case AddedInIndex:
return "A"
case DeletedInIndex:
return "D"
case RenamedInIndex:
return "R"
case CopiedInIndex:
return "C"
case UntrackedInIndex:
return "?"
default:
return ""
}
}
func (wds WDStatus) StatusString() string {
switch wds {
case IndexAndWorkingTreeMatch:
return " "
case WorktreeChangedSinceIndex:
return "M"
case TypeChangedInWorktreeSinceIndex:
return "T"
case DeletedInWorktree:
return "D"
case RenamedInWorktree:
return "R"
case CopiedInWorktree:
return "C"
case Untracked:
return "?"
default:
return ""
}
}
// Ls recursively lists files in path that are not ignored
func Ls(path string) ([]*FileStatus, error) {
var files []*FileStatus
if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
// do not add ignored files
if !IsIgnored(path, config.GitIgnore) {
files = append(files, &FileStatus{
path: strings.TrimPrefix(path, WorkingDirectory()),
wd: &fileInfo{
Finfo: info,
},
})
}
return nil
}); err != nil {
return files, err
}
return files, nil
}
func (fi *Finfo) Name() string {
return fi.NName
}
func (fi *Finfo) Size() int64 { return int64(fi.SSize) }
func (fi *Finfo) Mode() os.FileMode { return 0 }
func (fi *Finfo) IsDir() bool { return false }
func (fi *Finfo) Sys() any { return nil }
func (fi *Finfo) ModTime() time.Time {
return time.Unix(int64(fi.MTimeS), int64(fi.MTimeN))
}
func (f FileStatus) Path() string {
return f.path
}
func (f FileStatus) IndexStatus() IndexStatus {
return f.idxStatus
}
func (f FileStatus) WorkingDirectoryStatus() WDStatus {
return f.wdStatus
}
func NewFfileSet(c []*FileStatus, i []*FileStatus, w []*FileStatus) (*FfileSet, error) {
fs := &FfileSet{}
fs.idx = make(map[string]*FileStatus)
for _, v := range c {
fs.idx[v.path] = v
fs.files = append(fs.files, v)
}
fs.mergeFiles(i, 2)
fs.mergeFiles(w, 3)
return fs, fs.updateStatus()
}
func (f *FfileSet) updateStatus() error {
for _, v := range f.files {
// worktree status
switch true {
case v.index != nil && v.wd == nil:
v.wdStatus = DeletedInWorktree
case v.index != nil && v.wd != nil && v.index.Finfo.ModTime().Compare(v.wd.Finfo.ModTime()) == 0:
v.wdStatus = IndexAndWorkingTreeMatch
case v.index != nil && v.wd != nil && v.index.Finfo.ModTime().Compare(v.wd.Finfo.ModTime()) != 0:
v.wdStatus = WorktreeChangedSinceIndex
case v.index == nil && v.wd != nil:
v.wdStatus = Untracked
case v.commit != nil && v.index == nil && v.wd == nil:
// in a commit but not in the index or wd
v.wdStatus = IndexAndWorkingTreeMatch // because should flag as deleted in index status
default:
return errors.New("no matches for working tree status")
}
// index status
switch true {
case v.commit == nil && v.index == nil:
v.idxStatus = UntrackedInIndex
case v.commit != nil && v.index == nil:
v.idxStatus = DeletedInIndex
case v.commit != nil && v.index != nil && !v.index.Sha.Matches(v.commit.Sha):
v.idxStatus = UpdatedInIndex
case v.commit != nil && v.index != nil && v.index.Sha.Matches(v.commit.Sha):
v.idxStatus = NotUpdated
case v.commit == nil && v.index != nil:
v.idxStatus = AddedInIndex
default:
return errors.New("no matches for index status")
}
}
return nil
}
func (f *FfileSet) mergeFiles(files []*FileStatus, ciw int) {
for _, v := range files {
v := v
ff, ok := f.idx[v.path]
if ok {
switch ciw {
case 1:
ff.commit = v.commit
case 2:
ff.index = v.index
case 3:
ff.wd = v.wd
}
} else {
f.files = append(f.files, v)
f.idx[v.path] = v
}
}
}
func (f *FfileSet) Files() []*FileStatus {
return f.files
}
func (f *FfileSet) Contains(path string) (*FileStatus, bool) {
v, ok := f.idx[path]
return v, ok
}
func (wds WDStatus) String() string {
switch wds {
case IndexAndWorkingTreeMatch:
return "IndexAndWorkingTreeMatch"
case WorktreeChangedSinceIndex:
return "WorktreeChangedSinceIndex"
case TypeChangedInWorktreeSinceIndex:
return "TypeChangedInWorktreeSinceIndex"
case DeletedInWorktree:
return "DeletedInWorktree"
case RenamedInWorktree:
return "RenamedInWorktree"
case CopiedInWorktree:
return "CopiedInWorktree"
case Untracked:
return "Untracked"
default:
return "UNKNOWN"
}
}
func (is IndexStatus) String() string {
switch is {
case NotUpdated:
return "NotUpdated"
case UpdatedInIndex:
return "UpdatedInIndex"
case TypeChangedInIndex:
return "TypeChangedInIndex"
case AddedInIndex:
return "AddedInIndex"
case DeletedInIndex:
return "DeletedInIndex"
case RenamedInIndex:
return "RenamedInIndex"
case CopiedInIndex:
return "CopiedInIndex"
case UntrackedInIndex:
return "UntrackedInIndex"
default:
return "UNKNOWN"
}
}