Search in sources :

Example 1 with RemoteInfoScmResult

use of org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult in project maven-scm by apache.

the class JGitCheckOutCommand method executeCheckOutCommand.

/**
 * For git, the given repository is a remote one. We have to clone it first if the working directory does not
 * contain a git repo yet, otherwise we have to git-pull it.
 * <p/>
 * {@inheritDoc}
 */
protected CheckOutScmResult executeCheckOutCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive, boolean shallow) throws ScmException {
    GitScmProviderRepository repository = (GitScmProviderRepository) repo;
    if (GitScmProviderRepository.PROTOCOL_FILE.equals(repository.getFetchInfo().getProtocol()) && repository.getFetchInfo().getPath().indexOf(fileSet.getBasedir().getPath()) >= 0) {
        throw new ScmException("remote repository must not be the working directory");
    }
    Git git = null;
    try {
        ProgressMonitor monitor = JGitUtils.getMonitor(getLogger());
        String branch = version != null ? version.getName() : null;
        if (StringUtils.isBlank(branch)) {
            branch = Constants.MASTER;
        }
        getLogger().debug("try checkout of branch: " + branch);
        if (!fileSet.getBasedir().exists() || !(new File(fileSet.getBasedir(), ".git").exists())) {
            if (fileSet.getBasedir().exists()) {
                // git refuses to clone otherwise
                fileSet.getBasedir().delete();
            }
            // FIXME only if windauze
            WindowCacheConfig cfg = new WindowCacheConfig();
            cfg.setPackedGitMMAP(false);
            cfg.install();
            // no git repo seems to exist, let's clone the original repo
            CredentialsProvider credentials = JGitUtils.getCredentials((GitScmProviderRepository) repo);
            getLogger().info("cloning [" + branch + "] to " + fileSet.getBasedir());
            CloneCommand command = Git.cloneRepository().setURI(repository.getFetchUrl());
            command.setCredentialsProvider(credentials).setBranch(branch).setDirectory(fileSet.getBasedir());
            command.setProgressMonitor(monitor);
            git = command.call();
        }
        JGitRemoteInfoCommand remoteInfoCommand = new JGitRemoteInfoCommand();
        remoteInfoCommand.setLogger(getLogger());
        RemoteInfoScmResult result = remoteInfoCommand.executeRemoteInfoCommand(repository, fileSet, null);
        if (git == null) {
            // deliberately not using JGitUtils.openRepo(), the caller told us exactly where to checkout
            git = Git.open(fileSet.getBasedir());
        }
        if (fileSet.getBasedir().exists() && new File(fileSet.getBasedir(), ".git").exists() && result.getBranches().size() > 0) {
            // git repo exists, so we must git-pull the changes
            CredentialsProvider credentials = JGitUtils.prepareSession(getLogger(), git, repository);
            if (version != null && StringUtils.isNotEmpty(version.getName()) && (version instanceof ScmTag)) {
                // A tag will not be pulled but we only fetch all the commits from the upstream repo
                // This is done because checking out a tag might not happen on the current branch
                // but create a 'detached HEAD'.
                // In fact, a tag in git may be in multiple branches. This occurs if
                // you create a branch after the tag has been created
                getLogger().debug("fetch...");
                git.fetch().setCredentialsProvider(credentials).setProgressMonitor(monitor).call();
            } else {
                getLogger().debug("pull...");
                git.pull().setCredentialsProvider(credentials).setProgressMonitor(monitor).call();
            }
        }
        Set<String> localBranchNames = JGitBranchCommand.getShortLocalBranchNames(git);
        if (version instanceof ScmTag) {
            getLogger().info("checkout tag [" + branch + "] at " + fileSet.getBasedir());
            git.checkout().setName(branch).call();
        } else if (localBranchNames.contains(branch)) {
            getLogger().info("checkout [" + branch + "] at " + fileSet.getBasedir());
            git.checkout().setName(branch).call();
        } else {
            getLogger().info("checkout remote branch [" + branch + "] at " + fileSet.getBasedir());
            git.checkout().setName(branch).setCreateBranch(true).setStartPoint(Constants.DEFAULT_REMOTE_NAME + "/" + branch).call();
        }
        RevWalk revWalk = new RevWalk(git.getRepository());
        RevCommit commit = revWalk.parseCommit(git.getRepository().resolve(Constants.HEAD));
        revWalk.release();
        final TreeWalk walk = new TreeWalk(git.getRepository());
        // drop the first empty tree, which we do not need here
        walk.reset();
        walk.setRecursive(true);
        walk.addTree(commit.getTree());
        List<ScmFile> listedFiles = new ArrayList<ScmFile>();
        while (walk.next()) {
            listedFiles.add(new ScmFile(walk.getPathString(), ScmFileStatus.CHECKED_OUT));
        }
        walk.release();
        getLogger().debug("current branch: " + git.getRepository().getBranch());
        return new CheckOutScmResult("checkout via JGit", listedFiles);
    } catch (Exception e) {
        throw new ScmException("JGit checkout failure!", e);
    } finally {
        JGitUtils.closeRepo(git);
    }
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) ScmException(org.apache.maven.scm.ScmException) WindowCacheConfig(org.eclipse.jgit.storage.file.WindowCacheConfig) GitScmProviderRepository(org.apache.maven.scm.provider.git.repository.GitScmProviderRepository) ArrayList(java.util.ArrayList) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) RevWalk(org.eclipse.jgit.revwalk.RevWalk) JGitRemoteInfoCommand(org.apache.maven.scm.provider.git.jgit.command.remoteinfo.JGitRemoteInfoCommand) RemoteInfoScmResult(org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult) ScmException(org.apache.maven.scm.ScmException) ScmFile(org.apache.maven.scm.ScmFile) ProgressMonitor(org.eclipse.jgit.lib.ProgressMonitor) Git(org.eclipse.jgit.api.Git) ScmTag(org.apache.maven.scm.ScmTag) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with RemoteInfoScmResult

