11package org .jenkinsci .plugins .gitserver ;
22
33import hudson .FilePath ;
4- import hudson .FilePath .FileCallable ;
54import hudson .remoting .Pipe ;
65import 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 ;
711import jenkins .MasterToSlaveFileCallable ;
812import org .apache .commons .io .IOUtils ;
913import org .eclipse .jgit .errors .NotSupportedException ;
2024import org .eclipse .jgit .transport .URIish ;
2125import org .eclipse .jgit .transport .UploadPack ;
2226
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-
2927/**
3028 * {@link Transport} implementation across pipes.
3129 *
3432public class ChannelTransport extends Transport implements PackTransport {
3533 private final FilePath remoteRepository ;
3634
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 ());
4239 }
4340
4441 public ChannelTransport (Repository local , FilePath remoteRepository ) throws URISyntaxException {
45- super (local , new URIish ("channel:" + remoteRepository .getRemote ()));
42+ super (local , new URIish ("channel:" + remoteRepository .getRemote ()));
4643 this .remoteRepository = remoteRepository ;
4744 }
4845
@@ -54,15 +51,18 @@ public FetchConnection openFetch() throws NotSupportedException, TransportExcept
5451 try {
5552 remoteRepository .actAsync (new GitFetchTask (l2r , r2l ));
5653 } 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 );
5855 } 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 );
6058 }
6159
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+ };
6666 }
6767
6868 @ Override
@@ -73,15 +73,18 @@ public PushConnection openPush() throws NotSupportedException, TransportExceptio
7373 try {
7474 remoteRepository .actAsync (new GitPushTask (l2r , r2l ));
7575 } 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 );
7777 } 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 );
7980 }
8081
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+ };
8588 }
8689
8790 @ Override
@@ -99,15 +102,13 @@ public GitFetchTask(Pipe l2r, Pipe r2l) {
99102 }
100103
101104 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 ()) {
104106 final UploadPack rp = new UploadPack (repo );
105107 rp .upload (new BufferedInputStream (l2r .getIn ()), new BufferedOutputStream (r2l .getOut ()), null );
106108 return null ;
107109 } finally {
108110 IOUtils .closeQuietly (l2r .getIn ());
109111 IOUtils .closeQuietly (r2l .getOut ());
110- repo .close ();
111112 }
112113 }
113114 }
@@ -122,15 +123,13 @@ public GitPushTask(Pipe l2r, Pipe r2l) {
122123 }
123124
124125 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 ()) {
127127 final ReceivePack rp = new ReceivePack (repo );
128128 rp .receive (new BufferedInputStream (l2r .getIn ()), new BufferedOutputStream (r2l .getOut ()), null );
129129 return null ;
130130 } finally {
131131 IOUtils .closeQuietly (l2r .getIn ());
132132 IOUtils .closeQuietly (r2l .getOut ());
133- repo .close ();
134133 }
135134 }
136135 }
0 commit comments