From e2eabd7d3ca7f43059fe8718a80fb14006e15b9b Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Mon, 4 Apr 2011 08:07:27 +0200 Subject: [PATCH] added SvnRepositoryViewer --- .../scm/repository/SvnChangesetViewer.java | 159 ++++++++++++++++++ .../scm/repository/SvnRepositoryHandler.java | 32 ++++ 2 files changed, 191 insertions(+) create mode 100644 plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnChangesetViewer.java diff --git a/plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnChangesetViewer.java b/plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnChangesetViewer.java new file mode 100644 index 0000000000..d5cda52af0 --- /dev/null +++ b/plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnChangesetViewer.java @@ -0,0 +1,159 @@ +/** + * 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.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.tmatesoft.svn.core.SVNException; +import org.tmatesoft.svn.core.SVNLogEntry; +import org.tmatesoft.svn.core.SVNURL; +import org.tmatesoft.svn.core.io.SVNRepository; +import org.tmatesoft.svn.core.io.SVNRepositoryFactory; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.File; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * + * @author Sebastian Sdorra + */ +public class SvnChangesetViewer implements ChangesetViewer +{ + + /** the logger for SvnChangesetViewer */ + private static final Logger logger = + LoggerFactory.getLogger(SvnChangesetViewer.class); + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + * + * @param handler + * @param repostory + */ + public SvnChangesetViewer(SvnRepositoryHandler handler, Repository repostory) + { + this.handler = handler; + this.repostory = repostory; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param start + * @param max + * + * @return + */ + @Override + public ChangesetPagingResult getChangesets(int start, int max) + { + ChangesetPagingResult changesets = null; + File directory = handler.getDirectory(repostory); + SVNRepository repository = null; + + try + { + repository = SVNRepositoryFactory.create(SVNURL.fromFile(directory)); + + long total = repository.getLatestRevision(); + long startRev = total - start; + long endRev = total - start - (max - 1); + + if (endRev < 0) + { + endRev = 0; + } + + List changesetList = new ArrayList(); + Collection entries = repository.log(new String[] { "" }, + null, startRev, endRev, true, true); + + for (SVNLogEntry entry : entries) + { + changesetList.add(createChangeset(entry)); + } + + changesets = new ChangesetPagingResult((int) total, changesetList); + } + catch (SVNException ex) + { + logger.error("could not open repository", ex); + } + finally + { + repository.closeSession(); + } + + return changesets; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param entry + * + * @return + */ + private Changeset createChangeset(SVNLogEntry entry) + { + return new Changeset(String.valueOf(entry.getRevision()), + entry.getDate().getTime(), entry.getAuthor(), + entry.getMessage()); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private SvnRepositoryHandler handler; + + /** Field description */ + private Repository repostory; +} diff --git a/plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnRepositoryHandler.java b/plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnRepositoryHandler.java index 4198310eec..9c8083d627 100644 --- a/plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnRepositoryHandler.java +++ b/plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnRepositoryHandler.java @@ -45,6 +45,7 @@ import sonia.scm.Type; import sonia.scm.io.FileSystem; import sonia.scm.plugin.ext.Extension; import sonia.scm.store.StoreFactory; +import sonia.scm.util.AssertUtil; //~--- JDK imports ------------------------------------------------------------ @@ -87,6 +88,37 @@ public class SvnRepositoryHandler //~--- get methods ---------------------------------------------------------- + /** + * Method description + * + * + * @param repository + * + * @return + */ + @Override + public ChangesetViewer getChangesetViewer(Repository repository) + { + SvnChangesetViewer changesetViewer = null; + + AssertUtil.assertIsNotNull(repository); + + String type = repository.getType(); + + AssertUtil.assertIsNotEmpty(type); + + if (TYPE_NAME.equals(type)) + { + changesetViewer = new SvnChangesetViewer(this, repository); + } + else + { + throw new IllegalArgumentException("mercurial repository is required"); + } + + return changesetViewer; + } + /** * Method description *