forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh.go
More file actions
250 lines (234 loc) · 6.99 KB
/
ssh.go
File metadata and controls
250 lines (234 loc) · 6.99 KB
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
package locking
import (
"fmt"
"strings"
"time"
"github.com/git-lfs/git-lfs/v3/errors"
"github.com/git-lfs/git-lfs/v3/git"
"github.com/git-lfs/git-lfs/v3/lfsapi"
"github.com/git-lfs/git-lfs/v3/ssh"
"github.com/git-lfs/git-lfs/v3/tr"
)
type sshLockClient struct {
transfer *ssh.SSHTransfer
*lfsapi.Client
}
func (c *sshLockClient) connection() *ssh.PktlineConnection {
return c.transfer.Connection(0)
}
func (c *sshLockClient) parseLockResponse(status int, args []string, lines []string) (*Lock, string, error) {
var lock *Lock
var message string
var err error
seen := make(map[string]struct{})
if status >= 200 && status <= 299 || status == 409 {
lock = &Lock{}
for _, entry := range args {
if strings.HasPrefix(entry, "id=") {
lock.Id = entry[3:]
seen["id"] = struct{}{}
} else if strings.HasPrefix(entry, "path=") {
lock.Path = entry[5:]
seen["path"] = struct{}{}
} else if strings.HasPrefix(entry, "ownername=") {
lock.Owner = &User{}
lock.Owner.Name = entry[10:]
seen["ownername"] = struct{}{}
} else if strings.HasPrefix(entry, "locked-at=") {
lock.LockedAt, err = time.Parse(time.RFC3339, entry[10:])
if err != nil {
return lock, "", errors.New(tr.Tr.Get("lock response: invalid locked-at: %s", entry))
}
seen["locked-at"] = struct{}{}
}
}
if len(seen) != 4 {
return nil, "", errors.New(tr.Tr.Get("incomplete fields for lock"))
}
}
if status > 299 && len(lines) > 0 {
message = lines[0]
}
return lock, message, nil
}
type owner string
const (
ownerOurs = owner("ours")
ownerTheirs = owner("theirs")
ownerUnknown = owner("")
)
type lockData struct {
lock Lock
who owner
}
func (c *sshLockClient) lockDataIsIncomplete(data *lockData) bool {
return data.lock.Path == "" || data.lock.Owner == nil || data.lock.LockedAt.IsZero()
}
func (c *sshLockClient) parseListLockResponse(status int, args []string, lines []string) (all []Lock, ours []Lock, theirs []Lock, nextCursor string, message string, err error) {
locks := make(map[string]*lockData)
var last *lockData
if status >= 200 && status <= 299 {
for _, entry := range args {
if strings.HasPrefix(entry, "next-cursor=") {
if len(nextCursor) > 0 {
return nil, nil, nil, "", "", errors.New(tr.Tr.Get("lock response: multiple next-cursor responses"))
}
nextCursor = entry[12:]
}
}
for _, entry := range lines {
values := strings.SplitN(entry, " ", 3)
var cmd string
if len(values) > 0 {
cmd = values[0]
}
if cmd == "lock" {
if len(values) != 2 {
return nil, nil, nil, "", "", errors.New(tr.Tr.Get("lock response: invalid response: %q", entry))
} else if last != nil && c.lockDataIsIncomplete(last) {
return nil, nil, nil, "", "", errors.New(tr.Tr.Get("lock response: incomplete lock data"))
}
id := values[1]
last = &lockData{who: ownerUnknown}
last.lock.Id = id
locks[id] = last
} else if len(values) != 3 {
return nil, nil, nil, "", "", errors.New(tr.Tr.Get("lock response: invalid response: %q", entry))
} else if last == nil || last.lock.Id != values[1] {
return nil, nil, nil, "", "", errors.New(tr.Tr.Get("lock response: interspersed response: %q", entry))
} else {
switch cmd {
case "path":
last.lock.Path = values[2]
case "owner":
last.who = owner(values[2])
case "ownername":
last.lock.Owner = &User{}
last.lock.Owner.Name = values[2]
case "locked-at":
last.lock.LockedAt, err = time.Parse(time.RFC3339, values[2])
if err != nil {
return nil, nil, nil, "", "", errors.New(tr.Tr.Get("lock response: invalid locked-at: %s", entry))
}
}
}
}
if last != nil && c.lockDataIsIncomplete(last) {
return nil, nil, nil, "", "", errors.New(tr.Tr.Get("lock response: incomplete lock data"))
}
for _, lock := range locks {
all = append(all, lock.lock)
if lock.who == ownerOurs {
ours = append(ours, lock.lock)
} else if lock.who == ownerTheirs {
theirs = append(theirs, lock.lock)
}
}
} else if status > 299 && len(lines) > 0 {
message = lines[0]
}
return all, ours, theirs, nextCursor, message, nil
}
func (c *sshLockClient) Lock(remote string, lockReq *lockRequest) (*lockResponse, int, error) {
args := make([]string, 0, 3)
args = append(args, fmt.Sprintf("path=%s", lockReq.Path))
if lockReq.Ref != nil {
args = append(args, fmt.Sprintf("refname=%s", lockReq.Ref.Name))
}
conn := c.connection()
conn.Lock()
defer conn.Unlock()
err := conn.SendMessage("lock", args)
if err != nil {
return nil, 0, err
}
status, args, lines, err := conn.ReadStatusWithLines()
if err != nil {
return nil, status, err
}
var lock lockResponse
lock.Lock, lock.Message, err = c.parseLockResponse(status, args, lines)
return &lock, status, err
}
func (c *sshLockClient) Unlock(ref *git.Ref, remote, id string, force bool) (*unlockResponse, int, error) {
args := make([]string, 0, 3)
if ref != nil {
args = append(args, fmt.Sprintf("refname=%s", ref.Name))
}
conn := c.connection()
conn.Lock()
defer conn.Unlock()
err := conn.SendMessage(fmt.Sprintf("unlock %s", id), args)
if err != nil {
return nil, 0, err
}
status, args, lines, err := conn.ReadStatusWithLines()
if err != nil {
return nil, status, err
}
var lock unlockResponse
lock.Lock, lock.Message, err = c.parseLockResponse(status, args, lines)
return &lock, status, err
}
func (c *sshLockClient) Search(remote string, searchReq *lockSearchRequest) (*lockList, int, error) {
values := searchReq.QueryValues()
args := make([]string, 0, len(values))
for key, value := range values {
args = append(args, fmt.Sprintf("%s=%s", key, value))
}
conn := c.connection()
conn.Lock()
defer conn.Unlock()
err := conn.SendMessage("list-lock", args)
if err != nil {
return nil, 0, err
}
status, args, lines, err := conn.ReadStatusWithLines()
if err != nil {
return nil, status, err
}
locks, _, _, nextCursor, message, err := c.parseListLockResponse(status, args, lines)
if err != nil {
return nil, status, err
}
list := &lockList{
Locks: locks,
NextCursor: nextCursor,
Message: message,
}
return list, status, nil
}
func (c *sshLockClient) SearchVerifiable(remote string, vreq *lockVerifiableRequest) (*lockVerifiableList, int, error) {
args := make([]string, 0, 3)
if vreq.Ref != nil {
args = append(args, fmt.Sprintf("refname=%s", vreq.Ref.Name))
}
if len(vreq.Cursor) > 0 {
args = append(args, fmt.Sprintf("cursor=%s", vreq.Cursor))
}
if vreq.Limit > 0 {
args = append(args, fmt.Sprintf("limit=%d", vreq.Limit))
}
conn := c.connection()
conn.Lock()
defer conn.Unlock()
err := conn.SendMessage("list-locks", args)
if err != nil {
return nil, 0, err
}
status, args, lines, err := conn.ReadStatusWithLines()
if err != nil {
return nil, status, err
}
_, ours, theirs, nextCursor, message, err := c.parseListLockResponse(status, args, lines)
if err != nil {
return nil, status, err
}
list := &lockVerifiableList{
Ours: ours,
Theirs: theirs,
NextCursor: nextCursor,
Message: message,
}
return list, status, nil
}