14
14
import java .util .List ;
15
15
16
16
import org .apache .commons .io .IOUtils ;
17
- import org .apache .commons .logging .Log ;
18
17
import org .eclipse .jgit .api .Git ;
19
18
import org .eclipse .jgit .api .MergeResult ;
20
19
import org .eclipse .jgit .api .ResetCommand .ResetType ;
26
25
import org .eclipse .jgit .transport .RefSpec ;
27
26
import org .eclipse .jgit .transport .UsernamePasswordCredentialsProvider ;
28
27
29
- import com .projectkaiser .scm .vcs .api .AbstractVCS ;
30
28
import com .projectkaiser .scm .vcs .api .IVCS ;
31
29
import com .projectkaiser .scm .vcs .api .PKVCSMergeResult ;
32
- import com .projectkaiser .scm .vcs .api .VCSWorkspace ;
33
30
import com .projectkaiser .scm .vcs .api .exceptions .EVCSBranchExists ;
34
31
import com .projectkaiser .scm .vcs .api .exceptions .EVCSException ;
35
32
import 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 ;
36
35
37
- public class GitVCS extends AbstractVCS implements IVCS {
36
+ public class GitVCS implements IVCS {
38
37
39
38
private CredentialsProvider credentials ;
40
39
41
- public GitVCS (Log logger , String workspacePath , String remoteUrl ) {
42
- super (logger , workspacePath , remoteUrl );
43
- }
40
+ IVCSRepository repo ;
44
41
45
42
public CredentialsProvider getCredentials () {
46
43
return credentials ;
47
44
}
48
-
45
+
46
+ public GitVCS (IVCSRepository repo ) {
47
+ this .repo = repo ;
48
+ }
49
+
49
50
public void setCredentials (CredentialsProvider credentials ) {
50
51
this .credentials = credentials ;
51
52
}
@@ -54,10 +55,8 @@ public void setCredentials(CredentialsProvider credentials) {
54
55
public void createBranch (String srcBranchName , String newBranchName , String commitMessage ) {
55
56
// note: no commit message could be attached in Git
56
57
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 )) {
61
60
62
61
git
63
62
.checkout ()
@@ -78,15 +77,7 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
78
77
.setRefSpecs (refSpec )
79
78
.setCredentialsProvider (credentials )
80
79
.call ();
81
-
82
- git
83
- .branchDelete ()
84
- .setBranchNames (newBranchName )
85
- .call ();
86
-
87
80
}
88
- } finally {
89
- workspace .unlock ();
90
81
}
91
82
} catch (RefAlreadyExistsException e ) {
92
83
throw new EVCSBranchExists (e );
@@ -100,9 +91,8 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
100
91
@ Override
101
92
public void deleteBranch (String branchName , String commitMessage ) {
102
93
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 )) {
106
96
107
97
git
108
98
.pull ()
@@ -129,39 +119,34 @@ public void deleteBranch(String branchName, String commitMessage) {
129
119
.setRemote ("origin" )
130
120
.setCredentialsProvider (credentials )
131
121
.call ();
132
-
133
122
}
134
-
135
- } finally {
136
- workspace .unlock ();
137
123
}
138
-
139
124
} catch (GitAPIException e ) {
140
125
throw new EVCSException (e );
141
126
} catch (Exception e ) {
142
127
throw new RuntimeException (e );
143
128
}
144
129
}
145
130
146
- public Git getLocalGit (VCSWorkspace workspace ) {
131
+ public Git getLocalGit (IVCSLockedWorkingCopy wc ) {
147
132
148
- Repository repo ;
133
+ Repository gitRepo ;
149
134
try {
150
- repo = new FileRepositoryBuilder ()
151
- .setGitDir (new File (workspace .getFolder (), ".git" ))
135
+ gitRepo = new FileRepositoryBuilder ()
136
+ .setGitDir (new File (wc .getFolder (), ".git" ))
152
137
.build ();
153
138
} catch (IOException e ) {
154
139
throw new RuntimeException (e );
155
140
}
156
- Boolean repoInited = repo
141
+ Boolean repoInited = gitRepo
157
142
.getObjectDatabase ()
158
143
.exists ();
159
144
if (!repoInited ) {
160
145
try {
161
146
Git
162
147
.cloneRepository ()
163
- .setDirectory (workspace .getFolder ())
164
- .setURI (baseUrl )
148
+ .setDirectory (wc .getFolder ())
149
+ .setURI (repo . getRepoUrl () )
165
150
.setCredentialsProvider (credentials )
166
151
.setNoCheckout (true )
167
152
.call ();
@@ -171,16 +156,14 @@ public Git getLocalGit(VCSWorkspace workspace) {
171
156
172
157
}
173
158
174
- return new Git (repo );
159
+ return new Git (gitRepo );
175
160
}
176
161
177
162
@ Override
178
163
public PKVCSMergeResult merge (String sourceBranchUrl , String destBranchUrl , String commitMessage ) {
179
-
180
164
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 )) {
184
167
185
168
git
186
169
.pull ()
@@ -212,7 +195,7 @@ public PKVCSMergeResult merge(String sourceBranchUrl, String destBranchUrl, Stri
212
195
.setMode (ResetType .HARD )
213
196
.call ();
214
197
} catch (Exception e ) {
215
- workspace .setCorrupt (true );
198
+ wc .setCorrupt (true );
216
199
}
217
200
} else {
218
201
git
@@ -225,10 +208,7 @@ public PKVCSMergeResult merge(String sourceBranchUrl, String destBranchUrl, Stri
225
208
226
209
return res ;
227
210
}
228
- } finally {
229
- workspace .unlock ();
230
211
}
231
-
232
212
} catch (GitAPIException e ) {
233
213
throw new EVCSException (e );
234
214
} catch (Exception e ) {
@@ -257,7 +237,7 @@ public void setProxy(final String host, final int port, String proxyUser, String
257
237
258
238
@ Override
259
239
public List <Proxy > select (URI uri ) {
260
- if (uri .toString ().contains (baseUrl )) {
240
+ if (uri .toString ().contains (repo . getRepoUrl () )) {
261
241
return Arrays .asList (new Proxy (Type .HTTP , InetSocketAddress
262
242
.createUnresolved (host , port )));
263
243
} else {
@@ -268,48 +248,46 @@ public List<Proxy> select(URI uri) {
268
248
269
249
@ Override
270
250
public void connectFailed (URI uri , SocketAddress sa , IOException ioe ) {
271
- if (uri .toString ().contains (baseUrl )) {
251
+ if (uri .toString ().contains (repo . getRepoUrl () )) {
272
252
throw new RuntimeException ("GitVCS proxy connect failed" );
273
253
}
274
254
}
275
255
});
276
256
}
277
257
278
258
@ Override
279
- public String getBaseUrl () {
280
- return baseUrl ;
259
+ public String getRepoUrl () {
260
+ return repo . getRepoUrl () ;
281
261
}
282
262
283
263
@ 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 {
303
282
return IOUtils .toString (file .toURI (), encoding );
283
+ } catch (IOException e ) {
284
+ throw new EVCSFileNotFound (String .format ("File %s is not found" , fileRelativePath ));
304
285
}
305
-
306
- } finally {
307
- workspace .unlock ();
308
286
}
309
287
} catch (GitAPIException e ) {
310
288
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 ;
313
291
} catch (Exception e ) {
314
292
throw new RuntimeException (e );
315
293
}
@@ -318,9 +296,8 @@ public String getFileContent(String branchName, String filePath, String encoding
318
296
@ Override
319
297
public void setFileContent (String branchName , String filePath , String content , String commitMessage ) {
320
298
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 )) {
324
301
325
302
git
326
303
.pull ()
@@ -334,7 +311,7 @@ public void setFileContent(String branchName, String filePath, String content, S
334
311
.setName (branchName )
335
312
.call ();
336
313
337
- File file = new File (workspace .getFolder (), filePath );
314
+ File file = new File (wc .getFolder (), filePath );
338
315
FileWriter fw = new FileWriter (file , false );
339
316
fw .write (content );
340
317
fw .close ();
@@ -354,11 +331,7 @@ public void setFileContent(String branchName, String filePath, String content, S
354
331
.setCredentialsProvider (credentials )
355
332
.call ();
356
333
}
357
-
358
- } finally {
359
- workspace .unlock ();
360
334
}
361
-
362
335
} catch (GitAPIException e ) {
363
336
throw new EVCSException (e );
364
337
} catch (Exception e ) {
0 commit comments