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.jgit;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFileSet;
29  import org.apache.maven.scm.command.info.InfoScmResult;
30  import org.apache.maven.scm.provider.git.AbstractGitScmProvider;
31  import org.apache.maven.scm.provider.git.command.GitCommand;
32  import org.apache.maven.scm.provider.git.jgit.command.PlexusInteractivityCredentialsProvider;
33  import org.apache.maven.scm.provider.git.jgit.command.add.JGitAddCommand;
34  import org.apache.maven.scm.provider.git.jgit.command.blame.JGitBlameCommand;
35  import org.apache.maven.scm.provider.git.jgit.command.branch.JGitBranchCommand;
36  import org.apache.maven.scm.provider.git.jgit.command.changelog.JGitChangeLogCommand;
37  import org.apache.maven.scm.provider.git.jgit.command.checkin.JGitCheckInCommand;
38  import org.apache.maven.scm.provider.git.jgit.command.checkout.JGitCheckOutCommand;
39  import org.apache.maven.scm.provider.git.jgit.command.diff.JGitDiffCommand;
40  import org.apache.maven.scm.provider.git.jgit.command.info.JGitInfoCommand;
41  import org.apache.maven.scm.provider.git.jgit.command.list.JGitListCommand;
42  import org.apache.maven.scm.provider.git.jgit.command.remoteinfo.JGitRemoteInfoCommand;
43  import org.apache.maven.scm.provider.git.jgit.command.remove.JGitRemoveCommand;
44  import org.apache.maven.scm.provider.git.jgit.command.status.JGitStatusCommand;
45  import org.apache.maven.scm.provider.git.jgit.command.tag.JGitTagCommand;
46  import org.apache.maven.scm.provider.git.jgit.command.untag.JGitUntagCommand;
47  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
48  import org.apache.maven.scm.repository.ScmRepositoryException;
49  import org.codehaus.plexus.components.interactivity.Prompter;
50  import org.eclipse.jgit.transport.CredentialsProvider;
51  
52  /**
53   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
54   * @author Dominik Bartholdi (imod)
55   * @since 1.9
56   */
57  @Singleton
58  @Named("jgit")
59  public class JGitScmProvider extends AbstractGitScmProvider {
60      private final PlexusInteractivityCredentialsProvider credentialsProvider;
61  
62      @Inject
63      public JGitScmProvider(Prompter prompter) {
64          credentialsProvider = new PlexusInteractivityCredentialsProvider(prompter);
65          CredentialsProvider.setDefault(credentialsProvider);
66      }
67  
68      @Override
69      public void setInteractive(boolean interactive) {
70          credentialsProvider.setInteractive(interactive);
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      protected GitCommand getAddCommand() {
78          return new JGitAddCommand();
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      protected GitCommand getBranchCommand() {
86          return new JGitBranchCommand();
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      protected GitCommand getChangeLogCommand() {
94          return new JGitChangeLogCommand();
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     protected GitCommand getCheckInCommand() {
102         return new JGitCheckInCommand();
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     protected GitCommand getCheckOutCommand() {
110         return new JGitCheckOutCommand();
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     protected GitCommand getDiffCommand() {
118         return new JGitDiffCommand();
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     protected GitCommand getExportCommand() {
126         throw new UnsupportedOperationException("getExportCommand");
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     protected GitCommand getRemoveCommand() {
134         return new JGitRemoveCommand();
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     protected GitCommand getStatusCommand() {
142         return new JGitStatusCommand();
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     protected GitCommand getTagCommand() {
150         return new JGitTagCommand();
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     @Override
157     protected GitCommand getUntagCommand() {
158         return new JGitUntagCommand();
159     }
160 
161     /**
162      * {@inheritDoc}
163      */
164     @Override
165     protected GitCommand getUpdateCommand() {
166         throw new UnsupportedOperationException("getUpdateCommand");
167     }
168 
169     /**
170      * {@inheritDoc}
171      */
172     protected GitCommand getListCommand() {
173         return new JGitListCommand();
174     }
175 
176     /**
177      * {@inheritDoc}
178      */
179     @Override
180     public GitCommand getInfoCommand() {
181         return new JGitInfoCommand();
182     }
183 
184     /**
185      * {@inheritDoc}
186      */
187     @Override
188     protected String getRepositoryURL(File path) throws ScmException {
189         // Note: I need to supply just 1 absolute path, but ScmFileSet won't let
190         // me without
191         // a basedir (which isn't used here anyway), so use a dummy file.
192         InfoScmResult result =
193                 info(new GitScmProviderRepository(path.toPath().toUri().toASCIIString()), new ScmFileSet(path), null);
194 
195         if (result.getInfoItems().size() != 1) {
196             throw new ScmRepositoryException("Cannot find URL: "
197                     + (result.getInfoItems().size() == 0 ? "no" : "multiple") + " items returned by the info command");
198         }
199 
200         return (result.getInfoItems().get(0)).getURL();
201     }
202 
203     /**
204      * {@inheritDoc}
205      */
206     @Override
207     protected GitCommand getBlameCommand() {
208         return new JGitBlameCommand();
209     }
210 
211     /**
212      * {@inheritDoc}
213      */
214     @Override
215     protected GitCommand getRemoteInfoCommand() {
216         return new JGitRemoteInfoCommand();
217     }
218 }