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