mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 23:45:44 +01:00
implement browse command with javahg
This commit is contained in:
@@ -30,44 +30,40 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
package sonia.scm.repository.spi;
|
package sonia.scm.repository.spi;
|
||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
|
import com.google.common.base.Strings;
|
||||||
|
|
||||||
import sonia.scm.repository.BrowserResult;
|
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.Repository;
|
||||||
import sonia.scm.repository.RepositoryException;
|
import sonia.scm.repository.RepositoryException;
|
||||||
|
import sonia.scm.repository.spi.javahg.HgFileviewCommand;
|
||||||
|
import sonia.scm.web.HgUtil;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
*/
|
*/
|
||||||
public class HgBrowseCommand extends AbstractHgCommand implements BrowseCommand
|
public class HgBrowseCommand extends AbstractCommand implements BrowseCommand
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs ...
|
* Constructs ...
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param handler
|
|
||||||
* @param context
|
* @param context
|
||||||
* @param repository
|
* @param repository
|
||||||
* @param repositoryDirectory
|
|
||||||
*/
|
*/
|
||||||
public HgBrowseCommand(HgRepositoryHandler handler, HgContext context,
|
public HgBrowseCommand(HgCommandContext context, Repository repository)
|
||||||
Repository repository, File repositoryDirectory)
|
|
||||||
{
|
{
|
||||||
super(handler, context, repository, repositoryDirectory);
|
super(context, repository);
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
@@ -76,9 +72,6 @@ public class HgBrowseCommand extends AbstractHgCommand implements BrowseCommand
|
|||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param revision
|
|
||||||
* @param path
|
|
||||||
*
|
|
||||||
* @param request
|
* @param request
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
@@ -88,11 +81,24 @@ public class HgBrowseCommand extends AbstractHgCommand implements BrowseCommand
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public BrowserResult getBrowserResult(BrowseCommandRequest request)
|
public BrowserResult getBrowserResult(BrowseCommandRequest request)
|
||||||
throws IOException, RepositoryException
|
throws IOException, RepositoryException
|
||||||
{
|
{
|
||||||
Map<String, String> env = createEnvironment(request);
|
HgFileviewCommand cmd = HgFileviewCommand.on(open());
|
||||||
|
|
||||||
return getResultFromScript(BrowserResult.class, HgPythonScript.FILELOG,
|
if (!Strings.isNullOrEmpty(request.getRevision()))
|
||||||
env);
|
{
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -136,8 +136,7 @@ public class HgRepositoryServiceProvider extends RepositoryServiceProvider
|
|||||||
@Override
|
@Override
|
||||||
public HgBrowseCommand getBrowseCommand()
|
public HgBrowseCommand getBrowseCommand()
|
||||||
{
|
{
|
||||||
return new HgBrowseCommand(handler, hgContextProvider.get(), repository,
|
return new HgBrowseCommand(context, repository);
|
||||||
repositoryDirectory);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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<FileObject> execute() throws IOException
|
||||||
|
{
|
||||||
|
cmdAppend("-t");
|
||||||
|
|
||||||
|
List<FileObject> 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;
|
||||||
|
}
|
||||||
@@ -44,7 +44,7 @@ public class HgFileviewExtension extends MercurialExtension
|
|||||||
{
|
{
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private static final String NAME = "fileview";
|
static final String NAME = "fileview";
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private static final String PATH =
|
private static final String PATH =
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ def createSubRepositoryMap(revCtx):
|
|||||||
def printSubRepository(ui, path, subrepository, transport):
|
def printSubRepository(ui, path, subrepository, transport):
|
||||||
format = '%s %s %s\n'
|
format = '%s %s %s\n'
|
||||||
if transport:
|
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))
|
ui.write( format % (appendTrailingSlash(path), subrepository.revision, subrepository.url))
|
||||||
|
|
||||||
def printDirectory(ui, path, transport):
|
def printDirectory(ui, path, transport):
|
||||||
|
|||||||
@@ -30,6 +30,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
package sonia.scm.repository.spi;
|
package sonia.scm.repository.spi;
|
||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
@@ -38,7 +39,6 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import sonia.scm.repository.BrowserResult;
|
import sonia.scm.repository.BrowserResult;
|
||||||
import sonia.scm.repository.FileObject;
|
import sonia.scm.repository.FileObject;
|
||||||
import sonia.scm.repository.HgContext;
|
|
||||||
import sonia.scm.repository.RepositoryException;
|
import sonia.scm.repository.RepositoryException;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
@@ -66,10 +66,9 @@ public class HgBrowseCommandTest extends AbstractHgCommandTestBase
|
|||||||
@Test
|
@Test
|
||||||
public void testBrowse() throws IOException, RepositoryException
|
public void testBrowse() throws IOException, RepositoryException
|
||||||
{
|
{
|
||||||
BrowserResult result = new HgBrowseCommand(
|
BrowserResult result =
|
||||||
handler, new HgContext(), repository,
|
new HgBrowseCommand(cmdContext,
|
||||||
repositoryDirectory).getBrowserResult(
|
repository).getBrowserResult(new BrowseCommandRequest());
|
||||||
new BrowseCommandRequest());
|
|
||||||
|
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
|
|
||||||
@@ -121,9 +120,8 @@ public class HgBrowseCommandTest extends AbstractHgCommandTestBase
|
|||||||
|
|
||||||
request.setPath("c");
|
request.setPath("c");
|
||||||
|
|
||||||
BrowserResult result = new HgBrowseCommand(handler, new HgContext(),
|
BrowserResult result = new HgBrowseCommand(cmdContext,
|
||||||
repository,
|
repository).getBrowserResult(request);
|
||||||
repositoryDirectory).getBrowserResult(request);
|
|
||||||
|
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user