Skip to content

Commit e69f56e

Browse files
committed
refactor, tests improved
1 parent 3b31463 commit e69f56e

File tree

2 files changed

+164
-257
lines changed

2 files changed

+164
-257
lines changed

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

Lines changed: 35 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
import org.apache.commons.io.FileUtils;
44
import org.apache.commons.lang3.StringUtils;
55
import org.scm4j.vcs.api.*;
6-
import org.scm4j.vcs.api.exceptions.EVCSBranchExists;
7-
import org.scm4j.vcs.api.exceptions.EVCSBranchNotFound;
8-
import org.scm4j.vcs.api.exceptions.EVCSException;
9-
import org.scm4j.vcs.api.exceptions.EVCSFileNotFound;
10-
import org.scm4j.vcs.api.exceptions.EVCSTagExists;
6+
import org.scm4j.vcs.api.exceptions.*;
117
import org.scm4j.vcs.api.workingcopy.IVCSLockedWorkingCopy;
128
import org.scm4j.vcs.api.workingcopy.IVCSRepositoryWorkspace;
139
import org.scm4j.vcs.api.workingcopy.IVCSWorkspace;
@@ -31,26 +27,25 @@ public class SVNVCS implements IVCS {
3127
private static final int SVN_PATH_IS_NOT_WORKING_COPY_ERROR_CODE = 155007;
3228
private static final int SVN_ITEM_EXISTS_ERROR_CODE = 160020;
3329
private static final int SVN_FILE_NOT_FOUND_ERROR_CODE = 160013;
30+
31+
public static final String MASTER_PATH= "trunk/";
32+
public static final String BRANCHES_PATH = "branches/";
33+
public static final String TAGS_PATH = "tags/";
34+
public static final String SVN_VCS_TYPE_STRING = "svn";
35+
3436
private BasicAuthenticationManager authManager;
3537
private SVNRepository repository;
3638
private ISVNOptions options;
3739
private SVNClientManager clientManager;
3840
private SVNURL trunkSVNUrl;
3941
private SVNAuthentication userPassAuth;
40-
private final IVCSRepositoryWorkspace repo;
42+
private IVCSRepositoryWorkspace repo;
4143
private String repoUrl;
42-
43-
public static final String MASTER_PATH= "trunk/";
44-
public static final String BRANCHES_PATH = "branches/";
45-
public static final String TAGS_PATH = "tags/";
46-
public static final String SVN_VCS_TYPE_STRING = "svn";
47-
4844

4945
public void setClientManager(SVNClientManager clientManager) {
5046
this.clientManager = clientManager;
5147
}
5248

53-
5449
public SVNClientManager getClientManager() {
5550
return clientManager;
5651
}
@@ -70,6 +65,10 @@ public SVNRepository getSVNRepository() {
7065
public void setSVNRepository(SVNRepository repository) {
7166
this.repository = repository;
7267
}
68+
69+
public void setRepo(IVCSRepositoryWorkspace repo) {
70+
this.repo = repo;
71+
}
7372

7473
public SVNVCS(IVCSRepositoryWorkspace repo, String user, String password) {
7574
this.repo = repo;
@@ -94,7 +93,7 @@ public SVNVCS(IVCSRepositoryWorkspace repo, String user, String password) {
9493
options, repository.getAuthenticationManager());
9594
}
9695

97-
public SVNURL getBranchUrl(String branchPath) throws Exception {
96+
public SVNURL getBranchUrl(String branchPath) throws SVNException {
9897
return SVNURL.parseURIEncoded(repoUrl + getBranchName(branchPath));
9998
}
10099

@@ -105,33 +104,29 @@ public void createBranch(String srcBranchName, String dstBranchName, String comm
105104
try {
106105
fromUrl = getBranchUrl(srcBranchName);
107106
toUrl = getBranchUrl(dstBranchName);
108-
} catch (Exception e) {
109-
throw new RuntimeException(e);
107+
} catch (SVNException e) {
108+
throw new EVCSException(e);
110109
}
111110
createBranch(fromUrl, toUrl, commitMessage);
112111
}
113112

114113
public void createBranch(SVNURL fromUrl, SVNURL toUrl, String commitMessage) {
115114
try {
116-
getBranchUrl("test"); // for exceptions rethrowing test only
117115
SVNCopyClient copyClient = clientManager.getCopyClient();
118116
SVNCopySource copySource = new SVNCopySource(SVNRevision.HEAD, SVNRevision.HEAD, fromUrl);
119117
copySource.setCopyContents(false);
120-
121118
copyClient.doCopy(new SVNCopySource[] { copySource }, toUrl,
122119
false, // isMove
123120
true, // make parents
124-
true, // failWhenDstExistsb
121+
true, // failWhenDstExists
125122
commitMessage, // commit message
126123
null); // SVNProperties
127124

128125
} catch (SVNException e) {
129126
if (e.getErrorMessage().getErrorCode().getCode() == SVN_ITEM_EXISTS_ERROR_CODE) {
130127
throw new EVCSBranchExists(e);
131-
}
132-
throw new EVCSException (e);
133-
} catch (Exception e) {
134-
throw new RuntimeException(e);
128+
}
129+
throw new EVCSException(e);
135130
}
136131
}
137132

@@ -143,8 +138,6 @@ public void deleteBranch(String branchName, String commitMessage) {
143138
.doDelete(new SVNURL[] { getBranchUrl(branchName) }, commitMessage);
144139
} catch (SVNException e) {
145140
throw new EVCSException(e);
146-
} catch (Exception e) {
147-
throw new RuntimeException(e);
148141
}
149142
}
150143

@@ -156,14 +149,10 @@ public VCSMergeResult merge(String srcBranchName, String dstBranchName, String c
156149

157150
DefaultSVNOptions options = (DefaultSVNOptions) diffClient.getOptions();
158151
final List<String> conflictingFiles = new ArrayList<>();
159-
options.setConflictHandler(new ISVNConflictHandler() {
160-
@Override
161-
public SVNConflictResult handleConflict(SVNConflictDescription conflictDescription)
162-
throws SVNException {
163-
conflictingFiles.add(conflictDescription.getMergeFiles().getLocalPath());
164-
return new SVNConflictResult(SVNConflictChoice.POSTPONE,
165-
conflictDescription.getMergeFiles().getResultFile());
166-
}
152+
options.setConflictHandler(conflictDescription -> {
153+
conflictingFiles.add(conflictDescription.getMergeFiles().getLocalPath());
154+
return new SVNConflictResult(SVNConflictChoice.POSTPONE,
155+
conflictDescription.getMergeFiles().getResultFile());
167156
});
168157

169158
SVNRevisionRange range = new SVNRevisionRange(SVNRevision.create(1), SVNRevision.HEAD);
@@ -189,7 +178,7 @@ public SVNConflictResult handleConflict(SVNConflictDescription conflictDescripti
189178
}
190179
}
191180
return new VCSMergeResult(success, conflictingFiles);
192-
} catch (Exception e) {
181+
} catch (SVNException e) {
193182
wc.setCorrupted(true);
194183
throw e;
195184
}
@@ -200,11 +189,11 @@ public SVNConflictResult handleConflict(SVNConflictDescription conflictDescripti
200189
}
201190
}
202191

203-
public SVNWCClient getRevertClient(DefaultSVNOptions options) {
192+
protected SVNWCClient getRevertClient(DefaultSVNOptions options) {
204193
return new SVNWCClient(authManager, options);
205194
}
206195

207-
public void checkout(SVNURL sourceUrl, File destPath, String revision) throws Exception {
196+
public void checkout(SVNURL sourceUrl, File destPath, String revision) throws SVNException {
208197
SVNUpdateClient updateClient = clientManager.getUpdateClient();
209198
updateClient.setIgnoreExternals(false);
210199
SVNRevision svnRevision = revision == null ? SVNRevision.HEAD : SVNRevision.parse(revision);
@@ -356,11 +345,10 @@ protected SVNLogEntry getDirFirstCommit(final String dir) throws SVNException {
356345
return entries.iterator().next();
357346
}
358347

359-
protected SVNLogEntry getBranchFirstCommit(final String branchPath) throws Exception {
348+
protected SVNLogEntry getBranchFirstCommit(final String branchPath) throws SVNException {
360349
return getDirFirstCommit(getBranchName(branchPath));
361350
}
362351

363-
364352
private List<VCSDiffEntry> getDiffEntries(final String srcBranchName, final String dstBranchName)
365353
throws Exception {
366354
final SvnOperationFactory svnOperationFactory = new SvnOperationFactory();
@@ -431,13 +419,11 @@ public Set<String> getBranches(String path) {
431419
return res;
432420
} catch (SVNException e) {
433421
throw new EVCSException(e);
434-
} catch (Exception e) {
435-
throw new RuntimeException(e);
436422
}
437423
}
438424

439425
@SuppressWarnings("unchecked")
440-
protected List<String> listEntries(String path) throws Exception {
426+
protected List<String> listEntries(String path) throws SVNException {
441427
List<String> res = new ArrayList<>();
442428
if (path == null) {
443429
return res;
@@ -489,8 +475,6 @@ public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
489475
return res;
490476
} catch (SVNException e) {
491477
throw new EVCSException(e);
492-
} catch (Exception e) {
493-
throw new RuntimeException(e);
494478
}
495479
}
496480

@@ -508,10 +492,7 @@ public VCSCommit removeFile(String branchName, String filePath, String commitMes
508492
return new VCSCommit(Long.toString(res.getNewRevision()), commitMessage, res.getAuthor());
509493
} catch (SVNException e) {
510494
throw new EVCSException(e);
511-
} catch (Exception e) {
512-
throw new RuntimeException(e);
513495
}
514-
515496
}
516497

517498
@Override
@@ -530,18 +511,13 @@ public List<VCSCommit> getCommitsRange(String branchName, String firstCommitId,
530511
untilCommit = getBranchFirstCommit(branchName).getRevision();
531512
}
532513
repository.log(new String[] { getBranchName(branchName) }, sinceCommit, untilCommit, true, true, limit,
533-
new ISVNLogEntryHandler() {
534-
@Override
535-
public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
536-
VCSCommit commit = svnLogEntryToVCSCommit(logEntry);
537-
res.add(commit);
538-
}
539-
});
514+
logEntry -> {
515+
VCSCommit commit = svnLogEntryToVCSCommit(logEntry);
516+
res.add(commit);
517+
});
540518
return res;
541519
} catch (SVNException e) {
542520
throw new EVCSException(e);
543-
} catch (Exception e) {
544-
throw new RuntimeException(e);
545521
}
546522
}
547523

@@ -559,17 +535,10 @@ public List<VCSCommit> getCommitsRange(String branchName, String firstCommitId,
559535
Long.parseLong(firstCommitId);
560536
Long untilCommit = untilCommitId == null ? -1L : Long.parseLong(untilCommitId);
561537
repository.log(new String[] { getBranchName(branchName) }, sinceCommit, untilCommit, true, true, 0 /* limit */,
562-
new ISVNLogEntryHandler() {
563-
@Override
564-
public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
565-
res.add(svnLogEntryToVCSCommit(logEntry));
566-
}
567-
});
538+
logEntry -> res.add(svnLogEntryToVCSCommit(logEntry)));
568539
return res;
569540
} catch (SVNException e) {
570541
throw new EVCSException(e);
571-
} catch (Exception e) {
572-
throw new RuntimeException(e);
573542
}
574543
}
575544

