From c0905b8a141c09b0c4776d32ce6fea32f1b0eda1 Mon Sep 17 00:00:00 2001 From: youngwan Date: Mon, 14 Nov 2011 10:43:34 +0900 Subject: [PATCH] added to view history of the specific file. --- .../scm/client/ClientChangesetHandler.java | 8 ++ .../java/sonia/scm/client/ScmUrlProvider.java | 24 +++- .../client/JerseyClientChangesetHandler.java | 37 ++++++ .../sonia/scm/repository/ChangesetViewer.java | 18 +++ .../scm/repository/ChangesetViewerUtil.java | 108 +++++++++++++++++- .../scm/repository/GitChangesetViewer.java | 70 ++++++++++++ .../scm/repository/HgChangesetViewer.java | 7 ++ .../scm/repository/SvnChangesetViewer.java | 25 +++- .../rest/resources/RepositoryResource.java | 14 ++- 9 files changed, 300 insertions(+), 11 deletions(-) diff --git a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ClientChangesetHandler.java b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ClientChangesetHandler.java index 866a0b3174..f77c3ba532 100644 --- a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ClientChangesetHandler.java +++ b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ClientChangesetHandler.java @@ -55,4 +55,12 @@ public interface ClientChangesetHandler * @return */ public ChangesetPagingResult getChangesets(int start, int limit); + + /** + * @param path + * @param start + * @param limit + * @return + */ + public ChangesetPagingResult getChangesets(String path, int start, int limit); } diff --git a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmUrlProvider.java b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmUrlProvider.java index 1382a0325e..b79f958808 100644 --- a/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmUrlProvider.java +++ b/scm-clients/scm-client-api/src/main/java/sonia/scm/client/ScmUrlProvider.java @@ -247,11 +247,12 @@ public class ScmUrlProvider * @param repositoryId * @since 1.8 * + * @param path * @param start * @param limit * @return */ - public String getRepositoryChangesetUrl(String repositoryId, int start, + public String getRepositoryChangesetUrl(String repositoryId, String path, int start, int limit) { String url = MessageFormat.format(getResourceUrl(URLPATTERN_CHANGESETS), @@ -268,10 +269,31 @@ public class ScmUrlProvider { url = url.concat(s).concat("limit=").concat(String.valueOf(limit)); } + + if (path != null) { + url = url.concat(s).concat("path=").concat(path); + } return url; } + /** + * Method description + * + * + * @param repositoryId + * @since 1.8 + * + * @param start + * @param limit + * @return + */ + public String getRepositoryChangesetUrl(String repositoryId, int start, + int limit) + { + return getRepositoryChangesetUrl(repositoryId, null, start, limit); + } + /** * Method description * diff --git a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientChangesetHandler.java b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientChangesetHandler.java index d451ecadca..d41328d411 100644 --- a/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientChangesetHandler.java +++ b/scm-clients/scm-client-impl/src/main/java/sonia/scm/client/JerseyClientChangesetHandler.java @@ -103,6 +103,43 @@ public class JerseyClientChangesetHandler implements ClientChangesetHandler return result; } + /** + * Method description + * + * + * @param start + * @param limit + * + * @return + */ + @Override + public ChangesetPagingResult getChangesets(String path, int start, int limit) + { + ChangesetPagingResult result = null; + String url = + session.getUrlProvider().getRepositoryChangesetUrl(repository.getId(), + start, limit); + WebResource resource = session.getClient().resource(url); + ClientResponse response = null; + + try + { + response = resource.get(ClientResponse.class); + + if (response.getStatus() != ScmClientException.SC_NOTFOUND) + { + ClientUtil.checkResponse(response, 200); + result = response.getEntity(ChangesetPagingResult.class); + } + } + finally + { + ClientUtil.close(response); + } + + return result; + } + //~--- fields --------------------------------------------------------------- /** Field description */ diff --git a/scm-core/src/main/java/sonia/scm/repository/ChangesetViewer.java b/scm-core/src/main/java/sonia/scm/repository/ChangesetViewer.java index ec91276384..63b734f897 100644 --- a/scm-core/src/main/java/sonia/scm/repository/ChangesetViewer.java +++ b/scm-core/src/main/java/sonia/scm/repository/ChangesetViewer.java @@ -60,4 +60,22 @@ public interface ChangesetViewer */ public ChangesetPagingResult getChangesets(int start, int max) throws IOException, RepositoryException; + + /** + * Method description + * + * + * + * + * @param path + * @param start + * @param max + * + * @return + * + * @throws IOException + * @throws RepositoryException + */ + public ChangesetPagingResult getChangesets(String path, int start, int max) + throws IOException, RepositoryException; } diff --git a/scm-core/src/main/java/sonia/scm/repository/ChangesetViewerUtil.java b/scm-core/src/main/java/sonia/scm/repository/ChangesetViewerUtil.java index c3e2d7ad47..8bee09e6a6 100644 --- a/scm-core/src/main/java/sonia/scm/repository/ChangesetViewerUtil.java +++ b/scm-core/src/main/java/sonia/scm/repository/ChangesetViewerUtil.java @@ -127,6 +127,38 @@ public class ChangesetViewerUtil extends PartCacheClearHook return getChangesets(repository, start, max); } + /** + * Method description + * + * + * @param repositoryId + * @param start + * @param max + * + * @return + * + * + * @throws IOException + * @throws NotSupportedFeatuerException + * @throws RepositoryException + */ + public ChangesetPagingResult getChangesets(String repositoryId, String path, + int start, int max) + throws IOException, RepositoryException, NotSupportedFeatuerException + { + AssertUtil.assertIsNotEmpty(repositoryId); + + Repository repository = repositoryManager.get(repositoryId); + + if (repository == null) + { + throw new RepositoryNotFoundException( + "could not find repository with id ".concat(repositoryId)); + } + + return getChangesets(repository, path, start, max); + } + /** * Method description * @@ -158,7 +190,7 @@ public class ChangesetViewerUtil extends PartCacheClearHook } ChangesetViewerCacheKey key = - new ChangesetViewerCacheKey(repository.getId(), start, max); + new ChangesetViewerCacheKey(repository.getId(), "", start, max); ChangesetPagingResult result = cache.get(key); if (result == null) @@ -188,6 +220,67 @@ public class ChangesetViewerUtil extends PartCacheClearHook return result; } + /** + * Method description + * + * + * @param repository + * @param path + * @param start + * @param max + * + * @return + * + * + * @throws IOException + * @throws NotSupportedFeatuerException + * @throws RepositoryException + */ + public ChangesetPagingResult getChangesets(Repository repository, String path, + int start, int max) + throws IOException, RepositoryException, NotSupportedFeatuerException + { + AssertUtil.assertIsNotNull(repository); + + ChangesetViewer viewer = repositoryManager.getChangesetViewer(repository); + + if (viewer == null) + { + throw new NotSupportedFeatuerException( + "ChangesetViewer is not supported for type ".concat( + repository.getType())); + } + + ChangesetViewerCacheKey key = + new ChangesetViewerCacheKey(repository.getId(), path, start, max); + ChangesetPagingResult result = cache.get(key); + + if (result == null) + { + result = viewer.getChangesets(path, start, max); + + if (result != null) + { + if (Util.isNotEmpty(result.getChangesets())) + { + callPreProcessors(result); + callPreProcessorFactories(repository, result); + } + + cache.put(key, result); + } + else + { + throw new RepositoryException("could not fetch changesets"); + } + } + else if (logger.isDebugEnabled()) + { + logger.debug("fetch changesetviewer results from cache"); + } + + return result; + } //~--- methods -------------------------------------------------------------- /** @@ -259,9 +352,10 @@ public class ChangesetViewerUtil extends PartCacheClearHook * @param start * @param max */ - public ChangesetViewerCacheKey(String repository, int start, int max) + public ChangesetViewerCacheKey(String repository, String path, int start, int max) { this.repository = repository; + this.path = path; this.start = start; this.max = max; } @@ -297,6 +391,13 @@ public class ChangesetViewerUtil extends PartCacheClearHook { return false; } + + if ((this.path == null) + ? (other.path != null) + : !this.path.equals(other.path)) + { + return false; + } if (this.start != other.start) { @@ -338,6 +439,9 @@ public class ChangesetViewerUtil extends PartCacheClearHook /** Field description */ private String repository; + + /** Field description */ + private String path; /** Field description */ private int start; diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitChangesetViewer.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitChangesetViewer.java index ff09cb6ab9..2ed030615d 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitChangesetViewer.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitChangesetViewer.java @@ -150,6 +150,76 @@ public class GitChangesetViewer implements ChangesetViewer return changesets; } + /** + * Method description + * + * + * @param path + * @param start + * @param max + * + * @return + */ + @Override + public ChangesetPagingResult getChangesets(String path, int start, int max) + { + ChangesetPagingResult changesets = null; + File directory = handler.getDirectory(repository); + org.eclipse.jgit.lib.Repository gr = null; + GitChangesetConverter converter = null; + + try + { + gr = GitUtil.open(directory); + + int counter = 0; + List changesetList = new ArrayList(); + + if (!gr.getAllRefs().isEmpty()) + { + converter = new GitChangesetConverter(gr, GitUtil.ID_LENGTH); + + Git git = new Git(gr); + ObjectId headId = GitUtil.getRepositoryHead(gr); + + if (headId != null) + { + for (RevCommit commit : git.log().addPath(path).call()) + { + if ((counter >= start) && (counter < start + max)) + { + changesetList.add(converter.createChangeset(commit)); + } + + counter++; + } + } + else if (logger.isWarnEnabled()) + { + logger.warn("could not find repository head of repository {}", + repository.getName()); + } + } + + changesets = new ChangesetPagingResult(counter, changesetList); + } + catch (NoHeadException ex) + { + logger.error("could not read changesets", ex); + } + catch (IOException ex) + { + logger.error("could not open repository", ex); + } + finally + { + IOUtil.close(converter); + GitUtil.close(gr); + } + + return changesets; + } + //~--- fields --------------------------------------------------------------- /** Field description */ diff --git a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgChangesetViewer.java b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgChangesetViewer.java index 07ca49deca..6f029f0732 100644 --- a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgChangesetViewer.java +++ b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgChangesetViewer.java @@ -114,6 +114,13 @@ public class HgChangesetViewer extends AbstractHgHandler null); } + @Override + public ChangesetPagingResult getChangesets(String path, int start, int max) + throws IOException, RepositoryException { + // TODO Auto-generated method stub + return null; + } + /** * Method description * diff --git a/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnChangesetViewer.java b/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnChangesetViewer.java index f16e8c7a14..4e378a4ecb 100644 --- a/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnChangesetViewer.java +++ b/scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/repository/SvnChangesetViewer.java @@ -47,6 +47,7 @@ import org.tmatesoft.svn.core.io.SVNRepositoryFactory; //~--- JDK imports ------------------------------------------------------------ import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Collection; @@ -92,6 +93,21 @@ public class SvnChangesetViewer implements ChangesetViewer @Override public ChangesetPagingResult getChangesets(int start, int max) { + return getChangesets("", start, max); + } + + /** + * Method description + * + * + * @param path + * @param start + * @param max + * + * @return + */ + @Override + public ChangesetPagingResult getChangesets(String path, int start, int max) { ChangesetPagingResult changesets = null; File directory = handler.getDirectory(repostory); SVNRepository repository = null; @@ -110,9 +126,9 @@ public class SvnChangesetViewer implements ChangesetViewer } List changesetList = new ArrayList(); - Collection entries = repository.log(new String[] { "" }, + Collection entries = repository.log(new String[] { path }, null, startRev, endRev, true, true); - + for (SVNLogEntry entry : entries) { changesetList.add(SvnUtil.createChangeset(entry)); @@ -132,12 +148,11 @@ public class SvnChangesetViewer implements ChangesetViewer return changesets; } - //~--- fields --------------------------------------------------------------- /** Field description */ private SvnRepositoryHandler handler; - /** Field description */ - private Repository repostory; + /** Field description */ + private Repository repostory; } diff --git a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java index 2fb48fec08..3a1d79d320 100644 --- a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java +++ b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java @@ -35,6 +35,8 @@ package sonia.scm.api.rest.resources; //~--- non-JDK imports -------------------------------------------------------- +import antlr.StringUtils; + import com.google.inject.Inject; import com.google.inject.Provider; import com.google.inject.Singleton; @@ -434,7 +436,8 @@ public class RepositoryResource @Path("{id}/changesets") @TypeHint(ChangesetPagingResult.class) @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) - public Response getChangesets(@PathParam("id") String id, @DefaultValue("0") + public Response getChangesets(@PathParam("id") String id, @DefaultValue("") + @QueryParam("path") String path, @DefaultValue("0") @QueryParam("start") int start, @DefaultValue("20") @QueryParam("limit") int limit) throws RepositoryException, IOException { @@ -442,8 +445,13 @@ public class RepositoryResource try { - ChangesetPagingResult changesets = changesetViewerUtil.getChangesets(id, - start, limit); + ChangesetPagingResult changesets; + if ("".equals(path)) { + changesets = changesetViewerUtil.getChangesets(id, start, limit); + } + else { + changesets = changesetViewerUtil.getChangesets(id, path, start, limit); + } if (changesets != null) {