From 756117adc7f5b4bd21948ede7ab0085aa42d5ccc Mon Sep 17 00:00:00 2001 From: James Moger Date: Tue, 7 Aug 2012 09:43:24 -0400 Subject: [PATCH] Fix to GitServlet to allow pushing to symlinked repositories (issue 116) --- docs/04_releases.mkd | 3 ++- src/com/gitblit/GitFilter.java | 22 ++++++++++++++++------ src/com/gitblit/GitServlet.java | 29 ++++++++++++----------------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/docs/04_releases.mkd b/docs/04_releases.mkd index a519175a..f5facc50 100644 --- a/docs/04_releases.mkd +++ b/docs/04_releases.mkd @@ -13,8 +13,9 @@ If you are updating from an earlier release AND you have indexed branches with t - Do not index blobs in submodules (issue 119) - Restore original user or team object on failure to update (issue 118) +- Fixes to relative path determination in repository search algorithm for symlinks (issue 116) +- Fix to GitServlet to allow pushing to symlinked repositories (issue 116) - Repository URL uses `X-Forwarded-Proto` and `X-Forwarded-Port`, if available, for reverse proxy configurations (issue 115) -- Fixes to relative path determination in repository searh algorithm for symlinks (issue 116) - Output real RAW content, not simulated RAW content (issue 114) - Fixed Lucene charset encoding bug when reindexing a repository (issue 112) - Fixed null pointer in LdapUserSerivce if account has a null email address (issue 110) diff --git a/src/com/gitblit/GitFilter.java b/src/com/gitblit/GitFilter.java index 4ae5b6c2..aa673462 100644 --- a/src/com/gitblit/GitFilter.java +++ b/src/com/gitblit/GitFilter.java @@ -32,11 +32,11 @@ import com.gitblit.utils.StringUtils; */ public class GitFilter extends AccessRestrictionFilter { - protected final String gitReceivePack = "/git-receive-pack"; + protected static final String gitReceivePack = "/git-receive-pack"; - protected final String gitUploadPack = "/git-upload-pack"; + protected static final String gitUploadPack = "/git-upload-pack"; - protected final String[] suffixes = { gitReceivePack, gitUploadPack, "/info/refs", "/HEAD", + protected static final String[] suffixes = { gitReceivePack, gitUploadPack, "/info/refs", "/HEAD", "/objects" }; /** @@ -45,9 +45,8 @@ public class GitFilter extends AccessRestrictionFilter { * @param url * @return repository name */ - @Override - protected String extractRepositoryName(String url) { - String repository = url; + public static String getRepositoryName(String value) { + String repository = value; // get the repository name from the url by finding a known url suffix for (String urlSuffix : suffixes) { if (repository.indexOf(urlSuffix) > -1) { @@ -57,6 +56,17 @@ public class GitFilter extends AccessRestrictionFilter { return repository; } + /** + * Extract the repository name from the url. + * + * @param url + * @return repository name + */ + @Override + protected String extractRepositoryName(String url) { + return GitFilter.getRepositoryName(url); + } + /** * Analyze the url and returns the action of the request. Return values are * either "/git-receive-pack" or "/git-upload-pack". diff --git a/src/com/gitblit/GitServlet.java b/src/com/gitblit/GitServlet.java index 0b5575bc..d81703d0 100644 --- a/src/com/gitblit/GitServlet.java +++ b/src/com/gitblit/GitServlet.java @@ -51,7 +51,6 @@ import org.slf4j.LoggerFactory; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; import com.gitblit.utils.ClientLogger; -import com.gitblit.utils.FileUtils; import com.gitblit.utils.HttpUtils; import com.gitblit.utils.StringUtils; @@ -94,9 +93,16 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { @Override public ReceivePack create(HttpServletRequest req, Repository db) throws ServiceNotEnabledException, ServiceNotAuthorizedException { - ReceivePack rp = super.create(req, db); + + // determine repository name from request + String repositoryName = req.getPathInfo().substring(1); + repositoryName = GitFilter.getRepositoryName(repositoryName); + GitblitReceiveHook hook = new GitblitReceiveHook(); + hook.repositoryName = repositoryName; hook.gitblitUrl = HttpUtils.getGitblitURL(req); + + ReceivePack rp = super.create(req, db); rp.setPreReceiveHook(hook); rp.setPostReceiveHook(hook); return rp; @@ -158,6 +164,8 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { protected final Logger logger = LoggerFactory.getLogger(GitblitReceiveHook.class); + protected String repositoryName; + protected String gitblitUrl; /** @@ -167,7 +175,7 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { */ @Override public void onPreReceive(ReceivePack rp, Collection commands) { - RepositoryModel repository = getRepositoryModel(rp); + RepositoryModel repository = GitBlit.self().getRepositoryModel(repositoryName); Set scripts = new LinkedHashSet(); scripts.addAll(GitBlit.self().getPreReceiveScriptsInherited(repository)); scripts.addAll(repository.preReceiveScripts); @@ -195,7 +203,7 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { logger.info("skipping post-receive hooks, no refs created, updated, or removed"); return; } - RepositoryModel repository = getRepositoryModel(rp); + RepositoryModel repository = GitBlit.self().getRepositoryModel(repositoryName); Set scripts = new LinkedHashSet(); scripts.addAll(GitBlit.self().getPostReceiveScriptsInherited(repository)); scripts.addAll(repository.postReceiveScripts); @@ -206,19 +214,6 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { // runNativeScript(rp, "hooks/post-receive", commands); } - /** - * Returns the RepositoryModel for the repository we are pushing into. - * - * @param rp - * @return a RepositoryModel - */ - protected RepositoryModel getRepositoryModel(ReceivePack rp) { - Repository repository = rp.getRepository(); - String repositoryName = FileUtils.getRelativePath(GitBlit.getRepositoriesFolder(), repository.getDirectory()); - RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName); - return model; - } - /** * Returns the UserModel for the user pushing the changes. * -- 2.39.5