use of org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult in project maven-scm by apache.

the class JGitRemoteInfoCommand method executeRemoteInfoCommand.

@Override
public RemoteInfoScmResult executeRemoteInfoCommand(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
    GitScmProviderRepository repo = (GitScmProviderRepository) repository;
    Git git = null;
    try {
        git = JGitUtils.openRepo(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).setTags(true).call();
        for (Ref ref : allTags) {
            tag.put(Repository.shortenRefName(ref.getName()), ref.getObjectId().name());
        }
        Map<String, String> heads = new HashMap<String, String>();
        Collection<Ref> allHeads = lsCommand.setHeads(true).setTags(false).call();
        for (Ref ref : allHeads) {
            heads.put(Repository.shortenRefName(ref.getName()), ref.getObjectId().name());
        }
        return new RemoteInfoScmResult("JGit remoteinfo", heads, tag);
    } catch (Exception e) {
        throw new ScmException("JGit remoteinfo failure!", e);
    } finally {
        JGitUtils.closeRepo(git);
    }
}
Also used : Ref(org.eclipse.jgit.lib.Ref) ScmException(org.apache.maven.scm.ScmException) Git(org.eclipse.jgit.api.Git) GitScmProviderRepository(org.apache.maven.scm.provider.git.repository.GitScmProviderRepository) HashMap(java.util.HashMap) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) LsRemoteCommand(org.eclipse.jgit.api.LsRemoteCommand) RemoteInfoScmResult(org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult) ScmException(org.apache.maven.scm.ScmException)

Example 3 with RemoteInfoScmResult

use of org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult in project maven-scm by apache.

the class SvnRemoteInfoCommand method executeRemoteInfoCommand.

