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