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.GitBlit;
  32. import com.gitblit.models.GitNote;
  33. import com.gitblit.models.PathModel.PathChangeModel;
  34. import com.gitblit.models.SubmoduleModel;
  35. import com.gitblit.utils.DiffUtils;
  36. import com.gitblit.utils.DiffUtils.DiffOutput;
  37. import com.gitblit.utils.DiffUtils.DiffOutputType;
  38. import com.gitblit.utils.JGitUtils;
  39. import com.gitblit.wicket.CacheControl;
  40. import com.gitblit.wicket.CacheControl.LastModified;
  41. import com.gitblit.wicket.WicketUtils;
  42. import com.gitblit.wicket.panels.CommitHeaderPanel;
  43. import com.gitblit.wicket.panels.CommitLegendPanel;
  44. import com.gitblit.wicket.panels.DiffStatPanel;
  45. import com.gitblit.wicket.panels.GravatarImage;
  46. import com.gitblit.wicket.panels.LinkPanel;
  47. import com.gitblit.wicket.panels.RefsPanel;
  48. @CacheControl(LastModified.BOOT)
  49. public class CommitDiffPage extends RepositoryPage {
  50. public CommitDiffPage(PageParameters params) {
  51. super(params);
  52. Repository r = getRepository();
  53. RevCommit commit = getCommit();
  54. final DiffOutput diff = DiffUtils.getCommitDiff(r, commit, DiffOutputType.HTML);
  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. // add commit diffstat
  74. int insertions = 0;
  75. int deletions = 0;
  76. for (PathChangeModel pcm : diff.stat.paths) {
  77. insertions += pcm.insertions;
  78. deletions += pcm.deletions;
  79. }
  80. add(new DiffStatPanel("diffStat", insertions, deletions));
  81. addFullText("fullMessage", commit.getFullMessage());
  82. // git notes
  83. List<GitNote> notes = JGitUtils.getNotesOnCommit(r, commit);
  84. ListDataProvider<GitNote> notesDp = new ListDataProvider<GitNote>(notes);
  85. DataView<GitNote> notesView = new DataView<GitNote>("notes", notesDp) {
  86. private static final long serialVersionUID = 1L;
  87. @Override
  88. public void populateItem(final Item<GitNote> item) {
  89. GitNote entry = item.getModelObject();
  90. item.add(new RefsPanel("refName", repositoryName, Arrays.asList(entry.notesRef)));
  91. item.add(createPersonPanel("authorName", entry.notesRef.getAuthorIdent(),
  92. Constants.SearchType.AUTHOR));
  93. item.add(new GravatarImage("noteAuthorAvatar", entry.notesRef.getAuthorIdent()));
  94. item.add(WicketUtils.createTimestampLabel("authorDate", entry.notesRef
  95. .getAuthorIdent().getWhen(), getTimeZone(), getTimeUtils()));
  96. item.add(new Label("noteContent", GitBlit.self().processPlainCommitMessage(repositoryName,
  97. entry.content)).setEscapeModelStrings(false));
  98. }
  99. };
  100. add(notesView.setVisible(notes.size() > 0));
  101. // changed paths list
  102. add(new CommitLegendPanel("commitLegend", diff.stat.paths));
  103. ListDataProvider<PathChangeModel> pathsDp = new ListDataProvider<PathChangeModel>(diff.stat.paths);
  104. DataView<PathChangeModel> pathsView = new DataView<PathChangeModel>("changedPath", pathsDp) {
  105. private static final long serialVersionUID = 1L;
  106. int counter;
  107. @Override
  108. public void populateItem(final Item<PathChangeModel> item) {
  109. final PathChangeModel entry = item.getModelObject();
  110. Label changeType = new Label("changeType", "");
  111. WicketUtils.setChangeTypeCssClass(changeType, entry.changeType);
  112. setChangeTypeTooltip(changeType, entry.changeType);
  113. item.add(changeType);
  114. item.add(new DiffStatPanel("diffStat", entry.insertions, entry.deletions, true));
  115. boolean hasSubmodule = false;
  116. String submodulePath = null;
  117. if (entry.isTree()) {
  118. // tree
  119. item.add(new LinkPanel("pathName", null, entry.path, TreePage.class,
  120. WicketUtils
  121. .newPathParameter(repositoryName, entry.commitId, entry.path)));
  122. } else if (entry.isSubmodule()) {
  123. // submodule
  124. String submoduleId = entry.objectId;
  125. SubmoduleModel submodule = getSubmodule(entry.path);
  126. submodulePath = submodule.gitblitPath;
  127. hasSubmodule = submodule.hasSubmodule;
  128. // add relative link
  129. item.add(new LinkPanel("pathName", "list", entry.path + " @ " + getShortObjectId(submoduleId), "#" + entry.path));
  130. } else {
  131. // add relative link
  132. item.add(new LinkPanel("pathName", "list", entry.path, "#" + entry.path));
  133. }
  134. // quick links
  135. if (entry.isSubmodule()) {
  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. }