@@ -588,12 +557,10 @@ public VCSCommit getHeadCommit(String branchName) {
588557
return null;
589558
}
590559
throw new EVCSException(e);
591-
} catch (Exception e) {
592-
throw new RuntimeException(e);
593560
}
594561
}
595562

596-
protected SVNLogEntry getDirHeadLogEntry(String dir) throws Exception {
563+
protected SVNLogEntry getDirHeadLogEntry(String dir) throws SVNException {
597564
@SuppressWarnings("unchecked")
598565
Collection<SVNLogEntry> entries = repository.log(new String[] { dir }, null, -1 /* start from head commit */,
599566
0 /* to the first commit */, true, true);
@@ -613,8 +580,6 @@ public Boolean fileExists(String branchName, String filePath) {
613580
return nodeKind == SVNNodeKind.FILE;
614581
} catch (SVNException e) {
615582
throw new EVCSException(e);
616-
} catch (Exception e) {
617-
throw new RuntimeException(e);
618583
}
619584
}
620585

@@ -640,12 +605,10 @@ public VCSTag createTag(String branchName, String tagName, String tagMessage, St
640605
throw new EVCSTagExists(e);
641606
}
642607
throw new EVCSException(e);
643-
} catch (Exception e) {
644-
throw new RuntimeException(e);
645608
}
646609
}
647610

