Skip to content

Commit a530c5d

Browse files
committed
libgit2/ssh: Embed connection fields in Subtransport
The connection type was created to group the connection related fields and use mutex to prevent race conditions. Since that's no longer the case, this puts back those fields in sshSmartSubtransport. Signed-off-by: Sunny <[email protected]>
1 parent 09fae63 commit a530c5d

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

pkg/git/libgit2/managed/ssh.go

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,6 @@ type sshSmartSubtransport struct {
114114

115115
closedSessions *int32
116116

117-
con connection
118-
}
119-
120-
type connection struct {
121117
client *ssh.Client
122118
session *ssh.Session
123119
currentStream *sshSmartSubtransportStream
@@ -155,17 +151,17 @@ func (t *sshSmartSubtransport) Action(transportOptionsURL string, action git2go.
155151
var cmd string
156152
switch action {
157153
case git2go.SmartServiceActionUploadpackLs, git2go.SmartServiceActionUploadpack:
158-
if t.con.currentStream != nil {
154+
if t.currentStream != nil {
159155
if t.lastAction == git2go.SmartServiceActionUploadpackLs {
160-
return t.con.currentStream, nil
156+
return t.currentStream, nil
161157
}
162158
}
163159
cmd = fmt.Sprintf("git-upload-pack '%s'", uPath)
164160

165161
case git2go.SmartServiceActionReceivepackLs, git2go.SmartServiceActionReceivepack:
166-
if t.con.currentStream != nil {
162+
if t.currentStream != nil {
167163
if t.lastAction == git2go.SmartServiceActionReceivepackLs {
168-
return t.con.currentStream, nil
164+
return t.currentStream, nil
169165
}
170166
}
171167
cmd = fmt.Sprintf("git-receive-pack '%s'", uPath)
@@ -212,7 +208,7 @@ func (t *sshSmartSubtransport) Action(transportOptionsURL string, action git2go.
212208
return nil
213209
}
214210

215-
if t.con.connected {
211+
if t.connected {
216212
// The connection is no longer shared across actions, so ensures
217213
// all has been released before starting a new connection.
218214
_ = t.Close()
@@ -224,18 +220,18 @@ func (t *sshSmartSubtransport) Action(transportOptionsURL string, action git2go.
224220
}
225221

226222
t.logger.V(logger.TraceLevel).Info("creating new ssh session")
227-
if t.con.session, err = t.con.client.NewSession(); err != nil {
223+
if t.session, err = t.client.NewSession(); err != nil {
228224
return nil, err
229225
}
230226

231-
if t.stdin, err = t.con.session.StdinPipe(); err != nil {
227+
if t.stdin, err = t.session.StdinPipe(); err != nil {
232228
return nil, err
233229
}
234230

235231
var w *io.PipeWriter
236232
var reader io.Reader
237233
t.stdout, w = io.Pipe()
238-
if reader, err = t.con.session.StdoutPipe(); err != nil {
234+
if reader, err = t.session.StdoutPipe(); err != nil {
239235
return nil, err
240236
}
241237

@@ -284,16 +280,16 @@ func (t *sshSmartSubtransport) Action(transportOptionsURL string, action git2go.
284280
}()
285281

286282
t.logger.V(logger.TraceLevel).Info("run on remote", "cmd", cmd)
287-
if err := t.con.session.Start(cmd); err != nil {
283+
if err := t.session.Start(cmd); err != nil {
288284
return nil, err
289285
}
290286

291287
t.lastAction = action
292-
t.con.currentStream = &sshSmartSubtransportStream{
288+
t.currentStream = &sshSmartSubtransportStream{
293289
owner: t,
294290
}
295291

296-
return t.con.currentStream, nil
292+
return t.currentStream, nil
297293
}
298294

299295
func (t *sshSmartSubtransport) createConn(addr string, sshConfig *ssh.ClientConfig) error {
@@ -310,8 +306,8 @@ func (t *sshSmartSubtransport) createConn(addr string, sshConfig *ssh.ClientConf
310306
return err
311307
}
312308

313-
t.con.connected = true
314-
t.con.client = ssh.NewClient(c, chans, reqs)
309+
t.connected = true
310+
t.client = ssh.NewClient(c, chans, reqs)
315311

316312
return nil
317313
}
@@ -328,25 +324,25 @@ func (t *sshSmartSubtransport) createConn(addr string, sshConfig *ssh.ClientConf
328324
func (t *sshSmartSubtransport) Close() error {
329325
t.logger.V(logger.TraceLevel).Info("sshSmartSubtransport.Close()")
330326

331-
t.con.currentStream = nil
332-
if t.con.client != nil && t.stdin != nil {
327+
t.currentStream = nil
328+
if t.client != nil && t.stdin != nil {
333329
_ = t.stdin.Close()
334330
}
335331
t.stdin = nil
336332

337-
if t.con.session != nil {
333+
if t.session != nil {
338334
t.logger.V(logger.TraceLevel).Info("session.Close()")
339-
_ = t.con.session.Close()
335+
_ = t.session.Close()
340336
}
341-
t.con.session = nil
337+
t.session = nil
342338

343-
if t.con.client != nil {
344-
_ = t.con.client.Close()
339+
if t.client != nil {
340+
_ = t.client.Close()
345341
t.logger.V(logger.TraceLevel).Info("close client")
346342
}
347-
t.con.client = nil
343+
t.client = nil
348344

349-
t.con.connected = false
345+
t.connected = false
350346
atomic.AddInt32(t.closedSessions, 1)
351347

352348
return nil

0 commit comments

Comments
 (0)