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.

RefsPanel.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.panels;
  17. import java.text.MessageFormat;
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.Comparator;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.apache.wicket.Component;
  24. import org.apache.wicket.markup.html.WebPage;
  25. import org.apache.wicket.markup.html.basic.Label;
  26. import org.apache.wicket.markup.repeater.Item;
  27. import org.apache.wicket.markup.repeater.data.DataView;
  28. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  29. import org.eclipse.jgit.lib.ObjectId;
  30. import org.eclipse.jgit.revwalk.RevCommit;
  31. import com.gitblit.Constants;
  32. import com.gitblit.models.RefModel;
  33. import com.gitblit.models.RepositoryModel;
  34. import com.gitblit.utils.StringUtils;
  35. import com.gitblit.wicket.WicketUtils;
  36. import com.gitblit.wicket.pages.CommitPage;
  37. import com.gitblit.wicket.pages.LogPage;
  38. import com.gitblit.wicket.pages.TagPage;
  39. import com.gitblit.wicket.pages.TicketsPage;
  40. public class RefsPanel extends BasePanel {
  41. private static final long serialVersionUID = 1L;
  42. public RefsPanel(String id, final String repositoryName, RevCommit c,
  43. Map<ObjectId, List<RefModel>> refs) {
  44. this(id, repositoryName, refs.get(c.getId()));
  45. }
  46. public RefsPanel(String id, final String repositoryName, List<RefModel> refs) {
  47. super(id);
  48. if (refs == null) {
  49. refs = new ArrayList<RefModel>();
  50. }
  51. Collections.sort(refs, new Comparator<RefModel>() {
  52. @Override
  53. public int compare(RefModel o1, RefModel o2) {
  54. // sort remote heads last, otherwise sort by name
  55. // this is so we can insert a break on the refs panel
  56. // [head][branch][branch][tag][tag]
  57. // [remote][remote][remote]
  58. boolean remote1 = o1.displayName.startsWith(Constants.R_REMOTES);
  59. boolean remote2 = o2.displayName.startsWith(Constants.R_REMOTES);
  60. if (remote1 && remote2) {
  61. // both are remote heads, sort by name
  62. return o1.displayName.compareTo(o2.displayName);
  63. }
  64. if (remote1) {
  65. // o1 is remote, o2 comes first
  66. return 1;
  67. }
  68. if (remote2) {
  69. // remote is o2, o1 comes first
  70. return -1;
  71. }
  72. // standard sort
  73. return o1.displayName.compareTo(o2.displayName);
  74. }
  75. });
  76. // count remote and determine if we should insert a break
  77. int remoteCount = 0;
  78. for (RefModel ref : refs) {
  79. if (ref.displayName.startsWith(Constants.R_REMOTES)) {
  80. remoteCount++;
  81. }
  82. }
  83. final boolean shouldBreak = remoteCount < refs.size();
  84. RepositoryModel repository = app().repositories().getRepositoryModel(repositoryName);
  85. final boolean hasTickets = app().tickets().hasTickets(repository);
  86. ListDataProvider<RefModel> refsDp = new ListDataProvider<RefModel>(refs);
  87. DataView<RefModel> refsView = new DataView<RefModel>("ref", refsDp) {
  88. private static final long serialVersionUID = 1L;
  89. private boolean alreadyInsertedBreak = !shouldBreak;
  90. @Override
  91. public void populateItem(final Item<RefModel> item) {
  92. RefModel entry = item.getModelObject();
  93. String name = entry.displayName;
  94. String objectid = entry.getReferencedObjectId().getName();
  95. boolean breakLine = false;
  96. Class<? extends WebPage> linkClass = CommitPage.class;
  97. String cssClass = "";
  98. String tooltip = "";
  99. if (name.startsWith(Constants.R_TICKET)) {
  100. // Gitblit ticket ref
  101. objectid = name.substring(Constants.R_TICKET.length());
  102. name = name.substring(Constants.R_HEADS.length());
  103. linkClass = TicketsPage.class;
  104. cssClass = "localBranch";
  105. } else if (name.startsWith(Constants.R_HEADS)) {
  106. // local branch
  107. linkClass = LogPage.class;
  108. name = name.substring(Constants.R_HEADS.length());
  109. cssClass = "localBranch";
  110. } else if (name.equals(Constants.HEAD)) {
  111. // local head
  112. linkClass = LogPage.class;
  113. cssClass = "headRef";
  114. } else if (name.startsWith(Constants.R_CHANGES)) {
  115. // Gitblit change ref
  116. name = name.substring(Constants.R_CHANGES.length());
  117. // strip leading nn/ from nn/#####nn/ps = #####nn-ps
  118. name = name.substring(name.indexOf('/') + 1).replace('/', '-');
  119. String [] values = name.split("-");
  120. // Gerrit change
  121. tooltip = MessageFormat.format(getString("gb.reviewPatchset"), values[0], values[1]);
  122. cssClass = "otherRef";
  123. } else if (name.startsWith(Constants.R_TICKETS_PATCHSETS)) {
  124. // Gitblit patchset ref
  125. name = name.substring(Constants.R_TICKETS_PATCHSETS.length());
  126. // strip leading nn/ from nn/#####nn/ps = #####nn-ps
  127. name = name.substring(name.indexOf('/') + 1).replace('/', '-');
  128. String [] values = name.split("-");
  129. tooltip = MessageFormat.format(getString("gb.ticketPatchset"), values[0], values[1]);
  130. linkClass = LogPage.class;
  131. cssClass = "otherRef";
  132. } else if (name.startsWith(Constants.R_PULL)) {
  133. // Pull Request ref
  134. String num = name.substring(Constants.R_PULL.length());
  135. if (num.endsWith("/head")) {
  136. // strip pull request head from name
  137. num = num.substring(0, num.length() - "/head".length());
  138. }
  139. name = "pr #" + num;
  140. tooltip = "pull request #" + num;
  141. cssClass = "pullRef";
  142. } else if (name.startsWith(Constants.R_REMOTES)) {
  143. // remote branch
  144. linkClass = LogPage.class;
  145. name = name.substring(Constants.R_REMOTES.length());
  146. cssClass = "remoteBranch";
  147. if (!alreadyInsertedBreak) {
  148. breakLine = true;
  149. alreadyInsertedBreak = true;
  150. }
  151. } else if (name.startsWith(Constants.R_TAGS)) {
  152. // tag
  153. if (entry.isAnnotatedTag()) {
  154. linkClass = TagPage.class;
  155. objectid = entry.getObjectId().getName();
  156. } else {
  157. linkClass = CommitPage.class;
  158. objectid = entry.getReferencedObjectId().getName();
  159. }
  160. name = name.substring(Constants.R_TAGS.length());
  161. cssClass = "tagRef";
  162. } else if (name.startsWith(Constants.R_NOTES)) {
  163. // codereview refs
  164. linkClass = CommitPage.class;
  165. cssClass = "otherRef";
  166. } else if (name.startsWith(com.gitblit.Constants.R_GITBLIT)) {
  167. // gitblit refs
  168. linkClass = LogPage.class;
  169. cssClass = "otherRef";
  170. name = name.substring(com.gitblit.Constants.R_GITBLIT.length());
  171. }
  172. Component c = new LinkPanel("refName", null, name, linkClass,
  173. WicketUtils.newObjectParameter(repositoryName, objectid));
  174. WicketUtils.setCssClass(c, cssClass);
  175. if (StringUtils.isEmpty(tooltip)) {
  176. WicketUtils.setHtmlTooltip(c, name);
  177. } else {
  178. WicketUtils.setHtmlTooltip(c, tooltip);
  179. }
  180. item.add(c);
  181. Label lb = new Label("lineBreak", "<br/>");
  182. lb.setVisible(breakLine);
  183. lb.setRenderBodyOnly(true);
  184. item.add(lb.setEscapeModelStrings(false));
  185. item.setRenderBodyOnly(true);
  186. }
  187. };
  188. add(refsView);
  189. }
  190. }