View Javadoc
1   package org.apache.maven.scm.provider.git.gitexe.command.remoteinfo;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Map;
23  
24  import org.apache.maven.scm.CommandParameters;
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFileSet;
27  import org.apache.maven.scm.command.remoteinfo.AbstractRemoteInfoCommand;
28  import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult;
29  import org.apache.maven.scm.provider.ScmProviderRepository;
30  import org.apache.maven.scm.provider.git.command.GitCommand;
31  import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
32  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
33  import org.codehaus.plexus.util.cli.CommandLineUtils;
34  import org.codehaus.plexus.util.cli.Commandline;
35  
36  /**
37   * @author Bertrand Paquet
38   */
39  public class GitRemoteInfoCommand
40      extends AbstractRemoteInfoCommand
41      implements GitCommand
42  {
43      private final Map<String, String> environmentVariables;
44  
45      public GitRemoteInfoCommand( Map<String, String> environmentVariables )
46      {
47          super();
48          this.environmentVariables = environmentVariables;
49      }
50  
51      @Override
52      public RemoteInfoScmResult executeRemoteInfoCommand( ScmProviderRepository repository, ScmFileSet fileSet,
53                                                           CommandParameters parameters )
54          throws ScmException
55      {
56          GitScmProviderRepository gitRepository = (GitScmProviderRepository) repository;
57  
58          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
59  
60          Commandline clLsRemote = createCommandLine( gitRepository );
61  
62          GitRemoteInfoConsumer consumer = new GitRemoteInfoConsumer( clLsRemote.toString() );
63  
64          int exitCode = GitCommandLineUtils.execute( clLsRemote, consumer, stderr );
65          if ( exitCode != 0 )
66          {
67              throw new ScmException( "unable to execute ls-remote on " + gitRepository.getFetchUrl() );
68          }
69  
70          return consumer.getRemoteInfoScmResult();
71      }
72  
73      // ----------------------------------------------------------------------
74      //
75      // ----------------------------------------------------------------------
76  
77      public Commandline createCommandLine( GitScmProviderRepository repository )
78      {
79          Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( null, "ls-remote", repository,
80                                                                      environmentVariables );
81  
82          cl.setWorkingDirectory( System.getProperty( "java.io.tmpdir" ) );
83  
84          String remoteUrl = repository.getPushUrl();
85          cl.createArg().setValue( remoteUrl );
86  
87          return cl;
88      }
89  
90  }