View Javadoc
1   package org.apache.maven.scm.provider.git.gitexe;
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 javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.apache.maven.scm.ScmException;
30  import org.apache.maven.scm.ScmFileSet;
31  import org.apache.maven.scm.command.info.InfoScmResult;
32  import org.apache.maven.scm.provider.git.AbstractGitScmProvider;
33  import org.apache.maven.scm.provider.git.command.GitCommand;
34  import org.apache.maven.scm.provider.git.gitexe.command.add.GitAddCommand;
35  import org.apache.maven.scm.provider.git.gitexe.command.blame.GitBlameCommand;
36  import org.apache.maven.scm.provider.git.gitexe.command.branch.GitBranchCommand;
37  import org.apache.maven.scm.provider.git.gitexe.command.changelog.GitChangeLogCommand;
38  import org.apache.maven.scm.provider.git.gitexe.command.checkin.GitCheckInCommand;
39  import org.apache.maven.scm.provider.git.gitexe.command.checkout.GitCheckOutCommand;
40  import org.apache.maven.scm.provider.git.gitexe.command.diff.GitDiffCommand;
41  import org.apache.maven.scm.provider.git.gitexe.command.info.GitInfoCommand;
42  import org.apache.maven.scm.provider.git.gitexe.command.remoteinfo.GitRemoteInfoCommand;
43  import org.apache.maven.scm.provider.git.gitexe.command.remove.GitRemoveCommand;
44  import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusCommand;
45  import org.apache.maven.scm.provider.git.gitexe.command.tag.GitTagCommand;
46  import org.apache.maven.scm.provider.git.gitexe.command.untag.GitUntagCommand;
47  import org.apache.maven.scm.provider.git.gitexe.command.update.GitUpdateCommand;
48  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
49  import org.apache.maven.scm.repository.ScmRepositoryException;
50  
51  /**
52   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
53   */
54  @Singleton
55  @Named( "git" )
56  public class GitExeScmProvider
57      extends AbstractGitScmProvider
58  {
59      private final Map<String, String> environmentVariables;
60  
61      public GitExeScmProvider()
62      {
63          environmentVariables = new HashMap<>();
64      }
65  
66      /** {@inheritDoc} */
67      protected GitCommand getAddCommand()
68      {
69          return new GitAddCommand();
70      }
71  
72      /** {@inheritDoc} */
73      protected GitCommand getBranchCommand()
74      {
75          return new GitBranchCommand();
76      }
77  
78      /** {@inheritDoc} */
79      protected GitCommand getChangeLogCommand()
80      {
81          return new GitChangeLogCommand();
82      }
83  
84      /** {@inheritDoc} */
85      protected GitCommand getCheckInCommand()
86      {
87          return new GitCheckInCommand( environmentVariables );
88      }
89  
90      /** {@inheritDoc} */
91      protected GitCommand getCheckOutCommand()
92      {
93          return new GitCheckOutCommand( environmentVariables );
94      }
95  
96      /** {@inheritDoc} */
97      protected GitCommand getDiffCommand()
98      {
99          return new GitDiffCommand();
100     }
101 
102     /** {@inheritDoc} */
103     protected GitCommand getExportCommand()
104     {
105         return null; //X TODO
106     }
107 
108     /** {@inheritDoc} */
109     protected GitCommand getRemoveCommand()
110     {
111         return new GitRemoveCommand();
112     }
113 
114     /** {@inheritDoc} */
115     protected GitCommand getStatusCommand()
116     {
117         return new GitStatusCommand();
118     }
119 
120     /** {@inheritDoc} */
121     protected GitCommand getTagCommand()
122     {
123         return new GitTagCommand();
124     }
125 
126     /** {@inheritDoc} */
127     protected GitCommand getUntagCommand()
128     {
129         return new GitUntagCommand();
130     }
131 
132     /** {@inheritDoc} */
133     protected GitCommand getUpdateCommand()
134     {
135         return new GitUpdateCommand();
136     }
137 
138     /** {@inheritDoc} */
139     public GitCommand getInfoCommand()
140     {
141         return new GitInfoCommand();
142     }
143 
144     /** {@inheritDoc} */
145     protected GitCommand getBlameCommand()
146     {
147         return new GitBlameCommand();
148     }
149 
150     /** {@inheritDoc} */
151     protected GitCommand getRemoteInfoCommand()
152     {
153         return new GitRemoteInfoCommand( environmentVariables );
154     }
155 
156     /** {@inheritDoc} */
157     protected String getRepositoryURL( File path )
158         throws ScmException
159     {
160         // Note: I need to supply just 1 absolute path, but ScmFileSet won't let me without
161         // a basedir (which isn't used here anyway), so use a dummy file.
162         // and a dummy ScmProviderRepository
163         InfoScmResult result = info( new GitScmProviderRepository( path.toPath().toUri().toASCIIString() ),
164                                      new ScmFileSet( path ), null );
165 
166         if ( result.getInfoItems().size() != 1 )
167         {
168             throw new ScmRepositoryException( "Cannot find URL: "
169                 + ( result.getInfoItems().size() == 0 ? "no" : "multiple" ) + " items returned by the info command" );
170         }
171 
172         return result.getInfoItems().get( 0 ).getURL();
173     }
174 
175     public void setEnvironmentVariable( String key, String value )
176     {
177         environmentVariables.put( key, value );
178     }
179 }