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