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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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.DailyLogEntry;
  30. import com.gitblit.models.PushLogEntry;
  31. import com.gitblit.models.RepositoryCommit;
  32. import com.gitblit.models.RepositoryModel;
  33. import com.gitblit.utils.PushLogUtils;
  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.ComparePage;
  38. import com.gitblit.wicket.pages.PushesPage;
  39. import com.gitblit.wicket.pages.SummaryPage;
  40. import com.gitblit.wicket.pages.TagPage;
  41. import com.gitblit.wicket.pages.TreePage;
  42. import com.gitblit.wicket.pages.UserPage;
  43. public class PushesPanel extends BasePanel {
  44. private static final long serialVersionUID = 1L;
  45. private final boolean hasPushes;
  46. private boolean hasMore;
  47. public PushesPanel(String wicketId, final RepositoryModel model, Repository r, int limit, int pageOffset, boolean showRepo) {
  48. super(wicketId);
  49. boolean pageResults = limit <= 0;
  50. int pushesPerPage = GitBlit.getInteger(Keys.web.pushesPerPage, 10);
  51. if (pushesPerPage <= 1) {
  52. pushesPerPage = 10;
  53. }
  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. setup(pushes, showRepo);
  65. // determine to show pager, more, or neither
  66. if (limit <= 0) {
  67. // no display limit
  68. add(new Label("morePushes").setVisible(false));
  69. } else {
  70. if (pageResults) {
  71. // paging
  72. add(new Label("morePushes").setVisible(false));
  73. } else {
  74. // more
  75. if (pushes.size() == limit) {
  76. // show more
  77. add(new LinkPanel("morePushes", "link", new StringResourceModel("gb.morePushes",
  78. this, null), PushesPage.class,
  79. WicketUtils.newRepositoryParameter(model.name)));
  80. } else {
  81. // no more
  82. add(new Label("morePushes").setVisible(false));
  83. }
  84. }
  85. }
  86. }
  87. public PushesPanel(String wicketId, List<PushLogEntry> pushes) {
  88. super(wicketId);
  89. hasPushes = pushes.size() > 0;
  90. setup(pushes, true);
  91. add(new Label("morePushes").setVisible(false));
  92. }
  93. protected void setup(List<PushLogEntry> pushes, final boolean showRepo) {
  94. final int hashLen = GitBlit.getInteger(Keys.web.shortCommitIdLength, 6);
  95. ListDataProvider<PushLogEntry> dp = new ListDataProvider<PushLogEntry>(pushes);
  96. DataView<PushLogEntry> pushView = new DataView<PushLogEntry>("push", dp) {
  97. private static final long serialVersionUID = 1L;
  98. public void populateItem(final Item<PushLogEntry> pushItem) {
  99. final PushLogEntry push = pushItem.getModelObject();
  100. String fullRefName = push.getChangedRefs().get(0);
  101. String shortRefName = fullRefName;
  102. boolean isTag = false;
  103. if (shortRefName.startsWith(org.eclipse.jgit.lib.Constants.R_HEADS)) {
  104. shortRefName = shortRefName.substring(org.eclipse.jgit.lib.Constants.R_HEADS.length());
  105. } else if (shortRefName.startsWith(org.eclipse.jgit.lib.Constants.R_TAGS)) {
  106. shortRefName = shortRefName.substring(org.eclipse.jgit.lib.Constants.R_TAGS.length());
  107. isTag = true;
  108. }
  109. boolean isDigest = push instanceof DailyLogEntry;
  110. pushItem.add(WicketUtils.createDateLabel("whenPushed", push.date, getTimeZone(), getTimeUtils()));
  111. Label pushIcon = new Label("pushIcon");
  112. if (showRepo) {
  113. // if we are showing the repo, we are showing multiple
  114. // repos. use the repository hash color to differentiate
  115. // the icon.
  116. String color = StringUtils.getColor(StringUtils.stripDotGit(push.repository));
  117. WicketUtils.setCssStyle(pushIcon, "color: " + color);
  118. }
  119. if (isTag) {
  120. WicketUtils.setCssClass(pushIcon, "iconic-tag");
  121. } else if (isDigest) {
  122. WicketUtils.setCssClass(pushIcon, "iconic-loop");
  123. } else {
  124. WicketUtils.setCssClass(pushIcon, "iconic-upload");
  125. }
  126. pushItem.add(pushIcon);
  127. if (isDigest && !isTag) {
  128. pushItem.add(new Label("whoPushed").setVisible(false));
  129. } else {
  130. if (push.user.username.equals(push.user.emailAddress) && push.user.emailAddress.indexOf('@') > -1) {
  131. // username is an email address - 1.2.1 push log bug
  132. pushItem.add(new Label("whoPushed", push.user.getDisplayName()));
  133. } else {
  134. // link to user account page
  135. pushItem.add(new LinkPanel("whoPushed", null, push.user.getDisplayName(),
  136. UserPage.class, WicketUtils.newUsernameParameter(push.user.username)));
  137. }
  138. }
  139. String preposition = "gb.of";
  140. boolean isDelete = false;
  141. boolean isRewind = false;
  142. String what;
  143. String by = null;
  144. switch(push.getChangeType(fullRefName)) {
  145. case CREATE:
  146. if (isTag) {
  147. if (isDigest) {
  148. what = getString("gb.createdNewTag");
  149. preposition = "gb.in";
  150. } else {
  151. what = getString("gb.pushedNewTag");
  152. preposition = "gb.to";
  153. }
  154. } else {
  155. what = getString("gb.pushedNewBranch");
  156. preposition = "gb.to";
  157. }
  158. break;
  159. case DELETE:
  160. isDelete = true;
  161. if (isTag) {
  162. what = getString("gb.deletedTag");
  163. } else {
  164. what = getString("gb.deletedBranch");
  165. }
  166. preposition = "gb.from";
  167. break;
  168. case UPDATE_NONFASTFORWARD:
  169. isRewind = true;
  170. default:
  171. if (isDigest) {
  172. what = MessageFormat.format(push.getCommitCount() > 1 ? getString("gb.commitsTo") : getString("gb.oneCommitTo"), push.getCommitCount());
  173. } else {
  174. what = MessageFormat.format(push.getCommitCount() > 1 ? getString("gb.pushedNCommitsTo") : getString("gb.pushedOneCommitTo") , push.getCommitCount());
  175. }
  176. if (push.getAuthorCount() == 1) {
  177. by = MessageFormat.format(getString("gb.byOneAuthor"), push.getAuthorIdent().getName());
  178. } else {
  179. by = MessageFormat.format(getString("gb.byNAuthors"), push.getAuthorCount());
  180. }
  181. break;
  182. }
  183. pushItem.add(new Label("whatPushed", what));
  184. pushItem.add(new Label("byAuthors", by).setVisible(!StringUtils.isEmpty(by)));
  185. pushItem.add(new Label("refRewind", getString("gb.rewind")).setVisible(isRewind));
  186. if (isDelete) {
  187. // can't link to deleted ref
  188. pushItem.add(new Label("refPushed", shortRefName));
  189. } else if (isTag) {
  190. // link to tag
  191. pushItem.add(new LinkPanel("refPushed", null, shortRefName,
  192. TagPage.class, WicketUtils.newObjectParameter(push.repository, fullRefName)));
  193. } else {
  194. // link to tree
  195. pushItem.add(new LinkPanel("refPushed", null, shortRefName,
  196. TreePage.class, WicketUtils.newObjectParameter(push.repository, fullRefName)));
  197. }
  198. if (showRepo) {
  199. // to/from/etc
  200. pushItem.add(new Label("repoPreposition", getString(preposition)));
  201. String repoName = StringUtils.stripDotGit(push.repository);
  202. pushItem.add(new LinkPanel("repoPushed", null, repoName,
  203. SummaryPage.class, WicketUtils.newRepositoryParameter(push.repository)));
  204. } else {
  205. // do not display repository name if we are viewing the push
  206. // log of a repository.
  207. pushItem.add(new Label("repoPreposition").setVisible(false));
  208. pushItem.add(new Label("repoPushed").setVisible(false));
  209. }
  210. int maxCommitCount = 5;
  211. List<RepositoryCommit> commits = push.getCommits();
  212. if (commits.size() > maxCommitCount) {
  213. commits = new ArrayList<RepositoryCommit>(commits.subList(0, maxCommitCount));
  214. }
  215. // compare link
  216. String compareLinkText = null;
  217. if ((push.getCommitCount() <= maxCommitCount) && (push.getCommitCount() > 1)) {
  218. compareLinkText = MessageFormat.format(getString("gb.viewComparison"), commits.size());
  219. } else if (push.getCommitCount() > maxCommitCount) {
  220. int diff = push.getCommitCount() - maxCommitCount;
  221. compareLinkText = MessageFormat.format(diff > 1 ? getString("gb.nMoreCommits") : getString("gb.oneMoreCommit"), diff);
  222. }
  223. if (StringUtils.isEmpty(compareLinkText)) {
  224. pushItem.add(new Label("compareLink").setVisible(false));
  225. } else {
  226. String endRangeId = push.getNewId(fullRefName);
  227. String startRangeId = push.getOldId(fullRefName);
  228. pushItem.add(new LinkPanel("compareLink", null, compareLinkText, ComparePage.class, WicketUtils.newRangeParameter(push.repository, startRangeId, endRangeId)));
  229. }
  230. final boolean showSwatch = showRepo && GitBlit.getBoolean(Keys.web.repositoryListSwatches, true);
  231. ListDataProvider<RepositoryCommit> cdp = new ListDataProvider<RepositoryCommit>(commits);
  232. DataView<RepositoryCommit> commitsView = new DataView<RepositoryCommit>("commit", cdp) {
  233. private static final long serialVersionUID = 1L;
  234. public void populateItem(final Item<RepositoryCommit> commitItem) {
  235. final RepositoryCommit commit = commitItem.getModelObject();
  236. // author gravatar
  237. commitItem.add(new GravatarImage("commitAuthor", commit.getAuthorIdent().getName(),
  238. commit.getAuthorIdent().getEmailAddress(), null, 16, false, false));
  239. // merge icon
  240. if (commit.getParentCount() > 1) {
  241. commitItem.add(WicketUtils.newImage("commitIcon", "commit_merge_16x16.png"));
  242. } else {
  243. commitItem.add(WicketUtils.newBlankImage("commitIcon"));
  244. }
  245. // short message
  246. String shortMessage = commit.getShortMessage();
  247. String trimmedMessage = shortMessage;
  248. if (commit.getRefs() != null && commit.getRefs().size() > 0) {
  249. trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG_REFS);
  250. } else {
  251. trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG);
  252. }
  253. LinkPanel shortlog = new LinkPanel("commitShortMessage", "list",
  254. trimmedMessage, CommitPage.class, WicketUtils.newObjectParameter(
  255. push.repository, commit.getName()));
  256. if (!shortMessage.equals(trimmedMessage)) {
  257. WicketUtils.setHtmlTooltip(shortlog, shortMessage);
  258. }
  259. commitItem.add(shortlog);
  260. // commit hash link
  261. LinkPanel commitHash = new LinkPanel("hashLink", null, commit.getName().substring(0, hashLen),
  262. CommitPage.class, WicketUtils.newObjectParameter(
  263. push.repository, commit.getName()));
  264. WicketUtils.setCssClass(commitHash, "shortsha1");
  265. WicketUtils.setHtmlTooltip(commitHash, commit.getName());
  266. commitItem.add(commitHash);
  267. if (showSwatch) {
  268. // set repository color
  269. String color = StringUtils.getColor(StringUtils.stripDotGit(push.repository));
  270. WicketUtils.setCssStyle(commitItem, MessageFormat.format("border-left: 2px solid {0};", color));
  271. }
  272. }
  273. };
  274. pushItem.add(commitsView);
  275. }
  276. };
  277. add(pushView);
  278. }
  279. public boolean hasMore() {
  280. return hasMore;
  281. }
  282. public boolean hideIfEmpty() {
  283. setVisible(hasPushes);
  284. return hasPushes;
  285. }
  286. }