James Moger
2013-11-26 4fcac9d2cbdafb51e3ee9ca3b3da64fd86103174
src/main/java/com/gitblit/wicket/panels/BranchesPanel.java
@@ -15,11 +15,13 @@
 */
package com.gitblit.wicket.panels;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.ExternalLink;
@@ -29,6 +31,9 @@
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.protocol.http.RequestUtils;
import org.apache.wicket.request.target.basic.RedirectRequestTarget;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import com.gitblit.Constants;
@@ -37,7 +42,9 @@
import com.gitblit.models.RefModel;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.UserModel;
import com.gitblit.utils.CommitCache;
import com.gitblit.utils.JGitUtils;
import com.gitblit.utils.RefLogUtils;
import com.gitblit.utils.StringUtils;
import com.gitblit.wicket.GitBlitWebSession;
import com.gitblit.wicket.WicketUtils;
@@ -94,15 +101,16 @@
         // 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;
         int counter;
         @Override
         public void populateItem(final Item<RefModel> item) {
            final RefModel entry = item.getModelObject();
@@ -129,7 +137,7 @@
               WicketUtils.setHtmlTooltip(shortlog, shortMessage);
            }
            item.add(shortlog);
            if (maxCount <= 0) {
               Fragment fragment = new Fragment("branchLinks", showDelete? "branchPageAdminLinks" : "branchPageLinks", this);
               fragment.add(new BookmarkablePageLink<Void>("log", LogPage.class, WicketUtils
@@ -191,19 +199,44 @@
               }
               return;
            }
            boolean success = JGitUtils.deleteBranchRef(r, entry.getName());
            final String branch = entry.getName();
            Ref ref = null;
            try {
               ref = r.getRef(branch);
               if (ref == null && !branch.startsWith(Constants.R_HEADS)) {
                  ref = r.getRef(Constants.R_HEADS + branch);
               }
            } catch (IOException e) {
            }
            if (ref != null) {
               boolean success = JGitUtils.deleteBranchRef(r, ref.getName());
               if (success) {
                  // clear commit cache
                  CommitCache.instance().clear(repositoryModel.name, branch);
                  // optionally update reflog
                  if (RefLogUtils.hasRefLogBranch(r)) {
                     UserModel user = GitBlitWebSession.get().getUser();
                     RefLogUtils.deleteRef(user, r, ref);
                  }
               }
               if (success) {
                  info(MessageFormat.format("Branch \"{0}\" deleted", branch));
               } else {
                  error(MessageFormat.format("Failed to delete branch \"{0}\"", branch));
               }
            }
            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));
            }
            // redirect to the owning page
            PageParameters params = WicketUtils.newRepositoryParameter(repositoryModel.name);
            String relativeUrl = urlFor(getPage().getClass(), params).toString();
            String absoluteUrl = RequestUtils.toAbsolutePath(relativeUrl);
            getRequestCycle().setRequestTarget(new RedirectRequestTarget(absoluteUrl));
         }
      };
      deleteLink.add(new JavascriptEventConfirmation("onclick", MessageFormat.format(
            "Delete branch \"{0}\"?", entry.displayName )));
      return deleteLink;