1414import java .util .List ;
1515
1616import org .apache .commons .io .IOUtils ;
17- import org .apache .commons .logging .Log ;
1817import org .eclipse .jgit .api .Git ;
1918import org .eclipse .jgit .api .MergeResult ;
2019import org .eclipse .jgit .api .ResetCommand .ResetType ;
2625import org .eclipse .jgit .transport .RefSpec ;
2726import org .eclipse .jgit .transport .UsernamePasswordCredentialsProvider ;
2827
29- import com .projectkaiser .scm .vcs .api .AbstractVCS ;
3028import com .projectkaiser .scm .vcs .api .IVCS ;
3129import com .projectkaiser .scm .vcs .api .PKVCSMergeResult ;
32- import com .projectkaiser .scm .vcs .api .VCSWorkspace ;
3330import com .projectkaiser .scm .vcs .api .exceptions .EVCSBranchExists ;
3431import com .projectkaiser .scm .vcs .api .exceptions .EVCSException ;
3532import com .projectkaiser .scm .vcs .api .exceptions .EVCSFileNotFound ;
33+ import com .projectkaiser .scm .vcs .api .workingcopy .IVCSLockedWorkingCopy ;
34+ import com .projectkaiser .scm .vcs .api .workingcopy .IVCSRepository ;
3635
37- public class GitVCS extends AbstractVCS implements IVCS {
36+ public class GitVCS implements IVCS {
3837
3938 private CredentialsProvider credentials ;
4039
41- public GitVCS (Log logger , String workspacePath , String remoteUrl ) {
42- super (logger , workspacePath , remoteUrl );
43- }
40+ IVCSRepository repo ;
4441
4542 public CredentialsProvider getCredentials () {
4643 return credentials ;
4744 }
48-
45+
46+ public GitVCS (IVCSRepository repo ) {
47+ this .repo = repo ;
48+ }
49+
4950 public void setCredentials (CredentialsProvider credentials ) {
5051 this .credentials = credentials ;
5152 }
@@ -54,10 +55,8 @@ public void setCredentials(CredentialsProvider credentials) {
5455 public void createBranch (String srcBranchName , String newBranchName , String commitMessage ) {
5556 // note: no commit message could be attached in Git
5657 try {
57- VCSWorkspace workspace = VCSWorkspace .getLockedWorkspace (repoFolder );
58- try {
59-
60- try (Git git = getLocalGit (workspace )) {
58+ try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ()) {
59+ try (Git git = getLocalGit (wc )) {
6160
6261 git
6362 .checkout ()
@@ -78,15 +77,7 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
7877 .setRefSpecs (refSpec )
7978 .setCredentialsProvider (credentials )
8079 .call ();
81-
82- git
83- .branchDelete ()
84- .setBranchNames (newBranchName )
85- .call ();
86-
8780 }
88- } finally {
89- workspace .unlock ();
9081 }
9182 } catch (RefAlreadyExistsException e ) {
9283 throw new EVCSBranchExists (e );
@@ -100,9 +91,8 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
10091 @ Override
10192 public void deleteBranch (String branchName , String commitMessage ) {
10293 try {
103- VCSWorkspace workspace = VCSWorkspace .getLockedWorkspace (repoFolder );
104- try {
105- try (Git git = getLocalGit (workspace )) {
94+ try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ()) {
95+ try (Git git = getLocalGit (wc )) {
10696
10797 git
10898 .pull ()
@@ -129,39 +119,34 @@ public void deleteBranch(String branchName, String commitMessage) {
129119 .setRemote ("origin" )
130120 .setCredentialsProvider (credentials )
131121 .call ();
132-
133122 }
134-
135- } finally {
136- workspace .unlock ();
137123 }
138-
139124 } catch (GitAPIException e ) {
140125 throw new EVCSException (e );
141126 } catch (Exception e ) {
142127 throw new RuntimeException (e );
143128 }
144129 }
145130
146- public Git getLocalGit (VCSWorkspace workspace ) {
131+ public Git getLocalGit (IVCSLockedWorkingCopy wc ) {
147132
148- Repository repo ;
133+ Repository gitRepo ;
149134 try {
150- repo = new FileRepositoryBuilder ()
151- .setGitDir (new File (workspace .getFolder (), ".git" ))
135+ gitRepo = new FileRepositoryBuilder ()
136+ .setGitDir (new File (wc .getFolder (), ".git" ))
152137 .build ();
153138 } catch (IOException e ) {
154139 throw new RuntimeException (e );
155140 }
156- Boolean repoInited = repo
141+ Boolean repoInited = gitRepo
157142 .getObjectDatabase ()
158143 .exists ();
159144 if (!repoInited ) {
160145 try {
161146 Git
162147 .cloneRepository ()
163- .setDirectory (workspace .getFolder ())
164- .setURI (baseUrl )
148+ .setDirectory (wc .getFolder ())
149+ .setURI (repo . getRepoUrl () )
165150 .setCredentialsProvider (credentials )
166151 .setNoCheckout (true )
167152 .call ();
@@ -171,16 +156,14 @@ public Git getLocalGit(VCSWorkspace workspace) {
171156
172157 }
173158
174- return new Git (repo );
159+ return new Git (gitRepo );
175160 }
176161
177162 @ Override
178163 public PKVCSMergeResult merge (String sourceBranchUrl , String destBranchUrl , String commitMessage ) {
179-
180164 try {
181- VCSWorkspace workspace = VCSWorkspace .getLockedWorkspace (repoFolder );
182- try {
183- try (Git git = getLocalGit (workspace )) {
165+ try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ()) {
166+ try (Git git = getLocalGit (wc )) {
184167
185168 git
186169 .pull ()
@@ -212,7 +195,7 @@ public PKVCSMergeResult merge(String sourceBranchUrl, String destBranchUrl, Stri
212195 .setMode (ResetType .HARD )
213196 .call ();
214197 } catch (Exception e ) {
215- workspace .setCorrupt (true );
198+ wc .setCorrupt (true );
216199 }
217200 } else {
218201 git
@@ -225,10 +208,7 @@ public PKVCSMergeResult merge(String sourceBranchUrl, String destBranchUrl, Stri
225208
226209 return res ;
227210 }
228- } finally {
229- workspace .unlock ();
230211 }
231-
232212 } catch (GitAPIException e ) {
233213 throw new EVCSException (e );
234214 } catch (Exception e ) {
@@ -257,7 +237,7 @@ public void setProxy(final String host, final int port, String proxyUser, String
257237
258238 @ Override
259239 public List <Proxy > select (URI uri ) {
260- if (uri .toString ().contains (baseUrl )) {
240+ if (uri .toString ().contains (repo . getRepoUrl () )) {
261241 return Arrays .asList (new Proxy (Type .HTTP , InetSocketAddress
262242 .createUnresolved (host , port )));
263243 } else {
@@ -268,48 +248,46 @@ public List<Proxy> select(URI uri) {
268248
269249 @ Override
270250 public void connectFailed (URI uri , SocketAddress sa , IOException ioe ) {
271- if (uri .toString ().contains (baseUrl )) {
251+ if (uri .toString ().contains (repo . getRepoUrl () )) {
272252 throw new RuntimeException ("GitVCS proxy connect failed" );
273253 }
274254 }
275255 });
276256 }
277257
278258 @ Override
279- public String getBaseUrl () {
280- return baseUrl ;
259+ public String getRepoUrl () {
260+ return repo . getRepoUrl () ;
281261 }
282262
283263 @ Override
284- public String getFileContent (String branchName , String filePath , String encoding ) {
285- try {
286- VCSWorkspace workspace = VCSWorkspace .getLockedWorkspace (repoFolder );
287- try {
288- try (Git git = getLocalGit (workspace )) {
289-
290- git
291- .pull ()
292- .setCredentialsProvider (credentials )
293- .call ();
294-
295- git
296- .checkout ()
297- .setCreateBranch (false )
298- .addPath (filePath )
299- .setName (branchName )
300- .call ();
301- File file = new File (workspace .getFolder (), filePath );
302-
264+ public String getFileContent (String branchName , String fileRelativePath , String encoding ) {
265+ try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ()) {
266+ try (Git git = getLocalGit (wc )) {
267+
268+ git
269+ .pull ()
270+ .setCredentialsProvider (credentials )
271+ .call ();
272+
273+ git
274+ .checkout ()
275+ .setCreateBranch (false )
276+ .addPath (fileRelativePath )
277+ .setName (branchName )
278+ .call ();
279+ File file = new File (wc .getFolder (), fileRelativePath );
280+
281+ try {
303282 return IOUtils .toString (file .toURI (), encoding );
283+ } catch (IOException e ) {
284+ throw new EVCSFileNotFound (String .format ("File %s is not found" , fileRelativePath ));
304285 }
305-
306- } finally {
307- workspace .unlock ();
308286 }
309287 } catch (GitAPIException e ) {
310288 throw new EVCSException (e );
311- } catch (IOException e ) {
312- throw new EVCSFileNotFound ( String . format ( "File %s is not found" , filePath )) ;
289+ } catch (EVCSFileNotFound e ) {
290+ throw e ;
313291 } catch (Exception e ) {
314292 throw new RuntimeException (e );
315293 }
@@ -318,9 +296,8 @@ public String getFileContent(String branchName, String filePath, String encoding
318296 @ Override
319297 public void setFileContent (String branchName , String filePath , String content , String commitMessage ) {
320298 try {
321- VCSWorkspace workspace = VCSWorkspace .getLockedWorkspace (repoFolder );
322- try {
323- try (Git git = getLocalGit (workspace )) {
299+ try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ()) {
300+ try (Git git = getLocalGit (wc )) {
324301
325302 git
326303 .pull ()
@@ -334,7 +311,7 @@ public void setFileContent(String branchName, String filePath, String content, S
334311 .setName (branchName )
335312 .call ();
336313
337- File file = new File (workspace .getFolder (), filePath );
314+ File file = new File (wc .getFolder (), filePath );
338315 FileWriter fw = new FileWriter (file , false );
339316 fw .write (content );
340317 fw .close ();
@@ -354,11 +331,7 @@ public void setFileContent(String branchName, String filePath, String content, S
354331 .setCredentialsProvider (credentials )
355332 .call ();
356333 }
357-
358- } finally {
359- workspace .unlock ();
360334 }
361-
362335 } catch (GitAPIException e ) {
363336 throw new EVCSException (e );
364337 } catch (Exception e ) {
0 commit comments