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 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. 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", GitBlit.self().processPlainCommitMessage(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. public void populateItem(final Item<PathChangeModel> item) {
  107. final PathChangeModel entry = item.getModelObject();
  108. Label changeType = new Label("changeType", "");
  109. WicketUtils.setChangeTypeCssClass(changeType, entry.changeType);
  110. setChangeTypeTooltip(changeType, entry.changeType);
  111. item.add(changeType);
  112. item.add(new DiffStatPanel("diffStat", entry.insertions, entry.deletions, true));
  113. boolean hasSubmodule = false;
  114. String submodulePath = null;
  115. if (entry.isTree()) {
  116. // tree
  117. item.add(new LinkPanel("pathName", null, entry.path, TreePage.class,
  118. WicketUtils
  119. .newPathParameter(repositoryName, entry.commitId, entry.path)));
  120. } else if (entry.isSubmodule()) {
  121. // submodule
  122. String submoduleId = entry.objectId;
  123. SubmoduleModel submodule = getSubmodule(entry.path);
  124. submodulePath = submodule.gitblitPath;
  125. hasSubmodule = submodule.hasSubmodule;
  126. // add relative link
  127. item.add(new LinkPanel("pathName", "list", entry.path + " @ " + getShortObjectId(submoduleId), "#" + entry.path));
  128. } else {
  129. // add relative link
  130. item.add(new LinkPanel("pathName", "list", entry.path, "#" + entry.path));
  131. }
  132. // quick links
  133. if (entry.isSubmodule()) {
  134. // submodule
  135. item.add(new ExternalLink("patch", "").setEnabled(false));
  136. item.add(new BookmarkablePageLink<Void>("view", CommitPage.class, WicketUtils
  137. .newObjectParameter(submodulePath, entry.objectId)).setEnabled(hasSubmodule));
  138. item.add(new ExternalLink("blame", "").setEnabled(false));
  139. item.add(new BookmarkablePageLink<Void>("history", HistoryPage.class, WicketUtils
  140. .newPathParameter(repositoryName, entry.commitId, entry.path))
  141. .setEnabled(!entry.changeType.equals(ChangeType.ADD)));
  142. } else {
  143. // tree or blob
  144. item.add(new BookmarkablePageLink<Void>("patch", PatchPage.class, WicketUtils
  145. .newPathParameter(repositoryName, entry.commitId, entry.path))
  146. .setEnabled(!entry.changeType.equals(ChangeType.ADD)
  147. && !entry.changeType.equals(ChangeType.DELETE)));
  148. item.add(new BookmarkablePageLink<Void>("view", BlobPage.class, WicketUtils
  149. .newPathParameter(repositoryName, entry.commitId, entry.path))
  150. .setEnabled(!entry.changeType.equals(ChangeType.DELETE)));
  151. item.add(new BookmarkablePageLink<Void>("blame", BlamePage.class, WicketUtils
  152. .newPathParameter(repositoryName, entry.commitId, entry.path))
  153. .setEnabled(!entry.changeType.equals(ChangeType.ADD)
  154. && !entry.changeType.equals(ChangeType.DELETE)));
  155. item.add(new BookmarkablePageLink<Void>("history", HistoryPage.class, WicketUtils
  156. .newPathParameter(repositoryName, entry.commitId, entry.path))
  157. .setEnabled(!entry.changeType.equals(ChangeType.ADD)));
  158. }
  159. WicketUtils.setAlternatingBackground(item, counter);
  160. counter++;
  161. }
  162. };
  163. add(pathsView);
  164. add(new Label("diffText", diff.content).setEscapeModelStrings(false));
  165. }
  166. @Override
  167. protected String getPageName() {
  168. return getString("gb.commitdiff");
  169. }
  170. @Override
  171. protected Class<? extends BasePage> getRepoNavPageClass() {
  172. return LogPage.class;
  173. }
  174. }