Rafael Cavazin
2012-12-07 6b35f8c32de5ebf7bedc429a6ff62709af9d5acd
src/com/gitblit/wicket/panels/BranchesPanel.java
@@ -32,11 +32,14 @@
import org.eclipse.jgit.lib.Repository;
import com.gitblit.Constants;
import com.gitblit.GitBlit;
import com.gitblit.SyndicationServlet;
import com.gitblit.models.RefModel;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.UserModel;
import com.gitblit.utils.JGitUtils;
import com.gitblit.utils.StringUtils;
import com.gitblit.wicket.GitBlitWebSession;
import com.gitblit.wicket.WicketUtils;
import com.gitblit.wicket.pages.BranchesPage;
import com.gitblit.wicket.pages.CommitPage;
@@ -51,15 +54,30 @@
   private final boolean hasBranches;
   public BranchesPanel(String wicketId, final RepositoryModel model, final Repository r,
   public BranchesPanel(String wicketId, final RepositoryModel model, Repository r,
         final int maxCount, final boolean showAdmin) {
      super(wicketId);
      // branches
      List<RefModel> branches = new ArrayList<RefModel>();
      branches.addAll(JGitUtils.getLocalBranches(r, false, maxCount));
      UserModel user = GitBlitWebSession.get().getUser();
      if (user == null) {
         user = UserModel.ANONYMOUS;
      }
      List<RefModel> localBranches = JGitUtils.getLocalBranches(r, false, -1);
      for (RefModel refModel : localBranches) {
         if (user.canView(model, refModel.reference.getName())) {
            branches.add(refModel);
         }
      }
      if (model.showRemoteBranches) {
         branches.addAll(JGitUtils.getRemoteBranches(r, false, maxCount));
         List<RefModel> remoteBranches = JGitUtils.getRemoteBranches(r, false, -1);
         for (RefModel refModel : remoteBranches) {
            if (user.canView(model, refModel.reference.getName())) {
               branches.add(refModel);
            }
         }
      }
      Collections.sort(branches);
      Collections.reverse(branches);
@@ -76,7 +94,10 @@
         // branches page
         add(new Label("branches", new StringResourceModel("gb.branches", this, null)));
      }
      // only allow delete if we have multiple branches
      final boolean showDelete = showAdmin && branches.size() > 1;
      ListDataProvider<RefModel> branchesDp = new ListDataProvider<RefModel>(branches);
      DataView<RefModel> branchesView = new DataView<RefModel>("branch", branchesDp) {
         private static final long serialVersionUID = 1L;
@@ -110,7 +131,7 @@
            item.add(shortlog);
            
            if (maxCount <= 0) {
               Fragment fragment = new Fragment("branchLinks", "branchPageLinks", this);
               Fragment fragment = new Fragment("branchLinks", showDelete? "branchPageAdminLinks" : "branchPageLinks", this);
               fragment.add(new BookmarkablePageLink<Void>("log", LogPage.class, WicketUtils
                     .newObjectParameter(model.name, entry.getName())));
               fragment.add(new BookmarkablePageLink<Void>("tree", TreePage.class, WicketUtils
@@ -120,9 +141,9 @@
               fragment.add(new ExternalLink("syndication", SyndicationServlet.asLink(
                     getRequest().getRelativePathPrefixToContextRoot(), model.name,
                     entry.getName(), 0)));
               fragment.add(createDeleteBranchLink(r, entry, showAdmin));
               if (showDelete) {
                  fragment.add(createDeleteBranchLink(model, entry));
               }
               item.add(fragment);
            } else {
               Fragment fragment = new Fragment("branchLinks", "branchPanelLinks", this);
@@ -154,15 +175,28 @@
      return this;
   }
   private Link<Void> createDeleteBranchLink(final Repository r, final RefModel entry, final boolean showAdmin)
   private Link<Void> createDeleteBranchLink(final RepositoryModel repositoryModel, final RefModel entry)
   {
      Link<Void> deleteLink = new Link<Void>("deleteBranch") {
         private static final long serialVersionUID = 1L;
         @Override
         public void onClick() {
            if( showAdmin && JGitUtils.deleteBranchRef(r, entry.getName()) ) {
            Repository r = GitBlit.self().getRepository(repositoryModel.name);
            if (r == null) {
               if (GitBlit.self().isCollectingGarbage(repositoryModel.name)) {
                  error(MessageFormat.format(getString("gb.busyCollectingGarbage"), repositoryModel.name));
               } else {
                  error(MessageFormat.format("Failed to find repository {0}", repositoryModel.name));
               }
               return;
            }
            boolean success = JGitUtils.deleteBranchRef(r, entry.getName());
            r.close();
            if (success) {
               info(MessageFormat.format("Branch \"{0}\" deleted", entry.displayName));
               // redirect to the owning page
               setResponsePage(getPage().getClass(), WicketUtils.newRepositoryParameter(repositoryModel.name));
            }
            else {
               error(MessageFormat.format("Failed to delete branch \"{0}\"", entry.displayName));
@@ -172,9 +206,6 @@
      
      deleteLink.add(new JavascriptEventConfirmation("onclick", MessageFormat.format(
            "Delete branch \"{0}\"?", entry.displayName )));
      deleteLink.setVisible(showAdmin);
      return deleteLink;
   }
}