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,19 +1,19 @@
/** /**
* Copyright (c) 2010, Sebastian Sdorra * Copyright (c) 2010, Sebastian Sdorra
* All rights reserved. * All rights reserved.
* * <p>
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* * <p>
* 1. Redistributions of source code must retain the above copyright notice, * 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. * this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, * 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation * this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution. * and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its * 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this * contributors may be used to endorse or promote products derived from this
* software without specific prior written permission. * software without specific prior written permission.
* * <p>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * 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 * 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 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* * <p>
* http://bitbucket.org/sdorra/scm-manager * 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.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter; 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 sonia.scm.repository.Repository;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
/** /**
* *
* @author Sebastian Sdorra * @author Sebastian Sdorra
*/ */
public class GitDiffCommand extends AbstractGitCommand implements DiffCommand public class GitDiffCommand extends AbstractGitCommand implements DiffCommand {
{
/** GitDiffCommand(GitContext context, Repository repository) {
* the logger for GitDiffCommand
*/
private static final Logger logger =
LoggerFactory.getLogger(GitDiffCommand.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
*
* @param context
* @param repository
*/
GitDiffCommand(GitContext context, Repository repository)
{
super(context, repository); super(context, repository);
} }
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param request
* @param output
*/
@Override @Override
public void getDiffResult(DiffCommandRequest request, OutputStream output) public void getDiffResult(DiffCommandRequest request, OutputStream output) throws IOException {
{ @SuppressWarnings("squid:S2095") // repository will be closed with the RepositoryService
DiffFormatter formatter = null; org.eclipse.jgit.lib.Repository repository = open();
try (DiffFormatter formatter = new DiffFormatter(new BufferedOutputStream(output))) {
try
{
org.eclipse.jgit.lib.Repository repository = open();
formatter = new DiffFormatter(new BufferedOutputStream(output));
formatter.setRepository(repository); formatter.setRepository(repository);
Differ.Diff diff = Differ.diff(repository, request); Differ.Diff diff = Differ.diff(repository, request);
for (DiffEntry e : diff.getEntries()) for (DiffEntry e : diff.getEntries()) {
{ if (!e.getOldId().equals(e.getNewId())) {
if (!e.getOldId().equals(e.getNewId()))
{
formatter.format(e); formatter.format(e);
} }
} }
formatter.flush(); 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.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter; import org.eclipse.jgit.diff.DiffFormatter;
import sonia.scm.repository.GitUtil; import sonia.scm.repository.GitUtil;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.Repository; import sonia.scm.repository.Repository;
import sonia.scm.repository.api.DiffFile; import sonia.scm.repository.api.DiffFile;
import sonia.scm.repository.api.DiffResult; import sonia.scm.repository.api.DiffResult;
@@ -47,7 +48,11 @@ public class GitDiffResultCommand extends AbstractGitCommand implements DiffResu
@Override @Override
public Iterator<DiffFile> iterator() { 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) { private String format(org.eclipse.jgit.lib.Repository repository, DiffEntry entry) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DiffFormatter formatter = new DiffFormatter(baos)) {
DiffFormatter formatter = new DiffFormatter(baos);
formatter.setRepository(repository); formatter.setRepository(repository);
formatter.format(entry); formatter.format(entry);
return baos.toString(); return baos.toString();
} catch (IOException ex) { } catch (IOException ex) {
throw Throwables.propagate(ex); throw new InternalRepositoryException(GitDiffResultCommand.this.repository, "failed to format diff entry", ex);
} }
} }