From d9b5d86c052f768318b3cce9bfeff7020bc51352 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Wed, 28 Oct 2020 11:44:35 +0100 Subject: [PATCH 1/2] Do not expose subversion commit with id 0 --- CHANGELOG.md | 4 +++ .../java/sonia/scm/repository/SvnUtil.java | 2 +- .../scm/repository/spi/SvnLogCommand.java | 2 ++ .../scm/repository/spi/SvnLogCommandTest.java | 34 ++++++++++++++++--- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd515bb632..6cbd392b0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased +### Fixed +- Do not expose subversion commit with id 0 ([#1395](https://github.com/scm-manager/scm-manager/pull/1395)) + ## [2.8.0] - 2020-10-27 ### Added - Generation of email addresses for users, where none is configured ([#1370](https://github.com/scm-manager/scm-manager/pull/1370)) diff --git a/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnUtil.java b/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnUtil.java index 26a851d1e2..c58eef328a 100644 --- a/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnUtil.java +++ b/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnUtil.java @@ -198,7 +198,7 @@ public final class SvnUtil Person.toPerson(entry.getAuthor()), entry.getMessage()); - if (revision > 0) + if (revision > 1) { changeset.getParents().add(String.valueOf(revision - 1)); } diff --git a/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/spi/SvnLogCommand.java b/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/spi/SvnLogCommand.java index 35f5ec1fec..d613f9284d 100644 --- a/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/spi/SvnLogCommand.java +++ b/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/spi/SvnLogCommand.java @@ -26,6 +26,7 @@ package sonia.scm.repository.spi; //~--- non-JDK imports -------------------------------------------------------- +import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.google.common.collect.Lists; import org.slf4j.Logger; @@ -76,6 +77,7 @@ public class SvnLogCommand extends AbstractSvnCommand implements LogCommand try { long revisioNumber = parseRevision(revision, repository); + Preconditions.checkArgument(revisioNumber > 0, "revision must be greater than zero: %d", revisioNumber); SVNRepository repo = open(); Collection entries = repo.log(null, null, revisioNumber, revisioNumber, true, true); diff --git a/scm-plugins/scm-svn-plugin/src/test/java/sonia/scm/repository/spi/SvnLogCommandTest.java b/scm-plugins/scm-svn-plugin/src/test/java/sonia/scm/repository/spi/SvnLogCommandTest.java index cab001573b..b758d7828c 100644 --- a/scm-plugins/scm-svn-plugin/src/test/java/sonia/scm/repository/spi/SvnLogCommandTest.java +++ b/scm-plugins/scm-svn-plugin/src/test/java/sonia/scm/repository/spi/SvnLogCommandTest.java @@ -32,10 +32,9 @@ import sonia.scm.repository.Changeset; import sonia.scm.repository.ChangesetPagingResult; import sonia.scm.repository.Modifications; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import java.util.stream.StreamSupport; + +import static org.junit.Assert.*; //~--- JDK imports ------------------------------------------------------------ @@ -56,11 +55,36 @@ public class SvnLogCommandTest extends AbstractSvnCommandTestBase assertEquals(5, result.getChangesets().size()); } + @Test + public void shouldNotReturnChangesetWithIdZero() { + ChangesetPagingResult result = createCommand().getChangesets(new LogCommandRequest()); + boolean found = StreamSupport.stream(result.spliterator(), false).anyMatch(c -> "0".equals(c.getId())); + assertFalse(found); + } + + @Test(expected = IllegalArgumentException.class) + public void shouldThrowIllegalArgumentExceptionForChangesetZero() { + createCommand().getChangeset("0", new LogCommandRequest()); + } + + @Test + public void shouldNotReturnChangesetZeroAsParent() { + Changeset changeset = createCommand().getChangeset("1", new LogCommandRequest()); + assertTrue(changeset.getParents().isEmpty()); + } + + @Test + public void shouldAppendParentChangeset() { + Changeset changeset = createCommand().getChangeset("2", new LogCommandRequest()); + assertEquals(1, changeset.getParents().size()); + assertEquals("1", changeset.getParents().get(0)); + } + @Test public void testShouldStartWithRevisionOne() { ChangesetPagingResult result = createCommand().getChangesets(new LogCommandRequest()); Changeset first = Iterables.getLast(result); - assertEquals(first.getId(), "1"); + assertEquals("1", first.getId()); } @Test From 6ebc131a42595bc272369f2dfe89b143b0209396 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Wed, 28 Oct 2020 11:46:39 +0100 Subject: [PATCH 2/2] Suppress non fixable SonarQube issue --- .../src/main/java/sonia/scm/repository/SvnUtil.java | 1 + 1 file changed, 1 insertion(+) diff --git a/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnUtil.java b/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnUtil.java index c58eef328a..7a8dd5e98d 100644 --- a/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnUtil.java +++ b/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnUtil.java @@ -232,6 +232,7 @@ public final class SvnUtil * * @return */ + @SuppressWarnings("java:S1149") // we can not use StringBuild SVNXMLUtil requires StringBuffer public static String createErrorBody(SVNErrorCode errorCode) { StringBuffer xmlBuffer = new StringBuffer();