From b938aeea1e892b9c95396ca0745ac2adb79ff78e Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Tue, 28 Feb 2012 17:22:40 -0500
Subject: [PATCH] Delete branch from index. Queue index update from the PostReceiveHook.

---
 src/com/gitblit/utils/LuceneUtils.java |   29 +++++++++++++++++++++++++----
 1 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/src/com/gitblit/utils/LuceneUtils.java b/src/com/gitblit/utils/LuceneUtils.java
index eaf02df..d463cdf 100644
--- a/src/com/gitblit/utils/LuceneUtils.java
+++ b/src/com/gitblit/utils/LuceneUtils.java
@@ -473,13 +473,24 @@
 				tags.get(tag.getReferencedObjectId().getName()).add(tag.displayName);
 			}
 
+			// detect branch deletion
+			// first assume all branches are deleted and then remove each
+			// existing branch from deletedBranches during indexing			
+			Set<String> deletedBranches = new TreeSet<String>();
+			for (String alias : config.getNames(CONF_ALIAS)) {
+				String branch = config.getString(CONF_ALIAS, null, alias);
+				deletedBranches.add(branch);
+			}
+			
+			// walk through each branches
 			List<RefModel> branches = JGitUtils.getLocalBranches(repository, true, -1);
-			// TODO detect branch deletion
-
-			// walk through each branch
 			for (RefModel branch : branches) {
-				// determine last commit
 				String branchName = branch.getName();
+
+				// remove this branch from the deletedBranches set
+				deletedBranches.remove(branchName);
+
+				// determine last commit				
 				String keyName = getBranchKey(branchName);
 				String lastCommit = config.getString(CONF_BRANCH, null, keyName);
 
@@ -504,6 +515,16 @@
 				config.setString(CONF_BRANCH, null, keyName, branch.getObjectId().getName());
 				config.save();
 			}
+			
+			// the deletedBranches set will normally be empty by this point
+			// unless a branch really was deleted and no longer exists
+			if (deletedBranches.size() > 0) {
+				for (String branch : deletedBranches) {
+					IndexWriter writer = getIndexWriter(repository, false);
+					writer.deleteDocuments(new Term(FIELD_BRANCH, branch));
+					writer.commit();
+				}
+			}
 			success = true;
 		} catch (Throwable t) {
 			t.printStackTrace();

--
Gitblit v1.9.1