From e523d77e5f9d61a7cbf0b2242c8795a22fdf7b45 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Wed, 31 Jul 2013 15:56:10 -0400
Subject: [PATCH] Manually redirect after branch deletion (issue 282)

---
 src/main/java/com/gitblit/utils/RefLogUtils.java |   55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 54 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/gitblit/utils/RefLogUtils.java b/src/main/java/com/gitblit/utils/RefLogUtils.java
index 73b7146..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
@@ -184,7 +232,7 @@
 				PersonIdent ident;
 				if (UserModel.ANONYMOUS.equals(user)) {
 					// anonymous push
-					ident = new PersonIdent("anonymous", "anonymous");
+					ident = new PersonIdent(user.username + "/" + user.username, user.username);
 				} else {
 					// construct real pushing account
 					ident =	new PersonIdent(MessageFormat.format("{0}/{1}", user.getDisplayName(), user.username),
@@ -548,8 +596,13 @@
         Map<String, DailyLogEntry> dailydigests = new HashMap<String, DailyLogEntry>();
         String linearParent = null;
         for (RefModel local : JGitUtils.getLocalBranches(repository, true, -1)) {
+        	if (!local.getDate().after(minimumDate)) {
+				// branch not recently updated
+        		continue;
+        	}
             String branch = local.getName();
             List<RepositoryCommit> commits = CommitCache.instance().getCommits(repositoryName, repository,  branch, minimumDate);
+            linearParent = null;
             for (RepositoryCommit commit : commits) {
             	if (linearParent != null) {
             		if (!commit.getName().equals(linearParent)) {

--
Gitblit v1.9.1