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.repository.ScmRepositoryException;
048import org.codehaus.plexus.components.interactivity.Prompter;
049import org.eclipse.jgit.transport.CredentialsProvider;
050
051/**
052 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
053 * @author Dominik Bartholdi (imod)
054 * @since 1.9
055 */
056@Singleton
057@Named("jgit")
058public class JGitScmProvider extends AbstractGitScmProvider {
059    private final PlexusInteractivityCredentialsProvider credentialsProvider;
060
061    @Inject
062    public JGitScmProvider(Prompter prompter) {
063        credentialsProvider = new PlexusInteractivityCredentialsProvider(prompter);
064        CredentialsProvider.setDefault(credentialsProvider);
065    }
066
067    @Override
068    public void setInteractive(boolean interactive) {
069        credentialsProvider.setInteractive(interactive);
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    protected GitCommand getAddCommand() {
077        return new JGitAddCommand();
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    protected GitCommand getBranchCommand() {
085        return new JGitBranchCommand();
086    }
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    protected GitCommand getChangeLogCommand() {
093        return new JGitChangeLogCommand();
094    }
095
096    /**
097     * {@inheritDoc}
098     */
099    @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}