Skip to content

Commit b1a5d7c

Browse files
committed
setFileContent returns SVNCommit instance instead of string commit id
getCommitsRange() overload added getHeadCommit() is implemented
1 parent 1c4f211 commit b1a5d7c

File tree

1 file changed

+70
-25
lines changed

1 file changed

+70
-25
lines changed

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

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
import java.util.Set;
1414

1515
import org.apache.commons.io.FileUtils;
16+
import org.scm4j.vcs.api.IVCS;
17+
import org.scm4j.vcs.api.VCSChangeType;
18+
import org.scm4j.vcs.api.VCSCommit;
19+
import org.scm4j.vcs.api.VCSDiffEntry;
20+
import org.scm4j.vcs.api.VCSMergeResult;
21+
import org.scm4j.vcs.api.WalkDirection;
22+
import org.scm4j.vcs.api.exceptions.EVCSBranchExists;
23+
import org.scm4j.vcs.api.exceptions.EVCSException;
24+
import org.scm4j.vcs.api.exceptions.EVCSFileNotFound;
25+
import org.scm4j.vcs.api.workingcopy.IVCSLockedWorkingCopy;
26+
import org.scm4j.vcs.api.workingcopy.IVCSRepositoryWorkspace;
27+
import org.scm4j.vcs.api.workingcopy.IVCSWorkspace;
1628
import org.tmatesoft.svn.core.ISVNLogEntryHandler;
1729
import org.tmatesoft.svn.core.SVNCommitInfo;
1830
import org.tmatesoft.svn.core.SVNDepth;
@@ -50,18 +62,6 @@
5062
import org.tmatesoft.svn.core.wc2.SvnOperationFactory;
5163
import org.tmatesoft.svn.core.wc2.SvnTarget;
5264

