Skip to content

Commit 2d35f31

Browse files
committed
getBranches() fixed
createBranch() optimized
1 parent f3a79c1 commit 2d35f31

File tree

2 files changed

+93
-69
lines changed

2 files changed

+93
-69
lines changed

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

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,10 @@ public void createBranch(String srcBranchName, String dstBranchName, String comm
150150

151151
public void createBranch(SVNURL fromUrl, SVNURL toUrl, String commitMessage) {
152152
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
153-
checkout(fromUrl, wc.getFolder(), null);
153+
//checkout(fromUrl, wc.getFolder(), null);
154154

155155
SVNCopyClient copyClient = new SVNCopyClient(authManager, options);
156-
SVNCopySource copySource = new SVNCopySource(SVNRevision.WORKING, SVNRevision.WORKING,
157-
wc.getFolder());
156+
SVNCopySource copySource = new SVNCopySource(SVNRevision.HEAD, SVNRevision.HEAD, fromUrl);
158157
copySource.setCopyContents(false);
159158

160159
copyClient.doCopy(new SVNCopySource[] { copySource }, toUrl,
@@ -450,10 +449,13 @@ public List<VCSDiffEntry> getBranchesDiff(final String srcBranchName, final Stri
450449
@Override
451450
public Set<String> getBranches(String path) {
452451
try {
453-
List<String> entries = new ArrayList<>();
454-
listEntries(entries, SVNVCS.BRANCHES_PATH, path == null ? "" : path);
452+
List<String> entries = listEntries(SVNVCS.BRANCHES_PATH, path == null ? "" : path);
455453
Set<String> res = new HashSet<>(entries);
456-
addTrunkIfExists(res);
454+
if (repository.checkPath(MASTER_PATH, -1) == SVNNodeKind.DIR) {
455+
if (path == null || MASTER_PATH.startsWith(path) ) {
456+
res.add(MASTER_PATH.replace("/", ""));
457+
}
458+
}
457459
return res;
458460
} catch (SVNException e) {
459461
throw new EVCSException(e);
@@ -462,20 +464,10 @@ public Set<String> getBranches(String path) {
462464
}
463465
}
464466

465-
protected void addTrunkIfExists(Set<String> res) {
466-
try {
467-
if (repository.checkPath(MASTER_PATH, -1) == SVNNodeKind.DIR) {
468-
res.add(MASTER_PATH.replace("/", ""));
469-
}
470-
} catch (SVNException e) {
471-
throw new EVCSException(e);
472-
}
473-
}
474-
475-
protected void listEntries(List<String> entries, String path, String subdir) throws Exception {
476-
467+
protected List<String> listEntries(String path, String subdir) throws Exception {
468+
List<String> res = new ArrayList<>();
477469
if (repository.checkPath(path + subdir , -1) == SVNNodeKind.NONE) {
478-
return;
470+
return res;
479471
}
480472
@SuppressWarnings("unchecked")
481473
Collection<SVNDirEntry> subEntries = repository.getDir(path + subdir, -1, null, (Collection<SVNDirEntry>) null);
@@ -494,10 +486,11 @@ public int compare(SVNDirEntry o1, SVNDirEntry o2) {
494486
});
495487
for (SVNDirEntry entry : list) {
496488
if (entry.getKind() == SVNNodeKind.DIR) {
497-
entries.add(((path.equals(SVNVCS.BRANCHES_PATH) ? "" : path) + subdir + entry.getName())
489+
res.add(((path.equals(SVNVCS.BRANCHES_PATH) ? "" : path) + subdir + entry.getName())
498490
.replace(SVNVCS.BRANCHES_PATH, ""));
499491
}
500492
}
493+
return res;
501494
}
502495

503496
@Override
@@ -678,7 +671,11 @@ protected SVNLogEntry revToSVNEntry(String branchName, Long rev) throws Exceptio
678671

679672
private class SVNTagBaseCommit implements ISVNLogEntryHandler {
680673

681-
public Long copyFromRevision;
674+
private Long copyFromRevision;
675+
676+
public Long getCopyFromRevision() {
677+
return copyFromRevision;
678+
}
682679

683680
@Override
684681
public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
@@ -691,9 +688,8 @@ public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
691688

692689
@Override
693690
public List<VCSTag> getTags() {
694-
List<String> entries = new ArrayList<>();
695691
try {
696-
listEntries(entries, TAGS_PATH, "");
692+
List<String> entries = listEntries(TAGS_PATH, "");
697693
List<VCSTag> res = new ArrayList<>();
698694
SVNTagBaseCommit handler;
699695
for (String entryStr : entries) {
@@ -705,7 +701,7 @@ public List<VCSTag> getTags() {
705701
repository.log(new String[] { entryStr }, -1 /* start from head descending */,
706702
0, true, true, -1, handler);
707703

708-
SVNDirEntry copyFromEntry = repository.info("", handler.copyFromRevision);
704+
SVNDirEntry copyFromEntry = repository.info("", handler.getCopyFromRevision());
709705

710706
res.add(new VCSTag(entryStr.replace(TAGS_PATH, ""), entry.getMessage(), entry.getAuthor(), new VCSCommit(Long.toString(copyFromEntry.getRevision()),
711707
copyFromEntry.getCommitMessage(), copyFromEntry.getAuthor())));
@@ -746,7 +742,30 @@ public void checkout(String branchName, String targetPath, String revision) {
746742

747743
@Override
748744
public List<VCSTag> getTagsOnRevision(String revision) {
749-
// TODO Auto-generated method stub
750-
return null;
745+
try {
746+
List<String> tagEntries = listEntries(TAGS_PATH, "");
747+
List<VCSTag> res = new ArrayList<>();
748+
SVNTagBaseCommit handler;
749+
for (String tagEntryStr : tagEntries) {
750+
751+
SVNLogEntry entry = revToSVNEntry(tagEntryStr, -1L);
752+
753+
handler = new SVNTagBaseCommit();
754+
755+
repository.log(new String[] { tagEntryStr }, -1 /* start from head descending */,
756+
0, true, true, -1, handler);
757+
758+
if (handler.getCopyFromRevision().equals(Long.parseLong(revision))) {
759+
SVNDirEntry copyFromEntry = repository.info("", handler.getCopyFromRevision());
760+
res.add(new VCSTag(tagEntryStr.replace(TAGS_PATH, ""), entry.getMessage(), entry.getAuthor(), new VCSCommit(Long.toString(copyFromEntry.getRevision()),
761+
copyFromEntry.getCommitMessage(), copyFromEntry.getAuthor())));
762+
}
763+
}
764+
return res;
765+
} catch (SVNException e) {
766+
throw new EVCSException(e);
767+
} catch (Exception e) {
768+
throw new RuntimeException(e);
769+
}
751770
}
752771
}

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

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
package org.scm4j.vcs.svn;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
import static org.junit.Assert.assertTrue;
7+
import static org.junit.Assert.fail;
8+
import static org.mockito.Matchers.any;
9+
import static org.mockito.Matchers.anyBoolean;
10+
import static org.mockito.Matchers.anyLong;
11+
import static org.mockito.Matchers.anyString;
12+
import static org.mockito.Matchers.isNull;
13+
import static org.mockito.Mockito.doCallRealMethod;
14+
import static org.mockito.Mockito.doReturn;
15+
import static org.mockito.Mockito.doThrow;
16+
import static org.mockito.Mockito.mock;
17+
import static org.mockito.Mockito.reset;
18+
import static org.mockito.Mockito.spy;
19+
import static org.mockito.Mockito.verify;
20+
21+
import java.io.File;
22+
import java.io.IOException;
23+
import java.io.OutputStream;
24+
import java.lang.reflect.Field;
25+
import java.lang.reflect.InvocationTargetException;
26+
import java.lang.reflect.Method;
27+
import java.lang.reflect.Modifier;
28+
import java.util.Arrays;
29+
import java.util.Collection;
30+
import java.util.List;
31+
332
import org.junit.After;
433
import org.junit.Test;
534
import org.mockito.Matchers;
@@ -10,33 +39,25 @@
1039
import org.scm4j.vcs.api.abstracttest.VCSAbstractTest;
1140
import org.scm4j.vcs.api.exceptions.EVCSException;
1241
import org.scm4j.vcs.api.workingcopy.IVCSRepositoryWorkspace;
13-
import org.tmatesoft.svn.core.*;
42+
import org.tmatesoft.svn.core.ISVNLogEntryHandler;
43+
import org.tmatesoft.svn.core.SVNDepth;
44+
import org.tmatesoft.svn.core.SVNDirEntry;
45+
import org.tmatesoft.svn.core.SVNErrorCode;
46+
import org.tmatesoft.svn.core.SVNErrorMessage;
47+
import org.tmatesoft.svn.core.SVNException;
48+
import org.tmatesoft.svn.core.SVNNodeKind;
49+
import org.tmatesoft.svn.core.SVNProperties;
50+
import org.tmatesoft.svn.core.SVNURL;
1451
import org.tmatesoft.svn.core.auth.ISVNProxyManager;
1552
import org.tmatesoft.svn.core.auth.SVNAuthentication;
1653
import org.tmatesoft.svn.core.auth.SVNPasswordAuthentication;
1754
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
1855
import org.tmatesoft.svn.core.io.SVNRepository;
19-
import org.tmatesoft.svn.core.wc.*;
20-
21-
import java.io.File;
22-
import java.io.IOException;
23-
import java.io.OutputStream;
24-
import java.lang.reflect.Field;
25-
import java.lang.reflect.InvocationTargetException;
26-
import java.lang.reflect.Method;
27-
import java.lang.reflect.Modifier;
28-
import java.util.ArrayList;
29-
import java.util.Arrays;
30-
import java.util.Collection;
31-
import java.util.List;
32-
33-
import static org.junit.Assert.*;
34-
import static org.mockito.Matchers.any;
35-
import static org.mockito.Matchers.anyBoolean;
36-
import static org.mockito.Matchers.anyLong;
37-
import static org.mockito.Matchers.anyString;
38-
import static org.mockito.Matchers.isNull;
39-
import static org.mockito.Mockito.*;
56+
import org.tmatesoft.svn.core.wc.SVNClientManager;
57+
import org.tmatesoft.svn.core.wc.SVNCommitClient;
58+
import org.tmatesoft.svn.core.wc.SVNStatusClient;
59+
import org.tmatesoft.svn.core.wc.SVNStatusType;
60+
import org.tmatesoft.svn.core.wc.SVNWCClient;
4061

4162
public class SVNVCSTest extends VCSAbstractTest {
4263

@@ -154,7 +175,7 @@ private void testExceptionThrowing(Exception testException, Method m, Object[] p
154175
reset(svn);
155176
doThrow(testException).when(svn).checkout(any(SVNURL.class), any(File.class), (String) isNull());
156177
doThrow(testException).when(svn).getBranchUrl(null);
157-
doThrow(testException).when(svn).listEntries(Matchers.<List<String>>any(), anyString(), anyString());
178+
doThrow(testException).when(svn).listEntries(anyString(), anyString());
158179
doThrow(testException).when(svn).getBranchFirstCommit(null);
159180
doThrow(testException).when(svn).revToSVNEntry(anyString(), any(Long.class));
160181
try {
@@ -186,7 +207,7 @@ private boolean wasMockedMethodInvoked() throws Exception {
186207
} catch (WantedButNotInvoked e1) {
187208
}
188209
try {
189-
verify(svn).listEntries(Matchers.<List<String>>any(), anyString(), anyString());
210+
verify(svn).listEntries(anyString(), anyString());
190211
return true;
191212
} catch (WantedButNotInvoked e1) {
192213
}
@@ -311,20 +332,6 @@ public void setFileContentWCCorruption() throws Exception {
311332
assertTrue(mockedLWC.getCorrupted());
312333
}
313334

314-
315-
@Test
316-
public void testAddTrunkIfExistsExceptions() throws Exception {
317-
SVNRepository mockedRepo = spy(svn.getSVNRepository());
318-
svn.setSVNRepository(mockedRepo);
319-
doThrow(testSVNException).when(mockedRepo).checkPath(anyString(),anyLong());
320-
try {
321-
svn.addTrunkIfExists(null);
322-
fail();
323-
} catch (EVCSException e) {
324-
checkEVCSException(e);
325-
}
326-
}
327-
328335
private void checkEVCSException(EVCSException e) {
329336
assertTrue(e.getCause() instanceof SVNException);
330337
assertTrue(e.getCause().getMessage().contains(testSVNException.getMessage()));
@@ -394,14 +401,12 @@ public void testListEntriesSorting() throws Exception {
394401
doReturn(Arrays.asList(entry1, entry2)).when(mockedRepo).getDir(anyString(), anyLong(), any(SVNProperties.class),
395402
Matchers.<Collection<SVNDirEntry>>any());
396403

397-
List<String> entries = new ArrayList<>();
398-
svn.listEntries(entries, "", "");
404+
List<String> entries = svn.listEntries("", "");
399405
assertEquals(entries.get(0), entry1.getName());
400406
assertEquals(entries.get(1), entry2.getName());
401407
doReturn(Arrays.asList(entry1, entry1)).when(mockedRepo).getDir(anyString(), anyLong(), any(SVNProperties.class),
402408
Matchers.<Collection<SVNDirEntry>>any());
403-
entries = new ArrayList<>();
404-
svn.listEntries(entries, "", "");
409+
entries = svn.listEntries("", "");
405410
assertEquals(entries.get(0), entry1.getName());
406411
assertEquals(entries.get(1), entry1.getName());
407412
}
@@ -431,6 +436,6 @@ public void testListEntriesNone() throws Exception {
431436
SVNRepository mockedRepo = spy(svn.getSVNRepository());
432437
svn.setSVNRepository(mockedRepo);
433438
doReturn(SVNNodeKind.NONE).when(mockedRepo).checkPath(anyString(), anyLong());
434-
svn.listEntries(null, null, null); // expecting no NPE
439+
svn.listEntries(null, null); // expecting no NPE
435440
}
436441
}

0 commit comments

Comments
 (0)