forked from golang/dep
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lock.go
210 lines (173 loc) · 4.6 KB
/
lock.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
// Copyright 2016 The Go 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 main
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"sort"
"github.com/sdboyer/gps"
)
type lock struct {
Memo []byte
P []gps.LockedProject
}
type rawLock struct {
Memo string `json:"memo"`
P []lockedDep `json:"projects"`
}
type lockedDep struct {
Name string `json:"name"`
Version string `json:"version,omitempty"`
Branch string `json:"branch,omitempty"`
Revision string `json:"revision"`
Repository string `json:"repo,omitempty"`
Packages []string `json:"packages"`
}
func readLock(r io.Reader) (*lock, error) {
rl := rawLock{}
err := json.NewDecoder(r).Decode(&rl)
if err != nil {
return nil, err
}
b, err := hex.DecodeString(rl.Memo)
if err != nil {
return nil, fmt.Errorf("invalid hash digest in lock's memo field")
}
l := &lock{
Memo: b,
P: make([]gps.LockedProject, len(rl.P)),
}
for i, ld := range rl.P {
r := gps.Revision(ld.Revision)
var v gps.Version
if ld.Version != "" {
if ld.Branch != "" {
return nil, fmt.Errorf("lock file specified both a branch (%s) and version (%s) for %s", ld.Branch, ld.Version, ld.Name)
}
v = gps.NewVersion(ld.Version).Is(r)
} else if ld.Branch != "" {
v = gps.NewBranch(ld.Branch).Is(r)
} else if r == "" {
return nil, fmt.Errorf("lock file has entry for %s, but specifies no version", ld.Name)
} else {
v = r
}
id := gps.ProjectIdentifier{
ProjectRoot: gps.ProjectRoot(ld.Name),
Source: ld.Repository,
}
l.P[i] = gps.NewLockedProject(id, v, ld.Packages)
}
return l, nil
}
func (l *lock) InputHash() []byte {
return l.Memo
}
func (l *lock) Projects() []gps.LockedProject {
return l.P
}
func (l *lock) MarshalJSON() ([]byte, error) {
raw := rawLock{
Memo: hex.EncodeToString(l.Memo),
P: make([]lockedDep, len(l.P)),
}
sort.Sort(sortedLockedProjects(l.P))
for k, lp := range l.P {
id := lp.Ident()
ld := lockedDep{
Name: string(id.ProjectRoot),
Repository: id.Source,
Packages: lp.Packages(),
}
v := lp.Version()
// Figure out how to get the underlying revision
switch tv := v.(type) {
case gps.UnpairedVersion:
// TODO we could error here, if we want to be very defensive about not
// allowing a lock to be written if without an immmutable revision
case gps.Revision:
ld.Revision = tv.String()
case gps.PairedVersion:
ld.Revision = tv.Underlying().String()
}
switch v.Type() {
case gps.IsBranch:
ld.Branch = v.String()
case gps.IsSemver, gps.IsVersion:
ld.Version = v.String()
}
raw.P[k] = ld
}
// TODO sort output - #15
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
enc.SetEscapeHTML(false)
err := enc.Encode(raw)
return buf.Bytes(), err
}
// lockFromInterface converts an arbitrary gps.Lock to dep's representation of a
// lock. If the input is already dep's *lock, the input is returned directly.
//
// Data is defensively copied wherever necessary to ensure the resulting *lock
// shares no memory with the original lock.
//
// As gps.Solution is a superset of gps.Lock, this can also be used to convert
// solutions to dep's lock format.
func lockFromInterface(in gps.Lock) *lock {
if in == nil {
return nil
} else if l, ok := in.(*lock); ok {
return l
}
h, p := in.InputHash(), in.Projects()
l := &lock{
Memo: make([]byte, len(h)),
P: make([]gps.LockedProject, len(p)),
}
copy(l.Memo, h)
copy(l.P, p)
return l
}
type sortedLockedProjects []gps.LockedProject
func (s sortedLockedProjects) Len() int { return len(s) }
func (s sortedLockedProjects) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s sortedLockedProjects) Less(i, j int) bool {
l, r := s[i].Ident(), s[j].Ident()
if l.ProjectRoot < r.ProjectRoot {
return true
}
if r.ProjectRoot < l.ProjectRoot {
return false
}
return l.Source < r.Source
}
// locksAreEquivalent compares two locks to see if they differ. If EITHER lock
// is nil, or their memos do not match, or any projects differ, then false is
// returned.
func locksAreEquivalent(l, r *lock) bool {
if l == nil || r == nil {
return false
}
if !bytes.Equal(l.Memo, r.Memo) {
return false
}
if len(l.P) != len(r.P) {
return false
}
sort.Sort(sortedLockedProjects(l.P))
sort.Sort(sortedLockedProjects(r.P))
for k, lp := range l.P {
// TODO(sdboyer) gps will be adding a func to compare LockedProjects in the next
// version; we can swap that in here when it lands
rp := r.P[k]
if !lp.Eq(rp) {
return false
}
}
return true
}