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    /**
059     * The environment variable that controls whether Git prompts for credentials in the terminal.
060     * @see <a href="https://git-scm.com/docs/git-credential#Documentation/git-credential.txt-envGIT_TERMINAL_PROMPT">GIT_TERMINAL_PROMPT</a>
061     */
062    private static final String GIT_TERMINAL_PROMPT = "GIT_TERMINAL_PROMPT";
063
064    public GitExeScmProvider() {
065        environmentVariables = new HashMap<>();
066    }
067
068    @Override
069    public void setInteractive(boolean interactive) {
070        if (interactive) {
071            // This is the default behavior therefore make sure to remove the variable if it was set before.
072            environmentVariables.remove(GIT_TERMINAL_PROMPT);
073        } else {
074            environmentVariables.put(GIT_TERMINAL_PROMPT, "0");
075        }
076    }
077
078    /** {@inheritDoc} */
079    protected GitCommand getAddCommand() {
080        return new GitAddCommand();
081    }
082
083    /** {@inheritDoc} */
084    protected GitCommand getBranchCommand() {
085        return new GitBranchCommand(environmentVariables);
086    }
087
088    /** {@inheritDoc} */
089    protected GitCommand getChangeLogCommand() {
090        return new GitChangeLogCommand();
091    }
092
093    /** {@inheritDoc} */
094    protected GitCommand getCheckInCommand() {
095        return new GitCheckInCommand(environmentVariables);
096    }
097
098    /** {@inheritDoc} */
099    protected GitCommand getCheckOutCommand() {
100        return new GitCheckOutCommand(environmentVariables);
101    }
102
103    /** {@inheritDoc} */
104    protected GitCommand getDiffCommand() {
105        return new GitDiffCommand();
106    }
107
108    /** {@inheritDoc} */
109    protected GitCommand getExportCommand() {
110        return null; // X TODO
111    }
112
113    /** {@inheritDoc} */
114    protected GitCommand getRemoveCommand() {
115        return new GitRemoveCommand();
116    }
117
118    /** {@inheritDoc} */
119    protected GitCommand getStatusCommand() {
120        return new GitStatusCommand();
121    }
122
123    /** {@inheritDoc} */
124    protected GitCommand getTagCommand() {
125        return new GitTagCommand(environmentVariables);
126    }
127
128    /** {@inheritDoc} */
129    protected GitCommand getUntagCommand() {
130        return new GitUntagCommand(environmentVariables);
131    }
132
133    /** {@inheritDoc} */
134    protected GitCommand getUpdateCommand() {
135        return new GitUpdateCommand();
136    }
137
138    /** {@inheritDoc} */
139    public GitCommand getInfoCommand() {
140        return new GitInfoCommand();
141    }
142
143    /** {@inheritDoc} */
144    protected GitCommand getBlameCommand() {
145        return new GitBlameCommand();
146    }
147
148    /** {@inheritDoc} */
149    protected GitCommand getRemoteInfoCommand() {
150        return new GitRemoteInfoCommand(environmentVariables);
151    }
152
153    /** {@inheritDoc} */
154    protected String getRepositoryURL(File path) throws ScmException {
155        // Note: I need to supply just 1 absolute path, but ScmFileSet won't let me without
156        // a basedir (which isn't used here anyway), so use a dummy file.
157        // and a dummy ScmProviderRepository
158        InfoScmResult result =
159                info(new GitScmProviderRepository(path.toPath().toUri().toASCIIString()), new ScmFileSet(path), null);
160
161        if (result.getInfoItems().size() != 1) {
162            throw new ScmRepositoryException("Cannot find URL: "
163                    + (result.getInfoItems().size() == 0 ? "no" : "multiple") + " items returned by the info command");
164        }
165
166        return result.getInfoItems().get(0).getURL();
167    }
168
169    public void setEnvironmentVariable(String key, String value) {
170        environmentVariables.put(key, value);
171    }
172}