選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

BranchesPanel.java 9.2KB

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