001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.git.jgit;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.io.File;
026
027import org.apache.maven.scm.ScmException;
028import org.apache.maven.scm.ScmFileSet;
029import org.apache.maven.scm.command.info.InfoScmResult;
030import org.apache.maven.scm.provider.git.AbstractGitScmProvider;
031import org.apache.maven.scm.provider.git.command.GitCommand;
032import org.apache.maven.scm.provider.git.jgit.command.PlexusInteractivityCredentialsProvider;
033import org.apache.maven.scm.provider.git.jgit.command.add.JGitAddCommand;
034import org.apache.maven.scm.provider.git.jgit.command.blame.JGitBlameCommand;
035import org.apache.maven.scm.provider.git.jgit.command.branch.JGitBranchCommand;
036import org.apache.maven.scm.provider.git.jgit.command.changelog.JGitChangeLogCommand;
037import org.apache.maven.scm.provider.git.jgit.command.checkin.JGitCheckInCommand;
038import org.apache.maven.scm.provider.git.jgit.command.checkout.JGitCheckOutCommand;
039import org.apache.maven.scm.provider.git.jgit.command.diff.JGitDiffCommand;
040import org.apache.maven.scm.provider.git.jgit.command.info.JGitInfoCommand;
041import org.apache.maven.scm.provider.git.jgit.command.list.JGitListCommand;
042import org.apache.maven.scm.provider.git.jgit.command.remoteinfo.JGitRemoteInfoCommand;
043import org.apache.maven.scm.provider.git.jgit.command.remove.JGitRemoveCommand;
044import org.apache.maven.scm.provider.git.jgit.command.status.JGitStatusCommand;
045import org.apache.maven.scm.provider.git.jgit.command.tag.JGitTagCommand;
046import org.apache.maven.scm.provider.git.jgit.command.untag.JGitUntagCommand;
047import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
048import org.apache.maven.scm.repository.ScmRepositoryException;
049import org.codehaus.plexus.components.interactivity.Prompter;
050import org.eclipse.jgit.transport.CredentialsProvider;
051
052/**
053 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
054 * @author Dominik Bartholdi (imod)
055 * @since 1.9
056 */
057@Singleton
058@Named("jgit")
059public class JGitScmProvider extends AbstractGitScmProvider {
060    private final PlexusInteractivityCredentialsProvider credentialsProvider;
061
062    @Inject
063    public JGitScmProvider(Prompter prompter) {
064        credentialsProvider = new PlexusInteractivityCredentialsProvider(prompter);
065        CredentialsProvider.setDefault(credentialsProvider);
066    }
067
068    @Override
069    public void setInteractive(boolean interactive) {
070        credentialsProvider.setInteractive(interactive);
071    }
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    protected GitCommand getAddCommand() {
078        return new JGitAddCommand();
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    protected GitCommand getBranchCommand() {
086        return new JGitBranchCommand();
087    }
088
089    /**
090     * {@inheritDoc}
091     */
092    @Override
093    protected GitCommand getChangeLogCommand() {
094        return new JGitChangeLogCommand();
095    }
096
097    /**
098     * {@inheritDoc}
099     */
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}