53-
import org.scm4j.vcs.api.IVCS;
54-
import org.scm4j.vcs.api.VCSChangeType;
55-
import org.scm4j.vcs.api.VCSCommit;
56-
import org.scm4j.vcs.api.VCSDiffEntry;
57-
import org.scm4j.vcs.api.VCSMergeResult;
58-
import org.scm4j.vcs.api.exceptions.EVCSBranchExists;
59-
import org.scm4j.vcs.api.exceptions.EVCSException;
60-
import org.scm4j.vcs.api.exceptions.EVCSFileNotFound;
61-
import org.scm4j.vcs.api.workingcopy.IVCSLockedWorkingCopy;
62-
import org.scm4j.vcs.api.workingcopy.IVCSRepositoryWorkspace;
63-
import org.scm4j.vcs.api.workingcopy.IVCSWorkspace;
64-
6565
public class SVNVCS implements IVCS {
6666

6767
private static final int SVN_PATH_IS_NOT_WORKING_COPY_ERROR_CODE = 155007;
@@ -119,10 +119,6 @@ private SVNURL getBranchUrl(String branchPath) throws SVNException {
119119
return SVNURL.parseURIEncoded(repoUrl + getBranchName(branchPath));
120120
}
121121

122-
private String getBranchPath(String branchPath) {
123-
return getBranchName(branchPath);
124-
}
125-
126122
@Override
127123
public void createBranch(String srcBranchName, String dstBranchName, String commitMessage) {
128124
try {
@@ -294,7 +290,7 @@ public String getFileContent(String branchName, String filePath) {
294290
}
295291

296292
@Override
297-
public String setFileContent(String branchName, String filePath, String content, String commitMessage) {
293+
public VCSCommit setFileContent(String branchName, String filePath, String content, String commitMessage) {
298294
try {
299295
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
300296
checkout(getBranchUrl(branchName), wc.getFolder());
@@ -317,11 +313,12 @@ public String setFileContent(String branchName, String filePath, String content,
317313
false, false, SVNDepth.EMPTY, false, true);
318314
}
319315

320-
SVNCommitInfo res = clientManager
316+
SVNCommitInfo newCommit = clientManager
321317
.getCommitClient()
322318
.doCommit(new File[] { wc.getFolder() }, false, commitMessage,
323319
new SVNProperties(), null, false, false, SVNDepth.INFINITY);
324-
return Long.toString(res.getNewRevision());
320+
return newCommit == SVNCommitInfo.NULL ? VCSCommit.EMPTY :
321+
new VCSCommit(Long.toString(newCommit.getNewRevision()), commitMessage, newCommit.getAuthor());
325322
}
326323
} catch (SVNException e) {
327324
throw new EVCSException(e);
@@ -344,14 +341,14 @@ private void fillUnifiedDiffs(final String srcBranchName, final String dstBranch
344341
final SvnDiff diff = svnOperationFactory.createDiff();
345342

346343
if (entry.getChangeType() == VCSChangeType.ADD) {
347-
SVNLogEntry firstCommit = getBranchFirstCommit(getBranchPath(dstBranchName));
344+
SVNLogEntry firstCommit = getBranchFirstCommit(getBranchName(dstBranchName));
348345
diff.setSource(SvnTarget.fromURL(getBranchUrl(srcBranchName).appendPath(entry.getFilePath(), true), SVNRevision.HEAD),
349346
SVNRevision.create(firstCommit.getRevision()),
350-
SVNRevision.create(repository.info(getBranchPath(dstBranchName), -1).getRevision()));
347+
SVNRevision.create(repository.info(getBranchName(dstBranchName), -1).getRevision()));
351348
} else if (entry.getChangeType() == VCSChangeType.DELETE) {
352-
SVNLogEntry firstCommit = getBranchFirstCommit(getBranchPath(dstBranchName));
349+
SVNLogEntry firstCommit = getBranchFirstCommit(getBranchName(dstBranchName));
353350
diff.setSource(SvnTarget.fromURL(getBranchUrl(dstBranchName).appendPath(entry.getFilePath(), true), SVNRevision.HEAD),
354-
SVNRevision.create(repository.info(getBranchPath(dstBranchName), -1).getRevision()),
351+
SVNRevision.create(repository.info(getBranchName(dstBranchName), -1).getRevision()),
355352
SVNRevision.create(firstCommit.getRevision()));
356353
} else {
357354
diff.setSources(
@@ -520,6 +517,40 @@ public String removeFile(String branchName, String filePath, String commitMessag
520517
}
521518

522519
}
520+
521+
@Override
522+
public List<VCSCommit> getCommitsRange(String branchName, String startFromCommitId, WalkDirection direction, int limit) {
523+
final List<VCSCommit> res = new ArrayList<>();
524+
try {
525+
String bn = getBranchName(branchName);
526+
Long sinceCommit;
527+
Long untilCommit;
528+
if (direction == WalkDirection.ASC) {
529+
sinceCommit = startFromCommitId == null ? getBranchFirstCommit(bn).getRevision() :
530+
Long.parseLong(startFromCommitId);
531+
untilCommit = Long.parseLong(getHeadCommit(branchName).getId());
532+
} else {
533+
sinceCommit = startFromCommitId == null ? getBranchFirstCommit(bn).getRevision() :
534+
Long.parseLong(startFromCommitId);
535+
untilCommit = getBranchFirstCommit(bn).getRevision();
536+
}
537+
538+
repository.log(new String[] { bn }, sinceCommit, untilCommit, true, true, limit,
539+
new ISVNLogEntryHandler() {
540+
@Override
541+
public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
542+
VCSCommit commit = new VCSCommit(Long.toString(logEntry.getRevision()), logEntry.getMessage(),
543+
logEntry.getAuthor());
544+
res.add(commit);
545+
}
546+
});
547+
return res;
548+
} catch (SVNException e) {
549+
throw new EVCSException(e);
550+
} catch (Exception e) {
551+
throw new RuntimeException(e);
552+
}
553+
}
523554

524555
@Override
525556
public List<VCSCommit> getCommitsRange(String branchName, String afterCommitId, String untilCommitId) {
@@ -530,8 +561,8 @@ public List<VCSCommit> getCommitsRange(String branchName, String afterCommitId,
530561
getBranchFirstCommit(bn).getRevision() :
531562
Long.parseLong(afterCommitId);
532563
Long untilCommit = untilCommitId == null ? -1L : Long.parseLong(untilCommitId);
533-
repository.log(new String[] { getBranchName(branchName) },
534-
sinceCommit, untilCommit, true, true, 0 /* limit */, new ISVNLogEntryHandler() {
564+
repository.log(new String[] { bn }, sinceCommit, untilCommit, true, true, 0 /* limit */,
565+
new ISVNLogEntryHandler() {
535566
@Override
536567
public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
537568
VCSCommit commit = new VCSCommit(Long.toString(logEntry.getRevision()), logEntry.getMessage(),
@@ -551,4 +582,18 @@ public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
551582
public IVCSWorkspace getWorkspace() {
552583
return repo.getWorkspace();
553584
}
585+
586+
@Override
587+
public VCSCommit getHeadCommit(String branchName) {
588+
try {
589+
SVNDirEntry entry = repository.info(getBranchName(branchName), -1);
590+
return new VCSCommit(Long.toString(entry.getRevision()), entry.getCommitMessage(),
591+
entry.getAuthor());
592+
} catch (SVNException e) {
593+
throw new EVCSException(e);
594+
} catch (Exception e) {
595+
throw new RuntimeException(e);
596+
}
597+
598+
}
554599
}

0 commit comments

Comments
 (0)