add blame and remote info commands
Project: http://git-wip-us.apache.org/repos/asf/maven-scm/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-scm/commit/8ed4afbe Tree: http://git-wip-us.apache.org/repos/asf/maven-scm/tree/8ed4afbe Diff: http://git-wip-us.apache.org/repos/asf/maven-scm/diff/8ed4afbe Branch: refs/heads/master Commit: 8ed4afbee0768d8e11d8df24969b1e18f162049a Parents: a500523 Author: imod <d...@fortysix.ch> Authored: Sun Jun 23 17:23:43 2013 +0200 Committer: imod <d...@fortysix.ch> Committed: Sun Jun 23 17:23:43 2013 +0200 ---------------------------------------------------------------------- README.md | 2 + .../scm/provider/git/jgit/JGitScmProvider.java | 189 +++++++++---------- .../jgit/command/blame/JGitBlameCommand.java | 63 +++++++ .../remoteinfo/JGitRemoteInfoCommand.java | 73 +++++++ 4 files changed, 225 insertions(+), 102 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-scm/blob/8ed4afbe/README.md ---------------------------------------------------------------------- diff --git a/README.md b/README.md index 5b891b7..890b676 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,8 @@ maven-scm-plugin | scm:checkin | yes | | scm:checkout | yes | | scm:diff | yes | +| scm:blame | yes | +| scm:remoteinfo | yes | maven-release-plugin --- http://git-wip-us.apache.org/repos/asf/maven-scm/blob/8ed4afbe/src/main/java/org/apache/maven/scm/provider/git/jgit/JGitScmProvider.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/maven/scm/provider/git/jgit/JGitScmProvider.java b/src/main/java/org/apache/maven/scm/provider/git/jgit/JGitScmProvider.java index 287ca1e..0d594a9 100644 --- a/src/main/java/org/apache/maven/scm/provider/git/jgit/JGitScmProvider.java +++ b/src/main/java/org/apache/maven/scm/provider/git/jgit/JGitScmProvider.java @@ -28,12 +28,14 @@ import org.apache.maven.scm.provider.git.AbstractGitScmProvider; import org.apache.maven.scm.provider.git.command.GitCommand; import org.apache.maven.scm.provider.git.command.info.GitInfoItem; import org.apache.maven.scm.provider.git.jgit.command.add.JGitAddCommand; +import org.apache.maven.scm.provider.git.jgit.command.blame.JGitBlameCommand; import org.apache.maven.scm.provider.git.jgit.command.branch.JGitBranchCommand; import org.apache.maven.scm.provider.git.jgit.command.changelog.JGitChangeLogCommand; import org.apache.maven.scm.provider.git.jgit.command.checkin.JGitCheckInCommand; import org.apache.maven.scm.provider.git.jgit.command.checkout.JGitCheckOutCommand; import org.apache.maven.scm.provider.git.jgit.command.diff.JGitDiffCommand; import org.apache.maven.scm.provider.git.jgit.command.list.JGitListCommand; +import org.apache.maven.scm.provider.git.jgit.command.remoteinfo.JGitRemoteInfoCommand; import org.apache.maven.scm.provider.git.jgit.command.status.JGitStatusCommand; import org.apache.maven.scm.provider.git.jgit.command.tag.JGitTagCommand; import org.apache.maven.scm.repository.ScmRepositoryException; @@ -42,113 +44,96 @@ import org.apache.maven.scm.repository.ScmRepositoryException; * @author <a href="mailto:strub...@yahoo.de">Mark Struberg</a> * @author Dominik Bartholdi (imod) * @version $Id: JGitScmProvider.java 894145 2009-12-28 10:13:39Z struberg $ - * @plexus.component role="org.apache.maven.scm.provider.ScmProvider" role-hint="jgit" + * @plexus.component role="org.apache.maven.scm.provider.ScmProvider" + * role-hint="jgit" */ -public class JGitScmProvider - extends AbstractGitScmProvider -{ - /** {@inheritDoc} */ - protected GitCommand getAddCommand() - { - return new JGitAddCommand(); - } - - /** {@inheritDoc} */ - protected GitCommand getBranchCommand() - { - return new JGitBranchCommand(); - } - - /** {@inheritDoc} */ - protected GitCommand getChangeLogCommand() - { - return new JGitChangeLogCommand(); - } - - /** {@inheritDoc} */ - protected GitCommand getCheckInCommand() - { - return new JGitCheckInCommand(); - } - - /** {@inheritDoc} */ - protected GitCommand getCheckOutCommand() - { - return new JGitCheckOutCommand(); - } - - /** {@inheritDoc} */ - protected GitCommand getDiffCommand() - { - return new JGitDiffCommand(); - } - - /** {@inheritDoc} */ - protected GitCommand getExportCommand() - { - throw new UnsupportedOperationException("getExportCommand"); - } - - /** {@inheritDoc} */ - protected GitCommand getRemoveCommand() - { - throw new UnsupportedOperationException("getRemoveCommand"); - } - - /** {@inheritDoc} */ - protected GitCommand getStatusCommand() - { - return new JGitStatusCommand(); - } - - /** {@inheritDoc} */ - protected GitCommand getTagCommand() - { - return new JGitTagCommand(); - } - - /** {@inheritDoc} */ - protected GitCommand getUpdateCommand() - { - throw new UnsupportedOperationException("getUpdateCommand"); - } - - /** {@inheritDoc} */ - protected GitCommand getListCommand() - { - return new JGitListCommand(); - } - - /** {@inheritDoc} */ - public GitCommand getInfoCommand() - { - throw new UnsupportedOperationException("getInfoCommand"); - } - - /** {@inheritDoc} */ - protected String getRepositoryURL( File path ) - throws ScmException - { - // Note: I need to supply just 1 absolute path, but ScmFileSet won't let me without - // a basedir (which isn't used here anyway), so use a dummy file. - InfoScmResult result = info( null, new ScmFileSet( new File( "" ), path ), null ); - - if ( result.getInfoItems().size() != 1 ) - { - throw new ScmRepositoryException( "Cannot find URL: " - + ( result.getInfoItems().size() == 0 ? "no" : "multiple" ) + " items returned by the info command" ); - } - - return ( (GitInfoItem) result.getInfoItems().get( 0 ) ).getURL(); - } - - /** {@inheritDoc} */ +public class JGitScmProvider extends AbstractGitScmProvider { + /** {@inheritDoc} */ + protected GitCommand getAddCommand() { + return new JGitAddCommand(); + } + + /** {@inheritDoc} */ + protected GitCommand getBranchCommand() { + return new JGitBranchCommand(); + } + + /** {@inheritDoc} */ + protected GitCommand getChangeLogCommand() { + return new JGitChangeLogCommand(); + } + + /** {@inheritDoc} */ + protected GitCommand getCheckInCommand() { + return new JGitCheckInCommand(); + } + + /** {@inheritDoc} */ + protected GitCommand getCheckOutCommand() { + return new JGitCheckOutCommand(); + } + + /** {@inheritDoc} */ + protected GitCommand getDiffCommand() { + return new JGitDiffCommand(); + } + + /** {@inheritDoc} */ + protected GitCommand getExportCommand() { + throw new UnsupportedOperationException("getExportCommand"); + } + + /** {@inheritDoc} */ + protected GitCommand getRemoveCommand() { + throw new UnsupportedOperationException("getRemoveCommand"); + } + + /** {@inheritDoc} */ + protected GitCommand getStatusCommand() { + return new JGitStatusCommand(); + } + + /** {@inheritDoc} */ + protected GitCommand getTagCommand() { + return new JGitTagCommand(); + } + + /** {@inheritDoc} */ + protected GitCommand getUpdateCommand() { + throw new UnsupportedOperationException("getUpdateCommand"); + } + + /** {@inheritDoc} */ + protected GitCommand getListCommand() { + return new JGitListCommand(); + } + + /** {@inheritDoc} */ + public GitCommand getInfoCommand() { + throw new UnsupportedOperationException("getInfoCommand"); + } + + /** {@inheritDoc} */ + protected String getRepositoryURL(File path) throws ScmException { + // Note: I need to supply just 1 absolute path, but ScmFileSet won't let + // me without + // a basedir (which isn't used here anyway), so use a dummy file. + InfoScmResult result = info(null, new ScmFileSet(new File(""), path), null); + + if (result.getInfoItems().size() != 1) { + throw new ScmRepositoryException("Cannot find URL: " + (result.getInfoItems().size() == 0 ? "no" : "multiple") + " items returned by the info command"); + } + + return ((GitInfoItem) result.getInfoItems().get(0)).getURL(); + } + + /** {@inheritDoc} */ protected GitCommand getBlameCommand() { - throw new UnsupportedOperationException("getBlameCommand"); + return new JGitBlameCommand(); } /** {@inheritDoc} */ protected GitCommand getRemoteInfoCommand() { - throw new UnsupportedOperationException("getRemoteInfoCommand"); + return new JGitRemoteInfoCommand(); } } http://git-wip-us.apache.org/repos/asf/maven-scm/blob/8ed4afbe/src/main/java/org/apache/maven/scm/provider/git/jgit/command/blame/JGitBlameCommand.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/maven/scm/provider/git/jgit/command/blame/JGitBlameCommand.java b/src/main/java/org/apache/maven/scm/provider/git/jgit/command/blame/JGitBlameCommand.java new file mode 100644 index 0000000..e9f3a33 --- /dev/null +++ b/src/main/java/org/apache/maven/scm/provider/git/jgit/command/blame/JGitBlameCommand.java @@ -0,0 +1,63 @@ +package org.apache.maven.scm.provider.git.jgit.command.blame; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.apache.maven.scm.ScmException; +import org.apache.maven.scm.ScmFileSet; +import org.apache.maven.scm.command.blame.AbstractBlameCommand; +import org.apache.maven.scm.command.blame.BlameLine; +import org.apache.maven.scm.command.blame.BlameScmResult; +import org.apache.maven.scm.provider.ScmProviderRepository; +import org.apache.maven.scm.provider.git.command.GitCommand; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.blame.BlameResult; + +/** + * + * @author Dominik Bartholdi (imod) + */ +public class JGitBlameCommand extends AbstractBlameCommand implements GitCommand { + + @Override + public BlameScmResult executeBlameCommand(ScmProviderRepository repo, ScmFileSet workingDirectory, String filename) throws ScmException { + + File basedir = workingDirectory.getBasedir(); + try { + Git git = Git.open(basedir); + BlameResult blameResult = git.blame().setFilePath(filename).call(); + + List<BlameLine> lines = new ArrayList<BlameLine>(); + + int i = 0; + while ((i = blameResult.computeNext()) != -1) { + lines.add(new BlameLine(blameResult.getSourceAuthor(i).getWhen(), blameResult.getSourceCommit(i).getName(), blameResult.getSourceAuthor(i).getName(), blameResult.getSourceCommitter(i).getName())); + } + + return new BlameScmResult("JGit blame", lines); + } catch (Exception e) { + throw new ScmException("JGit blame failure!", e); + } + } + +} http://git-wip-us.apache.org/repos/asf/maven-scm/blob/8ed4afbe/src/main/java/org/apache/maven/scm/provider/git/jgit/command/remoteinfo/JGitRemoteInfoCommand.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/maven/scm/provider/git/jgit/command/remoteinfo/JGitRemoteInfoCommand.java b/src/main/java/org/apache/maven/scm/provider/git/jgit/command/remoteinfo/JGitRemoteInfoCommand.java new file mode 100644 index 0000000..375029c --- /dev/null +++ b/src/main/java/org/apache/maven/scm/provider/git/jgit/command/remoteinfo/JGitRemoteInfoCommand.java @@ -0,0 +1,73 @@ +package org.apache.maven.scm.provider.git.jgit.command.remoteinfo; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.maven.scm.CommandParameters; +import org.apache.maven.scm.ScmException; +import org.apache.maven.scm.ScmFileSet; +import org.apache.maven.scm.command.remoteinfo.AbstractRemoteInfoCommand; +import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult; +import org.apache.maven.scm.provider.ScmProviderRepository; +import org.apache.maven.scm.provider.git.command.GitCommand; +import org.apache.maven.scm.provider.git.jgit.command.JGitUtils; +import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.LsRemoteCommand; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.transport.CredentialsProvider; + +/** + * + * @author Dominik Bartholdi (imod) + */ +public class JGitRemoteInfoCommand extends AbstractRemoteInfoCommand implements GitCommand { + + @Override + public RemoteInfoScmResult executeRemoteInfoCommand(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException { + + GitScmProviderRepository repo = (GitScmProviderRepository) repository; + try { + Git git = Git.open(fileSet.getBasedir()); + CredentialsProvider credentials = JGitUtils.getCredentials(repo); + + LsRemoteCommand lsCommand = git.lsRemote().setRemote(repo.getPushUrl()).setCredentialsProvider(credentials); + + Map<String, String> tag = new HashMap<String, String>(); + Collection<Ref> allTags = lsCommand.setHeads(false).call(); + for (Ref ref : allTags) { + tag.put(ref.getName(), ref.getObjectId().name()); + } + + Map<String, String> heads = new HashMap<String, String>(); + Collection<Ref> allHeads = lsCommand.setTags(false).call(); + for (Ref ref : allHeads) { + heads.put(ref.getName(), ref.getObjectId().name()); + } + + return new RemoteInfoScmResult("JGit remoteinfo", heads, tag); + } catch (Exception e) { + throw new ScmException("JGit remoteinfo failure!", e); + } + } +}