mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
use hgblame.py script to show commit description in mercurial blame view
This commit is contained in:
@@ -50,7 +50,7 @@ import javax.xml.bind.annotation.XmlRootElement;
|
|||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
* @since 1.8
|
* @since 1.8
|
||||||
*/
|
*/
|
||||||
@XmlRootElement(name = "blame-paging")
|
@XmlRootElement(name = "blame-result")
|
||||||
@XmlAccessorType(XmlAccessType.FIELD)
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
public class BlameResult
|
public class BlameResult
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -40,23 +40,16 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import sonia.scm.util.AssertUtil;
|
import sonia.scm.util.AssertUtil;
|
||||||
import sonia.scm.util.IOUtil;
|
import sonia.scm.util.IOUtil;
|
||||||
import sonia.scm.util.Util;
|
import sonia.scm.web.HgUtil;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import javax.xml.bind.JAXBContext;
|
||||||
import java.text.SimpleDateFormat;
|
import javax.xml.bind.JAXBException;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -65,17 +58,13 @@ import java.util.regex.Pattern;
|
|||||||
public class HgBlameViewer implements BlameViewer
|
public class HgBlameViewer implements BlameViewer
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
public static final String RESOURCE_BLAME = "/sonia/scm/hgblame.py";
|
||||||
|
|
||||||
/** the logger for HgBlameViewer */
|
/** the logger for HgBlameViewer */
|
||||||
private static final Logger logger =
|
private static final Logger logger =
|
||||||
LoggerFactory.getLogger(HgBlameViewer.class);
|
LoggerFactory.getLogger(HgBlameViewer.class);
|
||||||
|
|
||||||
// sample: "Sebastian Sdorra <s.sdorra@ostfalia.de> 34 Wed Sep 08 10:22:46 2010 +0200:1: <?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
public static final Pattern REGEX_FIRSTPART =
|
|
||||||
Pattern.compile(
|
|
||||||
"(?i)^(.*)\\s+([0-9]+)\\s+([a-z]{3}\\s[a-z]{3}\\s[0-9]{2}\\s[0-9]{2}:[0-9]{2}:[0-9]{2}\\s[0-9]{4}\\s[+-][0-9]{4}):([0-9]+):\\s(.*)$");
|
|
||||||
|
|
||||||
//~--- constructors ---------------------------------------------------------
|
//~--- constructors ---------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -83,25 +72,15 @@ public class HgBlameViewer implements BlameViewer
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param handler
|
* @param handler
|
||||||
* @param repositoryDirectory
|
* @param repository
|
||||||
|
* @param blameResultContext
|
||||||
*/
|
*/
|
||||||
public HgBlameViewer(HgRepositoryHandler handler, File repositoryDirectory)
|
public HgBlameViewer(HgRepositoryHandler handler, Repository repository,
|
||||||
|
JAXBContext blameResultContext)
|
||||||
{
|
{
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
this.repositoryDirectory = repositoryDirectory;
|
this.repository = repository;
|
||||||
}
|
this.blameResultContext = blameResultContext;
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs ...
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param handler
|
|
||||||
* @param repository
|
|
||||||
*/
|
|
||||||
public HgBlameViewer(HgRepositoryHandler handler, Repository repository)
|
|
||||||
{
|
|
||||||
this(handler, handler.getDirectory(repository));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
@@ -122,146 +101,49 @@ public class HgBlameViewer implements BlameViewer
|
|||||||
{
|
{
|
||||||
AssertUtil.assertIsNotEmpty(path);
|
AssertUtil.assertIsNotEmpty(path);
|
||||||
|
|
||||||
if (Util.isEmpty(revision))
|
Process p = HgUtil.createPythonProcess(handler, repository, revision, path);
|
||||||
{
|
|
||||||
revision = "tip";
|
|
||||||
}
|
|
||||||
|
|
||||||
ProcessBuilder builder =
|
|
||||||
new ProcessBuilder(handler.getConfig().getHgBinary(), "annotate", "-r",
|
|
||||||
revision, "-dnulv", Util.nonNull(path));
|
|
||||||
|
|
||||||
if (logger.isDebugEnabled())
|
|
||||||
{
|
|
||||||
StringBuilder msg = new StringBuilder();
|
|
||||||
|
|
||||||
for (String param : builder.command())
|
|
||||||
{
|
|
||||||
msg.append(param).append(" ");
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(msg.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
Process p = builder.directory(repositoryDirectory).start();
|
|
||||||
BufferedReader reader = null;
|
|
||||||
BlameResult result = null;
|
BlameResult result = null;
|
||||||
|
InputStream resource = null;
|
||||||
|
InputStream input = null;
|
||||||
|
OutputStream output = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
resource = HgBlameViewer.class.getResourceAsStream(RESOURCE_BLAME);
|
||||||
result = parseBlameInput(reader);
|
output = p.getOutputStream();
|
||||||
|
IOUtil.copy(resource, output);
|
||||||
|
output.close();
|
||||||
|
|
||||||
|
// IOUtil.copy(p.getErrorStream(), System.err);
|
||||||
|
input = p.getInputStream();
|
||||||
|
result =
|
||||||
|
(BlameResult) blameResultContext.createUnmarshaller().unmarshal(input);
|
||||||
|
|
||||||
|
// IOUtil.copy(input, System.out);
|
||||||
|
input.close();
|
||||||
|
}
|
||||||
|
catch (JAXBException ex)
|
||||||
|
{
|
||||||
|
logger.error("could not parse result", ex);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
IOUtil.close(reader);
|
IOUtil.close(resource);
|
||||||
|
IOUtil.close(input);
|
||||||
|
IOUtil.close(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param reader
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
private BlameResult parseBlameInput(BufferedReader reader) throws IOException
|
|
||||||
{
|
|
||||||
List<BlameLine> blameLines = new ArrayList<BlameLine>();
|
|
||||||
String line = reader.readLine();
|
|
||||||
int i = 1;
|
|
||||||
|
|
||||||
while (line != null)
|
|
||||||
{
|
|
||||||
BlameLine blameLine = parseBlameLine(i, line);
|
|
||||||
|
|
||||||
if (blameLine != null)
|
|
||||||
{
|
|
||||||
blameLines.add(blameLine);
|
|
||||||
}
|
|
||||||
|
|
||||||
line = reader.readLine();
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new BlameResult(blameLines.size(), blameLines);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param nr
|
|
||||||
* @param line
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private BlameLine parseBlameLine(int nr, String line)
|
|
||||||
{
|
|
||||||
BlameLine blameLine = null;
|
|
||||||
Matcher m = REGEX_FIRSTPART.matcher(line);
|
|
||||||
|
|
||||||
if (m.matches())
|
|
||||||
{
|
|
||||||
Person authorPerson = Person.toPerson(m.group(1));
|
|
||||||
|
|
||||||
// todo parse date
|
|
||||||
Long when = getDate(m.group(3));
|
|
||||||
|
|
||||||
blameLine = new BlameLine(nr, m.group(2), when, authorPerson, null,
|
|
||||||
m.group(5));
|
|
||||||
}
|
|
||||||
else if (logger.isWarnEnabled())
|
|
||||||
{
|
|
||||||
logger.warn("line '{}' does not match", line);
|
|
||||||
}
|
|
||||||
|
|
||||||
return blameLine;
|
|
||||||
}
|
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param dateString
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private Long getDate(String dateString)
|
|
||||||
{
|
|
||||||
Long date = null;
|
|
||||||
|
|
||||||
// Wed Sep 08 10:22:46 2010 +0200
|
|
||||||
try
|
|
||||||
{
|
|
||||||
SimpleDateFormat sdf =
|
|
||||||
new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy zzzz", Locale.ENGLISH);
|
|
||||||
|
|
||||||
date = sdf.parse(dateString).getTime();
|
|
||||||
}
|
|
||||||
catch (ParseException ex)
|
|
||||||
{
|
|
||||||
logger.warn("could not parse date string ".concat(dateString), ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
return date;
|
|
||||||
}
|
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private JAXBContext blameResultContext;
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private HgRepositoryHandler handler;
|
private HgRepositoryHandler handler;
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private File repositoryDirectory;
|
private Repository repository;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,6 +114,7 @@ public class HgRepositoryHandler
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
this.browserResultContext = JAXBContext.newInstance(BrowserResult.class);
|
this.browserResultContext = JAXBContext.newInstance(BrowserResult.class);
|
||||||
|
this.blameResultContext = JAXBContext.newInstance(BlameResult.class);
|
||||||
}
|
}
|
||||||
catch (JAXBException ex)
|
catch (JAXBException ex)
|
||||||
{
|
{
|
||||||
@@ -208,7 +209,7 @@ public class HgRepositoryHandler
|
|||||||
|
|
||||||
if (TYPE_NAME.equals(type))
|
if (TYPE_NAME.equals(type))
|
||||||
{
|
{
|
||||||
blameViewer = new HgBlameViewer(this, repository);
|
blameViewer = new HgBlameViewer(this, repository, blameResultContext);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -510,6 +511,9 @@ public class HgRepositoryHandler
|
|||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private JAXBContext blameResultContext;
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private JAXBContext browserResultContext;
|
private JAXBContext browserResultContext;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user