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.

CommitDiffPage.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import org.apache.wicket.PageParameters;
  21. import org.apache.wicket.markup.html.basic.Label;
  22. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  23. import org.apache.wicket.markup.html.link.ExternalLink;
  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.diff.DiffEntry.ChangeType;
  28. import org.eclipse.jgit.lib.Repository;
  29. import org.eclipse.jgit.revwalk.RevCommit;
  30. import com.gitblit.Constants;
  31. import com.gitblit.Keys;
  32. import com.gitblit.models.GitNote;
  33. import com.gitblit.models.PathModel.PathChangeModel;
  34. import com.gitblit.models.SubmoduleModel;
  35. import com.gitblit.servlet.RawServlet;
  36. import com.gitblit.utils.DiffUtils;
  37. import com.gitblit.utils.DiffUtils.DiffOutput;
  38. import com.gitblit.utils.DiffUtils.DiffOutputType;
  39. import com.gitblit.utils.JGitUtils;
  40. import com.gitblit.wicket.CacheControl;
  41. import com.gitblit.wicket.CacheControl.LastModified;
  42. import com.gitblit.wicket.WicketUtils;
  43. import com.gitblit.wicket.panels.CommitHeaderPanel;
  44. import com.gitblit.wicket.panels.CommitLegendPanel;
  45. import com.gitblit.wicket.panels.DiffStatPanel;
  46. import com.gitblit.wicket.panels.GravatarImage;
  47. import com.gitblit.wicket.panels.LinkPanel;
  48. import com.gitblit.wicket.panels.RefsPanel;
  49. @CacheControl(LastModified.BOOT)
  50. public class CommitDiffPage extends RepositoryPage {
  51. public CommitDiffPage(PageParameters params) {
  52. super(params);
  53. Repository r = getRepository();
  54. RevCommit commit = getCommit();
  55. List<String> parents = new ArrayList<String>();
  56. if (commit.getParentCount() > 0) {
  57. for (RevCommit parent : commit.getParents()) {
  58. parents.add(parent.name());
  59. }
  60. }
  61. // commit page links
  62. if (parents.size() == 0) {
  63. add(new Label("parentLink", getString("gb.none")));
  64. } else {
  65. add(new LinkPanel("parentLink", null, parents.get(0).substring(0, 8),
  66. CommitDiffPage.class, newCommitParameter(parents.get(0))));
  67. }
  68. add(new BookmarkablePageLink<Void>("patchLink", PatchPage.class,
  69. WicketUtils.newObjectParameter(repositoryName, objectId)));
  70. add(new BookmarkablePageLink<Void>("commitLink", CommitPage.class,
  71. WicketUtils.newObjectParameter(repositoryName, objectId)));
  72. add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
  73. final List<String> imageExtensions = app().settings().getStrings(Keys.web.imageExtensions);
  74. final ImageDiffHandler handler = new ImageDiffHandler(this, repositoryName,
  75. parents.isEmpty() ? null : parents.get(0), commit.getName(), imageExtensions);
  76. final DiffOutput diff = DiffUtils.getCommitDiff(r, commit, DiffOutputType.HTML, handler);
  77. if (handler.getImgDiffCount() > 0) {
  78. addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs
  79. }
  80. // add commit diffstat
  81. int insertions = 0;
  82. int deletions = 0;
  83. for (PathChangeModel pcm : diff.stat.paths) {
  84. insertions += pcm.insertions;
  85. deletions += pcm.deletions;
  86. }
  87. add(new DiffStatPanel("diffStat", insertions, deletions));
  88. addFullText("fullMessage", commit.getFullMessage());
  89. // git notes
  90. List<GitNote> notes = JGitUtils.getNotesOnCommit(r, commit);
  91. ListDataProvider<GitNote> notesDp = new ListDataProvider<GitNote>(notes);
  92. DataView<GitNote> notesView = new DataView<GitNote>("notes", notesDp) {
  93. private static final long serialVersionUID = 1L;
  94. @Override
  95. public void populateItem(final Item<GitNote> item) {
  96. GitNote entry = item.getModelObject();
  97. item.add(new RefsPanel("refName", repositoryName, Arrays.asList(entry.notesRef)));
  98. item.add(createPersonPanel("authorName", entry.notesRef.getAuthorIdent(),
  99. Constants.SearchType.AUTHOR));
  100. item.add(new GravatarImage("noteAuthorAvatar", entry.notesRef.getAuthorIdent()));
  101. item.add(WicketUtils.createTimestampLabel("authorDate", entry.notesRef
  102. .getAuthorIdent().getWhen(), getTimeZone(), getTimeUtils()));
  103. item.add(new Label("noteContent", bugtraqProcessor().processPlainCommitMessage(getRepository(), repositoryName,
  104. entry.content)).setEscapeModelStrings(false));
  105. }
  106. };
  107. add(notesView.setVisible(notes.size() > 0));
  108. // changed paths list
  109. add(new CommitLegendPanel("commitLegend", diff.stat.paths));
  110. ListDataProvider<PathChangeModel> pathsDp = new ListDataProvider<PathChangeModel>(diff.stat.paths);
  111. DataView<PathChangeModel> pathsView = new DataView<PathChangeModel>("changedPath", pathsDp) {
  112. private static final long serialVersionUID = 1L;
  113. int counter;
  114. @Override
  115. public void populateItem(final Item<PathChangeModel> item) {
  116. final PathChangeModel entry = item.getModelObject();
  117. Label changeType = new Label("changeType", "");
  118. WicketUtils.setChangeTypeCssClass(changeType, entry.changeType);
  119. setChangeTypeTooltip(changeType, entry.changeType);
  120. item.add(changeType);
  121. item.add(new DiffStatPanel("diffStat", entry.insertions, entry.deletions, true));
  122. boolean hasSubmodule = false;
  123. String submodulePath = null;
  124. if (entry.isTree()) {
  125. // tree
  126. item.add(new LinkPanel("pathName", null, entry.path, TreePage.class,
  127. WicketUtils
  128. .newPathParameter(repositoryName, entry.commitId, entry.path)));
  129. } else if (entry.isSubmodule()) {
  130. // submodule
  131. String submoduleId = entry.objectId;
  132. SubmoduleModel submodule = getSubmodule(entry.path);
  133. submodulePath = submodule.gitblitPath;
  134. hasSubmodule = submodule.hasSubmodule;
  135. // add relative link
  136. item.add(new LinkPanel("pathName", "list", entry.path + " @ " + getShortObjectId(submoduleId), "#n" + entry.objectId));
  137. } else {
  138. // add relative link
  139. item.add(new LinkPanel("pathName", "list", entry.path, "#n" + entry.objectId));
  140. }
  141. // quick links
  142. if (entry.isSubmodule()) {
  143. item.add(new ExternalLink("raw", "").setEnabled(false));
  144. // submodule
  145. item.add(new ExternalLink("patch", "").setEnabled(false));
  146. item.add(new BookmarkablePageLink<Void>("view", CommitPage.class, WicketUtils
  147. .newObjectParameter(submodulePath, entry.objectId)).setEnabled(hasSubmodule));
  148. item.add(new ExternalLink("blame", "").setEnabled(false));
  149. item.add(new BookmarkablePageLink<Void>("history", HistoryPage.class, WicketUtils
  150. .newPathParameter(repositoryName, entry.commitId, entry.path))
  151. .setEnabled(!entry.changeType.equals(ChangeType.ADD)));
  152. } else {
  153. // tree or blob
  154. item.add(new BookmarkablePageLink<Void>("patch", PatchPage.class, WicketUtils
  155. .newPathParameter(repositoryName, entry.commitId, entry.path))
  156. .setEnabled(!entry.changeType.equals(ChangeType.ADD)
  157. && !entry.changeType.equals(ChangeType.DELETE)));
  158. item.add(new BookmarkablePageLink<Void>("view", BlobPage.class, WicketUtils
  159. .newPathParameter(repositoryName, entry.commitId, entry.path))
  160. .setEnabled(!entry.changeType.equals(ChangeType.DELETE)));
  161. String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, entry.commitId, entry.path);
  162. item.add(new ExternalLink("raw", rawUrl)
  163. .setEnabled(!entry.changeType.equals(ChangeType.DELETE)));
  164. item.add(new BookmarkablePageLink<Void>("blame", BlamePage.class, WicketUtils
  165. .newPathParameter(repositoryName, entry.commitId, entry.path))
  166. .setEnabled(!entry.changeType.equals(ChangeType.ADD)
  167. && !entry.changeType.equals(ChangeType.DELETE)));
  168. item.add(new BookmarkablePageLink<Void>("history", HistoryPage.class, WicketUtils
  169. .newPathParameter(repositoryName, entry.commitId, entry.path))
  170. .setEnabled(!entry.changeType.equals(ChangeType.ADD)));
  171. }
  172. WicketUtils.setAlternatingBackground(item, counter);
  173. counter++;
  174. }
  175. };
  176. add(pathsView);
  177. add(new Label("diffText", diff.content).setEscapeModelStrings(false));
  178. }
  179. @Override
  180. protected String getPageName() {
  181. return getString("gb.commitdiff");
  182. }
  183. @Override
  184. protected boolean isCommitPage() {
  185. return true;
  186. }
  187. @Override
  188. protected Class<? extends BasePage> getRepoNavPageClass() {
  189. return LogPage.class;
  190. }
  191. }