You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BlamePage.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. import java.text.DateFormat;
  18. import java.text.MessageFormat;
  19. import java.text.SimpleDateFormat;
  20. import java.util.List;
  21. import org.apache.wicket.PageParameters;
  22. import org.apache.wicket.markup.html.basic.Label;
  23. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  24. import org.apache.wicket.markup.repeater.Item;
  25. import org.apache.wicket.markup.repeater.data.DataView;
  26. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  27. import org.eclipse.jgit.lib.Constants;
  28. import org.eclipse.jgit.lib.ObjectId;
  29. import org.eclipse.jgit.revwalk.RevCommit;
  30. import com.gitblit.GitBlit;
  31. import com.gitblit.Keys;
  32. import com.gitblit.models.AnnotatedLine;
  33. import com.gitblit.models.PathModel;
  34. import com.gitblit.utils.DiffUtils;
  35. import com.gitblit.utils.JGitUtils;
  36. import com.gitblit.utils.StringUtils;
  37. import com.gitblit.wicket.CacheControl;
  38. import com.gitblit.wicket.CacheControl.LastModified;
  39. import com.gitblit.wicket.WicketUtils;
  40. import com.gitblit.wicket.panels.CommitHeaderPanel;
  41. import com.gitblit.wicket.panels.LinkPanel;
  42. import com.gitblit.wicket.panels.PathBreadcrumbsPanel;
  43. @CacheControl(LastModified.BOOT)
  44. public class BlamePage extends RepositoryPage {
  45. public BlamePage(PageParameters params) {
  46. super(params);
  47. final String blobPath = WicketUtils.getPath(params);
  48. RevCommit commit = getCommit();
  49. add(new BookmarkablePageLink<Void>("blobLink", BlobPage.class,
  50. WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
  51. add(new BookmarkablePageLink<Void>("commitLink", CommitPage.class,
  52. WicketUtils.newObjectParameter(repositoryName, objectId)));
  53. add(new BookmarkablePageLink<Void>("commitDiffLink", CommitDiffPage.class,
  54. WicketUtils.newObjectParameter(repositoryName, objectId)));
  55. // blame page links
  56. add(new BookmarkablePageLink<Void>("headLink", BlamePage.class,
  57. WicketUtils.newPathParameter(repositoryName, Constants.HEAD, blobPath)));
  58. add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
  59. WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
  60. add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
  61. add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
  62. String format = GitBlit.getString(Keys.web.datetimestampLongFormat,
  63. "EEEE, MMMM d, yyyy HH:mm Z");
  64. final DateFormat df = new SimpleDateFormat(format);
  65. df.setTimeZone(getTimeZone());
  66. PathModel pathModel = null;
  67. List<PathModel> paths = JGitUtils.getFilesInPath(getRepository(), StringUtils.getRootPath(blobPath), commit);
  68. for (PathModel path : paths) {
  69. if (path.path.equals(blobPath)) {
  70. pathModel = path;
  71. break;
  72. }
  73. }
  74. if (pathModel == null) {
  75. add(new Label("annotation").setVisible(false));
  76. add(new Label("missingBlob", missingBlob(blobPath, commit)).setEscapeModelStrings(false));
  77. return;
  78. }
  79. add(new Label("missingBlob").setVisible(false));
  80. List<AnnotatedLine> lines = DiffUtils.blame(getRepository(), blobPath, objectId);
  81. ListDataProvider<AnnotatedLine> blameDp = new ListDataProvider<AnnotatedLine>(lines);
  82. DataView<AnnotatedLine> blameView = new DataView<AnnotatedLine>("annotation", blameDp) {
  83. private static final long serialVersionUID = 1L;
  84. private int count;
  85. private String lastCommitId = "";
  86. private boolean showInitials = true;
  87. private String zeroId = ObjectId.zeroId().getName();
  88. public void populateItem(final Item<AnnotatedLine> item) {
  89. AnnotatedLine entry = item.getModelObject();
  90. item.add(new Label("line", "" + entry.lineNumber));
  91. item.add(new Label("data", StringUtils.escapeForHtml(entry.data, true))
  92. .setEscapeModelStrings(false));
  93. if (!lastCommitId.equals(entry.commitId)) {
  94. lastCommitId = entry.commitId;
  95. count++;
  96. if (zeroId.equals(entry.commitId)) {
  97. // unknown commit
  98. item.add(new Label("commit", "<?>"));
  99. showInitials = false;
  100. } else {
  101. // show the link for first line
  102. LinkPanel commitLink = new LinkPanel("commit", null,
  103. getShortObjectId(entry.commitId), CommitPage.class,
  104. newCommitParameter(entry.commitId));
  105. WicketUtils.setHtmlTooltip(commitLink,
  106. MessageFormat.format("{0}, {1}", entry.author, df.format(entry.when)));
  107. item.add(commitLink);
  108. showInitials = true;
  109. }
  110. } else {
  111. if (showInitials) {
  112. showInitials = false;
  113. // show author initials
  114. item.add(new Label("commit", getInitials(entry.author)));
  115. } else {
  116. // hide the commit link until the next block
  117. item.add(new Label("commit").setVisible(false));
  118. }
  119. }
  120. if (count % 2 == 0) {
  121. WicketUtils.setCssClass(item, "even");
  122. } else {
  123. WicketUtils.setCssClass(item, "odd");
  124. }
  125. }
  126. };
  127. add(blameView);
  128. }
  129. private String getInitials(String author) {
  130. StringBuilder sb = new StringBuilder();
  131. String[] chunks = author.split(" ");
  132. for (String chunk : chunks) {
  133. sb.append(chunk.charAt(0));
  134. }
  135. return sb.toString().toUpperCase();
  136. }
  137. @Override
  138. protected String getPageName() {
  139. return getString("gb.blame");
  140. }
  141. @Override
  142. protected Class<? extends BasePage> getRepoNavPageClass() {
  143. return TreePage.class;
  144. }
  145. protected String missingBlob(String blobPath, RevCommit commit) {
  146. StringBuilder sb = new StringBuilder();
  147. sb.append("<div class=\"alert alert-error\">");
  148. String pattern = getString("gb.doesNotExistInTree").replace("{0}", "<b>{0}</b>").replace("{1}", "<b>{1}</b>");
  149. sb.append(MessageFormat.format(pattern, blobPath, commit.getTree().getId().getName()));
  150. sb.append("</div>");
  151. return sb.toString();
  152. }
  153. }