]> source.dussan.org Git - gitblit.git/commitdiff
Revise push/mirror tickets branch triggering
authorJames Moger <james.moger@gitblit.com>
Thu, 6 Mar 2014 18:36:23 +0000 (13:36 -0500)
committerJames Moger <james.moger@gitblit.com>
Thu, 6 Mar 2014 18:36:23 +0000 (13:36 -0500)
src/main/java/com/gitblit/git/GitblitReceivePack.java
src/main/java/com/gitblit/git/PatchsetReceivePack.java
src/main/java/com/gitblit/git/ReceiveCommandEvent.java [new file with mode: 0644]
src/main/java/com/gitblit/service/MirrorService.java
src/main/java/com/gitblit/tickets/BranchTicketService.java
src/main/java/com/gitblit/tickets/ITicketService.java

index 605632cf964de55936808a9891e30b7b22062534..e3e2faeb58506b3065aa5d12ac11b8256a0e7b83 100644 (file)
@@ -51,7 +51,6 @@ import com.gitblit.manager.IGitblit;
 import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.models.UserModel;\r
 import com.gitblit.tickets.BranchTicketService;\r
-import com.gitblit.tickets.BranchTicketService.TicketsBranchUpdated;\r
 import com.gitblit.utils.ArrayUtils;\r
 import com.gitblit.utils.ClientLogger;\r
 import com.gitblit.utils.CommitCache;\r
@@ -344,12 +343,13 @@ public class GitblitReceivePack extends ReceivePack implements PreReceiveHook, P
                } catch (Exception e) {\r
                        LOGGER.error(MessageFormat.format("Failed to update {0} pushlog", repository.name), e);\r
                }\r
-               \r
+\r
                // check for updates pushed to the BranchTicketService branch\r
+               // if the BranchTicketService is active it will reindex, as appropriate\r
                for (ReceiveCommand cmd : commands) {\r
                        if (Result.OK.equals(cmd.getResult())\r
                                        && BranchTicketService.BRANCH.equals(cmd.getRefName())) {\r
-                               rp.getRepository().fireEvent(new TicketsBranchUpdated(repository));\r
+                               rp.getRepository().fireEvent(new ReceiveCommandEvent(repository, cmd));\r
                        }\r
                }\r
 \r
index 2044f60497e30090f817ed20d66560568932cc8a..c0ab8aeb9f599fbf02c6c3463b5125290bcd6c44 100644 (file)
@@ -60,6 +60,7 @@ import com.gitblit.models.TicketModel.Patchset;
 import com.gitblit.models.TicketModel.PatchsetType;\r
 import com.gitblit.models.TicketModel.Status;\r
 import com.gitblit.models.UserModel;\r
+import com.gitblit.tickets.BranchTicketService;\r
 import com.gitblit.tickets.ITicketService;\r
 import com.gitblit.tickets.TicketMilestone;\r
 import com.gitblit.tickets.TicketNotifier;\r
