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       * @see <a href="https://git-scm.com/docs/git-credential#Documentation/git-credential.txt-envGIT_TERMINAL_PROMPT">GIT_TERMINAL_PROMPT</a>
61       */
62      private static final String GIT_TERMINAL_PROMPT = "GIT_TERMINAL_PROMPT";
63  
64      public GitExeScmProvider() {
65          environmentVariables = new HashMap<>();
66      }
67  
68      @Override
69      public void setInteractive(boolean interactive) {
70          if (interactive) {
71              // This is the default behavior therefore make sure to remove the variable if it was set before.
72              environmentVariables.remove(GIT_TERMINAL_PROMPT);
73          } else {
74              environmentVariables.put(GIT_TERMINAL_PROMPT, "0");
75          }
76      }
77  
78      /** {@inheritDoc} */
79      protected GitCommand getAddCommand() {
80          return new GitAddCommand();
81      }
82  
83      /** {@inheritDoc} */
84      protected GitCommand getBranchCommand() {
85          return new GitBranchCommand(environmentVariables);
86      }
87  
88      /** {@inheritDoc} */
89      protected GitCommand getChangeLogCommand() {
90          return new GitChangeLogCommand();
91      }
92  
93      /** {@inheritDoc} */
94      protected GitCommand getCheckInCommand() {
95          return new GitCheckInCommand(environmentVariables);
96      }
97  
98      /** {@inheritDoc} */
99      protected GitCommand getCheckOutCommand() {
100         return new GitCheckOutCommand(environmentVariables);
101     }
102 
103     /** {@inheritDoc} */
104     protected GitCommand getDiffCommand() {
105         return new GitDiffCommand();
106     }
107 
108     /** {@inheritDoc} */
109     protected GitCommand getExportCommand() {
110         return null; // X TODO
111     }
112 
113     /** {@inheritDoc} */
114     protected GitCommand getRemoveCommand() {
115         return new GitRemoveCommand();
116     }
117 
118     /** {@inheritDoc} */
119     protected GitCommand getStatusCommand() {
120         return new GitStatusCommand();
121     }
122 
123     /** {@inheritDoc} */
124     protected GitCommand getTagCommand() {
125         return new GitTagCommand(environmentVariables);
126     }
127 
128     /** {@inheritDoc} */
129     protected GitCommand getUntagCommand() {
130         return new GitUntagCommand(environmentVariables);
131     }
132 
133     /** {@inheritDoc} */
134     protected GitCommand getUpdateCommand() {
135         return new GitUpdateCommand();
136     }
137 
138     /** {@inheritDoc} */
139     public GitCommand getInfoCommand() {
140         return new GitInfoCommand();
141     }
142 
143     /** {@inheritDoc} */
144     protected GitCommand getBlameCommand() {
145         return new GitBlameCommand();
146     }
147 
148     /** {@inheritDoc} */
149     protected GitCommand getRemoteInfoCommand() {
150         return new GitRemoteInfoCommand(environmentVariables);
151     }
152 
153     /** {@inheritDoc} */
154     protected String getRepositoryURL(File path) throws ScmException {
155         // Note: I need to supply just 1 absolute path, but ScmFileSet won't let me without
156         // a basedir (which isn't used here anyway), so use a dummy file.
157         // and a dummy ScmProviderRepository
158         InfoScmResult result =
159                 info(new GitScmProviderRepository(path.toPath().toUri().toASCIIString()), new ScmFileSet(path), null);
160 
161         if (result.getInfoItems().size() != 1) {
162             throw new ScmRepositoryException("Cannot find URL: "
163                     + (result.getInfoItems().size() == 0 ? "no" : "multiple") + " items returned by the info command");
164         }
165 
166         return result.getInfoItems().get(0).getURL();
167     }
168 
169     public void setEnvironmentVariable(String key, String value) {
170         environmentVariables.put(key, value);
171     }
172 }