From f01bc58183dca2378ffe931315396583d053c6be Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Tue, 16 Jul 2013 09:35:31 -0400
Subject: [PATCH] Reset dashboard and activity commit cache on branch REWIND or DELETE

---
 src/main/java/com/gitblit/utils/CommitCache.java |   17 +++++++++++++++++
 releases.moxie                                   |    1 +
 src/main/java/com/gitblit/git/ReceiveHook.java   |   17 +++++++++++++++++
 3 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/releases.moxie b/releases.moxie
index 711bf14..bff4eb1 100644
--- a/releases.moxie
+++ b/releases.moxie
@@ -13,6 +13,7 @@
 	- Gitblit-as-viewer with no repository urls failed to display summary page (issue 269)
 	- Fixed missing model class dependencies in Gitblit Manager build
 	- Fix for IE10 compatability mode
+	- Reset dashboard and activity commit cache on branch REWIND or DELETE
     changes: ~
     additions: ~
     dependencyChanges: ~
diff --git a/src/main/java/com/gitblit/git/ReceiveHook.java b/src/main/java/com/gitblit/git/ReceiveHook.java
index e0b7798..d75a238 100644
--- a/src/main/java/com/gitblit/git/ReceiveHook.java
+++ b/src/main/java/com/gitblit/git/ReceiveHook.java
@@ -36,6 +36,7 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.gitblit.Constants;
 import com.gitblit.Constants.AccessRestrictionType;
 import com.gitblit.GitBlit;
 import com.gitblit.Keys;
@@ -44,6 +45,7 @@
 import com.gitblit.models.UserModel;
 import com.gitblit.utils.ArrayUtils;
 import com.gitblit.utils.ClientLogger;
+import com.gitblit.utils.CommitCache;
 import com.gitblit.utils.JGitUtils;
 import com.gitblit.utils.RefLogUtils;
 import com.gitblit.utils.StringUtils;
@@ -186,6 +188,21 @@
 				return;
 			}
 		}
+		
+		// reset branch commit cache on REWIND and DELETE
+		for (ReceiveCommand cmd : commands) {
+			String ref = cmd.getRefName();
+			if (ref.startsWith(Constants.R_HEADS)) {
+				switch (cmd.getType()) {
+				case UPDATE_NONFASTFORWARD:
+				case DELETE:
+					CommitCache.instance().clear(repository.name, ref);
+					break;
+				default:
+					break;
+				}
+			}
+		}
 
 		Set<String> scripts = new LinkedHashSet<String>();
 		scripts.addAll(GitBlit.self().getPreReceiveScriptsInherited(repository));
diff --git a/src/main/java/com/gitblit/utils/CommitCache.java b/src/main/java/com/gitblit/utils/CommitCache.java
index 9db5f0c..e84506e 100644
--- a/src/main/java/com/gitblit/utils/CommitCache.java
+++ b/src/main/java/com/gitblit/utils/CommitCache.java
@@ -110,6 +110,23 @@
 	}
 	
 	/**
+	 * Clears the commit cache for a specific branch of a specific repository.
+	 * 
+	 * @param repositoryName
+	 * @param branch
+	 */
+	public void clear(String repositoryName, String branch) {
+		String repoKey = repositoryName.toLowerCase();
+		ObjectCache<List<RepositoryCommit>> repoCache = cache.get(repoKey);
+		if (repoCache != null) {
+			List<RepositoryCommit> commits = repoCache.remove(branch.toLowerCase());
+			if (!ArrayUtils.isEmpty(commits)) {
+				logger.info(MessageFormat.format("{0}:{1} commit cache cleared", repositoryName, branch));
+			}
+		}
+	}
+	
+	/**
 	 * Get all commits for the specified repository:branch that are in the cache.
 	 * 
 	 * @param repositoryName

--
Gitblit v1.9.1