diff --git a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgBrowseCommand.java b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgBrowseCommand.java index 56ce86734c..df66c29a86 100644 --- a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgBrowseCommand.java +++ b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgBrowseCommand.java @@ -30,44 +30,40 @@ */ + package sonia.scm.repository.spi; //~--- non-JDK imports -------------------------------------------------------- +import com.google.common.base.Strings; + import sonia.scm.repository.BrowserResult; -import sonia.scm.repository.HgContext; -import sonia.scm.repository.HgPythonScript; -import sonia.scm.repository.HgRepositoryHandler; import sonia.scm.repository.Repository; import sonia.scm.repository.RepositoryException; +import sonia.scm.repository.spi.javahg.HgFileviewCommand; +import sonia.scm.web.HgUtil; //~--- JDK imports ------------------------------------------------------------ -import java.io.File; import java.io.IOException; -import java.util.Map; - /** * * @author Sebastian Sdorra */ -public class HgBrowseCommand extends AbstractHgCommand implements BrowseCommand +public class HgBrowseCommand extends AbstractCommand implements BrowseCommand { /** * Constructs ... * * - * @param handler * @param context * @param repository - * @param repositoryDirectory */ - public HgBrowseCommand(HgRepositoryHandler handler, HgContext context, - Repository repository, File repositoryDirectory) + public HgBrowseCommand(HgCommandContext context, Repository repository) { - super(handler, context, repository, repositoryDirectory); + super(context, repository); } //~--- get methods ---------------------------------------------------------- @@ -76,9 +72,6 @@ public class HgBrowseCommand extends AbstractHgCommand implements BrowseCommand * Method description * * - * @param revision - * @param path - * * @param request * * @return @@ -88,11 +81,24 @@ public class HgBrowseCommand extends AbstractHgCommand implements BrowseCommand */ @Override public BrowserResult getBrowserResult(BrowseCommandRequest request) - throws IOException, RepositoryException + throws IOException, RepositoryException { - Map env = createEnvironment(request); + HgFileviewCommand cmd = HgFileviewCommand.on(open()); - return getResultFromScript(BrowserResult.class, HgPythonScript.FILELOG, - env); + if (!Strings.isNullOrEmpty(request.getRevision())) + { + cmd.rev(HgUtil.getRevision(request.getRevision())); + } + + if (!Strings.isNullOrEmpty(request.getPath())) + { + cmd.path(request.getPath()); + } + + BrowserResult result = new BrowserResult(); + + result.setFiles(cmd.execute()); + + return result; } } diff --git a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgRepositoryServiceProvider.java b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgRepositoryServiceProvider.java index 0746801340..3cc88e6c00 100644 --- a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgRepositoryServiceProvider.java +++ b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgRepositoryServiceProvider.java @@ -136,8 +136,7 @@ public class HgRepositoryServiceProvider extends RepositoryServiceProvider @Override public HgBrowseCommand getBrowseCommand() { - return new HgBrowseCommand(handler, hgContextProvider.get(), repository, - repositoryDirectory); + return new HgBrowseCommand(context, repository); } /** diff --git a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/javahg/HgFileviewCommand.java b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/javahg/HgFileviewCommand.java new file mode 100644 index 0000000000..d5925ffcde --- /dev/null +++ b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/javahg/HgFileviewCommand.java @@ -0,0 +1,264 @@ +/** + * 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.spi.javahg; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.aragost.javahg.DateTime; +import com.aragost.javahg.Repository; +import com.aragost.javahg.internals.AbstractCommand; +import com.aragost.javahg.internals.HgInputStream; + +import com.google.common.collect.Lists; + +import sonia.scm.repository.FileObject; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.IOException; + +import java.util.List; + +/** + * + * @author Sebastian Sdorra + */ +public class HgFileviewCommand extends AbstractCommand +{ + + /** + * Constructs ... + * + * + * @param repository + */ + public HgFileviewCommand(Repository repository) + { + super(repository); + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param repository + * + * @return + */ + public static HgFileviewCommand on(Repository repository) + { + return new HgFileviewCommand(repository); + } + + /** + * Method description + * + * + * @return + * + * @throws IOException + */ + public List execute() throws IOException + { + cmdAppend("-t"); + + List files = Lists.newArrayList(); + + HgInputStream stream = launchStream(); + + while (stream.peek() != -1) + { + FileObject file = null; + char type = (char) stream.read(); + + if (type == 'd') + { + file = readDirectory(stream); + } + else if (type == 'f') + { + file = readFile(stream); + } + else if (type == 's') {} + + if (file != null) + { + files.add(file); + } + } + + return files; + } + + /** + * Method description + * + * + * @param path + * + * @return + */ + public HgFileviewCommand path(String path) + { + cmdAppend("-p", path); + + return this; + } + + /** + * Method description + * + * + * @param revision + * + * @return + */ + public HgFileviewCommand rev(String revision) + { + cmdAppend("-r", rev); + + return this; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public String getCommandName() + { + return HgFileviewExtension.NAME; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param stream + * + * @return + * + * @throws IOException + */ + private FileObject readDirectory(HgInputStream stream) throws IOException + { + FileObject directory = new FileObject(); + String path = removeTrailingSlash(stream.textUpTo('\0')); + + directory.setName(getNameFromPath(path)); + directory.setDirectory(true); + directory.setPath(path); + + return directory; + } + + /** + * Method description + * + * + * @param stream + * + * @return + * + * @throws IOException + */ + private FileObject readFile(HgInputStream stream) throws IOException + { + FileObject file = new FileObject(); + String path = removeTrailingSlash(stream.textUpTo('\n')); + + file.setName(getNameFromPath(path)); + file.setPath(path); + file.setDirectory(false); + file.setLength(stream.decimalIntUpTo(' ')); + + DateTime timestamp = stream.dateTimeUpTo(' '); + + file.setLastModified(timestamp.getDate().getTime()); + file.setDescription(stream.textUpTo('\0')); + + return file; + } + + /** + * Method description + * + * + * @param path + * + * @return + */ + private String removeTrailingSlash(String path) + { + if (path.endsWith("/")) + { + path = path.substring(0, path.length() - 1); + } + + return path; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param path + * + * @return + */ + private String getNameFromPath(String path) + { + int index = path.lastIndexOf("/"); + + if (index > 0) + { + path = path.substring(index + 1); + } + + return path; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private String rev; +} diff --git a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/javahg/HgFileviewExtension.java b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/javahg/HgFileviewExtension.java index ab51aaea25..1a01efeae8 100644 --- a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/javahg/HgFileviewExtension.java +++ b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/javahg/HgFileviewExtension.java @@ -44,7 +44,7 @@ public class HgFileviewExtension extends MercurialExtension { /** Field description */ - private static final String NAME = "fileview"; + static final String NAME = "fileview"; /** Field description */ private static final String PATH = diff --git a/scm-plugins/scm-hg-plugin/src/main/resources/sonia/scm/hg/ext/fileview.py b/scm-plugins/scm-hg-plugin/src/main/resources/sonia/scm/hg/ext/fileview.py index f69ea7b1ba..caec46c70b 100644 --- a/scm-plugins/scm-hg-plugin/src/main/resources/sonia/scm/hg/ext/fileview.py +++ b/scm-plugins/scm-hg-plugin/src/main/resources/sonia/scm/hg/ext/fileview.py @@ -102,7 +102,7 @@ def createSubRepositoryMap(revCtx): def printSubRepository(ui, path, subrepository, transport): format = '%s %s %s\n' if transport: - format = 'd%s\n%s %s\0' + format = 's%s\n%s %s\0' ui.write( format % (appendTrailingSlash(path), subrepository.revision, subrepository.url)) def printDirectory(ui, path, transport): diff --git a/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/HgBrowseCommandTest.java b/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/HgBrowseCommandTest.java index e637e0c12a..0bfda68d64 100644 --- a/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/HgBrowseCommandTest.java +++ b/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/HgBrowseCommandTest.java @@ -30,6 +30,7 @@ */ + package sonia.scm.repository.spi; //~--- non-JDK imports -------------------------------------------------------- @@ -38,7 +39,6 @@ import org.junit.Test; import sonia.scm.repository.BrowserResult; import sonia.scm.repository.FileObject; -import sonia.scm.repository.HgContext; import sonia.scm.repository.RepositoryException; import static org.junit.Assert.*; @@ -66,10 +66,9 @@ public class HgBrowseCommandTest extends AbstractHgCommandTestBase @Test public void testBrowse() throws IOException, RepositoryException { - BrowserResult result = new HgBrowseCommand( - handler, new HgContext(), repository, - repositoryDirectory).getBrowserResult( - new BrowseCommandRequest()); + BrowserResult result = + new HgBrowseCommand(cmdContext, + repository).getBrowserResult(new BrowseCommandRequest()); assertNotNull(result); @@ -121,9 +120,8 @@ public class HgBrowseCommandTest extends AbstractHgCommandTestBase request.setPath("c"); - BrowserResult result = new HgBrowseCommand(handler, new HgContext(), - repository, - repositoryDirectory).getBrowserResult(request); + BrowserResult result = new HgBrowseCommand(cmdContext, + repository).getBrowserResult(request); assertNotNull(result);