View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.git.gitexe;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.File;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.apache.maven.scm.ScmException;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.command.info.InfoScmResult;
31  import org.apache.maven.scm.provider.git.AbstractGitScmProvider;
32  import org.apache.maven.scm.provider.git.command.GitCommand;
33  import org.apache.maven.scm.provider.git.gitexe.command.add.GitAddCommand;
34  import org.apache.maven.scm.provider.git.gitexe.command.blame.GitBlameCommand;
35  import org.apache.maven.scm.provider.git.gitexe.command.branch.GitBranchCommand;
36  import org.apache.maven.scm.provider.git.gitexe.command.changelog.GitChangeLogCommand;
37  import org.apache.maven.scm.provider.git.gitexe.command.checkin.GitCheckInCommand;
38  import org.apache.maven.scm.provider.git.gitexe.command.checkout.GitCheckOutCommand;
39  import org.apache.maven.scm.provider.git.gitexe.command.diff.GitDiffCommand;
40  import org.apache.maven.scm.provider.git.gitexe.command.info.GitInfoCommand;
41  import org.apache.maven.scm.provider.git.gitexe.command.remoteinfo.GitRemoteInfoCommand;
42  import org.apache.maven.scm.provider.git.gitexe.command.remove.GitRemoveCommand;
43  import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusCommand;
44  import org.apache.maven.scm.provider.git.gitexe.command.tag.GitTagCommand;
45  import org.apache.maven.scm.provider.git.gitexe.command.untag.GitUntagCommand;
46  import org.apache.maven.scm.provider.git.gitexe.command.update.GitUpdateCommand;
47  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
48  import org.apache.maven.scm.repository.ScmRepositoryException;
49  
50  /**
51   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
52   */
53  @Singleton
54  @Named("git")
55  public class GitExeScmProvider extends AbstractGitScmProvider {
56      private final Map<String, String> environmentVariables;
57  
58      /**
59       * The environment variable that controls whether Git prompts for credentials in the terminal.
60       *
61       * @see <a href="https://git-scm.com/docs/git-credential#Documentation/git-credential.txt-envGIT_TERMINAL_PROMPT">GIT_TERMINAL_PROMPT</a>
62       */
63      private static final String GIT_TERMINAL_PROMPT = "GIT_TERMINAL_PROMPT";
64  
65      public GitExeScmProvider() {
66          environmentVariables = new HashMap<>();
67      }
68  
69      @Override
70      public void setInteractive(boolean interactive) {
71          if (interactive) {
72              // This is the default behavior therefore make sure to remove the variable if it was set before.
73              environmentVariables.remove(GIT_TERMINAL_PROMPT);
74          } else {
75              environmentVariables.put(GIT_TERMINAL_PROMPT, "0");
76          }
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      protected GitCommand getAddCommand() {
83          return new GitAddCommand();
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      protected GitCommand getBranchCommand() {
90          return new GitBranchCommand(environmentVariables);
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      protected GitCommand getChangeLogCommand() {
97          return new GitChangeLogCommand();
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     protected GitCommand getCheckInCommand() {
104         return new GitCheckInCommand(environmentVariables);
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     protected GitCommand getCheckOutCommand() {
111         return new GitCheckOutCommand(environmentVariables);
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     protected GitCommand getDiffCommand() {
118         return new GitDiffCommand();
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     protected GitCommand getExportCommand() {
125         return null; // X TODO
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     protected GitCommand getRemoveCommand() {
132         return new GitRemoveCommand();
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     protected GitCommand getStatusCommand() {
139         return new GitStatusCommand();
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     protected GitCommand getTagCommand() {
146         return new GitTagCommand(environmentVariables);
147     }
148 
149     /**
150      * {@inheritDoc}
151      */
152     protected GitCommand getUntagCommand() {
153         return new GitUntagCommand(environmentVariables);
154     }
155 
156     /**
157      * {@inheritDoc}
158      */
159     protected GitCommand getUpdateCommand() {
160         return new GitUpdateCommand();
161     }
162 
163     /**
164      * {@inheritDoc}
165      */
166     public GitCommand getInfoCommand() {
167         return new GitInfoCommand();
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     protected GitCommand getBlameCommand() {
174         return new GitBlameCommand();
175     }
176 
177     /**
178      * {@inheritDoc}
179      */
180     protected GitCommand getRemoteInfoCommand() {
181         return new GitRemoteInfoCommand(environmentVariables);
182     }
183 
184     /**
185      * {@inheritDoc}
186      */
187     protected String getRepositoryURL(File path) throws ScmException {
188         // Note: I need to supply just 1 absolute path, but ScmFileSet won't let me without
189         // a basedir (which isn't used here anyway), so use a dummy file.
190         // and a dummy ScmProviderRepository
191         InfoScmResult result =
192                 info(new GitScmProviderRepository(path.toPath().toUri().toASCIIString()), new ScmFileSet(path), null);
193 
194         if (result.getInfoItems().size() != 1) {
195             throw new ScmRepositoryException("Cannot find URL: "
196                     + (result.getInfoItems().size() == 0 ? "no" : "multiple") + " items returned by the info command");
197         }
198 
199         return result.getInfoItems().get(0).getURL();
200     }
201 
202     public void setEnvironmentVariable(String key, String value) {
203         environmentVariables.put(key, value);
204     }
205 }