Skip to content

Commit 3abf475

Browse files
committed
refactor
1 parent 76174d1 commit 3abf475

File tree

1 file changed

+93
-106
lines changed

1 file changed

+93
-106
lines changed

src/main/java/org/scm4j/vcs/svn/SVNVCS.java

Lines changed: 93 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ public SVNURL getBranchUrl(String branchPath) throws Exception {
9595
return SVNURL.parseURIEncoded(repoUrl + getBranchName(branchPath));
9696
}
9797

98-
99-
10098
@Override
10199
public void createBranch(String srcBranchName, String dstBranchName, String commitMessage) throws EVCSBranchExists {
102100
SVNURL fromUrl;
@@ -111,22 +109,21 @@ public void createBranch(String srcBranchName, String dstBranchName, String comm
111109
}
112110

113111
public void createBranch(SVNURL fromUrl, SVNURL toUrl, String commitMessage) {
114-
try {
115-
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
116-
checkout(fromUrl, wc.getFolder());
117-
118-
SVNCopyClient copyClient = new SVNCopyClient(authManager, options);
119-
SVNCopySource copySource = new SVNCopySource(SVNRevision.WORKING, SVNRevision.WORKING,
120-
wc.getFolder());
121-
copySource.setCopyContents(false);
122-
123-
copyClient.doCopy(new SVNCopySource[] { copySource }, toUrl,
124-
false, // isMove
125-
true, // make parents
126-
true, // failWhenDstExistsb
127-
commitMessage, // commit message
128-
null); // SVNProperties
129-
}
112+
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
113+
checkout(fromUrl, wc.getFolder());
114+
115+
SVNCopyClient copyClient = new SVNCopyClient(authManager, options);
116+
SVNCopySource copySource = new SVNCopySource(SVNRevision.WORKING, SVNRevision.WORKING,
117+
wc.getFolder());
118+
copySource.setCopyContents(false);
119+
120+
copyClient.doCopy(new SVNCopySource[] { copySource }, toUrl,
121+
false, // isMove
122+
true, // make parents
123+
true, // failWhenDstExistsb
124+
commitMessage, // commit message
125+
null); // SVNProperties
126+
130127
} catch (SVNException e) {
131128
if (e.getErrorMessage().getErrorCode().getCode() == SVN_ITEM_EXISTS_ERROR_CODE) {
132129
throw new EVCSBranchExists(e);
@@ -153,50 +150,48 @@ public void deleteBranch(String branchName, String commitMessage) {
153150
@Override
154151
public VCSMergeResult merge(String srcBranchName, String dstBranchName, String commitMessage) {
155152
SVNDiffClient diffClient = clientManager.getDiffClient();
156-
try {
157-
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
158-
checkout(getBranchUrl(dstBranchName), wc.getFolder());
159-
160-
DefaultSVNOptions options = (DefaultSVNOptions) diffClient.getOptions();
161-
final List<String> conflictingFiles = new ArrayList<>();
162-
options.setConflictHandler(new ISVNConflictHandler() {
163-
@Override
164-
public SVNConflictResult handleConflict(SVNConflictDescription conflictDescription)
165-
throws SVNException {
166-
conflictingFiles.add(conflictDescription.getMergeFiles().getLocalPath());
167-
return new SVNConflictResult(SVNConflictChoice.POSTPONE,
168-
conflictDescription.getMergeFiles().getResultFile());
169-
}
170-
});
153+
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
154+
checkout(getBranchUrl(dstBranchName), wc.getFolder());
171155

172-
173-
try {
174-
SVNRevisionRange range = new SVNRevisionRange(SVNRevision.create(1), SVNRevision.HEAD);
175-
diffClient.doMerge(getBranchUrl(srcBranchName),
176-
SVNRevision.HEAD, Collections.singleton(range),
177-
wc.getFolder(), SVNDepth.UNKNOWN, true, false, false, false);
178-
179-
Boolean success = conflictingFiles.isEmpty();
180-
if (success) {
181-
clientManager
182-
.getCommitClient()
183-
.doCommit(new File[] {wc.getFolder()}, false, commitMessage,
184-
new SVNProperties(), null, true, true, SVNDepth.INFINITY);
185-
} else {
186-
try {
187-
SVNWCClient wcClient = getRevertClient(options);
188-
wcClient.doRevert(new File[] {wc.getFolder()}, SVNDepth.INFINITY, null);
189-
} catch (Exception e) {
190-
// It doesn't matter if we failed to revert. Just make the workspace corrupted.
191-
wc.setCorrupted(true);
192-
}
156+
DefaultSVNOptions options = (DefaultSVNOptions) diffClient.getOptions();
157+
final List<String> conflictingFiles = new ArrayList<>();
158+
options.setConflictHandler(new ISVNConflictHandler() {
159+
@Override
160+
public SVNConflictResult handleConflict(SVNConflictDescription conflictDescription)
161+
throws SVNException {
162+
conflictingFiles.add(conflictDescription.getMergeFiles().getLocalPath());
163+
return new SVNConflictResult(SVNConflictChoice.POSTPONE,
164+
conflictDescription.getMergeFiles().getResultFile());
165+
}
166+
});
167+
168+
SVNRevisionRange range = new SVNRevisionRange(SVNRevision.create(1), SVNRevision.HEAD);
169+
try {
170+
diffClient.doMerge(getBranchUrl(srcBranchName),
171+
SVNRevision.HEAD, Collections.singleton(range),
172+
wc.getFolder(), SVNDepth.UNKNOWN, true, false, false, false);
173+
174+
Boolean success = conflictingFiles.isEmpty();
175+
176+
if (success) {
177+
clientManager
178+
.getCommitClient()
179+
.doCommit(new File[] {wc.getFolder()}, false, commitMessage,
180+
new SVNProperties(), null, true, true, SVNDepth.INFINITY);
181+
} else {
182+
try {
183+
SVNWCClient wcClient = getRevertClient(options);
184+
wcClient.doRevert(new File[] {wc.getFolder()}, SVNDepth.INFINITY, null);
185+
} catch (Exception e) {
186+
// It doesn't matter if we failed to revert. Just make the workspace corrupted.
187+
wc.setCorrupted(true);
193188
}
194-
return new VCSMergeResult(success, conflictingFiles);
195-
} catch (Exception e) {
196-
wc.setCorrupted(true);
197-
throw e;
198189
}
199-
}
190+
return new VCSMergeResult(success, conflictingFiles);
191+
} catch (Exception e) {
192+
wc.setCorrupted(true);
193+
throw e;
194+
}
200195
} catch (SVNException e) {
201196
throw new EVCSException(e);
202197
} catch (Exception e) {
@@ -234,7 +229,8 @@ public boolean isWorkingCopyInited(File destPath) {
234229

235230
@Override
236231
public void setCredentials(String user, String password) {
237-
userPassAuth = SVNPasswordAuthentication.newInstance(user, password == null ? null : password.toCharArray(), true, trunkSVNUrl, false);
232+
userPassAuth = SVNPasswordAuthentication.newInstance(user, password == null ? null : password.toCharArray(),
233+
true, trunkSVNUrl, false);
238234
authManager.setAuthentications(new SVNAuthentication[] {userPassAuth});
239235
}
240236

@@ -259,7 +255,6 @@ public String getFileContent(String branchName, String filePath, String encoding
259255
} catch (Exception e) {
260256
throw new RuntimeException(e);
261257
}
262-
263258
}
264259

265260
private String getBranchName(String branchName) {
@@ -273,39 +268,37 @@ public String getFileContent(String branchName, String filePath) {
273268

274269
@Override
275270
public VCSCommit setFileContent(String branchName, String filePath, String content, String commitMessage) {
276-
try {
277-
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
278-
checkout(getBranchUrl(branchName), wc.getFolder());
279-
File file = new File(wc.getFolder(), filePath);
280-
Boolean needToAdd = !file.exists();
281-
if (!file.exists()) {
282-
FileUtils.forceMkdir(file.getParentFile());
283-
file.createNewFile();
284-
}
285-
286-
FileWriter writer = new FileWriter(file);
287-
writer.write(content);
288-
writer.close();
289-
290-
if (needToAdd) {
291-
clientManager
292-
.getWCClient()
293-
.doAdd(file,
294-
true /* force, avoiding "file is already under version control" exception*/,
295-
false, false, SVNDepth.EMPTY, false, true);
296-
}
297-
298-
try {
299-
SVNCommitInfo newCommit = clientManager
300-
.getCommitClient()
301-
.doCommit(new File[] { wc.getFolder() }, false, commitMessage,
302-
new SVNProperties(), null, false, false, SVNDepth.INFINITY);
303-
return newCommit == SVNCommitInfo.NULL ? VCSCommit.EMPTY :
304-
new VCSCommit(Long.toString(newCommit.getNewRevision()), commitMessage, newCommit.getAuthor());
305-
} catch (SVNException e) {
306-
wc.setCorrupted(true);
307-
throw e;
308-
}
271+
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
272+
checkout(getBranchUrl(branchName), wc.getFolder());
273+
File file = new File(wc.getFolder(), filePath);
274+
Boolean needToAdd = !file.exists();
275+
if (!file.exists()) {
276+
FileUtils.forceMkdir(file.getParentFile());
277+
file.createNewFile();
278+
}
279+
280+
FileWriter writer = new FileWriter(file);
281+
writer.write(content);
282+
writer.close();
283+
284+
if (needToAdd) {
285+
clientManager
286+
.getWCClient()
287+
.doAdd(file,
288+
true /* force, avoiding "file is already under version control" exception */,
289+
false, false, SVNDepth.EMPTY, false, true);
290+
}
291+
292+
try {
293+
SVNCommitInfo newCommit = clientManager
294+
.getCommitClient()
295+
.doCommit(new File[] { wc.getFolder() }, false, commitMessage,
296+
new SVNProperties(), null, false, false, SVNDepth.INFINITY);
297+
return newCommit == SVNCommitInfo.NULL ? VCSCommit.EMPTY :
298+
new VCSCommit(Long.toString(newCommit.getNewRevision()), commitMessage, newCommit.getAuthor());
299+
} catch (SVNException e) {
300+
wc.setCorrupted(true);
301+
throw e;
309302
}
310303
} catch (SVNException e) {
311304
throw new EVCSException(e);
@@ -405,13 +398,11 @@ public VCSChangeType SVNChangeTypeToVCSChangeType(SVNStatusType modificationType
405398

406399
@Override
407400
public List<VCSDiffEntry> getBranchesDiff(final String srcBranchName, final String dstBranchName) {
408-
try {
409-
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
410-
checkout(getBranchUrl(dstBranchName), wc.getFolder());
411-
List<VCSDiffEntry> entries = getDiffEntries(srcBranchName, dstBranchName);
412-
entries = fillUnifiedDiffs(srcBranchName, dstBranchName, entries);
413-
return entries;
414-
}
401+
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
402+
checkout(getBranchUrl(dstBranchName), wc.getFolder());
403+
List<VCSDiffEntry> entries = getDiffEntries(srcBranchName, dstBranchName);
404+
entries = fillUnifiedDiffs(srcBranchName, dstBranchName, entries);
405+
return entries;
415406
} catch (SVNException e) {
416407
throw new EVCSException(e);
417408
} catch (Exception e) {
@@ -442,11 +433,9 @@ protected void addTrunkIfExists(Set<String> res) {
442433
} catch (SVNException e) {
443434
throw new EVCSException(e);
444435
}
445-
446436
}
447437

448-
449-
public void listEntries(List<String> entries, String path) throws Exception {
438+
protected void listEntries(List<String> entries, String path) throws Exception {
450439
@SuppressWarnings("unchecked")
451440
Collection<SVNDirEntry> subEntries = repository.getDir(path, -1, null, (Collection<SVNDirEntry>) null);
452441
List<SVNDirEntry> list = new ArrayList<>(subEntries);
@@ -514,7 +503,6 @@ public VCSCommit removeFile(String branchName, String filePath, String commitMes
514503
public List<VCSCommit> getCommitsRange(String branchName, String firstCommitId, WalkDirection direction, int limit) {
515504
final List<VCSCommit> res = new ArrayList<>();
516505
try {
517-
518506
Long sinceCommit;
519507
Long untilCommit;
520508
if (direction == WalkDirection.ASC) {
@@ -683,7 +671,6 @@ public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
683671
}
684672
}
685673

686-
687674
@Override
688675
public VCSTag getLastTag() {
689676
try {

0 commit comments

Comments
 (0)