From 271a68b3b0ce6707130c77b5b009b3457d6b9d3b Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Mon, 22 Jul 2013 15:57:36 -0400
Subject: [PATCH] Update Gitblit reflog on branch delete from UI if Gitblit has an existing reflog

---
 src/main/java/com/gitblit/utils/RefLogUtils.java           |   48 ++++++++++++++++++++++++
 releases.moxie                                             |    2 +
 src/main/java/com/gitblit/wicket/panels/ReflogPanel.java   |    5 ++
 src/main/java/com/gitblit/wicket/panels/BranchesPanel.java |   23 +++++++++--
 4 files changed, 73 insertions(+), 5 deletions(-)

diff --git a/releases.moxie b/releases.moxie
index addf96e..17894a5 100644
--- a/releases.moxie
+++ b/releases.moxie
@@ -22,6 +22,8 @@
 	- Fixed Ubuntu service script for LSB compliance
 	- Inserted "sleep 5" in Ubuntu & Centos bash script for service restart
     changes:    
+	- Use trash icon in Gitblit Reflog for branch and tag deletion
+	- Update Gitblit Reflog on branch deletion from web UI
 	- updated Chinese translation
 	- updated Dutch translation
 	- updated Spanish translation
diff --git a/src/main/java/com/gitblit/utils/RefLogUtils.java b/src/main/java/com/gitblit/utils/RefLogUtils.java
index 643fbc0..dfb5f56 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.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.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;
@@ -95,6 +97,21 @@
 			parameters.add(0, repository.getDirectory().getAbsolutePath());
 		}
 		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;
 	}
 
 	/**
@@ -156,6 +173,37 @@
 	}
 	
 	/**
+	 * 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.
 	 * 
 	 * @param user
diff --git a/src/main/java/com/gitblit/wicket/panels/BranchesPanel.java b/src/main/java/com/gitblit/wicket/panels/BranchesPanel.java
index 1262077..7aa185b 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.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 @@
 					}
 					return;
 				}
-				boolean success = JGitUtils.deleteBranchRef(r, entry.getName());
-				r.close();
+				final String branch = entry.getName();
+				boolean success = JGitUtils.deleteBranchRef(r, branch);
 				if (success) {
-					info(MessageFormat.format("Branch \"{0}\" deleted", entry.displayName));
+					// 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", 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 55c19cc..99ce55a 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.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 @@
 				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");

--
Gitblit v1.9.1