@@ -105,7 +106,7 @@ public class PatchsetReceivePack extends GitblitReceivePack {
 \r
        protected final TicketNotifier ticketNotifier;\r
 \r
-       private boolean requireCleanMerge;\r
+       private boolean requireMergeablePatchset;\r
 \r
        public PatchsetReceivePack(IGitblit gitblit, Repository db, RepositoryModel repository, UserModel user) {\r
                super(gitblit, db, repository, user);\r
@@ -257,12 +258,26 @@ public class PatchsetReceivePack extends GitblitReceivePack {
        /** Execute commands to update references. */\r
        @Override\r
        protected void executeCommands() {\r
+               // we process patchsets unless the user is pushing something special\r
+               boolean processPatchsets = true;\r
+               for (ReceiveCommand cmd : filterCommands(Result.NOT_ATTEMPTED)) {\r
+                       if (ticketService instanceof BranchTicketService\r
+                                       && BranchTicketService.BRANCH.equals(cmd.getRefName())) {\r
+                               // the user is pushing an update to the BranchTicketService data\r
+                               processPatchsets = false;\r
+                       }\r
+               }\r
+\r
                // workaround for JGit's awful scoping choices\r
                //\r
                // reset the patchset refs to NOT_ATTEMPTED (see validateCommands)\r
                for (ReceiveCommand cmd : filterCommands(Result.OK)) {\r
                        if (isPatchsetRef(cmd.getRefName())) {\r
                                cmd.setResult(Result.NOT_ATTEMPTED);\r
+                       } else if (ticketService instanceof BranchTicketService\r
+                                       && BranchTicketService.BRANCH.equals(cmd.getRefName())) {\r
+                               // the user is pushing an update to the BranchTicketService data\r
+                               processPatchsets = false;\r
                        }\r
                }\r
 \r
@@ -292,7 +307,7 @@ public class PatchsetReceivePack extends GitblitReceivePack {
                                continue;\r
                        }\r
 \r
-                       if (isPatchsetRef(cmd.getRefName())) {\r
+                       if (isPatchsetRef(cmd.getRefName()) && processPatchsets) {\r
                                if (ticketService == null) {\r
                                        sendRejection(cmd, "Sorry, the ticket service is unavailable and can not accept patchsets at this time.");\r
                                        continue;\r
@@ -393,6 +408,8 @@ public class PatchsetReceivePack extends GitblitReceivePack {
                                for (ReceiveCommand cmd : toApply) {\r
                                        if (cmd.getResult() == Result.NOT_ATTEMPTED) {\r
                                                sendRejection(cmd, "lock error: {0}", err.getMessage());\r
+                                               LOGGER.error(MessageFormat.format("failed to lock {0}:{1}",\r
+                                                               repository.name, cmd.getRefName()), err);\r
                                        }\r
                                }\r
                        }\r
@@ -539,7 +556,7 @@ public class PatchsetReceivePack extends GitblitReceivePack {
                case MERGEABLE:\r
                        break;\r
                default:\r
-                       if (ticket == null || requireCleanMerge) {\r
+                       if (ticket == null || requireMergeablePatchset) {\r
                                sendError("");\r
                                sendError("Your patchset can not be cleanly merged into {0}.", forBranch);\r
                                sendError("Please rebase your patchset and push again.");\r
diff --git a/src/main/java/com/gitblit/git/ReceiveCommandEvent.java b/src/main/java/com/gitblit/git/ReceiveCommandEvent.java
new file mode 100644 (file)
index 0000000..84dabb3
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2014 gitblit.com.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gitblit.git;
+
+import org.eclipse.jgit.events.RefsChangedEvent;
+import org.eclipse.jgit.transport.ReceiveCommand;
+
+import com.gitblit.models.RepositoryModel;
+
+/**
+ * The event fired by other classes to allow this service to index tickets.
+ *
+ * @author James Moger
+ */
+public class ReceiveCommandEvent extends RefsChangedEvent {
+
+       public final RepositoryModel model;
+
+       public final ReceiveCommand cmd;
+
+       public ReceiveCommandEvent(RepositoryModel model, ReceiveCommand cmd) {
+               this.model = model;
+               this.cmd = cmd;
+       }
+}
\ No newline at end of file
index 1eb54202a9ce3617d617c8454051d62d74c7a115..cf9ccb5584b1695968f83d1cad566cc61c2d45fb 100644 (file)
@@ -28,6 +28,8 @@ import org.eclipse.jgit.lib.RefUpdate.Result;
 import org.eclipse.jgit.lib.Repository;\r
 import org.eclipse.jgit.lib.StoredConfig;\r
 import org.eclipse.jgit.transport.FetchResult;\r
+import org.eclipse.jgit.transport.ReceiveCommand;\r
+import org.eclipse.jgit.transport.ReceiveCommand.Type;\r
 import org.eclipse.jgit.transport.RemoteConfig;\r
 import org.eclipse.jgit.transport.TrackingRefUpdate;\r
 import org.slf4j.Logger;\r
@@ -35,11 +37,11 @@ import org.slf4j.LoggerFactory;
 \r
 import com.gitblit.IStoredSettings;\r
 import com.gitblit.Keys;\r
+import com.gitblit.git.ReceiveCommandEvent;\r
 import com.gitblit.manager.IRepositoryManager;\r
 import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.models.UserModel;\r
 import com.gitblit.tickets.BranchTicketService;\r
-import com.gitblit.tickets.BranchTicketService.TicketsBranchUpdated;\r
 import com.gitblit.utils.JGitUtils;\r
 \r
 /**\r
@@ -147,7 +149,7 @@ public class MirrorService implements Runnable {
                                FetchResult result = git.fetch().setRemote(mirror.getName()).setDryRun(testing).call();\r
                                Collection<TrackingRefUpdate> refUpdates = result.getTrackingRefUpdates();\r
                                if (refUpdates.size() > 0) {\r
-                                       boolean ticketBranchUpdated = false;\r
+                                       ReceiveCommand ticketBranchCmd = null;\r
                                        for (TrackingRefUpdate ru : refUpdates) {\r
                                                StringBuilder sb = new StringBuilder();\r
                                                sb.append("updated mirror ");\r
@@ -164,14 +166,33 @@ public class MirrorService implements Runnable {
                                                sb.append("..");\r
                                                sb.append(ru.getNewObjectId() == null ? "" : ru.getNewObjectId().abbreviate(7).name());\r
                                                logger.info(sb.toString());\r
-                                               \r
+\r
                                                if (BranchTicketService.BRANCH.equals(ru.getLocalName())) {\r
-                                                       ticketBranchUpdated = true;\r
+                                                       ReceiveCommand.Type type = null;\r
+                                                       switch (ru.getResult()) {\r
+                                                       case NEW:\r
+                                                               type = Type.CREATE;\r
+                                                               break;\r
+                                                       case FAST_FORWARD:\r
+                                                               type = Type.UPDATE;\r
+                                                               break;\r
+                                                       case FORCED:\r
+                                                               type = Type.UPDATE_NONFASTFORWARD;\r
+                                                               break;\r
+                                                       default:\r
+                                                               type = null;\r
+                                                               break;\r
+                                                       }\r
+\r
+                                                       if (type != null) {\r
+                                                               ticketBranchCmd = new ReceiveCommand(ru.getOldObjectId(),\r
+                                                                       ru.getNewObjectId(), ru.getLocalName(), type);\r
+                                                       }\r
                                                }\r
                                        }\r
-                                       \r
-                                       if (ticketBranchUpdated) {\r
-                                               repository.fireEvent(new TicketsBranchUpdated(model));\r
+\r
+                                       if (ticketBranchCmd != null) {\r
+                                               repository.fireEvent(new ReceiveCommandEvent(model, ticketBranchCmd));\r
                                        }\r
                                }\r
                        } catch (Exception e) {\r
index 60dca276fe87ad44dacba390bb2c83aef5a98316..fc0bd8f07b3aec2c98cc8e35b4933900fa104b5f 100644 (file)
@@ -27,6 +27,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
 
 import javax.inject.Inject;
@@ -50,15 +51,18 @@ import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.revwalk.RevTree;
 import org.eclipse.jgit.revwalk.RevWalk;
+import org.eclipse.jgit.transport.ReceiveCommand;
 import org.eclipse.jgit.treewalk.CanonicalTreeParser;
 import org.eclipse.jgit.treewalk.TreeWalk;
 
 import com.gitblit.Constants;
+import com.gitblit.git.ReceiveCommandEvent;
 import com.gitblit.manager.INotificationManager;
 import com.gitblit.manager.IRepositoryManager;
 import com.gitblit.manager.IRuntimeManager;
 import com.gitblit.manager.IUserManager;
 import com.gitblit.models.PathModel;
+import com.gitblit.models.PathModel.PathChangeModel;
 import com.gitblit.models.RefModel;
 import com.gitblit.models.RepositoryModel;
 import com.gitblit.models.TicketModel;
@@ -78,18 +82,6 @@ import com.gitblit.utils.StringUtils;
  */
 public class BranchTicketService extends ITicketService implements RefsChangedListener {
 
-       /**
-        *  The event fired by other classes to allow this service to index tickets.
-        */
-       public static class TicketsBranchUpdated extends RefsChangedEvent {
-               
-               public final RepositoryModel model;
-               
-               public TicketsBranchUpdated(RepositoryModel model) {
-                       this.model = model;
-               }
-       }
-       
        public static final String BRANCH = "refs/gitblit/tickets";
 
        private static final String JOURNAL = "journal.json";
@@ -111,7 +103,7 @@ public class BranchTicketService extends ITicketService implements RefsChangedLi
                                repositoryManager);
 
                lastAssignedId = new ConcurrentHashMap<String, AtomicLong>();
-               
+
                // register the branch ticket service for repository ref changes
                Repository.getGlobalListenerList().addRefsChangedListener(this);
        }
@@ -138,16 +130,60 @@ public class BranchTicketService extends ITicketService implements RefsChangedLi
        }
 
        /**
-        * Listen for refs changed events and reindex that repository.
+        * Listen for tickets branch changes and (re)index tickets, as appropriate
         */
        @Override
-       public void onRefsChanged(RefsChangedEvent event) {
-               if (!(event instanceof TicketsBranchUpdated)) {
+       public synchronized void onRefsChanged(RefsChangedEvent event) {
+               if (!(event instanceof ReceiveCommandEvent)) {
                        return;
                }
-               RepositoryModel repository = ((TicketsBranchUpdated) event).model;
+
+               ReceiveCommandEvent branchUpdate = (ReceiveCommandEvent) event;
+               RepositoryModel repository = branchUpdate.model;
+               ReceiveCommand cmd = branchUpdate.cmd;
                try {
-                       reindex(repository);
+                       switch (cmd.getType()) {
+                       case CREATE:
+                       case UPDATE_NONFASTFORWARD:
+                               // reindex everything
+                               reindex(repository);
+                               break;
+                       case UPDATE:
+                               // incrementally index ticket updates
+                               resetCaches(repository);
+                               long start = System.nanoTime();
+                               log.info("incrementally indexing {} ticket branch due to received ref update", repository.name);
+                               Repository db = repositoryManager.getRepository(repository.name);
+                               try {
+                                       Set<Long> ids = new HashSet<Long>();
+                                       List<PathChangeModel> paths = JGitUtils.getFilesInRange(db,
+                                                       cmd.getOldId().getName(), cmd.getNewId().getName());
+                                       for (PathChangeModel path : paths) {
+                                               String name = path.name.substring(path.name.lastIndexOf('/') + 1);
+                                               if (!JOURNAL.equals(name)) {
+                                                       continue;
+                                               }
+                                               String tid = path.path.split("/")[2];
+                                               long ticketId = Long.parseLong(tid);
+                                               if (!ids.contains(ticketId)) {
+                                                       ids.add(ticketId);
+                                                       TicketModel ticket = getTicket(repository, ticketId);
+                                                       log.info(MessageFormat.format("indexing ticket #{0,number,0}: {1}",
+                                                                       ticketId, ticket.title));
+                                                       indexer.index(ticket);
+                                               }
+                                       }
+                                       long end = System.nanoTime();
+                                       log.info("incremental indexing of {0} ticket(s) completed in {1} msecs",
+                                                       ids.size(), TimeUnit.NANOSECONDS.toMillis(end - start));
+                               } finally {
+                                       db.close();
+                               }
+                               break;
+                       default:
+                               log.warn("Unexpected receive type {} in BranchTicketService.onRefsChanged" + cmd.getType());
+                               break;
+                       }
                } catch (Exception e) {
                        log.error("failed to reindex " + repository.name, e);
                }
index d04cd5e1d3af9d6ecc4963c446ed7ff1b5fb69eb..90f9c6dd3342644508ba48965ed822f2395a5979 100644 (file)
@@ -897,6 +897,7 @@ public abstract class ITicketService {
        public boolean deleteAll(RepositoryModel repository) {
                boolean success = deleteAllImpl(repository);
                if (success) {
+                       log.info("Deleted all tickets for {}", repository.name);
                        resetCaches(repository);
                        indexer.deleteAll(repository);
                }
@@ -936,6 +937,8 @@ public abstract class ITicketService {
                TicketModel ticket = getTicket(repository, ticketId);
                boolean success = deleteTicketImpl(repository, ticket, deletedBy);
                if (success) {
+                       log.info(MessageFormat.format("Deleted {0} ticket #{1,number,0}: {2}",
+                                       repository.name, ticketId, ticket.title));
                        ticketsCache.invalidate(new TicketKey(repository, ticketId));
                        indexer.delete(ticket);
                        return true;
@@ -1074,6 +1077,7 @@ public abstract class ITicketService {
                long end = System.nanoTime();
                long secs = TimeUnit.NANOSECONDS.toMillis(end - start);
                log.info("reindexing completed in {} msecs.", secs);
+               resetCaches(repository);
        }
 
        /**