From 1aabf0a23f112f06cd7ea5e43a11c11bd177872b Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Tue, 27 Mar 2012 16:51:35 -0400
Subject: [PATCH] Implemented support for "default" branch

---
 src/com/gitblit/wicket/pages/EditRepositoryPage.java |    7 ++-
 src/com/gitblit/client/RepositoriesPanel.java        |    9 +++-
 src/com/gitblit/LuceneExecutor.java                  |   63 ++++++++++++++++++++++++++++---
 src/com/gitblit/Constants.java                       |    2 +
 4 files changed, 70 insertions(+), 11 deletions(-)

diff --git a/src/com/gitblit/Constants.java b/src/com/gitblit/Constants.java
index 879c5a4..929ab64 100644
--- a/src/com/gitblit/Constants.java
+++ b/src/com/gitblit/Constants.java
@@ -70,6 +70,8 @@
 	
 	public static final int LEN_SHORTLOG_REFS = 60;
 	
+	public static final String DEFAULT_BRANCH = "default";
+	
 	public static String getGitBlitVersion() {
 		return NAME + " v" + VERSION;
 	}
diff --git a/src/com/gitblit/LuceneExecutor.java b/src/com/gitblit/LuceneExecutor.java
index 25ed9da..961267b 100644
--- a/src/com/gitblit/LuceneExecutor.java
+++ b/src/com/gitblit/LuceneExecutor.java
@@ -447,7 +447,7 @@
 			ObjectId defaultBranchId = JGitUtils.getDefaultBranch(repository);
 			for (RefModel branch :  branches) {
 				if (branch.getObjectId().equals(defaultBranchId)) {
-					defaultBranch = branch;					
+					defaultBranch = branch;
 					break;
 				}
 			}
