mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-03 03:55:51 +01:00
improve logging and exception handling of git commands
This commit is contained in:
@@ -33,6 +33,9 @@ package sonia.scm.repository.spi;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.base.Strings;
|
||||
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.PersonIdent;
|
||||
@@ -46,7 +49,7 @@ import sonia.scm.repository.BlameResult;
|
||||
import sonia.scm.repository.GitUtil;
|
||||
import sonia.scm.repository.Person;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.util.AssertUtil;
|
||||
import sonia.scm.repository.RepositoryException;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -92,23 +95,31 @@ public class GitBlameCommand extends AbstractGitCommand implements BlameCommand
|
||||
* @param request
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
@Override
|
||||
public BlameResult getBlameResult(BlameCommandRequest request)
|
||||
throws IOException, RepositoryException
|
||||
{
|
||||
AssertUtil.assertIsNotEmpty(request.getPath());
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("try to create blame for {}", request);
|
||||
}
|
||||
|
||||
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.getPath()),
|
||||
"path is empty or null");
|
||||
|
||||
org.eclipse.jgit.blame.BlameResult gitBlameResult = null;
|
||||
sonia.scm.repository.BlameResult blameResult = null;
|
||||
org.eclipse.jgit.lib.Repository gr = null;
|
||||
Git git = null;
|
||||
|
||||
try
|
||||
{
|
||||
gr = open();
|
||||
git = new Git(gr);
|
||||
|
||||
org.eclipse.jgit.api.BlameCommand blame = git.blame();
|
||||
org.eclipse.jgit.api.BlameCommand blame = new Git(gr).blame();
|
||||
|
||||
blame.setFilePath(request.getPath());
|
||||
|
||||
@@ -116,7 +127,13 @@ public class GitBlameCommand extends AbstractGitCommand implements BlameCommand
|
||||
|
||||
blame.setStartCommit(revId);
|
||||
gitBlameResult = blame.call();
|
||||
AssertUtil.assertIsNotNull(gitBlameResult);
|
||||
|
||||
if (gitBlameResult == null)
|
||||
{
|
||||
throw new RepositoryException(
|
||||
"could not create blame result for path ".concat(
|
||||
request.getPath()));
|
||||
}
|
||||
|
||||
List<BlameLine> blameLines = new ArrayList<BlameLine>();
|
||||
int total = gitBlameResult.getResultContents().size();
|
||||
@@ -153,9 +170,9 @@ public class GitBlameCommand extends AbstractGitCommand implements BlameCommand
|
||||
|
||||
blameResult = new sonia.scm.repository.BlameResult(i, blameLines);
|
||||
}
|
||||
catch (IOException ex)
|
||||
finally
|
||||
{
|
||||
logger.error("could not open repository", ex);
|
||||
GitUtil.close(gr);
|
||||
}
|
||||
|
||||
return blameResult;
|
||||
|
||||
@@ -111,8 +111,11 @@ public class GitBrowseCommand extends AbstractGitCommand
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public BrowserResult getBrowserResult(BrowseCommandRequest request)
|
||||
public BrowserResult getBrowserResult(BrowseCommandRequest request) throws IOException, RepositoryException
|
||||
{
|
||||
if ( logger.isDebugEnabled() ){
|
||||
logger.debug("try to create browse result for {}", request);
|
||||
}
|
||||
BrowserResult result = null;
|
||||
org.eclipse.jgit.lib.Repository repo = null;
|
||||
|
||||
@@ -150,10 +153,6 @@ public class GitBrowseCommand extends AbstractGitCommand
|
||||
new ArrayList<FileObject>());
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.error("could not fetch browser result", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
GitUtil.close(repo);
|
||||
|
||||
@@ -92,10 +92,19 @@ public class GitCatCommand extends AbstractGitCommand implements CatCommand
|
||||
*
|
||||
* @param request
|
||||
* @param output
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
@Override
|
||||
public void getCatResult(CatCommandRequest request, OutputStream output)
|
||||
throws IOException, RepositoryException
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("try to read content for {}", request);
|
||||
}
|
||||
|
||||
org.eclipse.jgit.lib.Repository repo = null;
|
||||
|
||||
try
|
||||
@@ -106,11 +115,6 @@ public class GitCatCommand extends AbstractGitCommand implements CatCommand
|
||||
|
||||
getContent(repo, revId, request.getPath(), output);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//TODO throw
|
||||
logger.error("could not fetch content", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
GitUtil.close(repo);
|
||||
@@ -132,7 +136,7 @@ public class GitCatCommand extends AbstractGitCommand implements CatCommand
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
void getContent(org.eclipse.jgit.lib.Repository repo, ObjectId revId,
|
||||
String path, OutputStream output)
|
||||
String path, OutputStream output)
|
||||
throws IOException, RepositoryException
|
||||
{
|
||||
TreeWalk treeWalk = null;
|
||||
|
||||
@@ -155,6 +155,7 @@ public class GitDiffCommand extends AbstractGitCommand implements DiffCommand
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// TODO throw exception
|
||||
logger.error("could not create diff", ex);
|
||||
}
|
||||
finally
|
||||
|
||||
@@ -57,6 +57,7 @@ import sonia.scm.util.IOUtil;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -150,9 +151,12 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
|
||||
* @param request
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public ChangesetPagingResult getChangesets(LogCommandRequest request)
|
||||
throws IOException
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
@@ -242,11 +246,16 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
|
||||
}
|
||||
catch (NoHeadException ex)
|
||||
{
|
||||
logger.error("could not read changesets", ex);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.error("could not open repository", ex);
|
||||
if (logger.isTraceEnabled())
|
||||
{
|
||||
logger.trace("repository seems to be empty", ex);
|
||||
}
|
||||
else if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.warn("repository seems to be empty");
|
||||
}
|
||||
|
||||
changesets = new ChangesetPagingResult(0, new ArrayList<Changeset>());
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user