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.0KB

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