648-
protected SVNLogEntry revToSVNEntry(String branchName, Long rev) throws Exception {
611+
protected SVNLogEntry revToSVNEntry(String branchName, Long rev) throws SVNException {
649612
SVNDirEntry info = repository.info(branchName, rev);
650613
@SuppressWarnings("unchecked")
651614
Collection<SVNLogEntry> entries = repository.log(new String[] {branchName}, null, info.getRevision(), info.getRevision(), true, true);
@@ -661,22 +624,17 @@ public List<VCSTag> getTags() {
661624
return getTags(null);
662625
} catch (SVNException e) {
663626
throw new EVCSException(e);
664-
} catch (Exception e) {
665-
throw new RuntimeException(e);
666627
}
667628
}
668629

669630
@Override
670631
public void removeTag(String tagName) {
671632
try {
672-
getBranchUrl(null); // for test exceptions throwing only
673633
clientManager
674634
.getCommitClient()
675635
.doDelete(new SVNURL[] { SVNURL.parseURIEncoded(repoUrl + TAGS_PATH + tagName) }, null);
676636
} catch (SVNException e) {
677637
throw new EVCSException(e);
678-
} catch (Exception e) {
679-
throw new RuntimeException(e);
680638
}
681639
}
682640

@@ -686,12 +644,10 @@ public void checkout(String branchName, String targetPath, String revision) {
686644
checkout(getBranchUrl(branchName), new File(targetPath), revision);
687645
} catch (SVNException e) {
688646
throw new EVCSException(e);
689-
} catch (Exception e) {
690-
throw new RuntimeException(e);
691647
}
692648
}
693649

694-
protected List<VCSTag> getTags(String onRevision) throws Exception {
650+
protected List<VCSTag> getTags(String onRevision) throws SVNException {
695651
List<VCSTag> res = new ArrayList<>();
696652
@SuppressWarnings("unchecked")
697653
Collection<SVNDirEntry> dirEntries = repository.getDir(TAGS_PATH, -1 , null, (Collection<SVNDirEntry>) null);
@@ -721,8 +677,6 @@ public List<VCSTag> getTagsOnRevision(String revision) {
721677
return new ArrayList<>();
722678
}
723679
throw new EVCSException(e);
724-
} catch (Exception e) {
725-
throw new RuntimeException(e);
726680
}
727681
}
728682
}

0 commit comments

Comments
 (0)