forked from sourcegraph/vcsstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
142 lines (119 loc) · 2.91 KB
/
store.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
// Copyright 2013 The hgo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package store provides access to Mercurial's ‘store’ repository format.
package store
import (
"errors"
"path/filepath"
"strings"
"github.com/beyang/hgo/revlog"
)
type Store struct {
root string
fe *filenameEncoder
}
func New(repoRoot string, requires map[string]bool) (s *Store) {
s = new(Store)
s.root = filepath.Join(repoRoot, ".hg", "store")
s.fe = newFilenameEncoder(requires)
return
}
func (s *Store) OpenRevlog(fileName string) (*revlog.Index, error) {
i, d := s.fe.Encode("data/" + fileName)
return revlog.Open(&encodedName{s.root, i, d})
}
type Manifests struct {
*revlog.Index
}
func (s *Store) OpenManifests() (m *Manifests, err error) {
const name = "00manifest"
c, err := revlog.Open(&encodedName{s.root, name, name})
if err == nil {
m = &Manifests{c}
}
return
}
type Manifest []ManifestEnt
func (m *Manifests) LookupRevision(linkrev int, wantId revlog.NodeId) (r *revlog.Rec, err error) {
r, err = revlog.FileRevSpec(linkrev).Lookup(m.Index)
if err != nil {
r = m.Tip()
err = nil
}
for int(r.Linkrev) > linkrev {
r = r.Prev()
}
if !wantId.Eq(r.Id()) {
err = errors.New("manifest node id does not match changelog entry")
}
return
}
func BuildManifest(r *revlog.Rec, fb *revlog.FileBuilder) (m Manifest, err error) {
err = fb.BuildWrite(nil, r)
if err != nil {
return
}
m, err = ParseManifestData(fb.Bytes())
return
}
// Create a map with filename keys from a list of manifest entries.
func (list Manifest) Map() (m map[string]*ManifestEnt) {
m = make(map[string]*ManifestEnt, len(list))
for i, e := range list {
m[e.FileName] = &list[i]
}
return
}
type ManifestEnt struct {
FileName string
hash string
}
func (e *ManifestEnt) value() (hash, opt string) {
hash = e.hash
if n := len(hash); n%2 == 1 {
n--
opt = hash[n:]
hash = hash[:n]
}
return
}
func (e *ManifestEnt) IsLink() bool {
_, o := e.value()
return o == "l"
}
func (e *ManifestEnt) IsExecutable() bool {
_, o := e.value()
return o == "x"
}
func (e *ManifestEnt) Id() (revlog.NodeId, error) {
hash, _ := e.value()
return revlog.NewId(hash)
}
func ParseManifestData(data []byte) (m Manifest, err error) {
for _, line := range strings.Split(string(data), "\n") {
f := strings.SplitN(line, "\000", 2)
if len(f) != 2 {
continue
}
m = append(m, ManifestEnt{f[0], f[1]})
}
return
}
func (s *Store) OpenChangeLog() (*revlog.Index, error) {
const name = "00changelog"
return revlog.Open(&encodedName{s.root, name, name})
}
type encodedName struct {
root string
indexPart string
dataPart string
}
func (e *encodedName) Index() string {
s := filepath.FromSlash(e.indexPart)
return filepath.Join(e.root, s+".i")
}
func (e *encodedName) Data() string {
s := filepath.FromSlash(e.dataPart)
return filepath.Join(e.root, s+".d")
}