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