@Override
public RemoteInfoScmResult executeRemoteInfoCommand(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
    String url = ((SvnScmProviderRepository) repository).getUrl();
    // use a default svn layout, url is here http://svn.apache.org/repos/asf/maven/maven-3/trunk
    // so as we presume we have good users using standard svn layout, we calculate tags and branches url
    String baseUrl = StringUtils.endsWith(url, "/") ? StringUtils.substringAfter(StringUtils.removeEnd(url, "/"), "/") : StringUtils.substringBeforeLast(url, "/");
    Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(fileSet == null ? null : fileSet.getBasedir(), (SvnScmProviderRepository) repository);
    cl.createArg().setValue("ls");
    cl.createArg().setValue(baseUrl + "/tags");
    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
    LsConsumer consumer = new LsConsumer(getLogger(), baseUrl);
    int exitCode = 0;
    Map<String, String> tagsInfos = null;
    try {
        exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr, getLogger());
        tagsInfos = consumer.infos;
    } catch (CommandLineException ex) {
        throw new ScmException("Error while executing svn command.", ex);
    }
    if (exitCode != 0) {
        return new RemoteInfoScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
    }
    cl = SvnCommandLineUtils.getBaseSvnCommandLine(fileSet == null ? null : fileSet.getBasedir(), (SvnScmProviderRepository) repository);
    cl.createArg().setValue("ls");
    cl.createArg().setValue(baseUrl + "/tags");
    stderr = new CommandLineUtils.StringStreamConsumer();
    consumer = new LsConsumer(getLogger(), baseUrl);
    Map<String, String> branchesInfos = null;
    try {
        exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr, getLogger());
        branchesInfos = consumer.infos;
    } catch (CommandLineException ex) {
        throw new ScmException("Error while executing svn command.", ex);
    }
    if (exitCode != 0) {
        return new RemoteInfoScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
    }
    return new RemoteInfoScmResult(cl.toString(), branchesInfos, tagsInfos);
}
Also used : ScmException(org.apache.maven.scm.ScmException) Commandline(org.codehaus.plexus.util.cli.Commandline) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) SvnCommandLineUtils(org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils) SvnScmProviderRepository(org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) RemoteInfoScmResult(org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult)

Example 4 with RemoteInfoScmResult

use of org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult in project maven-scm by apache.

the class AbstractRemoteInfoCommandTckTest method testRemoteInfoCommand.

public void testRemoteInfoCommand() throws Exception {
    ScmProvider provider = getScmManager().getProviderByRepository(getScmRepository());
    RemoteInfoScmResult result = provider.remoteInfo(getScmProviderRepository(), new ScmFileSet(getWorkingCopy()), null);
    checkResult(result);
}
Also used : ScmProvider(org.apache.maven.scm.provider.ScmProvider) ScmFileSet(org.apache.maven.scm.ScmFileSet) RemoteInfoScmResult(org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult)

Example 5 with RemoteInfoScmResult

use of org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult in project maven-scm by apache.

the class GitCheckOutCommand method executeCheckOutCommand.

/**
 * For git, the given repository is a remote one.
 * We have to clone it first if the working directory does not contain a git repo yet,
 * otherwise we have to git-pull it.
 * <p/>
 * TODO We currently assume a '.git' directory, so this does not work for --bare repos
 * {@inheritDoc}
 */
