From ab07d0d86d8b64b4f7c88b45bc81f1eec22105db Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 10 Apr 2014 19:00:05 -0400
Subject: [PATCH] Preserve key index when re-adding an existing key

---
 src/main/java/com/gitblit/git/GitblitReceivePack.java |   34 ++++++++++++++++++++++++++++++----
 1 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/gitblit/git/GitblitReceivePack.java b/src/main/java/com/gitblit/git/GitblitReceivePack.java
index 605632c..73da3d3 100644
--- a/src/main/java/com/gitblit/git/GitblitReceivePack.java
+++ b/src/main/java/com/gitblit/git/GitblitReceivePack.java
@@ -51,7 +51,6 @@
 import com.gitblit.models.RepositoryModel;
 import com.gitblit.models.UserModel;
 import com.gitblit.tickets.BranchTicketService;
-import com.gitblit.tickets.BranchTicketService.TicketsBranchUpdated;
 import com.gitblit.utils.ArrayUtils;
 import com.gitblit.utils.ClientLogger;
 import com.gitblit.utils.CommitCache;
@@ -125,6 +124,32 @@
 	}
 
 	/**
+	 * Returns true if the user is permitted to apply the receive commands to
+	 * the repository.
+	 *
+	 * @param commands
+	 * @return true if the user may push these commands
+	 */
+	protected boolean canPush(Collection<ReceiveCommand> commands) {
+		// TODO Consider supporting branch permissions here (issue-36)
+		// Not sure if that should be Gerrit-style, refs/meta/config, or
+		// gitolite-style, permissions in users.conf
+		//
+		// How could commands be empty?
+		//
+		// Because a subclass, like PatchsetReceivePack, filters receive
+		// commands before this method is called.  This makes it possible for
+		// this method to test an empty list.  In this case, we assume that the
+		// subclass receive pack properly enforces push restrictions. for the
+		// ref.
+		//
+		// The empty test is not explicitly required, it's written here to
+		// clarify special-case behavior.
+
+		return commands.isEmpty() ? true : user.canPush(repository);
+	}
+
+	/**
 	 * Instrumentation point where the incoming push event has been parsed,
 	 * validated, objects created BUT refs have not been updated. You might
 	 * use this to enforce a branch-write permissions model.
@@ -156,7 +181,7 @@
 			return;
 		}
 
-		if (!user.canPush(repository)) {
+		if (!canPush(commands)) {
 			// user does not have push permissions
 			for (ReceiveCommand cmd : commands) {
 				sendRejection(cmd, "User \"{0}\" does not have push permissions for \"{1}\"!", user.username, repository.name);
@@ -344,12 +369,13 @@
 		} catch (Exception e) {
 			LOGGER.error(MessageFormat.format("Failed to update {0} pushlog", repository.name), e);
 		}
-		
+
 		// check for updates pushed to the BranchTicketService branch
+		// if the BranchTicketService is active it will reindex, as appropriate
 		for (ReceiveCommand cmd : commands) {
 			if (Result.OK.equals(cmd.getResult())
 					&& BranchTicketService.BRANCH.equals(cmd.getRefName())) {
-				rp.getRepository().fireEvent(new TicketsBranchUpdated(repository));
+				rp.getRepository().fireEvent(new ReceiveCommandEvent(repository, cmd));
 			}
 		}
 

--
Gitblit v1.9.1