From 3eb83d71a5a1ed8385a606c7bdede066b1a00d72 Mon Sep 17 00:00:00 2001 From: saheba Date: Mon, 25 Mar 2013 21:32:06 +0100 Subject: added attribute useIncrementalRevisionNumbers to repository model --- src/com/gitblit/GitBlit.java | 2 ++ src/com/gitblit/client/EditRepositoryDialog.java | 7 +++++++ src/com/gitblit/models/RepositoryModel.java | 2 ++ src/com/gitblit/wicket/GitBlitWebApp.properties | 2 ++ src/com/gitblit/wicket/pages/EditRepositoryPage.html | 1 + src/com/gitblit/wicket/pages/EditRepositoryPage.java | 1 + 6 files changed, 15 insertions(+) diff --git a/src/com/gitblit/GitBlit.java b/src/com/gitblit/GitBlit.java index 4cfd61e4..3ac8d170 100644 --- a/src/com/gitblit/GitBlit.java +++ b/src/com/gitblit/GitBlit.java @@ -1668,6 +1668,7 @@ public class GitBlit implements ServletContextListener { model.addOwners(ArrayUtils.fromString(getConfig(config, "owner", ""))); model.useTickets = getConfig(config, "useTickets", false); model.useDocs = getConfig(config, "useDocs", false); + model.useIncrementalRevisionNumbers = getConfig(config, "useIncrementalRevisionNumbers", false); model.allowForks = getConfig(config, "allowForks", true); model.accessRestriction = AccessRestrictionType.fromName(getConfig(config, "accessRestriction", settings.getString(Keys.git.defaultAccessRestriction, null))); @@ -2186,6 +2187,7 @@ public class GitBlit implements ServletContextListener { config.setString(Constants.CONFIG_GITBLIT, null, "owner", ArrayUtils.toString(repository.owners)); config.setBoolean(Constants.CONFIG_GITBLIT, null, "useTickets", repository.useTickets); config.setBoolean(Constants.CONFIG_GITBLIT, null, "useDocs", repository.useDocs); + config.setBoolean(Constants.CONFIG_GITBLIT, null, "useIncrementalRevisionNumbers", repository.useIncrementalRevisionNumbers); config.setBoolean(Constants.CONFIG_GITBLIT, null, "allowForks", repository.allowForks); config.setString(Constants.CONFIG_GITBLIT, null, "accessRestriction", repository.accessRestriction.name()); config.setString(Constants.CONFIG_GITBLIT, null, "authorizationControl", repository.authorizationControl.name()); diff --git a/src/com/gitblit/client/EditRepositoryDialog.java b/src/com/gitblit/client/EditRepositoryDialog.java index 8851de43..25f42f4a 100644 --- a/src/com/gitblit/client/EditRepositoryDialog.java +++ b/src/com/gitblit/client/EditRepositoryDialog.java @@ -92,6 +92,8 @@ public class EditRepositoryDialog extends JDialog { private JCheckBox useDocs; + private JCheckBox useIncrementalRevisionNumbers; + private JCheckBox showRemoteBranches; private JCheckBox showReadme; @@ -212,6 +214,8 @@ public class EditRepositoryDialog extends JDialog { anRepository.useTickets); useDocs = new JCheckBox(Translation.get("gb.useDocsDescription"), anRepository.useDocs); + useIncrementalRevisionNumbers = new JCheckBox(Translation.get("gb.useIncrementalRevisionNumbersDescription"), + anRepository.useIncrementalRevisionNumbers); showRemoteBranches = new JCheckBox( Translation.get("gb.showRemoteBranchesDescription"), anRepository.showRemoteBranches); @@ -309,6 +313,8 @@ public class EditRepositoryDialog extends JDialog { useTickets)); fieldsPanel .add(newFieldPanel(Translation.get("gb.enableDocs"), useDocs)); + fieldsPanel + .add(newFieldPanel(Translation.get("gb.enableIncrementalRevisionNumbers"), useIncrementalRevisionNumbers)); fieldsPanel.add(newFieldPanel(Translation.get("gb.showRemoteBranches"), showRemoteBranches)); fieldsPanel.add(newFieldPanel(Translation.get("gb.showReadme"), @@ -563,6 +569,7 @@ public class EditRepositoryDialog extends JDialog { repository.gcThreshold = gcThreshold.getText(); repository.useTickets = useTickets.isSelected(); repository.useDocs = useDocs.isSelected(); + repository.useIncrementalRevisionNumbers = useIncrementalRevisionNumbers.isSelected(); repository.showRemoteBranches = showRemoteBranches.isSelected(); repository.showReadme = showReadme.isSelected(); repository.skipSizeCalculation = skipSizeCalculation.isSelected(); diff --git a/src/com/gitblit/models/RepositoryModel.java b/src/com/gitblit/models/RepositoryModel.java index a2dab3c5..1f2ee2df 100644 --- a/src/com/gitblit/models/RepositoryModel.java +++ b/src/com/gitblit/models/RepositoryModel.java @@ -50,6 +50,7 @@ public class RepositoryModel implements Serializable, Comparable
+ diff --git a/src/com/gitblit/wicket/pages/EditRepositoryPage.java b/src/com/gitblit/wicket/pages/EditRepositoryPage.java index d68d6550..2cbb88cc 100644 --- a/src/com/gitblit/wicket/pages/EditRepositoryPage.java +++ b/src/com/gitblit/wicket/pages/EditRepositoryPage.java @@ -422,6 +422,7 @@ public class EditRepositoryPage extends RootSubPage { new FederationTypeRenderer())); form.add(new CheckBox("useTickets")); form.add(new CheckBox("useDocs")); + form.add(new CheckBox("useIncrementalRevisionNumbers")); form.add(new CheckBox("showRemoteBranches")); form.add(new CheckBox("showReadme")); form.add(new CheckBox("skipSizeCalculation")); -- cgit v1.2.3 From 99f359e07087d826fdb024e4d205c2cb645f1d2a Mon Sep 17 00:00:00 2001 From: saheba Date: Mon, 25 Mar 2013 21:32:53 +0100 Subject: +feature: automatic tagging of each push with an incremental revision number per repository --- src/com/gitblit/GitServlet.java | 40 +++++++++++++++++++-- src/com/gitblit/utils/JGitUtils.java | 70 ++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 2 deletions(-) diff --git a/src/com/gitblit/GitServlet.java b/src/com/gitblit/GitServlet.java index 77be963f..9c7fb425 100644 --- a/src/com/gitblit/GitServlet.java +++ b/src/com/gitblit/GitServlet.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.text.MessageFormat; import java.util.Collection; import java.util.Enumeration; +import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -32,7 +33,9 @@ import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.Part; +import org.eclipse.jgit.api.Git; import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory; import org.eclipse.jgit.http.server.resolver.DefaultUploadPackFactory; import org.eclipse.jgit.lib.PersonIdent; @@ -81,6 +84,11 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { private File groovyDir; + @Override + public void destroy() { + super.destroy(); + } + @Override public void init(ServletConfig config) throws ServletException { groovyDir = GitBlit.getGroovyScriptsFolder(); @@ -100,8 +108,9 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { @Override public ReceivePack create(HttpServletRequest req, Repository db) throws ServiceNotEnabledException, ServiceNotAuthorizedException { - // determine repository name from request + org.eclipse.jgit.http.server.glue.WrappedRequest wrreq = (org.eclipse.jgit.http.server.glue.WrappedRequest) req; + String repositoryName = req.getPathInfo().substring(1); repositoryName = GitFilter.getRepositoryName(repositoryName); @@ -163,6 +172,7 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { return up; } }); + super.init(new GitblitServletConfig(config)); } @@ -296,7 +306,33 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { UserModel user = getUserModel(rp); RepositoryModel repository = GitBlit.self().getRepositoryModel(repositoryName); - + + if (repository.useIncrementalRevisionNumbers) { + List allCommands = rp.getAllCommands(); + String cmds = ""; + for (ReceiveCommand receiveCommand : allCommands) { + cmds += receiveCommand.getType() + "_" + + receiveCommand.getResult() + "_" + + receiveCommand.getMessage() + ", "; + if (receiveCommand.getType().equals( + ReceiveCommand.Type.UPDATE) + && receiveCommand.getResult().equals( + ReceiveCommand.Result.OK)) { + String objectId = receiveCommand.getNewId().toString() + .replace("AnyObjectId[", "").replace("]", ""); + System.err.println("SHB id " + objectId); + System.err.println("SHB id " + + objectId.getBytes().length); + // if type=update and update was ok, autotag + boolean result = JGitUtils + .createIncrementalRevisionTag( + rp.getRepository(), objectId); + System.err.println("SHB res " + result); + } + } + System.err.println("SHB cmds: " + cmds); + } + // log ref changes for (ReceiveCommand cmd : commands) { if (Result.OK.equals(cmd.getResult())) { diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java index 1f2ae943..4326700d 100644 --- a/src/com/gitblit/utils/JGitUtils.java +++ b/src/com/gitblit/utils/JGitUtils.java @@ -25,6 +25,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -33,6 +34,7 @@ import java.util.regex.Pattern; import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.FetchCommand; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.TagCommand; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.diff.DiffEntry.ChangeType; @@ -80,6 +82,8 @@ import org.eclipse.jgit.util.io.DisabledOutputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.gitblit.GitBlit; +import com.gitblit.Keys; import com.gitblit.models.GitNote; import com.gitblit.models.PathModel; import com.gitblit.models.PathModel.PathChangeModel; @@ -94,6 +98,7 @@ import com.gitblit.models.SubmoduleModel; */ public class JGitUtils { + private static final String REVISION_TAG_PREFIX = "rev_"; static final Logger LOGGER = LoggerFactory.getLogger(JGitUtils.class); /** @@ -1687,7 +1692,72 @@ public class JGitUtils { } return list; } + + /** + * this method creates an incremental revision number as a tag according to + * the amount of already existing tags, which start with a defined prefix {@link REVISION_TAG_PREFIX} + * + * @param repository + * @param objectId + * @return true if operation was successful, otherwise false + */ + public static boolean createIncrementalRevisionTag(Repository repository, String objectId) { + boolean result = false; + Iterator> iterator = repository.getTags().entrySet().iterator(); + long revisionNumber = 1; + while (iterator.hasNext()) { + Entry entry = iterator.next(); + if (entry.getKey().startsWith(REVISION_TAG_PREFIX)) { + revisionNumber++; + } + } + result = createTag(repository,REVISION_TAG_PREFIX+revisionNumber,objectId); + return result; + } + /** + * creates a tag in a repository referring to the current head + * + * @param repository + * @param tag, the string label + * @return boolean, true if operation was successful, otherwise false + */ + public static boolean createTag(Repository repository, String tag) { + return createTag(repository, tag, null); + } + + /** + * creates a tag in a repository + * + * @param repository + * @param tag, the string label + * @param objectId, the ref the tag points towards + * @return boolean, true if operation was successful, otherwise false + */ + public static boolean createTag(Repository repository, String tag, + String objectId) { + try { + PersonIdent author = new PersonIdent("GitblitAutoTagPush", + "gitblit@localhost"); + + LOGGER.debug("createTag in repo: "+repository.getDirectory().getAbsolutePath()); + Git gitClient = Git.open(repository.getDirectory()); + TagCommand tagCommand = gitClient.tag(); + tagCommand.setTagger(author); + tagCommand.setMessage("autotag"); + if (objectId != null) { + RevObject revObj = getCommit(repository, objectId); + tagCommand.setObjectId(revObj); + } + tagCommand.setName(tag); + Ref call = tagCommand.call(); + return call != null ? true : false; + } catch (Exception e) { + e.printStackTrace(); + } + return false; + } + /** * Create an orphaned branch in a repository. * -- cgit v1.2.3 From 2a8a74bedafdb56b7e7edcb42642dc1eb5e54fff Mon Sep 17 00:00:00 2001 From: saheba Date: Mon, 25 Mar 2013 21:34:45 +0100 Subject: code cleanup --- src/com/gitblit/GitServlet.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/com/gitblit/GitServlet.java b/src/com/gitblit/GitServlet.java index 9c7fb425..b8e12066 100644 --- a/src/com/gitblit/GitServlet.java +++ b/src/com/gitblit/GitServlet.java @@ -83,11 +83,6 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { private GroovyScriptEngine gse; private File groovyDir; - - @Override - public void destroy() { - super.destroy(); - } @Override public void init(ServletConfig config) throws ServletException { @@ -109,8 +104,6 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { public ReceivePack create(HttpServletRequest req, Repository db) throws ServiceNotEnabledException, ServiceNotAuthorizedException { // determine repository name from request - org.eclipse.jgit.http.server.glue.WrappedRequest wrreq = (org.eclipse.jgit.http.server.glue.WrappedRequest) req; - String repositoryName = req.getPathInfo().substring(1); repositoryName = GitFilter.getRepositoryName(repositoryName); -- cgit v1.2.3 From e0c37df7ca2fe51bb538bdfe2412f74d5880e17d Mon Sep 17 00:00:00 2001 From: saheba Date: Mon, 25 Mar 2013 21:36:23 +0100 Subject: code cleanup --- src/com/gitblit/GitServlet.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/com/gitblit/GitServlet.java b/src/com/gitblit/GitServlet.java index b8e12066..cb47b323 100644 --- a/src/com/gitblit/GitServlet.java +++ b/src/com/gitblit/GitServlet.java @@ -311,19 +311,14 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { ReceiveCommand.Type.UPDATE) && receiveCommand.getResult().equals( ReceiveCommand.Result.OK)) { + // if type=update and update was ok, autotag String objectId = receiveCommand.getNewId().toString() .replace("AnyObjectId[", "").replace("]", ""); - System.err.println("SHB id " + objectId); - System.err.println("SHB id " - + objectId.getBytes().length); - // if type=update and update was ok, autotag boolean result = JGitUtils .createIncrementalRevisionTag( - rp.getRepository(), objectId); - System.err.println("SHB res " + result); + rp.getRepository(), objectId); } - } - System.err.println("SHB cmds: " + cmds); + } } // log ref changes -- cgit v1.2.3