From 271a68b3b0ce6707130c77b5b009b3457d6b9d3b Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 22 Jul 2013 15:57:36 -0400 Subject: Update Gitblit reflog on branch delete from UI if Gitblit has an existing reflog --- src/main/java/com/gitblit/utils/RefLogUtils.java | 48 ++++++++++++++++++++++ .../com/gitblit/wicket/panels/BranchesPanel.java | 21 ++++++++-- .../com/gitblit/wicket/panels/ReflogPanel.java | 5 ++- 3 files changed, 70 insertions(+), 4 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/gitblit/utils/RefLogUtils.java b/src/main/java/com/gitblit/utils/RefLogUtils.java index 643fbc0f..dfb5f56d 100644 --- a/src/main/java/com/gitblit/utils/RefLogUtils.java +++ b/src/main/java/com/gitblit/utils/RefLogUtils.java @@ -20,6 +20,7 @@ import java.text.DateFormat; import java.text.MessageFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; @@ -41,6 +42,7 @@ import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.PersonIdent; +import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.RefRename; import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.RefUpdate.Result; @@ -96,6 +98,21 @@ public class RefLogUtils { } LOGGER.error(MessageFormat.format(pattern, parameters.toArray()), t); } + + /** + * Returns true if the repository has a reflog branch. + * + * @param repository + * @return true if the repository has a reflog branch + */ + public static boolean hasRefLogBranch(Repository repository) { + try { + return repository.getRef(GB_REFLOG) != null; + } catch(Exception e) { + LOGGER.error("failed to determine hasRefLogBranch", e); + } + return false; + } /** * Returns a RefModel for the reflog branch in the repository. If the @@ -155,6 +172,37 @@ public class RefLogUtils { return user; } + /** + * Logs a ref deletion. + * + * @param user + * @param repository + * @param ref + * @return true, if the update was successful + */ + public static boolean deleteRef(UserModel user, Repository repository, String ref) { + try { + Ref refObj = repository.getRef(ref); + if (refObj == null && !ref.startsWith(Constants.R_HEADS) && ref.startsWith(Constants.R_TAGS)) { + // find fully qualified ref + refObj = repository.getRef(Constants.R_HEADS + ref); + if (refObj == null) { + refObj = repository.getRef(Constants.R_TAGS + ref); + } + } + + if (refObj == null) { + return false; + } + + ReceiveCommand cmd = new ReceiveCommand(refObj.getObjectId(), ObjectId.zeroId(), refObj.getName()); + return updateRefLog(user, repository, Arrays.asList(cmd)); + } catch (Throwable t) { + error(t, repository, "Failed to commit reflog entry to {0}"); + } + return false; + } + /** * Updates the reflog with the received commands. * diff --git a/src/main/java/com/gitblit/wicket/panels/BranchesPanel.java b/src/main/java/com/gitblit/wicket/panels/BranchesPanel.java index 1262077c..7aa185bd 100644 --- a/src/main/java/com/gitblit/wicket/panels/BranchesPanel.java +++ b/src/main/java/com/gitblit/wicket/panels/BranchesPanel.java @@ -37,7 +37,9 @@ import com.gitblit.SyndicationServlet; 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; @@ -191,15 +193,28 @@ public class BranchesPanel extends BasePanel { } return; } - boolean success = JGitUtils.deleteBranchRef(r, entry.getName()); + final String branch = entry.getName(); + boolean success = JGitUtils.deleteBranchRef(r, branch); + if (success) { + // clear commit cache + CommitCache.instance().clear(repositoryModel.name, branch); + + // optionally update reflog + if (RefLogUtils.hasRefLogBranch(r)) { + UserModel user = GitBlitWebSession.get().getUser(); + success = RefLogUtils.deleteRef(user, r, branch); + } + } + r.close(); + if (success) { - info(MessageFormat.format("Branch \"{0}\" deleted", entry.displayName)); + info(MessageFormat.format("Branch \"{0}\" deleted", branch)); // redirect to the owning page setResponsePage(getPage().getClass(), WicketUtils.newRepositoryParameter(repositoryModel.name)); } else { - error(MessageFormat.format("Failed to delete branch \"{0}\"", entry.displayName)); + error(MessageFormat.format("Failed to delete branch \"{0}\"", branch)); } } }; diff --git a/src/main/java/com/gitblit/wicket/panels/ReflogPanel.java b/src/main/java/com/gitblit/wicket/panels/ReflogPanel.java index 55c19cc5..99ce55a1 100644 --- a/src/main/java/com/gitblit/wicket/panels/ReflogPanel.java +++ b/src/main/java/com/gitblit/wicket/panels/ReflogPanel.java @@ -30,6 +30,7 @@ import org.apache.wicket.markup.repeater.data.DataView; import org.apache.wicket.markup.repeater.data.ListDataProvider; import org.apache.wicket.model.StringResourceModel; import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.transport.ReceiveCommand.Type; import com.gitblit.Constants; import com.gitblit.GitBlit; @@ -154,7 +155,9 @@ public class ReflogPanel extends BasePanel { changeItem.add(new Label("whenChanged", fuzzydate + ", " + df.format(changeDate))); Label changeIcon = new Label("changeIcon"); - if (isTag) { + if (Type.DELETE.equals(change.getChangeType(fullRefName))) { + WicketUtils.setCssClass(changeIcon, "iconic-trash-stroke"); + } else if (isTag) { WicketUtils.setCssClass(changeIcon, "iconic-tag"); } else { WicketUtils.setCssClass(changeIcon, "iconic-upload"); -- cgit v1.2.3