Skip to content

Commit d3ba11c

Browse files
committed
fixed getBranches() working with path
1 parent 258105e commit d3ba11c

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

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

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.ByteArrayOutputStream;
44
import java.io.File;
55
import java.io.FileWriter;
6+
import java.net.URL;
67
import java.nio.charset.StandardCharsets;
78
import java.util.ArrayList;
89
import java.util.Collection;
@@ -13,6 +14,7 @@
1314
import java.util.Set;
1415

1516
import org.apache.commons.io.FileUtils;
17+
import org.apache.commons.lang3.StringUtils;
1618
import org.scm4j.vcs.api.IVCS;
1719
import org.scm4j.vcs.api.VCSChangeType;
1820
import org.scm4j.vcs.api.VCSCommit;
@@ -59,10 +61,12 @@
5961
import org.tmatesoft.svn.core.wc.SVNWCClient;
6062
import org.tmatesoft.svn.core.wc.SVNWCUtil;
6163
import org.tmatesoft.svn.core.wc2.ISvnObjectReceiver;
64+
import org.tmatesoft.svn.core.wc2.SvnCopySource;
6265
import org.tmatesoft.svn.core.wc2.SvnDiff;
6366
import org.tmatesoft.svn.core.wc2.SvnDiffStatus;
6467
import org.tmatesoft.svn.core.wc2.SvnDiffSummarize;
6568
import org.tmatesoft.svn.core.wc2.SvnOperationFactory;
69+
import org.tmatesoft.svn.core.wc2.SvnRemoteCopy;
6670
import org.tmatesoft.svn.core.wc2.SvnTarget;
6771