@@ -457,8 +457,22 @@
 			// walk through each branch
 			for (RefModel branch : branches) {
 
+				boolean indexBranch = false;
+				if (model.indexedBranches.contains(com.gitblit.Constants.DEFAULT_BRANCH)
+						&& branch.equals(defaultBranch)) {
+					// indexing "default" branch
+					indexBranch = true;
+				} else if (IssueUtils.GB_ISSUES.equals(branch)) {
+					// skip the GB_ISSUES branch because it is indexed later
+					// note: this is different than updateIndex
+					indexBranch = false;
+				} else {
+					// normal explicit branch check
+					indexBranch = model.indexedBranches.contains(branch.getName());
+				}
+				
 				// if this branch is not specifically indexed then skip
-				if (!model.indexedBranches.contains(branch.getName())) {
+				if (!indexBranch) {
 					continue;
 				}
 
@@ -782,20 +796,55 @@
 				deletedBranches.add(branch);
 			}
 
-			// walk through each branches
+			// get the local branches
 			List<RefModel> branches = JGitUtils.getLocalBranches(repository, true, -1);
+			
+			// sort them by most recently updated
+			Collections.sort(branches, new Comparator<RefModel>() {
+				@Override
+				public int compare(RefModel ref1, RefModel ref2) {
+					return ref2.getDate().compareTo(ref1.getDate());
+				}
+			});
+						
+			// reorder default branch to first position
+			RefModel defaultBranch = null;
+			ObjectId defaultBranchId = JGitUtils.getDefaultBranch(repository);
+			for (RefModel branch :  branches) {
+				if (branch.getObjectId().equals(defaultBranchId)) {
+					defaultBranch = branch;
+					break;
+				}
+			}
+			branches.remove(defaultBranch);
+			branches.add(0, defaultBranch);
+			
+			// walk through each branches
 			for (RefModel branch : branches) {
 				String branchName = branch.getName();
 
-				// determine if we should skip this branch
-				if (!IssueUtils.GB_ISSUES.equals(branch)
-						&& !model.indexedBranches.contains(branch.getName())) {
+				boolean indexBranch = false;
+				if (model.indexedBranches.contains(com.gitblit.Constants.DEFAULT_BRANCH)
+						&& branch.equals(defaultBranch)) {
+					// indexing "default" branch
+					indexBranch = true;
+				} else if (IssueUtils.GB_ISSUES.equals(branch)) {
+					// update issues modified on the GB_ISSUES branch
+					// note: this is different than reindex
+					indexBranch = true;
+				} else {
+					// normal explicit branch check
+					indexBranch = model.indexedBranches.contains(branch.getName());
+				}
+				
+				// if this branch is not specifically indexed then skip
+				if (!indexBranch) {
 					continue;
 				}
 				
 				// 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);
diff --git a/src/com/gitblit/client/RepositoriesPanel.java b/src/com/gitblit/client/RepositoriesPanel.java
index 0643b60..685a70a 100644
--- a/src/com/gitblit/client/RepositoriesPanel.java
+++ b/src/com/gitblit/client/RepositoriesPanel.java
@@ -27,6 +27,7 @@
 import java.awt.event.MouseEvent;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import javax.swing.JButton;
@@ -44,6 +45,7 @@
 import javax.swing.table.TableCellRenderer;
 import javax.swing.table.TableRowSorter;
 
+import com.gitblit.Constants;
 import com.gitblit.Constants.RpcRequest;
 import com.gitblit.models.FeedModel;
 import com.gitblit.models.RepositoryModel;
@@ -357,7 +359,7 @@
 		dialog.setTeams(gitblit.getTeamnames(), null);
 		dialog.setRepositories(gitblit.getRepositories());
 		dialog.setFederationSets(gitblit.getFederationSets(), null);
-		dialog.setIndexedBranches(new ArrayList<String>(), null);
+		dialog.setIndexedBranches(new ArrayList<String>(Arrays.asList(Constants.DEFAULT_BRANCH)), null);
 		dialog.setPreReceiveScripts(gitblit.getPreReceiveScriptsUnused(null),
 				gitblit.getPreReceiveScriptsInherited(null), null);
 		dialog.setPostReceiveScripts(gitblit.getPostReceiveScriptsUnused(null),
@@ -420,7 +422,10 @@
 		dialog.setTeams(gitblit.getTeamnames(), gitblit.getPermittedTeamnames(repository));
 		dialog.setRepositories(gitblit.getRepositories());
 		dialog.setFederationSets(gitblit.getFederationSets(), repository.federationSets);
-		dialog.setIndexedBranches(repository.getLocalBranches(), repository.indexedBranches);
+		List<String> allLocalBranches = new ArrayList<String>();
+		allLocalBranches.add(Constants.DEFAULT_BRANCH);
+		allLocalBranches.addAll(repository.getLocalBranches());
+		dialog.setIndexedBranches(allLocalBranches, repository.indexedBranches);
 		dialog.setPreReceiveScripts(gitblit.getPreReceiveScriptsUnused(repository),
 				gitblit.getPreReceiveScriptsInherited(repository), repository.preReceiveScripts);
 		dialog.setPostReceiveScripts(gitblit.getPostReceiveScriptsUnused(repository),
diff --git a/src/com/gitblit/wicket/pages/EditRepositoryPage.java b/src/com/gitblit/wicket/pages/EditRepositoryPage.java
index ded1545..c4bacc6 100644
--- a/src/com/gitblit/wicket/pages/EditRepositoryPage.java
+++ b/src/com/gitblit/wicket/pages/EditRepositoryPage.java
@@ -42,6 +42,7 @@
 
 import com.gitblit.Constants.AccessRestrictionType;
 import com.gitblit.Constants.FederationStrategy;
+import com.gitblit.Constants;
 import com.gitblit.GitBlit;
 import com.gitblit.GitBlitException;
 import com.gitblit.Keys;
@@ -116,12 +117,14 @@
 				new StringChoiceRenderer(), 8, false);
 
 		// indexed local branches palette
-		List<String> allLocalBranches = repositoryModel.getLocalBranches();
+		List<String> allLocalBranches = new ArrayList<String>();
+		allLocalBranches.add(Constants.DEFAULT_BRANCH);
+		allLocalBranches.addAll(repositoryModel.getLocalBranches());
 		boolean luceneEnabled = GitBlit.getBoolean(Keys.web.allowLuceneIndexing, true);
 		final Palette<String> indexedBranchesPalette = new Palette<String>("indexedBranches", new ListModel<String>(
 				indexedBranches), new CollectionModel<String>(allLocalBranches),
 				new StringChoiceRenderer(), 8, false);
-		indexedBranchesPalette.setEnabled(luceneEnabled && (allLocalBranches.size() > 0));
+		indexedBranchesPalette.setEnabled(luceneEnabled);
 		
 		// federation sets palette
 		List<String> sets = GitBlit.getStrings(Keys.federation.sets);

--
Gitblit v1.9.1