]> source.dussan.org Git - gitblit.git/commitdiff
+feature: automatic tagging of each push with an incremental revision number per...
authorsaheba <mail@saheba.net>
Mon, 25 Mar 2013 20:32:53 +0000 (21:32 +0100)
committersaheba <mail@saheba.net>
Mon, 25 Mar 2013 20:32:53 +0000 (21:32 +0100)
src/com/gitblit/GitServlet.java
src/com/gitblit/utils/JGitUtils.java

index 77be963f0933f309eada352368d5aaf2201aaec2..9c7fb42537ba609b3f7e1915be0b95b787684c9f 100644 (file)
@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.text.MessageFormat;\r
 import java.util.Collection;\r
 import java.util.Enumeration;\r
+import java.util.Iterator;\r
 import java.util.LinkedHashSet;\r
 import java.util.List;\r
 import java.util.Map;\r
@@ -32,7 +33,9 @@ import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;\r
 import javax.servlet.ServletException;\r
 import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.Part;\r
 \r
+import org.eclipse.jgit.api.Git;\r
 import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory;\r
 import org.eclipse.jgit.http.server.resolver.DefaultUploadPackFactory;\r
 import org.eclipse.jgit.lib.PersonIdent;\r
@@ -81,6 +84,11 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet {
 \r
        private File groovyDir;\r
 \r
+       @Override\r
+       public void destroy() {\r
+               super.destroy();\r
+       }\r
+       \r
        @Override\r
        public void init(ServletConfig config) throws ServletException {\r
                groovyDir = GitBlit.getGroovyScriptsFolder();\r
@@ -100,8 +108,9 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet {
                        @Override\r
                        public ReceivePack create(HttpServletRequest req, Repository db)\r
                                        throws ServiceNotEnabledException, ServiceNotAuthorizedException {\r
-                               \r
                                // determine repository name from request\r
+                               org.eclipse.jgit.http.server.glue.WrappedRequest wrreq = (org.eclipse.jgit.http.server.glue.WrappedRequest) req;\r
+                               \r
                                String repositoryName = req.getPathInfo().substring(1);\r
                                repositoryName = GitFilter.getRepositoryName(repositoryName);\r
                                \r
@@ -163,6 +172,7 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet {
                                return up;\r
                        }\r
                });\r
+               \r
                super.init(new GitblitServletConfig(config));\r
        }\r
 \r
@@ -296,7 +306,33 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet {
 \r
                        UserModel user = getUserModel(rp);\r
                        RepositoryModel repository = GitBlit.self().getRepositoryModel(repositoryName);\r
-\r
+                       \r
+                       if (repository.useIncrementalRevisionNumbers) {\r
+                               List<ReceiveCommand> allCommands = rp.getAllCommands();\r
+                               String cmds = "";\r
+                               for (ReceiveCommand receiveCommand : allCommands) {\r
+                                       cmds += receiveCommand.getType() + "_"\r
+                                                       + receiveCommand.getResult() + "_"\r
+                                                       + receiveCommand.getMessage() + ", ";\r
+                                       if (receiveCommand.getType().equals(\r
+                                                       ReceiveCommand.Type.UPDATE)\r
+                                                       && receiveCommand.getResult().equals(\r
+                                                                       ReceiveCommand.Result.OK)) {\r
+                                               String objectId = receiveCommand.getNewId().toString()\r
+                                                               .replace("AnyObjectId[", "").replace("]", "");\r
+                                               System.err.println("SHB id " + objectId);\r
+                                               System.err.println("SHB id "\r
+                                                               + objectId.getBytes().length);\r
+                                               // if type=update and update was ok, autotag\r
+                                               boolean result = JGitUtils\r
+                                                               .createIncrementalRevisionTag(\r
+                                                                               rp.getRepository(), objectId);\r
+                                               System.err.println("SHB res " + result);\r
+                                       }\r
+                               }\r
+                               System.err.println("SHB cmds: " + cmds);\r
+                       }\r
+                       \r
                        // log ref changes\r
                        for (ReceiveCommand cmd : commands) {\r
                                if (Result.OK.equals(cmd.getResult())) {\r
index 1f2ae943be017b67e250950bce591e7ab479b913..4326700df28231a3665080b82cb0184b0217fcf1 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Arrays;
 import java.util.Collections;\r
 import java.util.Date;\r
 import java.util.HashMap;\r
+import java.util.Iterator;\r
 import java.util.List;\r
 import java.util.Map;\r
 import java.util.Map.Entry;\r
@@ -33,6 +34,7 @@ import java.util.regex.Pattern;
 import org.eclipse.jgit.api.CloneCommand;\r
 import org.eclipse.jgit.api.FetchCommand;\r
 import org.eclipse.jgit.api.Git;\r
+import org.eclipse.jgit.api.TagCommand;\r
 import org.eclipse.jgit.api.errors.GitAPIException;\r
 import org.eclipse.jgit.diff.DiffEntry;\r
 import org.eclipse.jgit.diff.DiffEntry.ChangeType;\r
@@ -80,6 +82,8 @@ import org.eclipse.jgit.util.io.DisabledOutputStream;
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
+import com.gitblit.GitBlit;\r
+import com.gitblit.Keys;\r
 import com.gitblit.models.GitNote;\r
 import com.gitblit.models.PathModel;\r
 import com.gitblit.models.PathModel.PathChangeModel;\r
@@ -94,6 +98,7 @@ import com.gitblit.models.SubmoduleModel;
  */\r
 public class JGitUtils {\r
 \r
+       private static final String REVISION_TAG_PREFIX = "rev_";\r
        static final Logger LOGGER = LoggerFactory.getLogger(JGitUtils.class);\r
 \r
        /**\r
@@ -1687,7 +1692,72 @@ public class JGitUtils {
                }\r
                return list;\r
        }\r
+       \r
+       /**\r
+        * this method creates an incremental revision number as a tag according to\r
+        * the amount of already existing tags, which start with a defined prefix {@link REVISION_TAG_PREFIX}\r
+        * \r
+        * @param repository\r
+        * @param objectId\r
+        * @return true if operation was successful, otherwise false\r
+        */\r
+       public static boolean createIncrementalRevisionTag(Repository repository, String objectId) {\r
+               boolean result = false;\r
+               Iterator<Entry<String, Ref>> iterator = repository.getTags().entrySet().iterator();\r
+               long revisionNumber = 1;\r
+               while (iterator.hasNext()) {\r
+                       Entry<String, Ref> entry = iterator.next();\r
+                       if (entry.getKey().startsWith(REVISION_TAG_PREFIX)) {\r
+                               revisionNumber++;\r
+                       }\r
+               }\r
+               result = createTag(repository,REVISION_TAG_PREFIX+revisionNumber,objectId);\r
+               return result;\r
+       }\r
 \r
+       /**\r
+        * creates a tag in a repository referring to the current head\r
+        * \r
+        * @param repository\r
+        * @param tag, the string label\r
+        * @return boolean, true if operation was successful, otherwise false\r
+        */\r
+       public static boolean createTag(Repository repository, String tag) {\r
+               return createTag(repository, tag, null);\r
+       }\r
+       \r
+       /**\r
+        * creates a tag in a repository\r
+        * \r
+        * @param repository\r
+        * @param tag, the string label\r
+        * @param objectId, the ref the tag points towards\r
+        * @return boolean, true if operation was successful, otherwise false\r
+        */\r
+       public static boolean createTag(Repository repository, String tag,\r
+                       String objectId) {\r
+               try {                   \r
+                       PersonIdent author = new PersonIdent("GitblitAutoTagPush",\r
+                                       "gitblit@localhost");\r
+\r
+                       LOGGER.debug("createTag in repo: "+repository.getDirectory().getAbsolutePath());\r
+                       Git gitClient = Git.open(repository.getDirectory());\r
+                       TagCommand tagCommand = gitClient.tag();\r
+                       tagCommand.setTagger(author);\r
+                       tagCommand.setMessage("autotag");\r
+                       if (objectId != null) {\r
+                               RevObject revObj = getCommit(repository, objectId);\r
+                               tagCommand.setObjectId(revObj);\r
+                       }\r
+                       tagCommand.setName(tag);\r
+                       Ref call = tagCommand.call();                   \r
+                       return call != null ? true : false;\r
+               } catch (Exception e) {\r
+                       e.printStackTrace();\r
+               }\r
+               return false;\r
+       }\r
+       \r
        /**\r
         * Create an orphaned branch in a repository.\r
         * \r