6872
public class SVNVCS implements IVCS {
@@ -451,12 +455,16 @@ public List<VCSDiffEntry> getBranchesDiff(final String srcBranchName, final Stri
451455
public Set<String> getBranches(String path) {
452456
try {
453457
List<String> entries = listEntries(SVNVCS.BRANCHES_PATH, path == null ? "" : path);
454-
Set<String> res = new HashSet<>(entries);
458+
Set<String> tempRes = new HashSet<>(entries);
455459
if (repository.checkPath(MASTER_PATH, -1) == SVNNodeKind.DIR) {
456460
if (path == null || MASTER_PATH.startsWith(path) ) {
457-
res.add(MASTER_PATH.replace("/", ""));
461+
tempRes.add(MASTER_PATH.replace("/", ""));
458462
}
459463
}
464+
Set<String> res = new HashSet<>();
465+
for (String str : tempRes) {
466+
res.add(StringUtils.removeStart(StringUtils.removeStart(str, SVNVCS.BRANCHES_PATH), path));
467+
}
460468
return res;
461469
} catch (SVNException e) {
462470
throw new EVCSException(e);
@@ -465,13 +473,13 @@ public Set<String> getBranches(String path) {
465473
}
466474
}
467475

468-
protected List<String> listEntries(String path, String subdir) throws Exception {
476+
protected List<String> listEntries(String path, String subdirStartsWith) throws Exception {
469477
List<String> res = new ArrayList<>();
470-
if (repository.checkPath(path + subdir , -1) == SVNNodeKind.NONE) {
478+
if (repository.checkPath(path , -1) == SVNNodeKind.NONE) {
471479
return res;
472480
}
473481
@SuppressWarnings("unchecked")
474-
Collection<SVNDirEntry> subEntries = repository.getDir(path + subdir, -1, null, (Collection<SVNDirEntry>) null);
482+
Collection<SVNDirEntry> subEntries = repository.getDir(path, -1, null, (Collection<SVNDirEntry>) null);
475483
List<SVNDirEntry> list = new ArrayList<>(subEntries);
476484
Collections.sort(list, new Comparator<SVNDirEntry>() {
477485
@Override
@@ -486,9 +494,8 @@ public int compare(SVNDirEntry o1, SVNDirEntry o2) {
486494
}
487495
});
488496
for (SVNDirEntry entry : list) {
489-
if (entry.getKind() == SVNNodeKind.DIR) {
490-
res.add(((path.equals(SVNVCS.BRANCHES_PATH) ? "" : path) + subdir + entry.getName())
491-
.replace(SVNVCS.BRANCHES_PATH, ""));
497+
if (entry.getKind() == SVNNodeKind.DIR && entry.getName().startsWith(subdirStartsWith)) {
498+
res.add(path + entry.getName());
492499
}
493500
}
494501
return res;
@@ -634,20 +641,29 @@ public Boolean fileExists(String branchName, String filePath) {
634641

635642
@Override
636643
public VCSTag createTag(String branchName, String tagName, String tagMessage, String revisionToTag) throws EVCSTagExists {
637-
try {
644+
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
645+
//checkout(getBranchUrl(branchName), wc.getFolder(), null);
638646
SVNURL srcURL = getBranchUrl(branchName);
639647
SVNURL dstURL = SVNURL.parseURIEncoded(repoUrl + TAGS_PATH + tagName);
640648
SVNCopySource copySource = revisionToTag == null ?
641649
new SVNCopySource(SVNRevision.HEAD, SVNRevision.HEAD, srcURL) :
642650
new SVNCopySource(SVNRevision.parse(revisionToTag), SVNRevision.parse(revisionToTag), srcURL);
643651

644-
clientManager.getCopyClient().doCopy(new SVNCopySource[] {copySource}, dstURL,
645-
false, false, true, tagMessage, null);
646-
652+
SvnRemoteCopy tagOperation = new SvnOperationFactory().createRemoteCopy();
653+
SvnCopySource source = SvnCopySource.create(SvnTarget.fromURL(srcURL), SVNRevision.UNDEFINED);
654+
tagOperation.addCopySource(source);
655+
tagOperation.setSingleTarget(SvnTarget.fromURL(dstURL));
656+
tagOperation.setFailWhenDstExists(true);
657+
tagOperation.setCommitMessage(tagMessage);
658+
tagOperation.run();
659+
//
660+
// clientManager.getCopyClient().doCopy(new SVNCopySource[] {copySource}, dstURL,
661+
// false, false, true, tagMessage, null);
662+
647663
SVNDirEntry entry = repository.info(TAGS_PATH + tagName, -1);
648664

649665
SVNLogEntry copyFromEntry = revToSVNEntry(getBranchName(branchName),
650-
revisionToTag == null ? SVNRevision.HEAD.getNumber() : Long.parseLong(revisionToTag));
666+
revisionToTag == null ? SVNRevision.BASE.getNumber() : Long.parseLong(revisionToTag));
651667

652668
return new VCSTag(tagName, tagMessage, entry.getAuthor(), svnLogEntryToVCSCommit(copyFromEntry));
653669
} catch (SVNException e) {
@@ -691,10 +707,11 @@ public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
691707
public List<VCSTag> getTags() {
692708
try {
693709
List<String> entries = listEntries(TAGS_PATH, "");
710+
694711
List<VCSTag> res = new ArrayList<>();
695712
SVNTagBaseCommit handler;
696713
for (String entryStr : entries) {
697-
714+
698715
SVNLogEntry entry = revToSVNEntry(entryStr, -1L);
699716

700717
handler = new SVNTagBaseCommit();

src/test/java/org/scm4j/vcs/svn/SVNVCSTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ public void testSVNVCSUtilsCreation() {
435435
public void testListEntriesNone() throws Exception {
436436
SVNRepository mockedRepo = spy(svn.getSVNRepository());
437437
svn.setSVNRepository(mockedRepo);
438-
doReturn(SVNNodeKind.NONE).when(mockedRepo).checkPath(anyString(), anyLong());
438+
doReturn(SVNNodeKind.NONE).when(mockedRepo).checkPath((String) isNull(), anyLong());
439439
svn.listEntries(null, null); // expecting no NPE
440440
}
441441
}

0 commit comments

Comments
 (0)