From 608ecec9dff2ea4ce8e2a1ea6fc8909a64492a68 Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 13 Apr 2011 09:43:56 -0400 Subject: [PATCH] Patch formatter. Ensure html escaping. css tweaks. --- src/com/gitblit/tests/JGitUtilsTest.java | 2 +- src/com/gitblit/utils/HtmlDiffFormatter.java | 23 ++-- src/com/gitblit/utils/JGitUtils.java | 78 ++++++++++-- src/com/gitblit/utils/PatchFormatter.java | 111 ++++++++++++++++++ src/com/gitblit/utils/Utils.java | 47 ++++++++ src/com/gitblit/wicket/GitBlitWebApp.java | 12 +- .../gitblit/wicket/GitBlitWebApp.properties | 4 +- src/com/gitblit/wicket/WicketUtils.java | 3 +- .../models}/TicGitTicket.java | 5 +- .../{DiffPage.html => BlobDiffPage.html} | 2 +- .../{DiffPage.java => BlobDiffPage.java} | 22 ++-- src/com/gitblit/wicket/pages/BlobPage.html | 2 +- src/com/gitblit/wicket/pages/BlobPage.java | 7 +- .../gitblit/wicket/pages/CommitDiffPage.html | 38 ++++++ .../gitblit/wicket/pages/CommitDiffPage.java | 80 +++++++++++++ src/com/gitblit/wicket/pages/CommitPage.html | 4 +- src/com/gitblit/wicket/pages/CommitPage.java | 9 +- src/com/gitblit/wicket/pages/PatchPage.html | 13 ++ src/com/gitblit/wicket/pages/PatchPage.java | 46 ++++++++ src/com/gitblit/wicket/pages/RawPage.html | 13 ++ src/com/gitblit/wicket/pages/RawPage.java | 91 ++++++++++++++ src/com/gitblit/wicket/pages/TagPage.html | 2 +- src/com/gitblit/wicket/pages/TagPage.java | 17 ++- src/com/gitblit/wicket/pages/TicGitPage.java | 2 +- .../wicket/pages/TicGitTicketPage.java | 4 +- src/com/gitblit/wicket/pages/TreePage.html | 2 +- src/com/gitblit/wicket/pages/TreePage.java | 6 +- src/com/gitblit/wicket/panels/LogPanel.java | 4 +- src/com/gitblit/wicket/resources/gitblit.css | 15 +-- 29 files changed, 590 insertions(+), 74 deletions(-) create mode 100644 src/com/gitblit/utils/PatchFormatter.java rename src/com/gitblit/{utils => wicket/models}/TicGitTicket.java (90%) rename src/com/gitblit/wicket/pages/{DiffPage.html => BlobDiffPage.html} (64%) rename src/com/gitblit/wicket/pages/{DiffPage.java => BlobDiffPage.java} (58%) create mode 100644 src/com/gitblit/wicket/pages/CommitDiffPage.html create mode 100644 src/com/gitblit/wicket/pages/CommitDiffPage.java create mode 100644 src/com/gitblit/wicket/pages/PatchPage.html create mode 100644 src/com/gitblit/wicket/pages/PatchPage.java create mode 100644 src/com/gitblit/wicket/pages/RawPage.html create mode 100644 src/com/gitblit/wicket/pages/RawPage.java diff --git a/src/com/gitblit/tests/JGitUtilsTest.java b/src/com/gitblit/tests/JGitUtilsTest.java index 708f35af..abc733c9 100644 --- a/src/com/gitblit/tests/JGitUtilsTest.java +++ b/src/com/gitblit/tests/JGitUtilsTest.java @@ -15,9 +15,9 @@ import org.eclipse.jgit.revwalk.RevTree; import org.eclipse.jgit.storage.file.FileRepository; import com.gitblit.utils.JGitUtils; -import com.gitblit.utils.TicGitTicket; import com.gitblit.wicket.models.PathModel; import com.gitblit.wicket.models.RefModel; +import com.gitblit.wicket.models.TicGitTicket; public class JGitUtilsTest extends TestCase { diff --git a/src/com/gitblit/utils/HtmlDiffFormatter.java b/src/com/gitblit/utils/HtmlDiffFormatter.java index 7c37862c..08c0c219 100644 --- a/src/com/gitblit/utils/HtmlDiffFormatter.java +++ b/src/com/gitblit/utils/HtmlDiffFormatter.java @@ -2,6 +2,7 @@ package com.gitblit.utils; import static org.eclipse.jgit.lib.Constants.encodeASCII; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -9,6 +10,7 @@ import org.eclipse.jgit.diff.DiffFormatter; import org.eclipse.jgit.diff.RawText; public class HtmlDiffFormatter extends DiffFormatter { + private final OutputStream os; public HtmlDiffFormatter(OutputStream os) { @@ -39,9 +41,6 @@ public class HtmlDiffFormatter extends DiffFormatter { os.write(' '); os.write('@'); os.write('@'); - // TODO not sure if JGit can determine hunk section - //os.write("".getBytes()); - //os.write("".getBytes()); os.write("".getBytes()); } @@ -82,18 +81,22 @@ public class HtmlDiffFormatter extends DiffFormatter { protected void writeLine(final char prefix, final RawText text, final int cur) throws IOException { switch (prefix) { case '+': - os.write("
".getBytes()); + os.write("".getBytes()); break; case '-': - os.write("
".getBytes()); + os.write("".getBytes()); break; } os.write(prefix); - text.writeLine(os, cur); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + text.writeLine(bos, cur); + String line = bos.toString(); + line = Utils.escapeForHtml(line, false); + os.write(line.getBytes()); switch (prefix) { case '+': case '-': - os.write("
".getBytes()); + os.write("
\n".getBytes()); break; default: os.write('\n'); @@ -115,14 +118,14 @@ public class HtmlDiffFormatter extends DiffFormatter { if (line.startsWith("diff")) { sb.append("
").append(line).append("
"); } else if (line.startsWith("---")) { - sb.append("
").append(line).append("
"); + sb.append("").append(line).append("
"); } else if (line.startsWith("+++")) { - sb.append("
").append(line).append("
"); + sb.append("").append(line).append("
"); } else { sb.append(line).append('\n'); } } - sb.append("
"); + sb.append("\n"); return sb.toString(); } } diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java index abecac41..69c46da4 100644 --- a/src/com/gitblit/utils/JGitUtils.java +++ b/src/com/gitblit/utils/JGitUtils.java @@ -42,10 +42,11 @@ import org.eclipse.jgit.util.io.DisabledOutputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.gitblit.utils.TicGitTicket.Comment; import com.gitblit.wicket.models.Metric; import com.gitblit.wicket.models.PathModel; import com.gitblit.wicket.models.RefModel; +import com.gitblit.wicket.models.TicGitTicket; +import com.gitblit.wicket.models.TicGitTicket.Comment; public class JGitUtils { @@ -98,12 +99,15 @@ public class JGitUtils { return getCommitDate(commit); } - public static RevCommit getCommit(Repository r, String commitId) { + public static RevCommit getCommit(Repository r, String objectId) { RevCommit commit = null; try { - ObjectId objectId = r.resolve(commitId); + if (objectId == null || objectId.trim().length() == 0) { + objectId = Constants.HEAD; + } + ObjectId object = r.resolve(objectId); RevWalk walk = new RevWalk(r); - RevCommit rev = walk.parseCommit(objectId); + RevCommit rev = walk.parseCommit(object); commit = rev; walk.dispose(); } catch (Throwable t) { @@ -205,8 +209,8 @@ public class JGitUtils { return new String(getRawContent(r, (RevBlob) obj)); } - public static List getFilesInPath(Repository r, String basePath, String commitId) { - RevCommit commit = getCommit(r, commitId); + public static List getFilesInPath(Repository r, String basePath, String objectId) { + RevCommit commit = getCommit(r, objectId); return getFilesInPath(r, basePath, commit); } @@ -298,11 +302,7 @@ public class JGitUtils { walk.setRecursive(true); walk.addTree(parentTree); walk.addTree(commitTree); - if (path != null && path.trim().length() > 0) { - walk.setFilter(PathFilter.create(path)); - } else { - walk.setFilter(TreeFilter.ANY_DIFF); - } + walk.setFilter(TreeFilter.ANY_DIFF); final ByteArrayOutputStream os = new ByteArrayOutputStream(); RawTextComparator cmp = RawTextComparator.DEFAULT; @@ -316,7 +316,16 @@ public class JGitUtils { df.setDiffComparator(cmp); df.setDetectRenames(true); List diffs = df.scan(parentTree, commitTree); - df.format(diffs); + if (path != null && path.length() > 0) { + for (DiffEntry diff : diffs) { + if (diff.getNewPath().equalsIgnoreCase(path)) { + df.format(diff); + break; + } + } + } else { + df.format(diffs); + } String diff; if (outputHtml) { // workaround for complex private methods in DiffFormatter @@ -331,6 +340,50 @@ public class JGitUtils { } return null; } + + public static String getCommitPatch(Repository r, RevCommit commit) { + return getCommitPatch(r, commit); + } + + public static String getCommitPatch(Repository r, RevCommit commit, String path) { + try { + final RevWalk rw = new RevWalk(r); + RevCommit parent = rw.parseCommit(commit.getParent(0).getId()); + RevTree parentTree = parent.getTree(); + RevTree commitTree = commit.getTree(); + + final TreeWalk walk = new TreeWalk(r); + walk.reset(); + walk.setRecursive(true); + walk.addTree(parentTree); + walk.addTree(commitTree); + walk.setFilter(TreeFilter.ANY_DIFF); + + final ByteArrayOutputStream os = new ByteArrayOutputStream(); + RawTextComparator cmp = RawTextComparator.DEFAULT; + PatchFormatter df = new PatchFormatter(os); + df.setRepository(r); + df.setDiffComparator(cmp); + df.setDetectRenames(true); + List diffs = df.scan(parentTree, commitTree); + if (path != null && path.length() > 0) { + for (DiffEntry diff : diffs) { + if (diff.getNewPath().equalsIgnoreCase(path)) { + df.format(diff); + break; + } + } + } else { + df.format(diffs); + } + String diff = df.getPatch(commit); + df.flush(); + return diff; + } catch (Throwable t) { + LOGGER.error("failed to generate commit diff!", t); + } + return null; + } private static PathModel getPathModel(TreeWalk walk, String basePath, RevCommit commit) { String name; @@ -523,6 +576,7 @@ public class JGitUtils { } public static List getDateMetrics(Repository r) { + final List tags = getTags(r, -1); final Map map = new HashMap(); try { DateFormat df = new SimpleDateFormat("yyyy-MM"); diff --git a/src/com/gitblit/utils/PatchFormatter.java b/src/com/gitblit/utils/PatchFormatter.java new file mode 100644 index 00000000..f019ce40 --- /dev/null +++ b/src/com/gitblit/utils/PatchFormatter.java @@ -0,0 +1,111 @@ +package com.gitblit.utils; + +import java.io.IOException; +import java.io.OutputStream; +import java.text.MessageFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.jgit.diff.DiffEntry; +import org.eclipse.jgit.diff.DiffFormatter; +import org.eclipse.jgit.diff.RawText; +import org.eclipse.jgit.revwalk.RevCommit; + +import com.gitblit.Constants; + +public class PatchFormatter extends DiffFormatter { + + private final OutputStream os; + + private PatchTouple currentTouple = null; + + Map changes = new HashMap(); + + public PatchFormatter(OutputStream os) { + super(os); + this.os = os; + } + + public void format(DiffEntry entry) throws IOException { + currentTouple = new PatchTouple(); + changes.put(entry.getNewPath(), currentTouple); + super.format(entry); + } + + @Override + protected void writeLine(final char prefix, final RawText text, final int cur) throws IOException { + switch (prefix) { + case '+': + currentTouple.insertions++; + break; + case '-': + currentTouple.deletions++; + break; + } + super.writeLine(prefix, text, cur); + } + + public String getPatch(RevCommit commit) { + StringBuilder patch = new StringBuilder(); + // hard-code the mon sep 17 2001 date string. + // I have no idea why that is there. it seems to be a constant. + patch.append("From " + commit.getName() + " Mon Sep 17 00:00:00 2001" + "\n"); + patch.append("From: " + JGitUtils.getDisplayName(commit.getAuthorIdent()) + "\n"); + patch.append("Date: " + (new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").format(new Date(commit.getCommitTime() * 1000l))) + "\n"); + patch.append("Subject: [PATCH] " + commit.getShortMessage() + "\n"); + patch.append("\n"); + patch.append("---"); + int maxPathLen = 0; + int files = 0; + int insertions = 0; + int deletions = 0; + for (String path : changes.keySet()) { + if (path.length() > maxPathLen) { + maxPathLen = path.length(); + } + PatchTouple touple = changes.get(path); + files++; + insertions += touple.insertions; + deletions += touple.deletions; + } + int columns = 60; + int total = insertions + deletions; + int unit = total / columns + (total % columns > 0 ? 1 : 0); + if (unit == 0) { + unit = 1; + } + for (String path : changes.keySet()) { + PatchTouple touple = changes.get(path); + patch.append("\n " + Utils.rightPad(path, maxPathLen, ' ') + " | " + Utils.leftPad("" + touple.total(), 4, ' ') + " " + touple.relativeScale(unit)); + } + patch.append(MessageFormat.format("\n {0} files changed, {1} insertions(+), {2} deletions(-)\n\n", files, insertions, deletions)); + patch.append(os.toString()); + patch.append("\n--\n"); + patch.append(Constants.getRunningVersion()); + return patch.toString(); + } + + private class PatchTouple { + int insertions = 0; + int deletions = 0; + + int total() { + return insertions + deletions; + } + + String relativeScale(int unit) { + int plus = (insertions / unit); + int minus = (deletions / unit); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < plus; i++) { + sb.append('+'); + } + for (int i = 0; i < minus; i++) { + sb.append('-'); + } + return sb.toString(); + } + } +} diff --git a/src/com/gitblit/utils/Utils.java b/src/com/gitblit/utils/Utils.java index bef41b29..c742fb62 100644 --- a/src/com/gitblit/utils/Utils.java +++ b/src/com/gitblit/utils/Utils.java @@ -116,4 +116,51 @@ public class Utils { } return ago; } + + public static String leftPad(String input, int length, char pad) { + if (input.length() < length) { + StringBuilder sb = new StringBuilder(); + for (int i = 0, len = length - input.length(); i < len; i++) { + sb.append(pad); + } + sb.append(input); + return sb.toString(); + } + return input; + } + + public static String rightPad(String input, int length, char pad) { + if (input.length() < length) { + StringBuilder sb = new StringBuilder(); + sb.append(input); + for (int i = 0, len = length - input.length(); i < len; i++) { + sb.append(pad); + } + return sb.toString(); + } + return input; + } + + public static String escapeForHtml(String inStr, boolean changeSpace) { + StringBuffer retStr = new StringBuffer(); + int i = 0; + while (i < inStr.length()) { + if (inStr.charAt(i) == '&') { + retStr.append("&"); + } else if (inStr.charAt(i) == '<') { + retStr.append("<"); + } else if (inStr.charAt(i) == '>') { + retStr.append(">"); + } else if (inStr.charAt(i) == '\"') { + retStr.append("""); + } else if (changeSpace && inStr.charAt(i) == ' ') { + retStr.append(" "); + } else if (changeSpace && inStr.charAt(i) == '\t') { + retStr.append("    "); + } else + retStr.append(inStr.charAt(i)); + i++; + } + return retStr.toString(); + } } diff --git a/src/com/gitblit/wicket/GitBlitWebApp.java b/src/com/gitblit/wicket/GitBlitWebApp.java index 90a03715..b869a05f 100644 --- a/src/com/gitblit/wicket/GitBlitWebApp.java +++ b/src/com/gitblit/wicket/GitBlitWebApp.java @@ -28,12 +28,15 @@ import com.gitblit.GitBlitServer; import com.gitblit.StoredSettings; import com.gitblit.utils.JGitUtils; import com.gitblit.wicket.models.RepositoryModel; +import com.gitblit.wicket.pages.BlobDiffPage; import com.gitblit.wicket.pages.BlobPage; import com.gitblit.wicket.pages.BranchesPage; import com.gitblit.wicket.pages.CommitPage; -import com.gitblit.wicket.pages.DiffPage; -import com.gitblit.wicket.pages.RepositoriesPage; +import com.gitblit.wicket.pages.CommitDiffPage; import com.gitblit.wicket.pages.LogPage; +import com.gitblit.wicket.pages.PatchPage; +import com.gitblit.wicket.pages.RawPage; +import com.gitblit.wicket.pages.RepositoriesPage; import com.gitblit.wicket.pages.SummaryPage; import com.gitblit.wicket.pages.TagPage; import com.gitblit.wicket.pages.TagsPage; @@ -70,7 +73,10 @@ public class GitBlitWebApp extends WebApplication { mount(new MixedParamUrlCodingStrategy("/tag", TagPage.class, new String[] { "r", "h" })); mount(new MixedParamUrlCodingStrategy("/tree", TreePage.class, new String[] { "r", "h", "f" })); mount(new MixedParamUrlCodingStrategy("/blob", BlobPage.class, new String[] { "r", "h", "f" })); - mount(new MixedParamUrlCodingStrategy("/diff", DiffPage.class, new String[] { "r", "h", "f" })); + mount(new MixedParamUrlCodingStrategy("/raw", RawPage.class, new String[] { "r", "h", "f" })); + mount(new MixedParamUrlCodingStrategy("/blobdiff", BlobDiffPage.class, new String[] { "r", "h", "f" })); + mount(new MixedParamUrlCodingStrategy("/commitdiff", CommitDiffPage.class, new String[] { "r", "h" })); + mount(new MixedParamUrlCodingStrategy("/patch", PatchPage.class, new String[] { "r", "h", "f" })); // setup extended urls mount(new MixedParamUrlCodingStrategy("/ticgit", TicGitPage.class, new String[] { "p" })); diff --git a/src/com/gitblit/wicket/GitBlitWebApp.properties b/src/com/gitblit/wicket/GitBlitWebApp.properties index 1d7e7bab..273ff5a7 100644 --- a/src/com/gitblit/wicket/GitBlitWebApp.properties +++ b/src/com/gitblit/wicket/GitBlitWebApp.properties @@ -38,4 +38,6 @@ gb.ticgit = ticgit gb.pageFirst = first gb.pagePrevious prev gb.pageNext = next -gb.parent = parent \ No newline at end of file +gb.parent = parent +gb.head = HEAD +gb.blame = blame \ No newline at end of file diff --git a/src/com/gitblit/wicket/WicketUtils.java b/src/com/gitblit/wicket/WicketUtils.java index 3a5ab921..65270a2c 100644 --- a/src/com/gitblit/wicket/WicketUtils.java +++ b/src/com/gitblit/wicket/WicketUtils.java @@ -10,6 +10,7 @@ import org.apache.wicket.Component; import org.apache.wicket.PageParameters; import org.apache.wicket.behavior.SimpleAttributeModifier; import org.apache.wicket.markup.html.basic.Label; +import org.eclipse.jgit.lib.Constants; import com.gitblit.StoredSettings; import com.gitblit.utils.Utils; @@ -108,7 +109,7 @@ public class WicketUtils { } public static String getObject(PageParameters params) { - return params.getString("h", ""); + return params.getString("h", Constants.HEAD); } public static String getPath(PageParameters params) { diff --git a/src/com/gitblit/utils/TicGitTicket.java b/src/com/gitblit/wicket/models/TicGitTicket.java similarity index 90% rename from src/com/gitblit/utils/TicGitTicket.java rename to src/com/gitblit/wicket/models/TicGitTicket.java index 48491a66..50f1e08f 100644 --- a/src/com/gitblit/utils/TicGitTicket.java +++ b/src/com/gitblit/wicket/models/TicGitTicket.java @@ -1,14 +1,11 @@ -package com.gitblit.utils; +package com.gitblit.wicket.models; import java.io.Serializable; import java.text.ParseException; import java.util.ArrayList; -import java.util.Collections; import java.util.Date; import java.util.List; -import com.gitblit.wicket.models.PathModel; - public class TicGitTicket implements Serializable, Comparable { private static final long serialVersionUID = 1L; diff --git a/src/com/gitblit/wicket/pages/DiffPage.html b/src/com/gitblit/wicket/pages/BlobDiffPage.html similarity index 64% rename from src/com/gitblit/wicket/pages/DiffPage.html rename to src/com/gitblit/wicket/pages/BlobDiffPage.html index 6a5d21bc..2267b37d 100644 --- a/src/com/gitblit/wicket/pages/DiffPage.html +++ b/src/com/gitblit/wicket/pages/BlobDiffPage.html @@ -12,7 +12,7 @@ diff --git a/src/com/gitblit/wicket/pages/DiffPage.java b/src/com/gitblit/wicket/pages/BlobDiffPage.java similarity index 58% rename from src/com/gitblit/wicket/pages/DiffPage.java rename to src/com/gitblit/wicket/pages/BlobDiffPage.java index 1912c42d..17d78968 100644 --- a/src/com/gitblit/wicket/pages/DiffPage.java +++ b/src/com/gitblit/wicket/pages/BlobDiffPage.java @@ -2,6 +2,7 @@ package com.gitblit.wicket.pages; import org.apache.wicket.PageParameters; import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.markup.html.link.BookmarkablePageLink; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; @@ -11,33 +12,28 @@ import com.gitblit.wicket.RepositoryPage; import com.gitblit.wicket.WicketUtils; import com.gitblit.wicket.panels.PathBreadcrumbsPanel; -public class DiffPage extends RepositoryPage { +public class BlobDiffPage extends RepositoryPage { - public DiffPage(PageParameters params) { + public BlobDiffPage(PageParameters params) { super(params); final String blobPath = WicketUtils.getPath(params); Repository r = getRepository(); RevCommit commit = JGitUtils.getCommit(r, objectId); - String diff; - if (blobPath != null && blobPath.length() > 0) { - // blob diff - diff = JGitUtils.getCommitDiff(r, commit, blobPath, true); - } else { - // commit diff - diff = JGitUtils.getCommitDiff(r, commit, true); - } + String diff = JGitUtils.getCommitDiff(r, commit, blobPath, true); + add(new BookmarkablePageLink("patchLink", PatchPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath))); + add(new BookmarkablePageLink("commitLink", CommitPage.class, WicketUtils.newObjectParameter(repositoryName, objectId))); + add(new BookmarkablePageLink("commitDiffLink", CommitDiffPage.class, WicketUtils.newObjectParameter(repositoryName, objectId))); // diff page links + add(new Label("blameLink", getString("gb.blame"))); add(new Label("historyLink", getString("gb.history"))); - add(new Label("rawLink", getString("gb.raw"))); - add(new Label("headLink", "HEAD")); add(new LinkPanel("shortlog", "title", commit.getShortMessage(), CommitPage.class, newCommitParameter())); add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId)); - + add(new Label("diffText", diff).setEscapeModelStrings(false)); } diff --git a/src/com/gitblit/wicket/pages/BlobPage.html b/src/com/gitblit/wicket/pages/BlobPage.html index 82ba3202..393470a0 100644 --- a/src/com/gitblit/wicket/pages/BlobPage.html +++ b/src/com/gitblit/wicket/pages/BlobPage.html @@ -21,7 +21,7 @@ diff --git a/src/com/gitblit/wicket/pages/BlobPage.java b/src/com/gitblit/wicket/pages/BlobPage.java index 7d7259a0..0c112d08 100644 --- a/src/com/gitblit/wicket/pages/BlobPage.java +++ b/src/com/gitblit/wicket/pages/BlobPage.java @@ -6,6 +6,8 @@ import java.util.Map; import org.apache.wicket.Component; import org.apache.wicket.PageParameters; import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.markup.html.link.BookmarkablePageLink; +import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; @@ -28,9 +30,10 @@ public class BlobPage extends RepositoryPage { RevCommit commit = JGitUtils.getCommit(r, objectId); // blob page links + add(new Label("blameLink", getString("gb.blame"))); add(new Label("historyLink", getString("gb.history"))); - add(new Label("rawLink", getString("gb.raw"))); - add(new Label("headLink", "HEAD")); + add(new BookmarkablePageLink("rawLink", RawPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath))); + add(new BookmarkablePageLink("headLink", BlobPage.class, WicketUtils.newPathParameter(repositoryName, Constants.HEAD, blobPath))); add(new LinkPanel("shortlog", "title", commit.getShortMessage(), CommitPage.class, newCommitParameter())); diff --git a/src/com/gitblit/wicket/pages/CommitDiffPage.html b/src/com/gitblit/wicket/pages/CommitDiffPage.html new file mode 100644 index 00000000..2759de8f --- /dev/null +++ b/src/com/gitblit/wicket/pages/CommitDiffPage.html @@ -0,0 +1,38 @@ + + + + + + + +
[page links]
+ + + + + +
[shortlog header]
+ + + + + + + +
[commit path] + +
+ + +
[diff text]
+ +
+ + \ No newline at end of file diff --git a/src/com/gitblit/wicket/pages/CommitDiffPage.java b/src/com/gitblit/wicket/pages/CommitDiffPage.java new file mode 100644 index 00000000..7f6d347e --- /dev/null +++ b/src/com/gitblit/wicket/pages/CommitDiffPage.java @@ -0,0 +1,80 @@ +package com.gitblit.wicket.pages; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.wicket.PageParameters; +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.markup.html.link.BookmarkablePageLink; +import org.apache.wicket.markup.repeater.Item; +import org.apache.wicket.markup.repeater.data.DataView; +import org.apache.wicket.markup.repeater.data.ListDataProvider; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; + +import com.gitblit.utils.JGitUtils; +import com.gitblit.wicket.LinkPanel; +import com.gitblit.wicket.RepositoryPage; +import com.gitblit.wicket.WicketUtils; +import com.gitblit.wicket.models.PathModel; + +public class CommitDiffPage extends RepositoryPage { + + public CommitDiffPage(PageParameters params) { + super(params); + + Repository r = getRepository(); + RevCommit commit = JGitUtils.getCommit(r, objectId); + String diff = JGitUtils.getCommitDiff(r, commit, true); + + List parents = new ArrayList(); + if (commit.getParentCount() > 0) { + for (RevCommit parent : commit.getParents()) { + parents.add(parent.name()); + } + } + + // commit page links + if (parents.size() == 0) { + add(new Label("parentLink", "none")); + } else { + add(new LinkPanel("parentLink", null, parents.get(0).substring(0, 8), CommitDiffPage.class, newCommitParameter(parents.get(0)))); + } + add(new BookmarkablePageLink("patchLink", PatchPage.class, WicketUtils.newObjectParameter(repositoryName, objectId))); + add(new BookmarkablePageLink("commitLink", CommitPage.class, WicketUtils.newObjectParameter(repositoryName, objectId))); + + add(new LinkPanel("shortlog", "title", commit.getShortMessage(), CommitPage.class, newCommitParameter())); + + // changed paths list + List paths = JGitUtils.getFilesInCommit(r, commit); + ListDataProvider pathsDp = new ListDataProvider(paths); + DataView pathsView = new DataView("changedPath", pathsDp) { + private static final long serialVersionUID = 1L; + int counter = 0; + + public void populateItem(final Item item) { + final PathModel entry = item.getModelObject(); + if (entry.isTree()) { + item.add(new LinkPanel("pathName", null, entry.path, TreePage.class, newPathParameter(entry.path))); + } else { + item.add(new LinkPanel("pathName", "list", entry.path, BlobPage.class, newPathParameter(entry.path))); + } + + item.add(new BookmarkablePageLink("patch", PatchPage.class, newPathParameter(entry.path))); + item.add(new BookmarkablePageLink("view", BlobPage.class, newPathParameter(entry.path))); + item.add(new BookmarkablePageLink("blame", BlobPage.class).setEnabled(false)); + item.add(new BookmarkablePageLink("history", BlobPage.class).setEnabled(false)); + + WicketUtils.setAlternatingBackground(item, counter); + counter++; + } + }; + add(pathsView); + add(new Label("diffText", diff).setEscapeModelStrings(false)); + } + + @Override + protected String getPageName() { + return getString("gb.commitdiff"); + } +} diff --git a/src/com/gitblit/wicket/pages/CommitPage.html b/src/com/gitblit/wicket/pages/CommitPage.html index 0614c063..a65c5884 100644 --- a/src/com/gitblit/wicket/pages/CommitPage.html +++ b/src/com/gitblit/wicket/pages/CommitPage.html @@ -12,7 +12,7 @@ @@ -40,7 +40,7 @@ [commit path] diff --git a/src/com/gitblit/wicket/pages/CommitPage.java b/src/com/gitblit/wicket/pages/CommitPage.java index a53e322e..d906f075 100644 --- a/src/com/gitblit/wicket/pages/CommitPage.java +++ b/src/com/gitblit/wicket/pages/CommitPage.java @@ -41,11 +41,11 @@ public class CommitPage extends RepositoryPage { add(new Label("commitdiffLink", getString("gb.commitdiff"))); } else { add(new LinkPanel("parentLink", null, parents.get(0).substring(0, 8), CommitPage.class, newCommitParameter(parents.get(0)))); - add(new LinkPanel("commitdiffLink", null, new StringResourceModel("gb.commitdiff", this, null), DiffPage.class, WicketUtils.newObjectParameter(repositoryName, objectId))); + add(new LinkPanel("commitdiffLink", null, new StringResourceModel("gb.commitdiff", this, null), CommitDiffPage.class, WicketUtils.newObjectParameter(repositoryName, objectId))); } - add(new Label("patchLink", getString("gb.patch"))); + add(new BookmarkablePageLink("patchLink", PatchPage.class, WicketUtils.newObjectParameter(repositoryName, objectId))); - add(new LinkPanel("shortlog", "title", c.getShortMessage(), LogPage.class, newRepositoryParameter())); + add(new LinkPanel("shortlog", "title", c.getShortMessage(), CommitDiffPage.class, WicketUtils.newObjectParameter(repositoryName, objectId))); addRefs(r, c); @@ -88,8 +88,9 @@ public class CommitPage extends RepositoryPage { item.add(new LinkPanel("pathName", "list", entry.path, BlobPage.class, newPathParameter(entry.path))); } - item.add(new BookmarkablePageLink("diff", DiffPage.class, newPathParameter(entry.path))); + item.add(new BookmarkablePageLink("diff", BlobDiffPage.class, newPathParameter(entry.path))); item.add(new BookmarkablePageLink("view", BlobPage.class, newPathParameter(entry.path))); + item.add(new BookmarkablePageLink("blame", BlobPage.class).setEnabled(false)); item.add(new BookmarkablePageLink("history", BlobPage.class).setEnabled(false)); WicketUtils.setAlternatingBackground(item, counter); diff --git a/src/com/gitblit/wicket/pages/PatchPage.html b/src/com/gitblit/wicket/pages/PatchPage.html new file mode 100644 index 00000000..719a46d1 --- /dev/null +++ b/src/com/gitblit/wicket/pages/PatchPage.html @@ -0,0 +1,13 @@ + + + + + + +
[patch content]
+ + + \ No newline at end of file diff --git a/src/com/gitblit/wicket/pages/PatchPage.java b/src/com/gitblit/wicket/pages/PatchPage.java new file mode 100644 index 00000000..52790d68 --- /dev/null +++ b/src/com/gitblit/wicket/pages/PatchPage.java @@ -0,0 +1,46 @@ +package com.gitblit.wicket.pages; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.wicket.PageParameters; +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.protocol.http.servlet.ServletWebRequest; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; + +import com.gitblit.utils.JGitUtils; +import com.gitblit.wicket.GitBlitWebApp; +import com.gitblit.wicket.WicketUtils; + + +public class PatchPage extends WebPage { + + public PatchPage(PageParameters params) { + super(params); + + if (!params.containsKey("r")) { + error("Repository not specified!"); + redirectToInterceptPage(new RepositoriesPage()); + } + final String repositoryName = WicketUtils.getRepositoryName(params); + final String objectId = WicketUtils.getObject(params); + final String blobPath = WicketUtils.getPath(params); + + ServletWebRequest servletWebRequest = (ServletWebRequest) getRequest(); + HttpServletRequest req = servletWebRequest.getHttpServletRequest(); + req.getServerName(); + + Repository r = GitBlitWebApp.get().getRepository(req, repositoryName); + if (r == null) { + error("Can not load repository " + repositoryName); + redirectToInterceptPage(new RepositoriesPage()); + return; + } + + RevCommit commit = JGitUtils.getCommit(r, objectId); + String patch = JGitUtils.getCommitPatch(r, commit, blobPath); + add(new Label("patchText", patch)); + r.close(); + } +} diff --git a/src/com/gitblit/wicket/pages/RawPage.html b/src/com/gitblit/wicket/pages/RawPage.html new file mode 100644 index 00000000..30dd3168 --- /dev/null +++ b/src/com/gitblit/wicket/pages/RawPage.html @@ -0,0 +1,13 @@ + + + + + + +
[raw content]
+ + + \ No newline at end of file diff --git a/src/com/gitblit/wicket/pages/RawPage.java b/src/com/gitblit/wicket/pages/RawPage.java new file mode 100644 index 00000000..18b91bcb --- /dev/null +++ b/src/com/gitblit/wicket/pages/RawPage.java @@ -0,0 +1,91 @@ +package com.gitblit.wicket.pages; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.wicket.Component; +import org.apache.wicket.PageParameters; +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.protocol.http.servlet.ServletWebRequest; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; + +import com.gitblit.StoredSettings; +import com.gitblit.utils.JGitUtils; +import com.gitblit.wicket.GitBlitWebApp; +import com.gitblit.wicket.WicketUtils; + + +public class RawPage extends WebPage { + + public RawPage(PageParameters params) { + super(params); + + if (!params.containsKey("r")) { + error("Repository not specified!"); + redirectToInterceptPage(new RepositoriesPage()); + } + final String repositoryName = WicketUtils.getRepositoryName(params); + final String objectId = WicketUtils.getObject(params); + final String blobPath = WicketUtils.getPath(params); + + ServletWebRequest servletWebRequest = (ServletWebRequest) getRequest(); + HttpServletRequest req = servletWebRequest.getHttpServletRequest(); + req.getServerName(); + + Repository r = GitBlitWebApp.get().getRepository(req, repositoryName); + if (r == null) { + error("Can not load repository " + repositoryName); + redirectToInterceptPage(new RepositoriesPage()); + return; + } + + RevCommit commit = JGitUtils.getCommit(r, objectId); + + String extension = null; + if (blobPath.lastIndexOf('.') > -1) { + extension = blobPath.substring(blobPath.lastIndexOf('.') + 1); + } + + // Map the extensions to types + Map map = new HashMap(); + for (String ext : StoredSettings.getStrings("imageExtensions")) { + map.put(ext.toLowerCase(), 2); + } + for (String ext : StoredSettings.getStrings("binaryExtensions")) { + map.put(ext.toLowerCase(), 3); + } + + if (extension != null) { + int type = 0; + if (map.containsKey(extension)) { + type = map.get(extension); + } + Component c = null; + switch (type) { + case 2: + // TODO image blobs + c = new Label("rawText", "Image File"); + break; + case 3: + // TODO binary blobs + c = new Label("rawText", "Binary File"); + break; + default: + // plain text + c = new Label("rawText", JGitUtils.getRawContentAsString(r, commit, blobPath)); + WicketUtils.setCssClass(c, "plainprint"); + } + add(c); + } else { + // plain text + Label blobLabel = new Label("rawText", JGitUtils.getRawContentAsString(r, commit, blobPath)); + WicketUtils.setCssClass(blobLabel, "plainprint"); + add(blobLabel); + } + r.close(); + } +} diff --git a/src/com/gitblit/wicket/pages/TagPage.html b/src/com/gitblit/wicket/pages/TagPage.html index 34c240cb..4eb3b4b4 100644 --- a/src/com/gitblit/wicket/pages/TagPage.html +++ b/src/com/gitblit/wicket/pages/TagPage.html @@ -11,7 +11,7 @@
[page links]
-
[shortlog header]
+
[shortlog header]
diff --git a/src/com/gitblit/wicket/pages/TagPage.java b/src/com/gitblit/wicket/pages/TagPage.java index 95be46ca..622c2b15 100644 --- a/src/com/gitblit/wicket/pages/TagPage.java +++ b/src/com/gitblit/wicket/pages/TagPage.java @@ -1,5 +1,7 @@ package com.gitblit.wicket.pages; +import java.util.List; + import org.apache.wicket.PageParameters; import org.apache.wicket.markup.html.basic.Label; import org.eclipse.jgit.lib.Repository; @@ -9,6 +11,7 @@ import com.gitblit.utils.JGitUtils; import com.gitblit.wicket.LinkPanel; import com.gitblit.wicket.RepositoryPage; import com.gitblit.wicket.WicketUtils; +import com.gitblit.wicket.models.RefModel; public class TagPage extends RepositoryPage { @@ -16,10 +19,18 @@ public class TagPage extends RepositoryPage { public TagPage(PageParameters params) { super(params); - Repository r = getRepository(); + Repository r = getRepository(); + List tags = JGitUtils.getTags(r, -1); RevCommit c = JGitUtils.getCommit(r, objectId); - - add(new LinkPanel("commit", "title", c.getName(), CommitPage.class, newCommitParameter())); + + String name = c.getName(); + for (RefModel tag:tags) { + if (tag.getName().equals(objectId)) { + name = tag.getDisplayName(); + } + } + + add(new LinkPanel("commit", "title", name, CommitPage.class, newCommitParameter())); add(new LinkPanel("tagId", "list", c.getName(), CommitPage.class, newCommitParameter(c.getName()))); add(new Label("tagAuthor", JGitUtils.getDisplayName(c.getAuthorIdent()))); diff --git a/src/com/gitblit/wicket/pages/TicGitPage.java b/src/com/gitblit/wicket/pages/TicGitPage.java index 2cfb7723..2a042a6d 100644 --- a/src/com/gitblit/wicket/pages/TicGitPage.java +++ b/src/com/gitblit/wicket/pages/TicGitPage.java @@ -9,11 +9,11 @@ import org.apache.wicket.markup.repeater.data.DataView; import org.apache.wicket.markup.repeater.data.ListDataProvider; import com.gitblit.utils.JGitUtils; -import com.gitblit.utils.TicGitTicket; import com.gitblit.wicket.GitBlitWebSession; import com.gitblit.wicket.LinkPanel; import com.gitblit.wicket.RepositoryPage; import com.gitblit.wicket.WicketUtils; +import com.gitblit.wicket.models.TicGitTicket; public class TicGitPage extends RepositoryPage { diff --git a/src/com/gitblit/wicket/pages/TicGitTicketPage.java b/src/com/gitblit/wicket/pages/TicGitTicketPage.java index a27c8ddf..fdd6ea0a 100644 --- a/src/com/gitblit/wicket/pages/TicGitTicketPage.java +++ b/src/com/gitblit/wicket/pages/TicGitTicketPage.java @@ -8,11 +8,11 @@ import org.apache.wicket.markup.repeater.data.ListDataProvider; import org.eclipse.jgit.lib.Repository; import com.gitblit.utils.JGitUtils; -import com.gitblit.utils.TicGitTicket; -import com.gitblit.utils.TicGitTicket.Comment; import com.gitblit.wicket.GitBlitWebSession; import com.gitblit.wicket.RepositoryPage; import com.gitblit.wicket.WicketUtils; +import com.gitblit.wicket.models.TicGitTicket; +import com.gitblit.wicket.models.TicGitTicket.Comment; public class TicGitTicketPage extends RepositoryPage { diff --git a/src/com/gitblit/wicket/pages/TreePage.html b/src/com/gitblit/wicket/pages/TreePage.html index 3eda96b7..85fbdfc8 100644 --- a/src/com/gitblit/wicket/pages/TreePage.html +++ b/src/com/gitblit/wicket/pages/TreePage.html @@ -12,7 +12,7 @@ diff --git a/src/com/gitblit/wicket/pages/TreePage.java b/src/com/gitblit/wicket/pages/TreePage.java index 90e0a65d..2bea987d 100644 --- a/src/com/gitblit/wicket/pages/TreePage.java +++ b/src/com/gitblit/wicket/pages/TreePage.java @@ -9,6 +9,7 @@ import org.apache.wicket.markup.html.panel.Fragment; import org.apache.wicket.markup.repeater.Item; import org.apache.wicket.markup.repeater.data.DataView; import org.apache.wicket.markup.repeater.data.ListDataProvider; +import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; @@ -34,7 +35,8 @@ public class TreePage extends RepositoryPage { // tree page links add(new Label("historyLink", getString("gb.history"))); - add(new Label("headLink", "HEAD")); + add(new BookmarkablePageLink("headLink", TreePage.class, WicketUtils.newPathParameter(repositoryName, Constants.HEAD, basePath))); + add(new LinkPanel("shortlog", "title", commit.getShortMessage(), CommitPage.class, newCommitParameter())); @@ -79,7 +81,7 @@ public class TreePage extends RepositoryPage { // links Fragment links = new Fragment("pathLinks", "blobLinks", this); links.add(new BookmarkablePageLink("view", BlobPage.class, WicketUtils.newPathParameter(repositoryName, entry.commitId, entry.path))); - links.add(new BookmarkablePageLink("raw", BlobPage.class).setEnabled(false)); + links.add(new BookmarkablePageLink("raw", RawPage.class, WicketUtils.newPathParameter(repositoryName, entry.commitId, entry.path))); links.add(new BookmarkablePageLink("history", BlobPage.class).setEnabled(false)); item.add(links); } diff --git a/src/com/gitblit/wicket/panels/LogPanel.java b/src/com/gitblit/wicket/panels/LogPanel.java index ccc46fd3..8ef1e3e7 100644 --- a/src/com/gitblit/wicket/panels/LogPanel.java +++ b/src/com/gitblit/wicket/panels/LogPanel.java @@ -19,7 +19,7 @@ import com.gitblit.utils.JGitUtils; import com.gitblit.wicket.LinkPanel; import com.gitblit.wicket.WicketUtils; import com.gitblit.wicket.pages.CommitPage; -import com.gitblit.wicket.pages.DiffPage; +import com.gitblit.wicket.pages.CommitDiffPage; import com.gitblit.wicket.pages.LogPage; import com.gitblit.wicket.pages.SummaryPage; import com.gitblit.wicket.pages.TreePage; @@ -82,7 +82,7 @@ public class LogPanel extends BasePanel { item.add(new RefsPanel("commitRefs", repositoryName, entry, allRefs)); item.add(new BookmarkablePageLink("view", CommitPage.class, WicketUtils.newObjectParameter(repositoryName, entry.getName()))); - item.add(new BookmarkablePageLink("diff", DiffPage.class, WicketUtils.newObjectParameter(repositoryName, entry.getName()))); + item.add(new BookmarkablePageLink("diff", CommitDiffPage.class, WicketUtils.newObjectParameter(repositoryName, entry.getName()))); item.add(new BookmarkablePageLink("tree", TreePage.class, WicketUtils.newObjectParameter(repositoryName, entry.getName()))); WicketUtils.setAlternatingBackground(item, counter); diff --git a/src/com/gitblit/wicket/resources/gitblit.css b/src/com/gitblit/wicket/resources/gitblit.css index c0895607..da0c317e 100644 --- a/src/com/gitblit/wicket/resources/gitblit.css +++ b/src/com/gitblit/wicket/resources/gitblit.css @@ -229,9 +229,9 @@ div.diff.header { background-color: #EDECE6; border-color: #D9D8D1; border-style: solid; - border-width: 1px 0; + border-width: 1px; font-weight: bold; - margin-top: 4px; + margin-top: 10px; padding: 4px 0 2px; } @@ -241,17 +241,17 @@ div.diff.extended_header { font-family: inherit; } -div.diff.add { +span.diff.add { color: #008800; font-family: inherit; } -div.diff.remove { +span.diff.remove { color: #cc0000; font-family: inherit; } -div.diff.unchanged { +span.diff.unchanged { color: inherit; font-family: inherit; } @@ -425,11 +425,12 @@ span .tagRef a span, span .headRef a span, span .remoteRef a span, span .otherRe span .tagRef a, span .headRef a, span .remoteRef a, span .otherRef a { text-decoration: none; - color: inherit; + color: black; } span .tagRef a:hover, span .headRef a:hover, span .remoteRef a:hover, span .otherRef a:hover { - color: inherit; + color: black; + text-decoration: underline; } span .otherRef { -- 2.39.5