diff options
author | James Moger <james.moger@gitblit.com> | 2013-12-16 08:59:58 -0500 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2013-12-16 09:36:57 -0500 |
commit | 206b4b09c872f3e9f078383c9a5c701b1549ff5c (patch) | |
tree | 7f837d3fa697ef541e5a3ae9d213aaf41514ebbd /src/main | |
parent | 0fe9792a9f194e68288c377810c33973d117923f (diff) | |
download | gitblit-206b4b09c872f3e9f078383c9a5c701b1549ff5c.tar.gz gitblit-206b4b09c872f3e9f078383c9a5c701b1549ff5c.zip |
Sync bugtraq support for spec v0.3
Change-Id: I2c24d1201d4c36963a708b178e780c1828707269
Diffstat (limited to 'src/main')
4 files changed, 147 insertions, 103 deletions
diff --git a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java index e1b6d5fd..8ba957b7 100644 --- a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java +++ b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java @@ -29,80 +29,104 @@ */ package com.syntevo.bugtraq; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import org.eclipse.jgit.errors.ConfigInvalidException; -import org.eclipse.jgit.lib.Config; -import org.eclipse.jgit.lib.Constants; -import org.eclipse.jgit.lib.FileMode; -import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.ObjectLoader; -import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.revwalk.RevCommit; -import org.eclipse.jgit.revwalk.RevTree; -import org.eclipse.jgit.revwalk.RevWalk; -import org.eclipse.jgit.storage.file.FileBasedConfig; -import org.eclipse.jgit.treewalk.TreeWalk; -import org.eclipse.jgit.treewalk.filter.PathFilterGroup; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; +import java.io.*; +import java.util.*; + +import org.eclipse.jgit.errors.*; +import org.eclipse.jgit.lib.*; +import org.eclipse.jgit.revwalk.*; +import org.eclipse.jgit.storage.file.*; +import org.eclipse.jgit.treewalk.*; +import org.eclipse.jgit.treewalk.filter.*; +import org.jetbrains.annotations.*; public final class BugtraqConfig { // Constants ============================================================== private static final String DOT_GIT_BUGTRAQ = ".gitbugtraq"; + private static final String DOT_TGITCONFIG = ".tgitconfig"; private static final String BUGTRAQ = "bugtraq"; private static final String URL = "url"; private static final String ENABLED = "enabled"; - private static final String LOG_REGEX = "logRegex"; + private static final String LOG_REGEX = "logregex"; + private static final String LOG_FILTERREGEX = "logfilterregex"; + private static final String LOG_LINKREGEX = "loglinkregex"; + private static final String LOG_LINKTEXT = "loglinktext"; // Static ================================================================= @Nullable public static BugtraqConfig read(@NotNull Repository repository) throws IOException, ConfigInvalidException { - final Config baseConfig = getBaseConfig(repository); + Config baseConfig = getBaseConfig(repository, DOT_GIT_BUGTRAQ); + if (baseConfig == null) { + baseConfig = getBaseConfig(repository, DOT_TGITCONFIG); + } + final Set<String> allNames = new HashSet<String>(); final Config config = repository.getConfig(); - allNames.addAll(config.getSubsections(BUGTRAQ)); - if (baseConfig != null) { - allNames.addAll(baseConfig.getSubsections(BUGTRAQ)); + if (getString(null, URL, config, baseConfig) != null) { + allNames.add(null); + } + else { + allNames.addAll(config.getSubsections(BUGTRAQ)); + if (baseConfig != null) { + allNames.addAll(baseConfig.getSubsections(BUGTRAQ)); + } } final List<BugtraqEntry> entries = new ArrayList<BugtraqEntry>(); for (String name : allNames) { final String url = getString(name, URL, config, baseConfig); + if (url == null) { + continue; + } + final String enabled = getString(name, ENABLED, config, baseConfig); if (enabled != null && !"true".equals(enabled)) { continue; } - final String logIdRegex = getString(name, LOG_REGEX, config, baseConfig); - if (url == null || logIdRegex == null) { + String idRegex = getString(name, LOG_REGEX, config, baseConfig); + if (idRegex == null) { return null; } - final List<String> logIdRegexs = new ArrayList<String>(); - logIdRegexs.add(logIdRegex); - - for (int index = 1; index < Integer.MAX_VALUE; index++) { - final String logIdRegexN = getString(name, LOG_REGEX + index, config, baseConfig); - if (logIdRegexN == null) { - break; + String filterRegex = getString(name, LOG_FILTERREGEX, config, baseConfig); + final String linkRegex = getString(name, LOG_LINKREGEX, config, baseConfig); + if (filterRegex == null && linkRegex == null) { + final String[] split = idRegex.split("\n", Integer.MAX_VALUE); + if (split.length == 2) { + // Compatibility with TortoiseGit + filterRegex = split[0]; + idRegex = split[1]; } + else { + // Backwards compatibility with specification version < 0.3 + final List<String> logIdRegexs = new ArrayList<String>(); + for (int index = 1; index < Integer.MAX_VALUE; index++) { + final String logIdRegexN = getString(name, LOG_REGEX + index, config, baseConfig); + if (logIdRegexN == null) { + break; + } + + logIdRegexs.add(logIdRegexN); + } - logIdRegexs.add(logIdRegexN); + if (logIdRegexs.size() > 1) { + throw new ConfigInvalidException("More than three " + LOG_REGEX + " entries found. This is not supported anymore since bugtraq version 0.3, use " + LOG_FILTERREGEX + " and " + LOG_LINKREGEX + " instead."); + } + else if (logIdRegexs.size() == 1) { + filterRegex = idRegex; + idRegex = logIdRegexs.get(0); + } + } } - entries.add(new BugtraqEntry(url, logIdRegexs)); + final String linkText = getString(name, LOG_LINKTEXT, config, baseConfig); + entries.add(new BugtraqEntry(url, idRegex, linkRegex, filterRegex, linkText)); } if (entries.isEmpty()) { @@ -133,14 +157,14 @@ public final class BugtraqConfig { // Utils ================================================================== @Nullable - private static Config getBaseConfig(Repository repository) throws IOException, ConfigInvalidException { + private static Config getBaseConfig(@NotNull Repository repository, @NotNull String configFileName) throws IOException, ConfigInvalidException { final Config baseConfig; if (repository.isBare()) { // read bugtraq config directly from the repository String content = null; RevWalk rw = new RevWalk(repository); TreeWalk tw = new TreeWalk(repository); - tw.setFilter(PathFilterGroup.createFromStrings(DOT_GIT_BUGTRAQ)); + tw.setFilter(PathFilterGroup.createFromStrings(configFileName)); try { ObjectId headId = repository.getRef(Constants.HEAD).getTarget().getObjectId(); RevCommit commit = rw.parseCommit(headId); @@ -155,7 +179,8 @@ public final class BugtraqConfig { break; } } - } finally { + } + finally { rw.dispose(); tw.release(); } @@ -173,7 +198,7 @@ public final class BugtraqConfig { } else { // read bugtraq config from work tree - final File baseFile = new File(repository.getWorkTree(), DOT_GIT_BUGTRAQ); + final File baseFile = new File(repository.getWorkTree(), configFileName); if (baseFile.isFile()) { FileBasedConfig fileConfig = new FileBasedConfig(baseFile, repository.getFS()); fileConfig.load(); @@ -187,7 +212,7 @@ public final class BugtraqConfig { } @Nullable - private static String getString(@NotNull String subsection, @NotNull String key, @NotNull Config config, @Nullable Config baseConfig) { + private static String getString(@Nullable String subsection, @NotNull String key, @NotNull Config config, @Nullable Config baseConfig) { final String value = config.getString(BUGTRAQ, subsection, key); if (value != null) { return trimMaybeNull(value); @@ -197,8 +222,8 @@ public final class BugtraqConfig { return trimMaybeNull(baseConfig.getString(BUGTRAQ, subsection, key)); } - return value; - } + return value; + } @Nullable private static String trimMaybeNull(@Nullable String string) { diff --git a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqEntry.java b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqEntry.java index 7ecd8bf8..62e496bd 100644 --- a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqEntry.java +++ b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqEntry.java @@ -29,8 +29,6 @@ */ package com.syntevo.bugtraq; -import java.util.*; - import org.jetbrains.annotations.*; final class BugtraqEntry { @@ -38,13 +36,15 @@ final class BugtraqEntry { // Fields ================================================================= private final String url; + private final String logLinkText; private final BugtraqParser parser; // Setup ================================================================== - public BugtraqEntry(@NotNull String url, @NotNull List<String> logIdRegexs) throws BugtraqException { + public BugtraqEntry(@NotNull String url, @NotNull String logIdRegex, @Nullable String logLinkRegex, @Nullable String logFilterRegex, @Nullable String logLinkText) throws BugtraqException { this.url = url; - this.parser = BugtraqParser.createInstance(logIdRegexs); + this.logLinkText = logLinkText; + this.parser = BugtraqParser.createInstance(logIdRegex, logLinkRegex, logFilterRegex); } // Accessing ============================================================== @@ -54,6 +54,11 @@ final class BugtraqEntry { return url; } + @Nullable + public String getLogLinkText() { + return logLinkText; + } + @NotNull public BugtraqParser getParser() { return parser; diff --git a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqFormatter.java b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqFormatter.java index 978cd8c9..5bb2c1cc 100644 --- a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqFormatter.java +++ b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqFormatter.java @@ -59,10 +59,6 @@ public final class BugtraqFormatter { for (BugtraqEntry entry : config.getEntries()) { final List<BugtraqParserIssueId> ids = entry.getParser().parse(message); - if (ids == null) { - continue; - } - for (BugtraqParserIssueId id : ids) { allIds.add(new IssueId(entry, id)); } @@ -76,8 +72,15 @@ public final class BugtraqFormatter { } appendText(message.substring(lastIdEnd + 1, id.getFrom()), outputHandler); + final String logLinkText = issueId.entry.getLogLinkText(); + final String linkText; + if (logLinkText != null) { + linkText = logLinkText.replace("%BUGID%", id.getId()); + } + else { + linkText = message.substring(id.getFrom(), id.getTo() + 1); + } - final String linkText = message.substring(id.getFrom(), id.getTo() + 1); final String target = issueId.entry.getUrl().replace("%BUGID%", id.getId()); outputHandler.appendLink(linkText, target); lastIdEnd = id.getTo(); diff --git a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqParser.java b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqParser.java index 8619c4c5..9dc38e51 100644 --- a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqParser.java +++ b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqParser.java @@ -39,9 +39,9 @@ final class BugtraqParser { // Static ================================================================= @NotNull - public static BugtraqParser createInstance(@NotNull List<String> regexs) throws BugtraqException { + public static BugtraqParser createInstance(@NotNull String idRegex, @Nullable String linkRegex, @Nullable String filterRegex) throws BugtraqException { try { - return new BugtraqParser(regexs); + return new BugtraqParser(idRegex, linkRegex, filterRegex); } catch (PatternSyntaxException ex) { throw new BugtraqException(ex); @@ -50,16 +50,16 @@ final class BugtraqParser { // Fields ================================================================= - private final List<Pattern> patterns; + private final Pattern idPattern; + private final Pattern linkPattern; + private final Pattern filterPattern; // Setup ================================================================== - private BugtraqParser(List<String> regexs) { - this.patterns = new ArrayList<Pattern>(); - - for (String regex : regexs) { - patterns.add(compilePatternSafe(regex)); - } + private BugtraqParser(@NotNull String idRegex, @Nullable String linkRegex, @Nullable String filterRegex) { + idPattern = compilePatternSafe(idRegex); + linkPattern = linkRegex != null ? compilePatternSafe(linkRegex) : null; + filterPattern = filterRegex != null ? compilePatternSafe(filterRegex) : null; } // Accessing ============================================================== @@ -69,49 +69,45 @@ final class BugtraqParser { List<Part> parts = new ArrayList<Part>(); parts.add(new Part(message, 0, message.length() - 1)); - boolean firstMatch = false; - - for (Pattern pattern : patterns) { - final List<Part> newParts = new ArrayList<Part>(); - for (Part part : parts) { - final Matcher matcher = pattern.matcher(part.text); - while (matcher.find()) { - firstMatch = true; - if (matcher.groupCount() == 0) { - addNewPart(part, matcher, 0, newParts); - } - else { - addNewPart(part, matcher, 1, newParts); - } - } - } - - parts = newParts; - if (parts.isEmpty()) { - parts = null; - break; - } + if (filterPattern != null) { + parts = collectParts(parts, filterPattern); } - if (!firstMatch) { - return null; - } - - if (parts == null) { - parts = new ArrayList<Part>(); + if (linkPattern != null) { + parts = collectParts(parts, linkPattern); } final List<BugtraqParserIssueId> ids = new ArrayList<BugtraqParserIssueId>(); - for (Part part : parts) { - final BugtraqParserIssueId id = new BugtraqParserIssueId(part.from, part.to, part.text); - if (ids.size() > 0) { - final BugtraqParserIssueId lastId = ids.get(ids.size() - 1); - if (id.getFrom() <= lastId.getTo()) { + for (final Part part : parts) { + final Matcher matcher = idPattern.matcher(part.text); + while (matcher.find()) { + final Part subPart = createSubPart(part, matcher, matcher.groupCount() == 0 ? 0 : 1); + if (subPart == null) { continue; } - } + + final BugtraqParserIssueId id; + if (linkPattern == null) { + id = new BugtraqParserIssueId(subPart.from, subPart.to, subPart.text); + } + else { + if (matcher.find()) { + // If we are using links, the last pattern (link) must produce exactly one id. + continue; + } + + id = new BugtraqParserIssueId(part.from, part.to, subPart.text); + } - ids.add(id); + if (ids.size() > 0) { + final BugtraqParserIssueId lastId = ids.get(ids.size() - 1); + if (id.getFrom() <= lastId.getTo()) { + continue; + } + } + + ids.add(id); + } } return ids; @@ -119,15 +115,30 @@ final class BugtraqParser { // Utils ================================================================== - private static void addNewPart(Part part, Matcher matcher, int group, List<Part> newParts) { + private static List<Part> collectParts(@NotNull List<Part> mainParts, @NotNull Pattern pattern) { + final List<Part> subParts = new ArrayList<Part>(); + for (final Part part : mainParts) { + final Matcher matcher = pattern.matcher(part.text); + while (matcher.find()) { + final Part newPart = createSubPart(part, matcher, matcher.groupCount() == 0 ? 0 : 1); + if (newPart != null) { + subParts.add(newPart); + } + } + } + + return subParts; + } + + @Nullable + private static Part createSubPart(Part part, Matcher matcher, int group) { final int textStart = matcher.start(group) + part.from; final int textEnd = matcher.end(group) - 1 + part.from; if (textEnd < 0) { - return; + return null; } - final Part newPart = new Part(matcher.group(group), textStart, textEnd); - newParts.add(newPart); + return new Part(matcher.group(group), textStart, textEnd); } private static Pattern compilePatternSafe(String pattern) throws PatternSyntaxException { |