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.

PushesPanel.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright 2013 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.List;
  20. import org.apache.wicket.markup.html.basic.Label;
  21. import org.apache.wicket.markup.repeater.Item;
  22. import org.apache.wicket.markup.repeater.data.DataView;
  23. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  24. import org.apache.wicket.model.StringResourceModel;
  25. import org.eclipse.jgit.lib.Repository;
  26. import com.gitblit.Constants;
  27. import com.gitblit.GitBlit;
  28. import com.gitblit.Keys;
  29. import com.gitblit.models.PushLogEntry;
  30. import com.gitblit.models.RepositoryCommit;
  31. import com.gitblit.models.RepositoryModel;
  32. import com.gitblit.utils.PushLogUtils;
  33. import com.gitblit.utils.StringUtils;
  34. import com.gitblit.wicket.WicketUtils;
  35. import com.gitblit.wicket.pages.CommitPage;
  36. import com.gitblit.wicket.pages.ComparePage;
  37. import com.gitblit.wicket.pages.PushesPage;
  38. import com.gitblit.wicket.pages.SummaryPage;
  39. import com.gitblit.wicket.pages.TagPage;
  40. import com.gitblit.wicket.pages.TreePage;
  41. import com.gitblit.wicket.pages.UserPage;
  42. public class PushesPanel extends BasePanel {
  43. private static final long serialVersionUID = 1L;
  44. private final boolean hasPushes;
  45. private boolean hasMore;
  46. public PushesPanel(String wicketId, final RepositoryModel model, Repository r, int limit, int pageOffset) {
  47. super(wicketId);
  48. boolean pageResults = limit <= 0;
  49. int pushesPerPage = GitBlit.getInteger(Keys.web.pushesPerPage, 10);
  50. if (pushesPerPage <= 1) {
  51. pushesPerPage = 10;
  52. }
  53. final int hashLen = GitBlit.getInteger(Keys.web.shortCommitIdLength, 6);
  54. List<PushLogEntry> pushes;
  55. if (pageResults) {
  56. pushes = PushLogUtils.getPushLogByRef(model.name, r, pageOffset * pushesPerPage, pushesPerPage);
  57. } else {
  58. pushes = PushLogUtils.getPushLogByRef(model.name, r, limit);
  59. }
  60. // inaccurate way to determine if there are more commits.
  61. // works unless commits.size() represents the exact end.
  62. hasMore = pushes.size() >= pushesPerPage;
  63. hasPushes = pushes.size() > 0;
  64. ListDataProvider<PushLogEntry> dp = new ListDataProvider<PushLogEntry>(pushes);
  65. DataView<PushLogEntry> pushView = new DataView<PushLogEntry>("push", dp) {
  66. private static final long serialVersionUID = 1L;
  67. public void populateItem(final Item<PushLogEntry> pushItem) {
  68. final PushLogEntry push = pushItem.getModelObject();
  69. String fullRefName = push.getChangedRefs().get(0);
  70. String shortRefName = fullRefName;
  71. boolean isTag = false;
  72. if (shortRefName.startsWith(org.eclipse.jgit.lib.Constants.R_HEADS)) {
  73. shortRefName = shortRefName.substring(org.eclipse.jgit.lib.Constants.R_HEADS.length());
  74. } else if (shortRefName.startsWith(org.eclipse.jgit.lib.Constants.R_TAGS)) {
  75. shortRefName = shortRefName.substring(org.eclipse.jgit.lib.Constants.R_TAGS.length());
  76. isTag = true;
  77. }
  78. pushItem.add(WicketUtils.createDateLabel("whenPushed", push.date, getTimeZone(), getTimeUtils()));
  79. Label pushIcon = new Label("pushIcon");
  80. if (isTag) {
  81. WicketUtils.setCssClass(pushIcon, "iconic-tag");
  82. } else {
  83. WicketUtils.setCssClass(pushIcon, "iconic-loop");
  84. }
  85. pushItem.add(pushIcon);
  86. if (push.user.username.equals(push.user.emailAddress) && push.user.emailAddress.indexOf('@') > -1) {
  87. // username is an email address - 1.2.1 push log bug
  88. pushItem.add(new Label("whoPushed", push.user.getDisplayName()));
  89. } else {
  90. // link to user acount page
  91. pushItem.add(new LinkPanel("whoPushed", null, push.user.getDisplayName(),
  92. UserPage.class, WicketUtils.newUsernameParameter(push.user.username)));
  93. }
  94. String preposition = "gb.at";
  95. boolean isDelete = false;
  96. boolean isRewind = false;
  97. String what;
  98. switch(push.getChangeType(fullRefName)) {
  99. case CREATE:
  100. if (isTag) {
  101. what = getString("gb.pushedNewTag");
  102. } else {
  103. what = getString("gb.pushedNewBranch");
  104. }
  105. preposition = "gb.to";
  106. break;
  107. case DELETE:
  108. isDelete = true;
  109. if (isTag) {
  110. what = getString("gb.deletedTag");
  111. } else {
  112. what = getString("gb.deletedBranch");
  113. }
  114. preposition = "gb.from";
  115. break;
  116. case UPDATE_NONFASTFORWARD:
  117. isRewind = true;
  118. default:
  119. what = MessageFormat.format(push.getCommitCount() > 1 ? getString("gb.pushedNCommitsTo") : getString("gb.pushedOneCommitTo") , push.getCommitCount());
  120. break;
  121. }
  122. pushItem.add(new Label("whatPushed", what));
  123. pushItem.add(new Label("refRewind", getString("gb.rewind")).setVisible(isRewind));
  124. if (isDelete) {
  125. // can't link to deleted ref
  126. pushItem.add(new Label("refPushed", shortRefName));
  127. } else if (isTag) {
  128. // link to tag
  129. pushItem.add(new LinkPanel("refPushed", null, shortRefName,
  130. TagPage.class, WicketUtils.newObjectParameter(model.name, fullRefName)));
  131. } else {
  132. // link to tree
  133. pushItem.add(new LinkPanel("refPushed", null, shortRefName,
  134. TreePage.class, WicketUtils.newObjectParameter(model.name, fullRefName)));
  135. }
  136. // to/from/etc
  137. pushItem.add(new Label("repoPreposition", getString(preposition)));
  138. String repoName = StringUtils.stripDotGit(model.name);
  139. pushItem.add(new LinkPanel("repoPushed", null, repoName,
  140. SummaryPage.class, WicketUtils.newRepositoryParameter(model.name)));
  141. int maxCommitCount = 5;
  142. List<RepositoryCommit> commits = push.getCommits();
  143. if (commits.size() > maxCommitCount) {
  144. commits = new ArrayList<RepositoryCommit>(commits.subList(0, maxCommitCount));
  145. }
  146. // compare link
  147. String compareLinkText = null;
  148. if ((push.getCommitCount() <= maxCommitCount) && (push.getCommitCount() > 1)) {
  149. compareLinkText = MessageFormat.format(getString("gb.viewComparison"), commits.size());
  150. } else if (push.getCommitCount() > maxCommitCount) {
  151. int diff = push.getCommitCount() - maxCommitCount;
  152. compareLinkText = MessageFormat.format(diff > 1 ? getString("gb.nMoreCommits") : getString("gb.oneMoreCommit"), diff);
  153. }
  154. if (StringUtils.isEmpty(compareLinkText)) {
  155. pushItem.add(new Label("compareLink").setVisible(false));
  156. } else {
  157. String endRangeId = push.getNewId(fullRefName);
  158. String startRangeId = push.getOldId(fullRefName);
  159. pushItem.add(new LinkPanel("compareLink", null, compareLinkText, ComparePage.class, WicketUtils.newRangeParameter(push.repository, startRangeId, endRangeId)));
  160. }
  161. ListDataProvider<RepositoryCommit> cdp = new ListDataProvider<RepositoryCommit>(commits);
  162. DataView<RepositoryCommit> commitsView = new DataView<RepositoryCommit>("commit", cdp) {
  163. private static final long serialVersionUID = 1L;
  164. public void populateItem(final Item<RepositoryCommit> commitItem) {
  165. final RepositoryCommit commit = commitItem.getModelObject();
  166. // author gravatar
  167. commitItem.add(new GravatarImage("commitAuthor", commit.getAuthorIdent().getName(),
  168. commit.getAuthorIdent().getEmailAddress(), null, 16, false, false));
  169. // merge icon
  170. if (commit.getParentCount() > 1) {
  171. commitItem.add(WicketUtils.newImage("commitIcon", "commit_merge_16x16.png"));
  172. } else {
  173. commitItem.add(WicketUtils.newBlankImage("commitIcon"));
  174. }
  175. // short message
  176. String shortMessage = commit.getShortMessage();
  177. String trimmedMessage = shortMessage;
  178. if (commit.getRefs() != null && commit.getRefs().size() > 0) {
  179. trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG_REFS);
  180. } else {
  181. trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG);
  182. }
  183. LinkPanel shortlog = new LinkPanel("commitShortMessage", "list",
  184. trimmedMessage, CommitPage.class, WicketUtils.newObjectParameter(
  185. model.name, commit.getName()));
  186. if (!shortMessage.equals(trimmedMessage)) {
  187. WicketUtils.setHtmlTooltip(shortlog, shortMessage);
  188. }
  189. commitItem.add(shortlog);
  190. // commit hash link
  191. LinkPanel commitHash = new LinkPanel("hashLink", null, commit.getName().substring(0, hashLen),
  192. CommitPage.class, WicketUtils.newObjectParameter(
  193. model.name, commit.getName()));
  194. WicketUtils.setCssClass(commitHash, "shortsha1");
  195. WicketUtils.setHtmlTooltip(commitHash, commit.getName());
  196. commitItem.add(commitHash);
  197. }
  198. };
  199. pushItem.add(commitsView);
  200. }
  201. };
  202. add(pushView);
  203. // determine to show pager, more, or neither
  204. if (limit <= 0) {
  205. // no display limit
  206. add(new Label("morePushes").setVisible(false));
  207. } else {
  208. if (pageResults) {
  209. // paging
  210. add(new Label("morePushes").setVisible(false));
  211. } else {
  212. // more
  213. if (pushes.size() == limit) {
  214. // show more
  215. add(new LinkPanel("morePushes", "link", new StringResourceModel("gb.morePushes",
  216. this, null), PushesPage.class,
  217. WicketUtils.newRepositoryParameter(model.name)));
  218. } else {
  219. // no more
  220. add(new Label("morePushes").setVisible(false));
  221. }
  222. }
  223. }
  224. }
  225. public boolean hasMore() {
  226. return hasMore;
  227. }
  228. public boolean hideIfEmpty() {
  229. setVisible(hasPushes);
  230. return hasPushes;
  231. }
  232. }