|
| 1 | +/* |
| 2 | + * Copyright 2011 gitblit.com. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.gitblit.wicket.pages; |
| 17 | + |
| 18 | +import java.awt.Color; |
| 19 | +import java.text.DateFormat; |
| 20 | +import java.text.MessageFormat; |
| 21 | +import java.text.SimpleDateFormat; |
| 22 | +import java.util.Comparator; |
| 23 | +import java.util.Date; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.TreeSet; |
| 29 | + |
| 30 | +import org.apache.wicket.Component; |
| 31 | +import org.apache.wicket.PageParameters; |
| 32 | +import org.apache.wicket.behavior.SimpleAttributeModifier; |
| 33 | +import org.apache.wicket.markup.html.basic.Label; |
| 34 | +import org.apache.wicket.markup.html.link.BookmarkablePageLink; |
| 35 | +import org.apache.wicket.markup.repeater.Item; |
| 36 | +import org.apache.wicket.markup.repeater.data.DataView; |
| 37 | +import org.apache.wicket.markup.repeater.data.ListDataProvider; |
| 38 | +import org.eclipse.jgit.lib.ObjectId; |
| 39 | +import org.eclipse.jgit.revwalk.RevCommit; |
| 40 | + |
| 41 | +import com.gitblit.Keys; |
| 42 | +import com.gitblit.models.AnnotatedLine; |
| 43 | +import com.gitblit.models.PathModel; |
| 44 | +import com.gitblit.utils.ColorFactory; |
| 45 | +import com.gitblit.utils.DiffUtils; |
| 46 | +import com.gitblit.utils.JGitUtils; |
| 47 | +import com.gitblit.utils.StringUtils; |
| 48 | +import com.gitblit.wicket.CacheControl; |
| 49 | +import com.gitblit.wicket.CacheControl.LastModified; |
| 50 | +import com.gitblit.wicket.WicketUtils; |
| 51 | +import com.gitblit.wicket.panels.CommitHeaderPanel; |
| 52 | +import com.gitblit.wicket.panels.LinkPanel; |
| 53 | +import com.gitblit.wicket.panels.PathBreadcrumbsPanel; |
| 54 | + |
| 55 | +@CacheControl(LastModified.BOOT) |
| 56 | +public class BlamePage extends RepositoryPage { |
| 57 | + |
| 58 | + /** |
| 59 | + * The different types of Blame visualizations. |
| 60 | + */ |
| 61 | + private enum BlameType { |
| 62 | + COMMIT, |
| 63 | + |
| 64 | + AUTHOR, |
| 65 | + |
| 66 | + AGE; |
| 67 | + |
| 68 | + private BlameType() { |
| 69 | + } |
| 70 | + |
| 71 | + public static BlameType get(String name) { |
| 72 | + for (BlameType blameType : BlameType.values()) { |
| 73 | + if (blameType.name().equalsIgnoreCase(name)) { |
| 74 | + return blameType; |
| 75 | + } |
| 76 | + } |
| 77 | + throw new IllegalArgumentException("Unknown Blame Type [" + name |
| 78 | + + "]"); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public String toString() { |
| 83 | + return name().toLowerCase(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public BlamePage(PageParameters params) { |
| 88 | + super(params); |
| 89 | + |
| 90 | + final String blobPath = WicketUtils.getPath(params); |
| 91 | + |
| 92 | + final String blameTypeParam = params.getString("blametype", BlameType.COMMIT.toString()); |
| 93 | + final BlameType activeBlameType = BlameType.get(blameTypeParam); |
| 94 | + |
| 95 | + RevCommit commit = getCommit(); |
| 96 | + |
| 97 | + add(new BookmarkablePageLink<Void>("blobLink", BlobPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath))); |
| 98 | + add(new BookmarkablePageLink<Void>("commitLink", CommitPage.class, |
| 99 | + WicketUtils.newObjectParameter(repositoryName, objectId))); |
| 100 | + add(new BookmarkablePageLink<Void>("commitDiffLink", CommitDiffPage.class, |
| 101 | + WicketUtils.newObjectParameter(repositoryName, objectId))); |
| 102 | + |
| 103 | + // blame page links |
| 104 | + add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class, |
| 105 | + WicketUtils.newPathParameter(repositoryName, objectId, blobPath))); |
| 106 | + |
| 107 | + // "Blame by" links |
| 108 | + for (BlameType type : BlameType.values()) { |
| 109 | + String typeString = type.toString(); |
| 110 | + PageParameters blameTypePageParam = |
| 111 | + WicketUtils.newBlameTypeParameter(repositoryName, commit.getName(), |
| 112 | + WicketUtils.getPath(params), typeString); |
| 113 | + |
| 114 | + String blameByLinkText = "blameBy" |
| 115 | + + Character.toUpperCase(typeString.charAt(0)) + typeString.substring(1) |
| 116 | + + "Link"; |
| 117 | + BookmarkablePageLink<Void> blameByPageLink = |
| 118 | + new BookmarkablePageLink<Void>(blameByLinkText, BlamePage.class, blameTypePageParam); |
| 119 | + |
| 120 | + if (activeBlameType == type) { |
| 121 | + blameByPageLink.add(new SimpleAttributeModifier("style", "font-weight:bold;")); |
| 122 | + } |
| 123 | + |
| 124 | + add(blameByPageLink); |
| 125 | + } |
| 126 | + |
| 127 | + add(new CommitHeaderPanel("commitHeader", repositoryName, commit)); |
| 128 | + |
| 129 | + add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId)); |
| 130 | + |
| 131 | + String format = app().settings().getString(Keys.web.datetimestampLongFormat, |
| 132 | + "EEEE, MMMM d, yyyy HH:mm Z"); |
| 133 | + final DateFormat df = new SimpleDateFormat(format); |
| 134 | + df.setTimeZone(getTimeZone()); |
| 135 | + |
| 136 | + PathModel pathModel = null; |
| 137 | + List<PathModel> paths = JGitUtils.getFilesInPath(getRepository(), StringUtils.getRootPath(blobPath), commit); |
| 138 | + for (PathModel path : paths) { |
| 139 | + if (path.path.equals(blobPath)) { |
| 140 | + pathModel = path; |
| 141 | + break; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if (pathModel == null) { |
| 146 | + final String notFound = MessageFormat.format("Blame page failed to find {0} in {1} @ {2}", blobPath, repositoryName, objectId); |
| 147 | + logger().error(notFound); |
| 148 | + add(new Label("annotation").setVisible(false)); |
| 149 | + add(new Label("missingBlob", missingBlob(blobPath, commit)).setEscapeModelStrings(false)); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + add(new Label("missingBlob").setVisible(false)); |
| 154 | + |
| 155 | + final int tabLength = app().settings().getInteger(Keys.web.tabLength, 4); |
| 156 | + List<AnnotatedLine> lines = DiffUtils.blame(getRepository(), blobPath, objectId); |
| 157 | + final Map<?, String> colorMap = initializeColors(activeBlameType, lines); |
| 158 | + ListDataProvider<AnnotatedLine> blameDp = new ListDataProvider<AnnotatedLine>(lines); |
| 159 | + DataView<AnnotatedLine> blameView = new DataView<AnnotatedLine>("annotation", blameDp) { |
| 160 | + private static final long serialVersionUID = 1L; |
| 161 | + private String lastCommitId = ""; |
| 162 | + private boolean showInitials = true; |
| 163 | + private final String zeroId = ObjectId.zeroId().getName(); |
| 164 | + |
| 165 | + @Override |
| 166 | + public void populateItem(final Item<AnnotatedLine> item) { |
| 167 | + final AnnotatedLine entry = item.getModelObject(); |
| 168 | + |
| 169 | + // commit id and author |
| 170 | + if (!lastCommitId.equals(entry.commitId)) { |
| 171 | + lastCommitId = entry.commitId; |
| 172 | + if (zeroId.equals(entry.commitId)) { |
| 173 | + // unknown commit |
| 174 | + item.add(new Label("commit", "<?>")); |
| 175 | + showInitials = false; |
| 176 | + } else { |
| 177 | + // show the link for first line |
| 178 | + LinkPanel commitLink = new LinkPanel("commit", null, |
| 179 | + getShortObjectId(entry.commitId), CommitPage.class, |
| 180 | + newCommitParameter(entry.commitId)); |
| 181 | + WicketUtils.setHtmlTooltip(commitLink, |
| 182 | + MessageFormat.format("{0}, {1}", entry.author, df.format(entry.when))); |
| 183 | + item.add(commitLink); |
| 184 | + WicketUtils.setCssStyle(item, "border-top: 1px solid #ddd;"); |
| 185 | + showInitials = true; |
| 186 | + } |
| 187 | + } else { |
| 188 | + if (showInitials) { |
| 189 | + showInitials = false; |
| 190 | + // show author initials |
| 191 | + item.add(new Label("commit", getInitials(entry.author))); |
| 192 | + } else { |
| 193 | + // hide the commit link until the next block |
| 194 | + item.add(new Label("commit").setVisible(false)); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + // line number |
| 199 | + item.add(new Label("line", "" + entry.lineNumber)); |
| 200 | + |
| 201 | + // line content |
| 202 | + String color; |
| 203 | + switch (activeBlameType) { |
| 204 | + case AGE: |
| 205 | + color = colorMap.get(entry.when); |
| 206 | + break; |
| 207 | + case AUTHOR: |
| 208 | + color = colorMap.get(entry.author); |
| 209 | + break; |
| 210 | + default: |
| 211 | + color = colorMap.get(entry.commitId); |
| 212 | + break; |
| 213 | + } |
| 214 | + Component data = new Label("data", StringUtils.escapeForHtml(entry.data, true, tabLength)).setEscapeModelStrings(false); |
| 215 | + data.add(new SimpleAttributeModifier("style", "background-color: " + color + ";")); |
| 216 | + item.add(data); |
| 217 | + } |
| 218 | + }; |
| 219 | + add(blameView); |
| 220 | + } |
| 221 | + |
| 222 | + private String getInitials(String author) { |
| 223 | + StringBuilder sb = new StringBuilder(); |
| 224 | + String[] chunks = author.split(" "); |
| 225 | + for (String chunk : chunks) { |
| 226 | + sb.append(chunk.charAt(0)); |
| 227 | + } |
| 228 | + return sb.toString().toUpperCase(); |
| 229 | + } |
| 230 | + |
| 231 | + @Override |
| 232 | + protected String getPageName() { |
| 233 | + return getString("gb.blame"); |
| 234 | + } |
| 235 | + |
| 236 | + @Override |
| 237 | + protected boolean isCommitPage() { |
| 238 | + return true; |
| 239 | + } |
| 240 | + |
| 241 | + @Override |
| 242 | + protected Class<? extends BasePage> getRepoNavPageClass() { |
| 243 | + return TreePage.class; |
| 244 | + } |
| 245 | + |
| 246 | + protected String missingBlob(String blobPath, RevCommit commit) { |
| 247 | + StringBuilder sb = new StringBuilder(); |
| 248 | + sb.append("<div class=\"alert alert-error\">"); |
| 249 | + String pattern = getString("gb.doesNotExistInTree").replace("{0}", "<b>{0}</b>").replace("{1}", "<b>{1}</b>"); |
| 250 | + sb.append(MessageFormat.format(pattern, blobPath, commit.getTree().getId().getName())); |
| 251 | + sb.append("</div>"); |
| 252 | + return sb.toString(); |
| 253 | + } |
| 254 | + |
| 255 | + private Map<?, String> initializeColors(BlameType blameType, List<AnnotatedLine> lines) { |
| 256 | + ColorFactory colorFactory = new ColorFactory(); |
| 257 | + Map<?, String> colorMap; |
| 258 | + |
| 259 | + if (BlameType.AGE == blameType) { |
| 260 | + Set<Date> keys = new TreeSet<Date>(new Comparator<Date>() { |
| 261 | + @Override |
| 262 | + public int compare(Date o1, Date o2) { |
| 263 | + // younger code has a brighter, older code lightens to white |
| 264 | + return o1.compareTo(o2); |
| 265 | + } |
| 266 | + }); |
| 267 | + |
| 268 | + for (AnnotatedLine line : lines) { |
| 269 | + keys.add(line.when); |
| 270 | + } |
| 271 | + |
| 272 | + // TODO consider making this a setting |
| 273 | + colorMap = colorFactory.getGraduatedColorMap(keys, Color.decode("#FFA63A")); |
| 274 | + } else { |
| 275 | + Set<String> keys = new HashSet<String>(); |
| 276 | + |
| 277 | + for (AnnotatedLine line : lines) { |
| 278 | + if (blameType == BlameType.AUTHOR) { |
| 279 | + keys.add(line.author); |
| 280 | + } else { |
| 281 | + keys.add(line.commitId); |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + colorMap = colorFactory.getRandomColorMap(keys); |
| 286 | + } |
| 287 | + |
| 288 | + return colorMap; |
| 289 | + } |
| 290 | +} |
0 commit comments