001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.git.gitexe.command.remoteinfo;
020
021import java.util.Map;
022
023import org.apache.maven.scm.CommandParameters;
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.command.remoteinfo.AbstractRemoteInfoCommand;
027import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult;
028import org.apache.maven.scm.provider.ScmProviderRepository;
029import org.apache.maven.scm.provider.git.command.GitCommand;
030import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
031import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
032import org.codehaus.plexus.util.cli.CommandLineUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035/**
036 * @author Bertrand Paquet
037 */
038public class GitRemoteInfoCommand extends AbstractRemoteInfoCommand implements GitCommand {
039    private final Map<String, String> environmentVariables;
040
041    public GitRemoteInfoCommand(Map<String, String> environmentVariables) {
042        super();
043        this.environmentVariables = environmentVariables;
044    }
045
046    @Override
047    public RemoteInfoScmResult executeRemoteInfoCommand(
048            ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
049        GitScmProviderRepository gitRepository = (GitScmProviderRepository) repository;
050
051        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
052
053        Commandline clLsRemote = createCommandLine(gitRepository);
054
055        GitRemoteInfoConsumer consumer = new GitRemoteInfoConsumer(clLsRemote.toString());
056
057        int exitCode = GitCommandLineUtils.execute(clLsRemote, consumer, stderr);
058        if (exitCode != 0) {
059            throw new ScmException("unable to execute ls-remote on " + gitRepository.getFetchUrl());
060        }
061
062        return consumer.getRemoteInfoScmResult();
063    }
064
065    // ----------------------------------------------------------------------
066    //
067    // ----------------------------------------------------------------------
068
069    public Commandline createCommandLine(GitScmProviderRepository repository) {
070        Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(null, "ls-remote", repository, environmentVariables);
071
072        cl.setWorkingDirectory(System.getProperty("java.io.tmpdir"));
073
074        String remoteUrl = repository.getPushUrl();
075        cl.createArg().setValue(remoteUrl);
076
077        return cl;
078    }
079}