mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-05-06 15:07:25 +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;
|
||||
}
|
||||
|
||||
@@ -33,16 +33,15 @@
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>SCM :: Manager - Git Repository</title>
|
||||
<title>SCM :: Manager - Git Repository - ${name}</title>
|
||||
<style type="text/css">
|
||||
body {
|
||||
background-color: #ffffff;
|
||||
font-size: 12px;
|
||||
font-family: Verdana, "Verdana CE", Arial, "Arial CE", "Lucida Grande CE", lucida, "Helvetica CE", sans-serif;
|
||||
color: #000000;
|
||||
margin: 10px;
|
||||
color: #202020;
|
||||
font-family: Verdana,Helvetica,Arial,sans-serif;
|
||||
font-size: 75%;
|
||||
}
|
||||
|
||||
h1, h2, h3, h4, h5 {
|
||||
font-family: Arial, "Arial CE", "Lucida Grande CE", lucida, "Helvetica CE", sans-serif;
|
||||
font-weight: bold;
|
||||
@@ -50,37 +49,61 @@
|
||||
padding: 0px;
|
||||
color: #D20005;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 18px;
|
||||
font-size: 18px;
|
||||
border-bottom: 1px solid #AFAFAF;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
h3 {
|
||||
font-size: 14px;
|
||||
border-bottom: 1px solid #AFAFAF;
|
||||
}
|
||||
|
||||
a:link, a:visited {
|
||||
color: #045491;
|
||||
font-weight : bold;
|
||||
font-weight: bold;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
}
|
||||
a:link:hover, a:visited:hover {
|
||||
color: #045491;
|
||||
font-weight : bold;
|
||||
text-decoration : underline;
|
||||
font-weight: bold;
|
||||
text-decoration: underline;
|
||||
}
|
||||
table {
|
||||
border: 0 none;
|
||||
border-collapse: collapse;
|
||||
font-size: 100%;
|
||||
margin: 20px 0;
|
||||
padding: 20px;
|
||||
width: 100%;
|
||||
}
|
||||
td, th {
|
||||
padding: 3px;
|
||||
vertical-align: top;
|
||||
border: 1px solid #CCCCCC;
|
||||
text-align: left;
|
||||
}
|
||||
.small {
|
||||
width: 20%;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>SCM :: Manager - Git Repository</h1>
|
||||
|
||||
<h1>SCM :: Manager - Git Repository - ${name}</h1>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="small">Time</th>
|
||||
<th class="small">Author</th>
|
||||
<th>Message</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${commits}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>Git Informations</h2>
|
||||
<ul>
|
||||
<li><a href="http://git-scm.com/" target="_blank">Git Homepage</a></li>
|
||||
<li><a href="http://www.kernel.org/pub/software/scm/git/docs/gittutorial.html" target="_blank">Git Tutorial</a></li>
|
||||
|
||||
@@ -123,7 +123,7 @@ public class RegexResourceProcessor extends AbstractResourceProcessor
|
||||
result = new StringBuffer();
|
||||
}
|
||||
|
||||
m.appendReplacement(result, value);
|
||||
m.appendReplacement(result, Matcher.quoteReplacement(value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user