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.

BranchesPanel.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.List;
  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.html.link.Link;
  25. import org.apache.wicket.markup.html.panel.Fragment;
  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.apache.wicket.model.StringResourceModel;
  30. import org.eclipse.jgit.lib.Repository;
  31. import com.gitblit.Constants;
  32. import com.gitblit.GitBlit;
  33. import com.gitblit.SyndicationServlet;
  34. import com.gitblit.models.RefModel;
  35. import com.gitblit.models.RepositoryModel;
  36. import com.gitblit.models.UserModel;
  37. import com.gitblit.utils.CommitCache;
  38. import com.gitblit.utils.JGitUtils;
  39. import com.gitblit.utils.RefLogUtils;
  40. import com.gitblit.utils.StringUtils;
  41. import com.gitblit.wicket.GitBlitWebSession;
  42. import com.gitblit.wicket.WicketUtils;
  43. import com.gitblit.wicket.pages.BranchesPage;
  44. import com.gitblit.wicket.pages.CommitPage;
  45. import com.gitblit.wicket.pages.GitSearchPage;
  46. import com.gitblit.wicket.pages.LogPage;
  47. import com.gitblit.wicket.pages.MetricsPage;
  48. import com.gitblit.wicket.pages.TreePage;
  49. public class BranchesPanel extends BasePanel {
  50. private static final long serialVersionUID = 1L;
  51. private final boolean hasBranches;
  52. public BranchesPanel(String wicketId, final RepositoryModel model, Repository r,
  53. final int maxCount, final boolean showAdmin) {
  54. super(wicketId);
  55. // branches
  56. List<RefModel> branches = new ArrayList<RefModel>();
  57. UserModel user = GitBlitWebSession.get().getUser();
  58. if (user == null) {
  59. user = UserModel.ANONYMOUS;
  60. }
  61. List<RefModel> localBranches = JGitUtils.getLocalBranches(r, false, -1);
  62. for (RefModel refModel : localBranches) {
  63. if (user.canView(model, refModel.reference.getName())) {
  64. branches.add(refModel);
  65. }
  66. }
  67. if (model.showRemoteBranches) {
  68. List<RefModel> remoteBranches = JGitUtils.getRemoteBranches(r, false, -1);
  69. for (RefModel refModel : remoteBranches) {
  70. if (user.canView(model, refModel.reference.getName())) {
  71. branches.add(refModel);
  72. }
  73. }
  74. }
  75. Collections.sort(branches);
  76. Collections.reverse(branches);
  77. if (maxCount > 0 && branches.size() > maxCount) {
  78. branches = new ArrayList<RefModel>(branches.subList(0, maxCount));
  79. }
  80. if (maxCount > 0) {
  81. // summary page
  82. // show branches page link
  83. add(new LinkPanel("branches", "title", new StringResourceModel("gb.branches", this,
  84. null), BranchesPage.class, WicketUtils.newRepositoryParameter(model.name)));
  85. } else {
  86. // branches page
  87. add(new Label("branches", new StringResourceModel("gb.branches", this, null)));
  88. }
  89. // only allow delete if we have multiple branches
  90. final boolean showDelete = showAdmin && branches.size() > 1;
  91. ListDataProvider<RefModel> branchesDp = new ListDataProvider<RefModel>(branches);
  92. DataView<RefModel> branchesView = new DataView<RefModel>("branch", branchesDp) {
  93. private static final long serialVersionUID = 1L;
  94. int counter;
  95. public void populateItem(final Item<RefModel> item) {
  96. final RefModel entry = item.getModelObject();
  97. item.add(WicketUtils.createDateLabel("branchDate", entry.getDate(), getTimeZone(), getTimeUtils()));
  98. item.add(new LinkPanel("branchName", "list name", StringUtils.trimString(
  99. entry.displayName, 28), LogPage.class, WicketUtils.newObjectParameter(
  100. model.name, entry.getName())));
  101. String author = entry.getAuthorIdent().getName();
  102. LinkPanel authorLink = new LinkPanel("branchAuthor", "list", author,
  103. GitSearchPage.class, WicketUtils.newSearchParameter(model.name,
  104. entry.getName(), author, Constants.SearchType.AUTHOR));
  105. setPersonSearchTooltip(authorLink, author, Constants.SearchType.AUTHOR);
  106. item.add(authorLink);
  107. // short message
  108. String shortMessage = entry.getShortMessage();
  109. String trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG);
  110. LinkPanel shortlog = new LinkPanel("branchLog", "list subject", trimmedMessage,
  111. CommitPage.class, WicketUtils.newObjectParameter(model.name,
  112. entry.getName()));
  113. if (!shortMessage.equals(trimmedMessage)) {
  114. WicketUtils.setHtmlTooltip(shortlog, shortMessage);
  115. }
  116. item.add(shortlog);
  117. if (maxCount <= 0) {
  118. Fragment fragment = new Fragment("branchLinks", showDelete? "branchPageAdminLinks" : "branchPageLinks", this);
  119. fragment.add(new BookmarkablePageLink<Void>("log", LogPage.class, WicketUtils
  120. .newObjectParameter(model.name, entry.getName())));
  121. fragment.add(new BookmarkablePageLink<Void>("tree", TreePage.class, WicketUtils
  122. .newObjectParameter(model.name, entry.getName())));
  123. fragment.add(new BookmarkablePageLink<Void>("metrics", MetricsPage.class,
  124. WicketUtils.newObjectParameter(model.name, entry.getName())));
  125. fragment.add(new ExternalLink("syndication", SyndicationServlet.asLink(
  126. getRequest().getRelativePathPrefixToContextRoot(), model.name,
  127. entry.getName(), 0)));
  128. if (showDelete) {
  129. fragment.add(createDeleteBranchLink(model, entry));
  130. }
  131. item.add(fragment);
  132. } else {
  133. Fragment fragment = new Fragment("branchLinks", "branchPanelLinks", this);
  134. fragment.add(new BookmarkablePageLink<Void>("log", LogPage.class, WicketUtils
  135. .newObjectParameter(model.name, entry.getName())));
  136. fragment.add(new BookmarkablePageLink<Void>("tree", TreePage.class, WicketUtils
  137. .newObjectParameter(model.name, entry.getName())));
  138. item.add(fragment);
  139. }
  140. WicketUtils.setAlternatingBackground(item, counter);
  141. counter++;
  142. }
  143. };
  144. add(branchesView);
  145. if (branches.size() < maxCount || maxCount <= 0) {
  146. add(new Label("allBranches", "").setVisible(false));
  147. } else {
  148. add(new LinkPanel("allBranches", "link", new StringResourceModel("gb.allBranches",
  149. this, null), BranchesPage.class, WicketUtils.newRepositoryParameter(model.name)));
  150. }
  151. // We always have 1 branch
  152. hasBranches = (branches.size() > 1)
  153. || ((branches.size() == 1) && !branches.get(0).displayName
  154. .equalsIgnoreCase("master"));
  155. }
  156. public BranchesPanel hideIfEmpty() {
  157. setVisible(hasBranches);
  158. return this;
  159. }
  160. private Link<Void> createDeleteBranchLink(final RepositoryModel repositoryModel, final RefModel entry)
  161. {
  162. Link<Void> deleteLink = new Link<Void>("deleteBranch") {
  163. private static final long serialVersionUID = 1L;
  164. @Override
  165. public void onClick() {
  166. Repository r = GitBlit.self().getRepository(repositoryModel.name);
  167. if (r == null) {
  168. if (GitBlit.self().isCollectingGarbage(repositoryModel.name)) {
  169. error(MessageFormat.format(getString("gb.busyCollectingGarbage"), repositoryModel.name));
  170. } else {
  171. error(MessageFormat.format("Failed to find repository {0}", repositoryModel.name));
  172. }
  173. return;
  174. }
  175. final String branch = entry.getName();
  176. boolean success = JGitUtils.deleteBranchRef(r, branch);
  177. if (success) {
  178. // clear commit cache
  179. CommitCache.instance().clear(repositoryModel.name, branch);
  180. // optionally update reflog
  181. if (RefLogUtils.hasRefLogBranch(r)) {
  182. UserModel user = GitBlitWebSession.get().getUser();
  183. success = RefLogUtils.deleteRef(user, r, branch);
  184. }
  185. }
  186. r.close();
  187. if (success) {
  188. info(MessageFormat.format("Branch \"{0}\" deleted", branch));
  189. // redirect to the owning page
  190. setResponsePage(getPage().getClass(), WicketUtils.newRepositoryParameter(repositoryModel.name));
  191. }
  192. else {
  193. error(MessageFormat.format("Failed to delete branch \"{0}\"", branch));
  194. }
  195. }
  196. };
  197. deleteLink.add(new JavascriptEventConfirmation("onclick", MessageFormat.format(
  198. "Delete branch \"{0}\"?", entry.displayName )));
  199. return deleteLink;
  200. }
  201. }