1
1
package org .jenkinsci .plugins .gitserver ;
2
2
3
3
import hudson .FilePath ;
4
- import hudson .FilePath .FileCallable ;
5
4
import hudson .remoting .Pipe ;
6
5
import hudson .remoting .VirtualChannel ;
6
+ import java .io .BufferedInputStream ;
7
+ import java .io .BufferedOutputStream ;
8
+ import java .io .File ;
9
+ import java .io .IOException ;
10
+ import java .net .URISyntaxException ;
7
11
import jenkins .MasterToSlaveFileCallable ;
8
12
import org .apache .commons .io .IOUtils ;
9
13
import org .eclipse .jgit .errors .NotSupportedException ;
20
24
import org .eclipse .jgit .transport .URIish ;
21
25
import org .eclipse .jgit .transport .UploadPack ;
22
26
23
- import java .io .BufferedInputStream ;
24
- import java .io .BufferedOutputStream ;
25
- import java .io .File ;
26
- import java .io .IOException ;
27
- import java .net .URISyntaxException ;
28
-
29
27
/**
30
28
* {@link Transport} implementation across pipes.
31
29
*
34
32
public class ChannelTransport extends Transport implements PackTransport {
35
33
private final FilePath remoteRepository ;
36
34
37
- public static Transport open (Repository local , FilePath remoteRepository ) throws NotSupportedException , URISyntaxException , TransportException {
38
- if (remoteRepository .isRemote ())
39
- return new ChannelTransport (local ,remoteRepository );
40
- else
41
- return Transport .open (local ,remoteRepository .getRemote ());
35
+ public static Transport open (Repository local , FilePath remoteRepository )
36
+ throws NotSupportedException , URISyntaxException , TransportException {
37
+ if (remoteRepository .isRemote ()) return new ChannelTransport (local , remoteRepository );
38
+ else return Transport .open (local , remoteRepository .getRemote ());
42
39
}
43
40
44
41
public ChannelTransport (Repository local , FilePath remoteRepository ) throws URISyntaxException {
45
- super (local , new URIish ("channel:" + remoteRepository .getRemote ()));
42
+ super (local , new URIish ("channel:" + remoteRepository .getRemote ()));
46
43
this .remoteRepository = remoteRepository ;
47
44
}
48
45
@@ -54,15 +51,18 @@ public FetchConnection openFetch() throws NotSupportedException, TransportExcept
54
51
try {
55
52
remoteRepository .actAsync (new GitFetchTask (l2r , r2l ));
56
53
} catch (IOException e ) {
57
- throw new TransportException ("Failed to open a fetch connection" ,e );
54
+ throw new TransportException ("Failed to open a fetch connection" , e );
58
55
} catch (InterruptedException e ) {
59
- throw new TransportException ("Failed to open a fetch connection" ,e );
56
+ Thread .currentThread ().interrupt ();
57
+ throw new TransportException ("Failed to open a fetch connection" , e );
60
58
}
61
59
62
- return new BasePackFetchConnection (this ) {{
63
- init (new BufferedInputStream (r2l .getIn ()), new BufferedOutputStream (l2r .getOut ()));
64
- readAdvertisedRefs ();
65
- }};
60
+ return new BasePackFetchConnection (this ) {
61
+ {
62
+ init (new BufferedInputStream (r2l .getIn ()), new BufferedOutputStream (l2r .getOut ()));
63
+ readAdvertisedRefs ();
64
+ }
65
+ };
66
66
}
67
67
68
68
@ Override
@@ -73,15 +73,18 @@ public PushConnection openPush() throws NotSupportedException, TransportExceptio
73
73
try {
74
74
remoteRepository .actAsync (new GitPushTask (l2r , r2l ));
75
75
} catch (IOException e ) {
76
- throw new TransportException ("Failed to open a fetch connection" ,e );
76
+ throw new TransportException ("Failed to open a fetch connection" , e );
77
77
} catch (InterruptedException e ) {
78
- throw new TransportException ("Failed to open a fetch connection" ,e );
78
+ Thread .currentThread ().interrupt ();
79
+ throw new TransportException ("Failed to open a fetch connection" , e );
79
80
}
80
81
81
- return new BasePackPushConnection (this ) {{
82
- init (new BufferedInputStream (r2l .getIn ()), new BufferedOutputStream (l2r .getOut ()));
83
- readAdvertisedRefs ();
84
- }};
82
+ return new BasePackPushConnection (this ) {
83
+ {
84
+ init (new BufferedInputStream (r2l .getIn ()), new BufferedOutputStream (l2r .getOut ()));
85
+ readAdvertisedRefs ();
86
+ }
87
+ };
85
88
}
86
89
87
90
@ Override
@@ -99,15 +102,13 @@ public GitFetchTask(Pipe l2r, Pipe r2l) {
99
102
}
100
103
101
104
public Void invoke (File f , VirtualChannel channel ) throws IOException , InterruptedException {
102
- Repository repo = new FileRepositoryBuilder ().setWorkTree (f ).build ();
103
- try {
105
+ try (Repository repo = new FileRepositoryBuilder ().setWorkTree (f ).build ()) {
104
106
final UploadPack rp = new UploadPack (repo );
105
107
rp .upload (new BufferedInputStream (l2r .getIn ()), new BufferedOutputStream (r2l .getOut ()), null );
106
108
return null ;
107
109
} finally {
108
110
IOUtils .closeQuietly (l2r .getIn ());
109
111
IOUtils .closeQuietly (r2l .getOut ());
110
- repo .close ();
111
112
}
112
113
}
113
114
}
@@ -122,15 +123,13 @@ public GitPushTask(Pipe l2r, Pipe r2l) {
122
123
}
123
124
124
125
public Void invoke (File f , VirtualChannel channel ) throws IOException , InterruptedException {
125
- Repository repo = new FileRepositoryBuilder ().setWorkTree (f ).build ();
126
- try {
126
+ try (Repository repo = new FileRepositoryBuilder ().setWorkTree (f ).build ()) {
127
127
final ReceivePack rp = new ReceivePack (repo );
128
128
rp .receive (new BufferedInputStream (l2r .getIn ()), new BufferedOutputStream (r2l .getOut ()), null );
129
129
return null ;
130
130
} finally {
131
131
IOUtils .closeQuietly (l2r .getIn ());
132
132
IOUtils .closeQuietly (r2l .getOut ());
133
- repo .close ();
134
133
}
135
134
}
136
135
}
0 commit comments