mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-08-31 03:26:50 +02:00
added new gitrepository browser view
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
/**
|
||||
* 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.web;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.NoHeadException;
|
||||
import org.eclipse.jgit.lib.PersonIdent;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.io.RegexResourceProcessor;
|
||||
import sonia.scm.io.ResourceProcessor;
|
||||
import sonia.scm.util.IOUtil;
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitRepositoryViewer
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String MIMETYPE_HTML = "text/html";
|
||||
|
||||
/** Field description */
|
||||
public static final String RESOURCE_GITINDEX = "/sonia/scm/git.index.html";
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param response
|
||||
* @param repository
|
||||
* @param repositoryName
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws NoHeadException
|
||||
* @throws ServletException
|
||||
*/
|
||||
public void handleRequest(HttpServletResponse response,
|
||||
Repository repository, String repositoryName)
|
||||
throws IOException, ServletException, NoHeadException
|
||||
{
|
||||
response.setContentType(MIMETYPE_HTML);
|
||||
|
||||
ResourceProcessor processor = new RegexResourceProcessor();
|
||||
|
||||
processor.addVariable("name", repositoryName);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
if (!repository.getAllRefs().isEmpty())
|
||||
{
|
||||
Git git = new Git(repository);
|
||||
|
||||
for (RevCommit commit : git.log().call())
|
||||
{
|
||||
appendCommit(sb, commit);
|
||||
}
|
||||
}
|
||||
|
||||
processor.addVariable("commits", sb.toString());
|
||||
|
||||
BufferedReader reader = null;
|
||||
PrintWriter writer = null;
|
||||
|
||||
try
|
||||
{
|
||||
reader = new BufferedReader(
|
||||
new InputStreamReader(
|
||||
GitRepositoryViewer.class.getResourceAsStream(
|
||||
RESOURCE_GITINDEX)));
|
||||
writer = response.getWriter();
|
||||
processor.process(reader, writer);
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close(reader);
|
||||
IOUtil.close(writer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param sb
|
||||
* @param commit
|
||||
*/
|
||||
private void appendCommit(StringBuilder sb, RevCommit commit)
|
||||
{
|
||||
sb.append("<tr><td class=\"small\">");
|
||||
|
||||
long time = commit.getCommitTime();
|
||||
|
||||
sb.append(Util.formatDate(new Date(time * 1000)));
|
||||
sb.append("</td><td class=\"small\">");
|
||||
|
||||
PersonIdent person = commit.getCommitterIdent();
|
||||
|
||||
if (person != null)
|
||||
{
|
||||
String name = person.getName();
|
||||
|
||||
if (Util.isNotEmpty(name))
|
||||
{
|
||||
sb.append(name);
|
||||
}
|
||||
}
|
||||
|
||||
sb.append("</td><td>").append(commit.getFullMessage());
|
||||
sb.append("</td></tr>");
|
||||
}
|
||||
}
|
||||
@@ -39,15 +39,17 @@ import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import org.eclipse.jgit.http.server.GitServlet;
|
||||
import org.eclipse.jgit.http.server.resolver.RepositoryResolver;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
|
||||
import sonia.scm.repository.GitRepositoryHandler;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -61,18 +63,16 @@ import javax.servlet.http.HttpServletResponse;
|
||||
public class ScmGitServlet extends GitServlet
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String MIMETYPE_HTML = "text/html";
|
||||
|
||||
/** Field description */
|
||||
public static final String REGEX_GITHTTPBACKEND =
|
||||
"(?x)^/git/(.*/(HEAD|info/refs|objects/(info/[^/]+|[0-9a-f]{2}/[0-9a-f]{38}|pack/pack-[0-9a-f]{40}\\.(pack|idx))|git-(upload|receive)-pack))$";
|
||||
|
||||
/** Field description */
|
||||
public static final String RESOURCE_GITINDEX = "/sonia/scm/git.index.html";
|
||||
private static final long serialVersionUID = -7712897339207470674L;
|
||||
|
||||
/** Field description */
|
||||
private static final long serialVersionUID = -7712897339207470674L;
|
||||
private static final Pattern REGEX_REPOSITORYNAME =
|
||||
Pattern.compile("/git/([^/]+)/?.*");
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
@@ -85,7 +85,8 @@ public class ScmGitServlet extends GitServlet
|
||||
@Inject
|
||||
public ScmGitServlet(GitRepositoryHandler handler)
|
||||
{
|
||||
setRepositoryResolver(new GitRepositoryResolver(handler.getConfig()));
|
||||
resolver = new GitRepositoryResolver(handler.getConfig());
|
||||
setRepositoryResolver(resolver);
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
@@ -113,7 +114,7 @@ public class ScmGitServlet extends GitServlet
|
||||
}
|
||||
else
|
||||
{
|
||||
printGitInformation(response);
|
||||
printGitInformation(request, response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,29 +122,49 @@ public class ScmGitServlet extends GitServlet
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws ServletException
|
||||
*/
|
||||
private void printGitInformation(HttpServletResponse response)
|
||||
private void printGitInformation(HttpServletRequest request,
|
||||
HttpServletResponse response)
|
||||
throws ServletException, IOException
|
||||
{
|
||||
response.setContentType(MIMETYPE_HTML);
|
||||
|
||||
InputStream input = null;
|
||||
OutputStream output = null;
|
||||
String uri = getRelativePath(request);
|
||||
Matcher m = REGEX_REPOSITORYNAME.matcher(uri);
|
||||
String name = null;
|
||||
Repository repository = null;
|
||||
|
||||
try
|
||||
{
|
||||
input = ScmGitServlet.class.getResourceAsStream(RESOURCE_GITINDEX);
|
||||
output = response.getOutputStream();
|
||||
IOUtil.copy(input, output);
|
||||
if (m.matches())
|
||||
{
|
||||
name = m.group(1);
|
||||
repository = resolver.open(request, name);
|
||||
}
|
||||
|
||||
if (repository != null)
|
||||
{
|
||||
new GitRepositoryViewer().handleRequest(response, repository, name);
|
||||
}
|
||||
else
|
||||
{
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new ServletException(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close(input);
|
||||
IOUtil.close(output);
|
||||
if (repository != null)
|
||||
{
|
||||
repository.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,4 +182,9 @@ public class ScmGitServlet extends GitServlet
|
||||
{
|
||||
return request.getRequestURI().substring(request.getContextPath().length());
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private RepositoryResolver resolver;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user