mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-06 05:25:44 +01:00
remove deprecated stuff
This commit is contained in:
@@ -1,178 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 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,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* 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.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.eclipse.jgit.api.BlameCommand;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.blame.BlameResult;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.PersonIdent;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.util.AssertUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Class description
|
||||
*
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitBlameViewer implements BlameViewer
|
||||
{
|
||||
|
||||
/** the logger for GitChangesetViewer */
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(GitChangesetViewer.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param handler
|
||||
* @param repository
|
||||
*/
|
||||
public GitBlameViewer(GitRepositoryHandler handler, Repository repository)
|
||||
{
|
||||
this.handler = handler;
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param revision
|
||||
* @param path
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public sonia.scm.repository.BlameResult getBlame(String revision, String path)
|
||||
{
|
||||
AssertUtil.assertIsNotEmpty(path);
|
||||
|
||||
BlameResult gitBlameResult = null;
|
||||
sonia.scm.repository.BlameResult blameResult = null;
|
||||
org.eclipse.jgit.lib.Repository gr = null;
|
||||
File directory = handler.getDirectory(repository);
|
||||
Git git = null;
|
||||
|
||||
try
|
||||
{
|
||||
gr = GitUtil.open(directory);
|
||||
git = new Git(gr);
|
||||
|
||||
BlameCommand blame = git.blame();
|
||||
|
||||
blame.setFilePath(path);
|
||||
|
||||
ObjectId revId = GitUtil.getRevisionId(gr, revision);
|
||||
|
||||
blame.setStartCommit(revId);
|
||||
gitBlameResult = blame.call();
|
||||
AssertUtil.assertIsNotNull(gitBlameResult);
|
||||
|
||||
List<BlameLine> blameLines = new ArrayList<BlameLine>();
|
||||
int total = gitBlameResult.getResultContents().size();
|
||||
int i = 0;
|
||||
|
||||
for (; i < total; i++)
|
||||
{
|
||||
RevCommit commit = gitBlameResult.getSourceCommit(i);
|
||||
|
||||
if (commit != null)
|
||||
{
|
||||
PersonIdent author = gitBlameResult.getSourceAuthor(i);
|
||||
BlameLine blameLine = new BlameLine();
|
||||
|
||||
blameLine.setLineNumber(i + 1);
|
||||
blameLine.setAuthor(new Person(author.getName(),
|
||||
author.getEmailAddress()));
|
||||
blameLine.setDescription(commit.getShortMessage());
|
||||
|
||||
long when = GitUtil.getCommitTime(commit);
|
||||
|
||||
blameLine.setWhen(when);
|
||||
|
||||
String rev = commit.getId().getName();
|
||||
|
||||
blameLine.setRevision(rev);
|
||||
|
||||
String content = gitBlameResult.getResultContents().getString(i);
|
||||
|
||||
blameLine.setCode(content);
|
||||
blameLines.add(blameLine);
|
||||
}
|
||||
}
|
||||
|
||||
blameResult = new sonia.scm.repository.BlameResult(i, blameLines);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.error("could not open repository", ex);
|
||||
}
|
||||
catch (GitAPIException ex)
|
||||
{
|
||||
logger.error("could not create blame view", ex);
|
||||
}
|
||||
|
||||
return blameResult;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private GitRepositoryHandler handler;
|
||||
|
||||
/** Field description */
|
||||
private Repository repository;
|
||||
}
|
||||
@@ -1,311 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 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,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* 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.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.api.errors.NoHeadException;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitChangesetViewer implements ChangesetViewer
|
||||
{
|
||||
|
||||
/** the logger for GitChangesetViewer */
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(GitChangesetViewer.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param handler
|
||||
* @param repository
|
||||
*/
|
||||
public GitChangesetViewer(GitRepositoryHandler handler, Repository repository)
|
||||
{
|
||||
this.handler = handler;
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param revision
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Changeset getChangeset(String revision)
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("fetch changeset {}", revision);
|
||||
}
|
||||
|
||||
Changeset changeset = null;
|
||||
File directory = handler.getDirectory(repository);
|
||||
org.eclipse.jgit.lib.Repository gr = null;
|
||||
GitChangesetConverter converter = null;
|
||||
|
||||
try
|
||||
{
|
||||
gr = GitUtil.open(directory);
|
||||
|
||||
if (!gr.getAllRefs().isEmpty())
|
||||
{
|
||||
RevWalk revWalk = new RevWalk(gr);
|
||||
ObjectId id = GitUtil.getRevisionId(gr, revision);
|
||||
RevCommit commit = revWalk.parseCommit(id);
|
||||
|
||||
if (commit != null)
|
||||
{
|
||||
converter = new GitChangesetConverter(gr, revWalk);
|
||||
changeset = converter.createChangeset(commit);
|
||||
}
|
||||
else if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.warn("could not find revision {}", revision);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.error("could not open repository", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close(converter);
|
||||
GitUtil.close(gr);
|
||||
}
|
||||
|
||||
return changeset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param start
|
||||
* @param max
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public ChangesetPagingResult getChangesets(int start, int max)
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("fetch changesets. start: {}, max: {}", start, max);
|
||||
}
|
||||
|
||||
ChangesetPagingResult changesets = null;
|
||||
File directory = handler.getDirectory(repository);
|
||||
org.eclipse.jgit.lib.Repository gr = null;
|
||||
GitChangesetConverter converter = null;
|
||||
|
||||
try
|
||||
{
|
||||
gr = GitUtil.open(directory);
|
||||
|
||||
int counter = 0;
|
||||
List<Changeset> changesetList = new ArrayList<Changeset>();
|
||||
|
||||
if (!gr.getAllRefs().isEmpty())
|
||||
{
|
||||
converter = new GitChangesetConverter(gr);
|
||||
|
||||
Git git = new Git(gr);
|
||||
ObjectId headId = GitUtil.getRepositoryHead(gr);
|
||||
|
||||
if (headId != null)
|
||||
{
|
||||
for (RevCommit commit : git.log().add(headId).call())
|
||||
{
|
||||
if ((counter >= start) && (counter < start + max))
|
||||
{
|
||||
changesetList.add(converter.createChangeset(commit));
|
||||
}
|
||||
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
else if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.warn("could not find repository head of repository {}",
|
||||
repository.getName());
|
||||
}
|
||||
}
|
||||
|
||||
changesets = new ChangesetPagingResult(counter, changesetList);
|
||||
}
|
||||
catch (NoHeadException ex)
|
||||
{
|
||||
logger.error("could not read changesets", ex);
|
||||
}
|
||||
catch (GitAPIException ex)
|
||||
{
|
||||
logger.error("could not read changesets", ex);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.error("could not open repository", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close(converter);
|
||||
GitUtil.close(gr);
|
||||
}
|
||||
|
||||
return changesets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param path
|
||||
* @param revision
|
||||
* @param start
|
||||
* @param max
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public ChangesetPagingResult getChangesets(String path, String revision,
|
||||
int start, int max)
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug(
|
||||
"fetch changesets for path {} and revision {}. start: {}, max: {}",
|
||||
new Object[] { path,
|
||||
revision, start, max });
|
||||
}
|
||||
|
||||
ChangesetPagingResult changesets = null;
|
||||
File directory = handler.getDirectory(repository);
|
||||
org.eclipse.jgit.lib.Repository gr = null;
|
||||
GitChangesetConverter converter = null;
|
||||
|
||||
try
|
||||
{
|
||||
gr = GitUtil.open(directory);
|
||||
|
||||
int counter = 0;
|
||||
List<Changeset> changesetList = new ArrayList<Changeset>();
|
||||
|
||||
if (!gr.getAllRefs().isEmpty())
|
||||
{
|
||||
converter = new GitChangesetConverter(gr);
|
||||
|
||||
Git git = new Git(gr);
|
||||
ObjectId revisionId = GitUtil.getRevisionId(gr, revision);
|
||||
|
||||
if (revisionId != null)
|
||||
{
|
||||
for (RevCommit commit :
|
||||
git.log().add(revisionId).addPath(path).call())
|
||||
{
|
||||
if ((counter >= start) && (counter < start + max))
|
||||
{
|
||||
changesetList.add(converter.createChangeset(commit));
|
||||
}
|
||||
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
else if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.warn("could not find repository head of repository {}",
|
||||
repository.getName());
|
||||
}
|
||||
}
|
||||
|
||||
changesets = new ChangesetPagingResult(counter, changesetList);
|
||||
}
|
||||
catch (NoHeadException ex)
|
||||
{
|
||||
logger.error("could not read changesets", ex);
|
||||
}
|
||||
catch (GitAPIException ex)
|
||||
{
|
||||
logger.error("could not read changesets", ex);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.error("could not open repository", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close(converter);
|
||||
GitUtil.close(gr);
|
||||
}
|
||||
|
||||
return changesets;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private GitRepositoryHandler handler;
|
||||
|
||||
/** Field description */
|
||||
private Repository repository;
|
||||
}
|
||||
@@ -1,174 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 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,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* 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.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.eclipse.jgit.diff.DiffEntry;
|
||||
import org.eclipse.jgit.diff.DiffFormatter;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevTree;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.eclipse.jgit.treewalk.EmptyTreeIterator;
|
||||
import org.eclipse.jgit.treewalk.TreeWalk;
|
||||
import org.eclipse.jgit.treewalk.filter.PathFilter;
|
||||
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitDiffViewer implements DiffViewer
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param directory
|
||||
*/
|
||||
public GitDiffViewer(File directory)
|
||||
{
|
||||
this.directory = directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param handler
|
||||
* @param repository
|
||||
*/
|
||||
public GitDiffViewer(GitRepositoryHandler handler, Repository repository)
|
||||
{
|
||||
this(handler.getDirectory(repository));
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param revision
|
||||
* @param path
|
||||
* @param output
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
@Override
|
||||
public void getDiff(String revision, String path, OutputStream output)
|
||||
throws IOException, RepositoryException
|
||||
{
|
||||
org.eclipse.jgit.lib.Repository gr = GitUtil.open(directory);
|
||||
RevWalk walk = null;
|
||||
TreeWalk treeWalk = null;
|
||||
DiffFormatter formatter = null;
|
||||
|
||||
try
|
||||
{
|
||||
walk = new RevWalk(gr);
|
||||
|
||||
RevCommit commit = walk.parseCommit(gr.resolve(revision));
|
||||
|
||||
walk.markStart(commit);
|
||||
commit = walk.next();
|
||||
treeWalk = new TreeWalk(gr);
|
||||
treeWalk.reset();
|
||||
treeWalk.setRecursive(true);
|
||||
|
||||
if (Util.isNotEmpty(path))
|
||||
{
|
||||
treeWalk.setFilter(PathFilter.create(path));
|
||||
}
|
||||
|
||||
if (commit.getParentCount() > 0)
|
||||
{
|
||||
RevTree tree = commit.getParent(0).getTree();
|
||||
|
||||
if (tree != null)
|
||||
{
|
||||
treeWalk.addTree(tree);
|
||||
}
|
||||
else
|
||||
{
|
||||
treeWalk.addTree(new EmptyTreeIterator());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
treeWalk.addTree(new EmptyTreeIterator());
|
||||
}
|
||||
|
||||
treeWalk.addTree(commit.getTree());
|
||||
formatter = new DiffFormatter(new BufferedOutputStream(output));
|
||||
formatter.setRepository(gr);
|
||||
|
||||
List<DiffEntry> entries = DiffEntry.scan(treeWalk);
|
||||
|
||||
for (DiffEntry e : entries)
|
||||
{
|
||||
if (!e.getOldId().equals(e.getNewId()))
|
||||
{
|
||||
formatter.format(e);
|
||||
}
|
||||
}
|
||||
|
||||
formatter.flush();
|
||||
}
|
||||
finally
|
||||
{
|
||||
GitUtil.release(walk);
|
||||
GitUtil.release(treeWalk);
|
||||
GitUtil.release(formatter);
|
||||
GitUtil.close(gr);
|
||||
}
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private File directory;
|
||||
}
|
||||
@@ -1,581 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 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,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* 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.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.eclipse.jgit.errors.MissingObjectException;
|
||||
import org.eclipse.jgit.lib.Constants;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.ObjectLoader;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevTree;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.eclipse.jgit.treewalk.TreeWalk;
|
||||
import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
|
||||
import org.eclipse.jgit.treewalk.filter.PathFilter;
|
||||
import org.eclipse.jgit.treewalk.filter.TreeFilter;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitRepositoryBrowser implements RepositoryBrowser
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String PATH_MODULES = ".gitmodules";
|
||||
|
||||
/** the logger for GitRepositoryBrowser */
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(GitRepositoryBrowser.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param handler
|
||||
* @param repository
|
||||
*/
|
||||
public GitRepositoryBrowser(GitRepositoryHandler handler,
|
||||
Repository repository)
|
||||
{
|
||||
this.handler = handler;
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param revision
|
||||
* @param path
|
||||
* @param output
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
@Override
|
||||
public void getContent(String revision, String path, OutputStream output)
|
||||
throws IOException, RepositoryException
|
||||
{
|
||||
File directory = handler.getDirectory(repository);
|
||||
org.eclipse.jgit.lib.Repository repo = GitUtil.open(directory);
|
||||
|
||||
try
|
||||
{
|
||||
ObjectId revId = GitUtil.getRevisionId(repo, revision);
|
||||
|
||||
getContent(repo, revId, path, output);
|
||||
}
|
||||
finally
|
||||
{
|
||||
GitUtil.close(repo);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param repo
|
||||
* @param revId
|
||||
* @param path
|
||||
* @param output
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public void getContent(org.eclipse.jgit.lib.Repository repo, ObjectId revId,
|
||||
String path, OutputStream output)
|
||||
throws IOException, RepositoryException
|
||||
{
|
||||
TreeWalk treeWalk = null;
|
||||
RevWalk revWalk = null;
|
||||
|
||||
try
|
||||
{
|
||||
treeWalk = new TreeWalk(repo);
|
||||
treeWalk.setRecursive(Util.nonNull(path).contains("/"));
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("load content for {} at {}", path, revId.name());
|
||||
}
|
||||
|
||||
revWalk = new RevWalk(repo);
|
||||
|
||||
RevCommit entry = revWalk.parseCommit(revId);
|
||||
RevTree revTree = entry.getTree();
|
||||
|
||||
if (revTree != null)
|
||||
{
|
||||
treeWalk.addTree(revTree);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.error("could not find tree for {}", revId.name());
|
||||
}
|
||||
|
||||
treeWalk.setFilter(PathFilter.create(path));
|
||||
|
||||
if (treeWalk.next())
|
||||
{
|
||||
|
||||
// Path exists
|
||||
if (treeWalk.getFileMode(0).getObjectType() == Constants.OBJ_BLOB)
|
||||
{
|
||||
ObjectId blobId = treeWalk.getObjectId(0);
|
||||
ObjectLoader loader = repo.open(blobId);
|
||||
|
||||
loader.copyTo(output);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// Not a blob, its something else (tree, gitlink)
|
||||
throw new PathNotFoundException(path);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PathNotFoundException(path);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
GitUtil.release(revWalk);
|
||||
GitUtil.release(treeWalk);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param revision
|
||||
* @param path
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
@Override
|
||||
public BrowserResult getResult(String revision, String path)
|
||||
throws IOException, RepositoryException
|
||||
{
|
||||
BrowserResult result = null;
|
||||
File directory = handler.getDirectory(repository);
|
||||
org.eclipse.jgit.lib.Repository repo = GitUtil.open(directory);
|
||||
|
||||
try
|
||||
{
|
||||
ObjectId revId = null;
|
||||
|
||||
if (Util.isEmpty(revision))
|
||||
{
|
||||
revId = GitUtil.getRepositoryHead(repo);
|
||||
}
|
||||
else
|
||||
{
|
||||
revId = GitUtil.getRevisionId(repo, revision);
|
||||
}
|
||||
|
||||
if (revId != null)
|
||||
{
|
||||
result = getResult(repo, revId, path);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Util.isNotEmpty(revision))
|
||||
{
|
||||
logger.error("could not find revision {}", revision);
|
||||
}
|
||||
else if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.warn("coul not find head of repository, empty?");
|
||||
}
|
||||
|
||||
result = new BrowserResult(Constants.HEAD, null, null,
|
||||
new ArrayList<FileObject>());
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
GitUtil.close(repo);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param files
|
||||
* @param repo
|
||||
* @param revId
|
||||
* @param path
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
private void appendSubModules(List<FileObject> files,
|
||||
org.eclipse.jgit.lib.Repository repo,
|
||||
ObjectId revId, String path)
|
||||
throws IOException, RepositoryException
|
||||
{
|
||||
path = Util.nonNull(path);
|
||||
|
||||
Map<String, SubRepository> subRepositories = getSubRepositories(repo,
|
||||
revId);
|
||||
|
||||
if (subRepositories != null)
|
||||
{
|
||||
for (Entry<String, SubRepository> e : subRepositories.entrySet())
|
||||
{
|
||||
String p = e.getKey();
|
||||
|
||||
if (p.startsWith(path))
|
||||
{
|
||||
p = p.substring(path.length());
|
||||
|
||||
if (p.startsWith("/"))
|
||||
{
|
||||
p = p.substring(1);
|
||||
}
|
||||
|
||||
if (p.endsWith("/"))
|
||||
{
|
||||
p = p.substring(0, p.length() - 1);
|
||||
}
|
||||
|
||||
if (!p.contains("/"))
|
||||
{
|
||||
FileObject fo = new FileObject();
|
||||
|
||||
fo.setDirectory(true);
|
||||
fo.setPath(path);
|
||||
fo.setName(p);
|
||||
fo.setSubRepository(e.getValue());
|
||||
|
||||
files.add(fo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param repo
|
||||
* @param revId
|
||||
* @param treeWalk
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private FileObject createFileObject(org.eclipse.jgit.lib.Repository repo,
|
||||
ObjectId revId, TreeWalk treeWalk)
|
||||
throws IOException
|
||||
{
|
||||
FileObject file = null;
|
||||
|
||||
try
|
||||
{
|
||||
file = new FileObject();
|
||||
|
||||
String path = treeWalk.getPathString();
|
||||
|
||||
file.setName(treeWalk.getNameString());
|
||||
file.setPath(path);
|
||||
|
||||
ObjectLoader loader = repo.open(treeWalk.getObjectId(0));
|
||||
|
||||
file.setDirectory(loader.getType() == Constants.OBJ_TREE);
|
||||
file.setLength(loader.getSize());
|
||||
|
||||
// don't show message and date for directories to improve performance
|
||||
if (!file.isDirectory())
|
||||
{
|
||||
RevCommit commit = getLatestCommit(repo, revId, path);
|
||||
|
||||
if (commit != null)
|
||||
{
|
||||
file.setLastModified(GitUtil.getCommitTime(commit));
|
||||
file.setDescription(commit.getShortMessage());
|
||||
}
|
||||
else if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.warn("could not find latest commit for {} on {}", path, revId);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (MissingObjectException ex)
|
||||
{
|
||||
file = null;
|
||||
logger.error("could not fetch object for id {}", revId);
|
||||
|
||||
if (logger.isTraceEnabled())
|
||||
{
|
||||
logger.trace("could not fetch object", ex);
|
||||
}
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param repo
|
||||
* @param revId
|
||||
* @param path
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private RevCommit getLatestCommit(org.eclipse.jgit.lib.Repository repo,
|
||||
ObjectId revId, String path)
|
||||
{
|
||||
RevCommit result = null;
|
||||
RevWalk walk = null;
|
||||
|
||||
try
|
||||
{
|
||||
walk = new RevWalk(repo);
|
||||
walk.setTreeFilter(AndTreeFilter.create(PathFilter.create(path),
|
||||
TreeFilter.ANY_DIFF));
|
||||
|
||||
RevCommit commit = walk.parseCommit(revId);
|
||||
|
||||
walk.markStart(commit);
|
||||
result = Util.getFirst(walk);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.error("could not parse commit for file", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
GitUtil.release(walk);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repo
|
||||
* @param revId
|
||||
* @param path
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
private BrowserResult getResult(org.eclipse.jgit.lib.Repository repo,
|
||||
ObjectId revId, String path)
|
||||
throws IOException, RepositoryException
|
||||
{
|
||||
BrowserResult result = null;
|
||||
RevWalk revWalk = null;
|
||||
TreeWalk treeWalk = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("load repository browser for revision {}", revId.name());
|
||||
}
|
||||
|
||||
treeWalk = new TreeWalk(repo);
|
||||
revWalk = new RevWalk(repo);
|
||||
|
||||
RevTree tree = revWalk.parseTree(revId);
|
||||
|
||||
if (tree != null)
|
||||
{
|
||||
treeWalk.addTree(tree);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.error("could not find tree for {}", revId.name());
|
||||
}
|
||||
|
||||
result = new BrowserResult();
|
||||
|
||||
List<FileObject> files = new ArrayList<FileObject>();
|
||||
|
||||
appendSubModules(files, repo, revId, path);
|
||||
|
||||
if (Util.isEmpty(path))
|
||||
{
|
||||
while (treeWalk.next())
|
||||
{
|
||||
FileObject fo = createFileObject(repo, revId, treeWalk);
|
||||
|
||||
if (fo != null)
|
||||
{
|
||||
files.add(fo);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
String[] parts = path.split("/");
|
||||
int current = 0;
|
||||
int limit = parts.length;
|
||||
|
||||
while (treeWalk.next())
|
||||
{
|
||||
String name = treeWalk.getNameString();
|
||||
|
||||
if (current >= limit)
|
||||
{
|
||||
String p = treeWalk.getPathString();
|
||||
|
||||
if (p.split("/").length > limit)
|
||||
{
|
||||
FileObject fo = createFileObject(repo, revId, treeWalk);
|
||||
|
||||
if (fo != null)
|
||||
{
|
||||
files.add(fo);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (name.equalsIgnoreCase(parts[current]))
|
||||
{
|
||||
current++;
|
||||
treeWalk.enterSubtree();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result.setFiles(files);
|
||||
result.setRevision(revId.getName());
|
||||
}
|
||||
finally
|
||||
{
|
||||
GitUtil.release(revWalk);
|
||||
GitUtil.release(treeWalk);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repo
|
||||
* @param revision
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
private Map<String, SubRepository> getSubRepositories(
|
||||
org.eclipse.jgit.lib.Repository repo, ObjectId revision)
|
||||
throws IOException, RepositoryException
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("read submodules of {} at {}", repository.getName(),
|
||||
revision);
|
||||
}
|
||||
|
||||
Map<String, SubRepository> subRepositories = null;
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
try
|
||||
{
|
||||
getContent(repo, revision, PATH_MODULES, baos);
|
||||
subRepositories = GitSubModuleParser.parse(baos.toString());
|
||||
}
|
||||
catch (PathNotFoundException ex)
|
||||
{
|
||||
logger.trace("could not find .gitmodules", ex);
|
||||
}
|
||||
|
||||
return subRepositories;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private GitRepositoryHandler handler;
|
||||
|
||||
/** Field description */
|
||||
private Repository repository;
|
||||
}
|
||||
@@ -97,99 +97,6 @@ public class GitRepositoryHandler
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public BlameViewer getBlameViewer(Repository repository)
|
||||
{
|
||||
GitBlameViewer blameViewer = null;
|
||||
|
||||
AssertUtil.assertIsNotNull(repository);
|
||||
|
||||
String type = repository.getType();
|
||||
|
||||
AssertUtil.assertIsNotEmpty(type);
|
||||
|
||||
if (TYPE_NAME.equals(type))
|
||||
{
|
||||
blameViewer = new GitBlameViewer(this, repository);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("git repository is required");
|
||||
}
|
||||
|
||||
return blameViewer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public ChangesetViewer getChangesetViewer(Repository repository)
|
||||
{
|
||||
GitChangesetViewer changesetViewer = null;
|
||||
|
||||
AssertUtil.assertIsNotNull(repository);
|
||||
|
||||
String type = repository.getType();
|
||||
|
||||
AssertUtil.assertIsNotEmpty(type);
|
||||
|
||||
if (TYPE_NAME.equals(type))
|
||||
{
|
||||
changesetViewer = new GitChangesetViewer(this, repository);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("git repository is required");
|
||||
}
|
||||
|
||||
return changesetViewer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public DiffViewer getDiffViewer(Repository repository)
|
||||
{
|
||||
GitDiffViewer diffViewer = null;
|
||||
|
||||
AssertUtil.assertIsNotNull(repository);
|
||||
|
||||
String type = repository.getType();
|
||||
|
||||
AssertUtil.assertIsNotEmpty(type);
|
||||
|
||||
if (TYPE_NAME.equals(type))
|
||||
{
|
||||
diffViewer = new GitDiffViewer(this, repository);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("git repository is required");
|
||||
}
|
||||
|
||||
return diffViewer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -202,22 +109,6 @@ public class GitRepositoryHandler
|
||||
return new GitImportHandler(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public RepositoryBrowser getRepositoryBrowser(Repository repository)
|
||||
{
|
||||
AssertUtil.assertIsNotNull(repository);
|
||||
|
||||
return new GitRepositoryBrowser(this, repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user