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.

DigestsPanel.java 9.9KB

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.DateFormat;
  18. import java.text.MessageFormat;
  19. import java.text.SimpleDateFormat;
  20. import java.util.ArrayList;
  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.TimeZone;
  24. import org.apache.wicket.markup.html.basic.Label;
  25. import org.apache.wicket.markup.repeater.Item;
  26. import org.apache.wicket.markup.repeater.data.DataView;
  27. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  28. import org.eclipse.jgit.lib.PersonIdent;
  29. import com.gitblit.Constants;
  30. import com.gitblit.Keys;
  31. import com.gitblit.models.DailyLogEntry;
  32. import com.gitblit.models.RepositoryCommit;
  33. import com.gitblit.utils.StringUtils;
  34. import com.gitblit.utils.TimeUtils;
  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.SummaryPage;
  39. import com.gitblit.wicket.pages.TagPage;
  40. import com.gitblit.wicket.pages.TreePage;
  41. public class DigestsPanel extends BasePanel {
  42. private static final long serialVersionUID = 1L;
  43. private final boolean hasChanges;
  44. private boolean hasMore;
  45. public DigestsPanel(String wicketId, List<DailyLogEntry> digests) {
  46. super(wicketId);
  47. hasChanges = digests.size() > 0;
  48. ListDataProvider<DailyLogEntry> dp = new ListDataProvider<DailyLogEntry>(digests);
  49. DataView<DailyLogEntry> pushView = new DataView<DailyLogEntry>("change", dp) {
  50. private static final long serialVersionUID = 1L;
  51. @Override
  52. public void populateItem(final Item<DailyLogEntry> logItem) {
  53. final DailyLogEntry change = logItem.getModelObject();
  54. String dateFormat = app().settings().getString(Keys.web.datestampLongFormat, "EEEE, MMMM d, yyyy");
  55. TimeZone timezone = getTimeZone();
  56. DateFormat df = new SimpleDateFormat(dateFormat);
  57. df.setTimeZone(timezone);
  58. String fullRefName = change.getChangedRefs().get(0);
  59. String shortRefName = fullRefName;
  60. boolean isTag = false;
  61. if (shortRefName.startsWith(Constants.R_HEADS)) {
  62. shortRefName = shortRefName.substring(Constants.R_HEADS.length());
  63. } else if (shortRefName.startsWith(Constants.R_TAGS)) {
  64. shortRefName = shortRefName.substring(Constants.R_TAGS.length());
  65. isTag = true;
  66. }
  67. String fuzzydate;
  68. TimeUtils tu = getTimeUtils();
  69. Date pushDate = change.date;
  70. if (TimeUtils.isToday(pushDate, timezone)) {
  71. fuzzydate = tu.today();
  72. } else if (TimeUtils.isYesterday(pushDate, timezone)) {
  73. fuzzydate = tu.yesterday();
  74. } else {
  75. fuzzydate = getTimeUtils().timeAgo(pushDate);
  76. }
  77. logItem.add(new Label("whenChanged", fuzzydate + ", " + df.format(pushDate)));
  78. Label changeIcon = new Label("changeIcon");
  79. // use the repository hash color to differentiate the icon.
  80. String color = StringUtils.getColor(StringUtils.stripDotGit(change.repository));
  81. WicketUtils.setCssStyle(changeIcon, "color: " + color);
  82. if (isTag) {
  83. WicketUtils.setCssClass(changeIcon, "iconic-tag");
  84. } else {
  85. WicketUtils.setCssClass(changeIcon, "iconic-loop");
  86. }
  87. logItem.add(changeIcon);
  88. if (isTag) {
  89. // tags are special
  90. PersonIdent ident = change.getCommits().get(0).getAuthorIdent();
  91. if (!StringUtils.isEmpty(ident.getName())) {
  92. logItem.add(new Label("whoChanged", ident.getName()));
  93. } else {
  94. logItem.add(new Label("whoChanged", ident.getEmailAddress()));
  95. }
  96. } else {
  97. logItem.add(new Label("whoChanged").setVisible(false));
  98. }
  99. String preposition = "gb.of";
  100. boolean isDelete = false;
  101. String what;
  102. String by = null;
  103. switch(change.getChangeType(fullRefName)) {
  104. case CREATE:
  105. if (isTag) {
  106. // new tag
  107. what = getString("gb.createdNewTag");
  108. preposition = "gb.in";
  109. } else {
  110. // new branch
  111. what = getString("gb.createdNewBranch");
  112. preposition = "gb.in";
  113. }
  114. break;
  115. case DELETE:
  116. isDelete = true;
  117. if (isTag) {
  118. what = getString("gb.deletedTag");
  119. } else {
  120. what = getString("gb.deletedBranch");
  121. }
  122. preposition = "gb.from";
  123. break;
  124. default:
  125. what = MessageFormat.format(change.getCommitCount() > 1 ? getString("gb.commitsTo") : getString("gb.oneCommitTo"), change.getCommitCount());
  126. if (change.getAuthorCount() == 1) {
  127. by = MessageFormat.format(getString("gb.byOneAuthor"), change.getAuthorIdent().getName());
  128. } else {
  129. by = MessageFormat.format(getString("gb.byNAuthors"), change.getAuthorCount());
  130. }
  131. break;
  132. }
  133. logItem.add(new Label("whatChanged", what));
  134. logItem.add(new Label("byAuthors", by).setVisible(!StringUtils.isEmpty(by)));
  135. if (isDelete) {
  136. // can't link to deleted ref
  137. logItem.add(new Label("refChanged", shortRefName));
  138. } else if (isTag) {
  139. // link to tag
  140. logItem.add(new LinkPanel("refChanged", null, shortRefName,
  141. TagPage.class, WicketUtils.newObjectParameter(change.repository, fullRefName)));
  142. } else {
  143. // link to tree
  144. logItem.add(new LinkPanel("refChanged", null, shortRefName,
  145. TreePage.class, WicketUtils.newObjectParameter(change.repository, fullRefName)));
  146. }
  147. // to/from/etc
  148. logItem.add(new Label("repoPreposition", getString(preposition)));
  149. String repoName = StringUtils.stripDotGit(change.repository);
  150. logItem.add(new LinkPanel("repoChanged", null, repoName,
  151. SummaryPage.class, WicketUtils.newRepositoryParameter(change.repository)));
  152. int maxCommitCount = 5;
  153. List<RepositoryCommit> commits = change.getCommits();
  154. if (commits.size() > maxCommitCount) {
  155. commits = new ArrayList<RepositoryCommit>(commits.subList(0, maxCommitCount));
  156. }
  157. // compare link
  158. String compareLinkText = null;
  159. if ((change.getCommitCount() <= maxCommitCount) && (change.getCommitCount() > 1)) {
  160. compareLinkText = MessageFormat.format(getString("gb.viewComparison"), commits.size());
  161. } else if (change.getCommitCount() > maxCommitCount) {
  162. int diff = change.getCommitCount() - maxCommitCount;
  163. compareLinkText = MessageFormat.format(diff > 1 ? getString("gb.nMoreCommits") : getString("gb.oneMoreCommit"), diff);
  164. }
  165. if (StringUtils.isEmpty(compareLinkText)) {
  166. logItem.add(new Label("compareLink").setVisible(false));
  167. } else {
  168. String endRangeId = change.getNewId(fullRefName);
  169. String startRangeId = change.getOldId(fullRefName);
  170. logItem.add(new LinkPanel("compareLink", null, compareLinkText, ComparePage.class, WicketUtils.newRangeParameter(change.repository, startRangeId, endRangeId)));
  171. }
  172. final boolean showSwatch = app().settings().getBoolean(Keys.web.repositoryListSwatches, true);
  173. ListDataProvider<RepositoryCommit> cdp = new ListDataProvider<RepositoryCommit>(commits);
  174. DataView<RepositoryCommit> commitsView = new DataView<RepositoryCommit>("commit", cdp) {
  175. private static final long serialVersionUID = 1L;
  176. @Override
  177. public void populateItem(final Item<RepositoryCommit> commitItem) {
  178. final RepositoryCommit commit = commitItem.getModelObject();
  179. // author gravatar
  180. commitItem.add(new GravatarImage("commitAuthor", commit.getAuthorIdent(), null, 16, false));
  181. // merge icon
  182. if (commit.getParentCount() > 1) {
  183. commitItem.add(WicketUtils.newImage("commitIcon", "commit_merge_16x16.png"));
  184. } else {
  185. commitItem.add(WicketUtils.newBlankImage("commitIcon"));
  186. }
  187. // short message
  188. String shortMessage = commit.getShortMessage();
  189. String trimmedMessage = shortMessage;
  190. if (commit.getRefs() != null && commit.getRefs().size() > 0) {
  191. trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG_REFS);
  192. } else {
  193. trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG);
  194. }
  195. LinkPanel shortlog = new LinkPanel("commitShortMessage", "list",
  196. trimmedMessage, CommitPage.class, WicketUtils.newObjectParameter(
  197. change.repository, commit.getName()));
  198. if (!shortMessage.equals(trimmedMessage)) {
  199. WicketUtils.setHtmlTooltip(shortlog, shortMessage);
  200. }
  201. commitItem.add(shortlog);
  202. // commit hash link
  203. int hashLen = app().settings().getInteger(Keys.web.shortCommitIdLength, 6);
  204. LinkPanel commitHash = new LinkPanel("hashLink", null, commit.getName().substring(0, hashLen),
  205. CommitPage.class, WicketUtils.newObjectParameter(
  206. change.repository, commit.getName()));
  207. WicketUtils.setCssClass(commitHash, "shortsha1");
  208. WicketUtils.setHtmlTooltip(commitHash, commit.getName());
  209. commitItem.add(commitHash);
  210. if (showSwatch) {
  211. // set repository color
  212. String color = StringUtils.getColor(StringUtils.stripDotGit(change.repository));
  213. WicketUtils.setCssStyle(commitItem, MessageFormat.format("border-left: 2px solid {0};", color));
  214. }
  215. }
  216. };
  217. logItem.add(commitsView);
  218. }
  219. };
  220. add(pushView);
  221. }
  222. public boolean hasMore() {
  223. return hasMore;
  224. }
  225. public boolean hideIfEmpty() {
  226. setVisible(hasChanges);
  227. return hasChanges;
  228. }
  229. }