forked from sourcegraph/vcsstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
570 lines (496 loc) · 13 KB
/
repo.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package hg
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"net/mail"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"github.com/beyang/hgo"
hg_changelog "github.com/beyang/hgo/changelog"
hg_revlog "github.com/beyang/hgo/revlog"
hg_store "github.com/beyang/hgo/store"
"golang.org/x/tools/godoc/vfs"
"sourcegraph.com/sourcegraph/go-vcs/vcs"
"sourcegraph.com/sourcegraph/go-vcs/vcs/hgcmd"
"sourcegraph.com/sourcegraph/go-vcs/vcs/internal"
"sourcegraph.com/sourcegraph/go-vcs/vcs/util"
"sourcegraph.com/sqs/pbtypes"
)
func init() {
// Overwrite the hg opener to return repositories that use the
// faster native-Go hg implementation.
vcs.RegisterOpener("hg", func(dir string) (vcs.Repository, error) {
return Open(dir)
})
}
type Repository struct {
*hgcmd.Repository
u *hgo.Repository
st *hg_store.Store
cl *hg_revlog.Index
allTags *hgo.Tags
branchHeads *hgo.BranchHeads
}
func Open(dir string) (*Repository, error) {
r, err := hgo.OpenRepository(dir)
if err != nil {
return nil, err
}
st := r.NewStore()
cl, err := st.OpenChangeLog()
if err != nil {
return nil, err
}
globalTags, allTags := r.Tags()
globalTags.Sort()
allTags.Sort()
allTags.Add("tip", cl.Tip().Id().Node())
bh, err := r.BranchHeads()
if err != nil {
return nil, err
}
cr, err := hgcmd.Open(dir)
if err != nil {
return nil, err
}
return &Repository{cr, r, st, cl, allTags, bh}, nil
}
func (r *Repository) ResolveRevision(spec string) (vcs.CommitID, error) {
if id, err := r.ResolveBranch(spec); err == nil {
return id, nil
}
if id, err := r.ResolveTag(spec); err == nil {
return id, nil
}
rec, err := r.parseRevisionSpec(spec).Lookup(r.cl)
if err != nil {
if err == hg_revlog.ErrRevNotFound || err == hex.ErrLength {
return "", vcs.ErrRevisionNotFound
}
return "", err
}
return vcs.CommitID(hex.EncodeToString(rec.Id())), nil
}
func (r *Repository) ResolveTag(name string) (vcs.CommitID, error) {
if id, ok := r.allTags.IdByName[name]; ok {
return vcs.CommitID(id), nil
}
return "", vcs.ErrTagNotFound
}
func (r *Repository) ResolveBranch(name string) (vcs.CommitID, error) {
if id, ok := r.branchHeads.IdByName[name]; ok {
return vcs.CommitID(id), nil
}
return "", vcs.ErrBranchNotFound
}
func (r *Repository) Branches(opt vcs.BranchesOptions) ([]*vcs.Branch, error) {
if opt.ContainsCommit != "" {
return nil, fmt.Errorf("vcs.BranchesOptions.ContainsCommit option not implemented")
}
bs := make([]*vcs.Branch, len(r.branchHeads.IdByName))
i := 0
for name, id := range r.branchHeads.IdByName {
bs[i] = &vcs.Branch{Name: name, Head: vcs.CommitID(id)}
i++
}
sort.Sort(vcs.Branches(bs))
return bs, nil
}
func (r *Repository) Tags() ([]*vcs.Tag, error) {
ts := make([]*vcs.Tag, len(r.allTags.IdByName))
i := 0
for name, id := range r.allTags.IdByName {
ts[i] = &vcs.Tag{Name: name, CommitID: vcs.CommitID(id)}
i++
}
sort.Sort(vcs.Tags(ts))
return ts, nil
}
func (r *Repository) getRec(id vcs.CommitID) (*hg_revlog.Rec, error) {
rec, err := hg_revlog.NodeIdRevSpec(id).Lookup(r.cl)
if err == hg_revlog.ErrRevNotFound {
err = vcs.ErrCommitNotFound
}
return rec, err
}
func (r *Repository) GetCommit(id vcs.CommitID) (*vcs.Commit, error) {
rec, err := r.getRec(id)
if err != nil {
return nil, err
}
return r.makeCommit(rec)
}
func (r *Repository) Commits(opt vcs.CommitsOptions) ([]*vcs.Commit, uint, error) {
rec, err := r.getRec(opt.Head)
if err != nil {
return nil, 0, err
}
var commits []*vcs.Commit
total := uint(0)
for ; ; rec = rec.Prev() {
if total >= opt.Skip && (opt.N == 0 || uint(len(commits)) < opt.N) {
c, err := r.makeCommit(rec)
if err != nil {
return nil, 0, err
}
commits = append(commits, c)
}
total++
if rec.IsStartOfBranch() {
break
}
// If we want total, keep going until the end.
if !opt.NoTotal {
continue
}
// Otherwise return once N has been satisfied.
if opt.N != 0 && uint(len(commits)) >= opt.N {
break
}
}
if opt.NoTotal {
total = 0
}
return commits, total, nil
}
func (r *Repository) makeCommit(rec *hg_revlog.Rec) (*vcs.Commit, error) {
fb := hg_revlog.NewFileBuilder()
ce, err := hg_changelog.BuildEntry(rec, fb)
if err != nil {
return nil, err
}
addr, err := mail.ParseAddress(ce.Committer)
if err != nil {
// This occurs when the commit author specifier is
// malformed. Fall back to just using the whole committer
// string as the name.
addr = &mail.Address{
Name: ce.Committer,
Address: "",
}
}
var parents []vcs.CommitID
if !rec.IsStartOfBranch() {
if p := rec.Parent(); p != nil {
parents = append(parents, vcs.CommitID(hex.EncodeToString(rec.Parent().Id())))
}
if rec.Parent2Present() {
parents = append(parents, vcs.CommitID(hex.EncodeToString(rec.Parent2().Id())))
}
}
return &vcs.Commit{
ID: vcs.CommitID(ce.Id),
Author: vcs.Signature{addr.Name, addr.Address, pbtypes.NewTimestamp(ce.Date)},
Message: ce.Comment,
Parents: parents,
}, nil
}
func (r *Repository) FileSystem(at vcs.CommitID) (vfs.FileSystem, error) {
rec, err := r.getRec(at)
if err != nil {
return nil, err
}
return &hgFSNative{
dir: r.Dir,
at: hg_revlog.FileRevSpec(rec.FileRev()),
repo: r.u,
st: r.st,
cl: r.cl,
fb: hg_revlog.NewFileBuilder(),
}, nil
}
func (r *Repository) parseRevisionSpec(s string) hg_revlog.RevisionSpec {
if s == "" {
s = "tip"
// TODO(sqs): determine per-repository default branch name (not always "default"?)
}
if s == "tip" {
return hg_revlog.TipRevSpec{}
}
if s == "null" {
return hg_revlog.NullRevSpec{}
}
if id, ok := r.allTags.IdByName[s]; ok {
s = id
} else if i, err := strconv.Atoi(s); err == nil {
return hg_revlog.FileRevSpec(i)
}
return hg_revlog.NodeIdRevSpec(s)
}
type hgFSNative struct {
dir string
at hg_revlog.FileRevSpec
repo *hgo.Repository
st *hg_store.Store
cl *hg_revlog.Index
fb *hg_revlog.FileBuilder
}
func (fs *hgFSNative) manifestEntry(chgId hg_revlog.FileRevSpec, fileName string) (me *hg_store.ManifestEnt, err error) {
m, err := fs.getManifest(chgId)
if err != nil {
return
}
me = m.Map()[fileName]
if me == nil {
err = ErrFileNotInManifest
}
return
}
func (fs *hgFSNative) getManifest(chgId hg_revlog.FileRevSpec) (m hg_store.Manifest, err error) {
rec, err := chgId.Lookup(fs.cl)
if err != nil {
return
}
c, err := hg_changelog.BuildEntry(rec, fs.fb)
if err != nil {
return
}
// st := fs.repo.NewStore()
mlog, err := fs.st.OpenManifests()
if err != nil {
return nil, err
}
rec2, err := mlog.LookupRevision(int(c.Linkrev), c.ManifestNode)
if err != nil {
return nil, err
}
return hg_store.BuildManifest(rec2, fs.fb)
}
func (fs *hgFSNative) getEntry(path string) (*hg_revlog.Rec, *hg_store.ManifestEnt, error) {
fileLog, err := fs.st.OpenRevlog(path)
if err != nil {
return nil, nil, err
}
// Get file entry from manifest (so we can look up the correct record in the file revlog)
ent, err := fs.manifestEntry(fs.at, path)
if err != nil {
return nil, nil, err
}
// Lookup record in revlog
entId, err := ent.Id()
if err != nil {
return nil, nil, err
}
linkRevSpec := hg_revlog.LinkRevSpec{
Rev: int(fs.at),
FindPresent: func(maybeAncestors []*hg_revlog.Rec) (index int, err error) {
// Find the present file record by matching against the manifest entry.
//
// Note: this is necessary due to some edge case where the returned file record nodeid does not match the
// entry in the manifest. So far, have not been able to repro this outside the Python standard library (try
// Stat() on the README)
for a, anc := range maybeAncestors {
if anc.Id().Eq(entId) {
return a, nil
}
}
ancIds := make([]hg_revlog.NodeId, len(maybeAncestors))
for a, anc := range maybeAncestors {
ancIds[a] = anc.Id()
}
return 0, fmt.Errorf("failed to find file record with nodeid matching manifest entry nodeid %v, candidates were %v",
entId, ancIds)
},
}
rec, err := linkRevSpec.Lookup(fileLog)
if err != nil {
// HACK HACK. The above workaround for the Python standard
// library breaks go-vcs for the file "README" in
// https://golang.org/x/tools rev
// 536b79981a09daabecc6bc20b8dd4438a55dc12b. So, if we get an
// error using the linkRevSpec with the FindPresent func, try
// again without FindPresent.
linkRevSpec.FindPresent = nil
rec, err = linkRevSpec.Lookup(fileLog)
if err != nil {
return nil, nil, err
}
}
if rec.FileRev() == -1 {
return nil, nil, hg_revlog.ErrRevisionNotFound
}
if int(rec.Linkrev) == int(fs.at) {
// The requested revision matches this record, which can be
// used as a sign that the file exists. (TODO(sqs): original comments
// say maybe this means the file is NOT existent yet? the word "not" is
// not there but that seems to be a mistake.)
return rec, ent, nil
}
if !rec.IsLeaf() {
// There are other records that have the current record as a parent.
// This means, the file was existent, no need to check the manifest.
return rec, ent, nil
}
return rec, ent, nil
}
func (fs *hgFSNative) Open(name string) (vfs.ReadSeekCloser, error) {
name = internal.Rel(name)
rec, _, err := fs.getEntry(name)
if err != nil {
return nil, standardizeHgError(err)
}
data, err := fs.readFile(rec)
if err != nil {
return nil, err
}
return util.NopCloser{bytes.NewReader(data)}, nil
}
func (fs *hgFSNative) readFile(rec *hg_revlog.Rec) ([]byte, error) {
fb := hg_revlog.NewFileBuilder()
return fb.Build(rec)
}
func (fs *hgFSNative) getModTime() (time.Time, error) {
r, err := fs.at.Lookup(fs.cl)
if err != nil {
return time.Time{}, err
}
c, err := hg_changelog.BuildEntry(r, fs.fb)
if err != nil {
return time.Time{}, err
}
return c.Date, nil
}
func (fs *hgFSNative) Lstat(path string) (os.FileInfo, error) {
fi, _, err := fs.lstat(path)
return fi, err
}
func (fs *hgFSNative) lstat(path string) (*util.FileInfo, []byte, error) {
path = filepath.Clean(internal.Rel(path))
rec, ent, err := fs.getEntry(path)
if os.IsNotExist(err) {
// check if path is a dir (dirs are not in hg's manifest, so we need to
// hack around to get them).
fi, err := fs.dirStat(path)
return fi, nil, err
}
if err != nil {
return nil, nil, standardizeHgError(err)
}
fi := fs.fileInfo(ent)
// read data to determine file size
data, err := fs.readFile(rec)
if err != nil {
return nil, nil, err
}
fi.Size_ = int64(len(data))
return fi, data, nil
}
func (fs *hgFSNative) Stat(path string) (os.FileInfo, error) {
path = internal.Rel(path)
fi, data, err := fs.lstat(path)
if err != nil {
return nil, err
}
if fi.Mode()&os.ModeSymlink != 0 {
// derefence symlink
derefPath := string(data)
fi, err := fs.Lstat(derefPath)
if err != nil {
return nil, err
}
fi.(*util.FileInfo).Name_ = filepath.Base(path)
return fi, nil
}
return fi, nil
}
// dirStat determines whether a directory exists at path by listing files
// underneath it. If it has files, then it's a directory. We must do it this way
// because hg doesn't track directories in the manifest.
func (fs *hgFSNative) dirStat(path string) (*util.FileInfo, error) {
mtime, err := fs.getModTime()
if err != nil {
return nil, err
}
if path == "." {
return &util.FileInfo{
Name_: ".",
Mode_: os.ModeDir,
ModTime_: mtime,
}, nil
}
m, err := fs.getManifest(fs.at)
if err != nil {
return nil, err
}
dirPrefix := filepath.Clean(path) + "/"
for _, e := range m {
if strings.HasPrefix(e.FileName, dirPrefix) {
return &util.FileInfo{
Name_: filepath.Base(path),
Mode_: os.ModeDir,
ModTime_: mtime,
}, nil
}
}
return nil, os.ErrNotExist
}
func (fs *hgFSNative) fileInfo(ent *hg_store.ManifestEnt) *util.FileInfo {
var mode os.FileMode
mtime, err := fs.getModTime()
if err != nil {
return nil
}
if ent.IsExecutable() {
mode |= 0111 // +x
}
if ent.IsLink() {
mode |= os.ModeSymlink
}
return &util.FileInfo{
Name_: filepath.Base(ent.FileName),
Mode_: mode,
ModTime_: mtime,
}
}
func (fs *hgFSNative) ReadDir(path string) ([]os.FileInfo, error) {
path = internal.Rel(path)
m, err := fs.getManifest(fs.at)
if err != nil {
return nil, err
}
var fis []os.FileInfo
subdirs := make(map[string]struct{})
var dirPrefix string
if path := filepath.Clean(path); path == "." {
dirPrefix = ""
} else {
dirPrefix = path + "/"
}
for _, e := range m {
if !strings.HasPrefix(e.FileName, dirPrefix) {
continue
}
name := strings.TrimPrefix(e.FileName, dirPrefix)
dir := filepath.Dir(name)
if dir == "." {
fis = append(fis, fs.fileInfo(&e))
} else {
subdir := strings.SplitN(dir, "/", 2)[0]
if _, seen := subdirs[subdir]; !seen {
fis = append(fis, &util.FileInfo{Name_: subdir, Mode_: os.ModeDir})
subdirs[subdir] = struct{}{}
}
}
}
return fis, nil
}
func (fs *hgFSNative) String() string {
return fmt.Sprintf("hg repository %s commit %s (native)", fs.dir, fs.at)
}
func standardizeHgError(err error) error {
switch err {
case hg_revlog.ErrRevisionNotFound:
fallthrough
case ErrFileNotInManifest:
return os.ErrNotExist
default:
return err
}
}
var ErrFileNotInManifest = errors.New("file does not exist in given revision")