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.gitexe;
020
021import javax.inject.Named;
022import javax.inject.Singleton;
023
024import java.io.File;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.apache.maven.scm.ScmException;
029import org.apache.maven.scm.ScmFileSet;
030import org.apache.maven.scm.command.info.InfoScmResult;
031import org.apache.maven.scm.provider.git.AbstractGitScmProvider;
032import org.apache.maven.scm.provider.git.command.GitCommand;
033import org.apache.maven.scm.provider.git.gitexe.command.add.GitAddCommand;
034import org.apache.maven.scm.provider.git.gitexe.command.blame.GitBlameCommand;
035import org.apache.maven.scm.provider.git.gitexe.command.branch.GitBranchCommand;
036import org.apache.maven.scm.provider.git.gitexe.command.changelog.GitChangeLogCommand;
037import org.apache.maven.scm.provider.git.gitexe.command.checkin.GitCheckInCommand;
038import org.apache.maven.scm.provider.git.gitexe.command.checkout.GitCheckOutCommand;
039import org.apache.maven.scm.provider.git.gitexe.command.diff.GitDiffCommand;
040import org.apache.maven.scm.provider.git.gitexe.command.info.GitInfoCommand;
041import org.apache.maven.scm.provider.git.gitexe.command.remoteinfo.GitRemoteInfoCommand;
042import org.apache.maven.scm.provider.git.gitexe.command.remove.GitRemoveCommand;
043import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusCommand;
044import org.apache.maven.scm.provider.git.gitexe.command.tag.GitTagCommand;
045import org.apache.maven.scm.provider.git.gitexe.command.untag.GitUntagCommand;
046import org.apache.maven.scm.provider.git.gitexe.command.update.GitUpdateCommand;
047import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
048import org.apache.maven.scm.repository.ScmRepositoryException;
049
050/**
051 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
052 */
053@Singleton
054@Named("git")
055public class GitExeScmProvider extends AbstractGitScmProvider {
056    private final Map<String, String> environmentVariables;
057
058    public GitExeScmProvider() {
059        environmentVariables = new HashMap<>();
060    }
061
062    /** {@inheritDoc} */
063    protected GitCommand getAddCommand() {
064        return new GitAddCommand();
065    }
066
067    /** {@inheritDoc} */
068    protected GitCommand getBranchCommand() {
069        return new GitBranchCommand();
070    }
071
072    /** {@inheritDoc} */
073    protected GitCommand getChangeLogCommand() {
074        return new GitChangeLogCommand();
075    }
076
077    /** {@inheritDoc} */
078    protected GitCommand getCheckInCommand() {
079        return new GitCheckInCommand(environmentVariables);
080    }
081
082    /** {@inheritDoc} */
083    protected GitCommand getCheckOutCommand() {
084        return new GitCheckOutCommand(environmentVariables);
085    }
086
087    /** {@inheritDoc} */
088    protected GitCommand getDiffCommand() {
089        return new GitDiffCommand();
090    }
091
092    /** {@inheritDoc} */
093    protected GitCommand getExportCommand() {
094        return null; // X TODO
095    }
096
097    /** {@inheritDoc} */
098    protected GitCommand getRemoveCommand() {
099        return new GitRemoveCommand();
100    }
101
102    /** {@inheritDoc} */
103    protected GitCommand getStatusCommand() {
104        return new GitStatusCommand();
105    }
106
107    /** {@inheritDoc} */
108    protected GitCommand getTagCommand() {
109        return new GitTagCommand();
110    }
111
112    /** {@inheritDoc} */
113    protected GitCommand getUntagCommand() {
114        return new GitUntagCommand();
115    }
116
117    /** {@inheritDoc} */
118    protected GitCommand getUpdateCommand() {
119        return new GitUpdateCommand();
120    }
121
122    /** {@inheritDoc} */
123    public GitCommand getInfoCommand() {
124        return new GitInfoCommand();
125    }
126
127    /** {@inheritDoc} */
128    protected GitCommand getBlameCommand() {
129        return new GitBlameCommand();
130    }
131
132    /** {@inheritDoc} */
133    protected GitCommand getRemoteInfoCommand() {
134        return new GitRemoteInfoCommand(environmentVariables);
135    }
136
137    /** {@inheritDoc} */
138    protected String getRepositoryURL(File path) throws ScmException {
139        // Note: I need to supply just 1 absolute path, but ScmFileSet won't let me without
140        // a basedir (which isn't used here anyway), so use a dummy file.
141        // and a dummy ScmProviderRepository
142        InfoScmResult result =
143                info(new GitScmProviderRepository(path.toPath().toUri().toASCIIString()), new ScmFileSet(path), null);
144
145        if (result.getInfoItems().size() != 1) {
146            throw new ScmRepositoryException("Cannot find URL: "
147                    + (result.getInfoItems().size() == 0 ? "no" : "multiple") + " items returned by the info command");
148        }
149
150        return result.getInfoItems().get(0).getURL();
151    }
152
153    public void setEnvironmentVariable(String key, String value) {
154        environmentVariables.put(key, value);
155    }
156}