protected CheckOutScmResult executeCheckOutCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive, boolean shallow) throws ScmException {
    GitScmProviderRepository repository = (GitScmProviderRepository) repo;
    if (GitScmProviderRepository.PROTOCOL_FILE.equals(repository.getFetchInfo().getProtocol()) && repository.getFetchInfo().getPath().indexOf(fileSet.getBasedir().getPath()) >= 0) {
        throw new ScmException("remote repository must not be the working directory");
    }
    int exitCode;
    CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
    String lastCommandLine = "git-nothing-to-do";
    if (!fileSet.getBasedir().exists() || !(new File(fileSet.getBasedir(), ".git").exists())) {
        if (fileSet.getBasedir().exists()) {
            // git refuses to clone otherwise
            fileSet.getBasedir().delete();
        }
        // no git repo seems to exist, let's clone the original repo
        Commandline clClone = createCloneCommand(repository, fileSet.getBasedir(), version, shallow);
        exitCode = GitCommandLineUtils.execute(clClone, stdout, stderr, getLogger());
        if (exitCode != 0) {
            return new CheckOutScmResult(clClone.toString(), "The git-clone command failed.", stderr.getOutput(), false);
        }
        lastCommandLine = clClone.toString();
    }
    GitRemoteInfoCommand gitRemoteInfoCommand = new GitRemoteInfoCommand();
    gitRemoteInfoCommand.setLogger(getLogger());
    RemoteInfoScmResult result = gitRemoteInfoCommand.executeRemoteInfoCommand(repository, null, null);
    if (fileSet.getBasedir().exists() && new File(fileSet.getBasedir(), ".git").exists() && result.getBranches().size() > 0) {
        // git repo exists, so we must git-pull the changes
        Commandline clPull = createPullCommand(repository, fileSet.getBasedir(), version);
        exitCode = GitCommandLineUtils.execute(clPull, stdout, stderr, getLogger());
        if (exitCode != 0) {
            return new CheckOutScmResult(clPull.toString(), "The git-pull command failed.", stderr.getOutput(), false);
        }
        lastCommandLine = clPull.toString();
        // and now lets do the git-checkout itself
        Commandline clCheckout = createCommandLine(repository, fileSet.getBasedir(), version);
        exitCode = GitCommandLineUtils.execute(clCheckout, stdout, stderr, getLogger());
        if (exitCode != 0) {
            return new CheckOutScmResult(clCheckout.toString(), "The git-checkout command failed.", stderr.getOutput(), false);
        }
        lastCommandLine = clCheckout.toString();
    }
    // and now search for the files
    GitListConsumer listConsumer = new GitListConsumer(getLogger(), fileSet.getBasedir(), ScmFileStatus.CHECKED_IN);
    Commandline clList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());
    exitCode = GitCommandLineUtils.execute(clList, listConsumer, stderr, getLogger());
    if (exitCode != 0) {
        return new CheckOutScmResult(clList.toString(), "The git-ls-files command failed.", stderr.getOutput(), false);
    }
    return new CheckOutScmResult(lastCommandLine, listConsumer.getListedFiles());
}
Also used : ScmException(org.apache.maven.scm.ScmException) GitScmProviderRepository(org.apache.maven.scm.provider.git.repository.GitScmProviderRepository) Commandline(org.codehaus.plexus.util.cli.Commandline) GitRemoteInfoCommand(org.apache.maven.scm.provider.git.gitexe.command.remoteinfo.GitRemoteInfoCommand) RemoteInfoScmResult(org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult) GitCommandLineUtils(org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) GitListConsumer(org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) File(java.io.File)

Aggregations

RemoteInfoScmResult (org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult)6 ScmException (org.apache.maven.scm.ScmException)4 GitScmProviderRepository (org.apache.maven.scm.provider.git.repository.GitScmProviderRepository)3 File (java.io.File)2 CheckOutScmResult (org.apache.maven.scm.command.checkout.CheckOutScmResult)2 CommandLineUtils (org.codehaus.plexus.util.cli.CommandLineUtils)2 Commandline (org.codehaus.plexus.util.cli.Commandline)2 Git (org.eclipse.jgit.api.Git)2 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ScmFile (org.apache.maven.scm.ScmFile)1 ScmFileSet (org.apache.maven.scm.ScmFileSet)1 ScmTag (org.apache.maven.scm.ScmTag)1 DefaultLog (org.apache.maven.scm.log.DefaultLog)1 ScmProvider (org.apache.maven.scm.provider.ScmProvider)1 GitCommandLineUtils (org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils)1 GitListConsumer (org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer)1 GitRemoteInfoCommand (org.apache.maven.scm.provider.git.gitexe.command.remoteinfo.GitRemoteInfoCommand)1 JGitRemoteInfoCommand (org.apache.maven.scm.provider.git.jgit.command.remoteinfo.JGitRemoteInfoCommand)1