fix SonarQube issues

This commit is contained in:
Sebastian Sdorra
2019-07-31 13:24:31 +02:00
parent bbdc5a1989
commit 00fa943e51
2 changed files with 26 additions and 68 deletions

View File

@@ -1,10 +1,10 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* <p>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* <p>
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
@@ -13,7 +13,7 @@
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* <p>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -24,9 +24,8 @@
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* <p>
* http://bitbucket.org/sdorra/scm-manager
*
*/
@@ -36,84 +35,39 @@ package sonia.scm.repository.spi;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.Repository;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
*
* @author Sebastian Sdorra
*/
public class GitDiffCommand extends AbstractGitCommand implements DiffCommand
{
public class GitDiffCommand extends AbstractGitCommand implements DiffCommand {
/**
* the logger for GitDiffCommand
*/
private static final Logger logger =
LoggerFactory.getLogger(GitDiffCommand.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
*
* @param context
* @param repository
*/
GitDiffCommand(GitContext context, Repository repository)
{
GitDiffCommand(GitContext context, Repository repository) {
super(context, repository);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param request
* @param output
*/
@Override
public void getDiffResult(DiffCommandRequest request, OutputStream output)
{
DiffFormatter formatter = null;
try
{
public void getDiffResult(DiffCommandRequest request, OutputStream output) throws IOException {
@SuppressWarnings("squid:S2095") // repository will be closed with the RepositoryService
org.eclipse.jgit.lib.Repository repository = open();
formatter = new DiffFormatter(new BufferedOutputStream(output));
try (DiffFormatter formatter = new DiffFormatter(new BufferedOutputStream(output))) {
formatter.setRepository(repository);
Differ.Diff diff = Differ.diff(repository, request);
for (DiffEntry e : diff.getEntries())
{
if (!e.getOldId().equals(e.getNewId()))
{
for (DiffEntry e : diff.getEntries()) {
if (!e.getOldId().equals(e.getNewId())) {
formatter.format(e);
}
}
formatter.flush();
}
catch (Exception ex)
{
// TODO throw exception
logger.error("could not create diff", ex);
}
finally
{
GitUtil.release(formatter);
}
}
}

View File

@@ -4,6 +4,7 @@ import com.google.common.base.Throwables;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.Repository;
import sonia.scm.repository.api.DiffFile;
import sonia.scm.repository.api.DiffResult;
@@ -47,7 +48,11 @@ public class GitDiffResultCommand extends AbstractGitCommand implements DiffResu
@Override
public Iterator<DiffFile> iterator() {
return diff.getEntries().stream().map(diffEntry -> new GitDiffFile(repository, diffEntry)).collect(Collectors.<DiffFile>toList()).iterator();
return diff.getEntries()
.stream()
.map(diffEntry -> new GitDiffFile(repository, diffEntry))
.collect(Collectors.<DiffFile>toList())
.iterator();
}
}
@@ -89,13 +94,12 @@ public class GitDiffResultCommand extends AbstractGitCommand implements DiffResu
}
private String format(org.eclipse.jgit.lib.Repository repository, DiffEntry entry) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
DiffFormatter formatter = new DiffFormatter(baos);
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DiffFormatter formatter = new DiffFormatter(baos)) {
formatter.setRepository(repository);
formatter.format(entry);
return baos.toString();
} catch (IOException ex) {
throw Throwables.propagate(ex);
throw new InternalRepositoryException(GitDiffResultCommand.this.repository, "failed to format diff entry", ex);
}
}