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.command.checkout;
020
021import java.io.File;
022import java.util.Map;
023
024import org.apache.commons.lang3.StringUtils;
025import org.apache.maven.scm.CommandParameter;
026import org.apache.maven.scm.CommandParameters;
027import org.apache.maven.scm.ScmBranch;
028import org.apache.maven.scm.ScmException;
029import org.apache.maven.scm.ScmFileSet;
030import org.apache.maven.scm.ScmFileStatus;
031import org.apache.maven.scm.ScmResult;
032import org.apache.maven.scm.ScmTag;
033import org.apache.maven.scm.ScmVersion;
034import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
035import org.apache.maven.scm.command.checkout.CheckOutScmResult;
036import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult;
037import org.apache.maven.scm.provider.ScmProviderRepository;
038import org.apache.maven.scm.provider.git.command.GitCommand;
039import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
040import org.apache.maven.scm.provider.git.gitexe.command.list.GitListCommand;
041import org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer;
042import org.apache.maven.scm.provider.git.gitexe.command.remoteinfo.GitRemoteInfoCommand;
043import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
044import org.codehaus.plexus.util.cli.CommandLineUtils;
045import org.codehaus.plexus.util.cli.Commandline;
046
047/**
048 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
049 */
050public class GitCheckOutCommand extends AbstractCheckOutCommand implements GitCommand {
051    private final Map<String, String> environmentVariables;
052
053    public GitCheckOutCommand(Map<String, String> environmentVariables) {
054        super();
055        this.environmentVariables = environmentVariables;
056    }
057
058    /**
059     * For git, the given repository is a remote one.
060     * We have to clone it first if the working directory does not contain a git repo yet,
061     * otherwise we have to git-pull it.
062     * <p>
063     * TODO We currently assume a '.git' directory, so this does not work for --bare repos
064     * {@inheritDoc}
065     */
066    @Override
067    public ScmResult executeCommand(ScmProviderRepository repo, ScmFileSet fileSet, CommandParameters parameters)
068            throws ScmException {
069        ScmVersion version = parameters.getScmVersion(CommandParameter.SCM_VERSION, null);
070        boolean binary = parameters.getBoolean(CommandParameter.BINARY, false);
071        boolean shallow = parameters.getBoolean(CommandParameter.SHALLOW, false);
072
073        GitScmProviderRepository repository = (GitScmProviderRepository) repo;
074
075        if (GitScmProviderRepository.PROTOCOL_FILE.equals(
076                        repository.getFetchInfo().getProtocol())
077                && repository
078                                .getFetchInfo()
079                                .getPath()
080                                .indexOf(fileSet.getBasedir().getPath())
081                        >= 0) {
082            throw new ScmException("remote repository must not be the working directory");
083        }
084
085        int exitCode;
086
087        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
088        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
089
090        String lastCommandLine = "git-nothing-to-do";
091
092        if (!fileSet.getBasedir().exists() || !(new File(fileSet.getBasedir(), ".git").exists())) {
093            if (fileSet.getBasedir().exists()) {
094                // git refuses to clone otherwise
095                fileSet.getBasedir().delete();
096            }
097
098            // no git repo seems to exist, let's clone the original repo
099            Commandline gitClone = createCloneCommand(repository, fileSet.getBasedir(), version, binary, shallow);
100
101            exitCode = GitCommandLineUtils.execute(gitClone, stdout, stderr);
102            if (exitCode != 0) {
103                return new CheckOutScmResult(
104                        gitClone.toString(), "The git clone command failed.", stderr.getOutput(), false);
105            }
106            lastCommandLine = gitClone.toString();
107        }
108
109        GitRemoteInfoCommand gitRemoteInfoCommand = new GitRemoteInfoCommand(environmentVariables);
110
111        RemoteInfoScmResult result = gitRemoteInfoCommand.executeRemoteInfoCommand(repository, null, null);
112
113        if (fileSet.getBasedir().exists()
114                && new File(fileSet.getBasedir(), ".git").exists()
115                && result.getBranches().size() > 0) {
116            // git repo exists, so we must git-pull the changes
117            Commandline gitPull = createPullCommand(repository, fileSet.getBasedir(), version);
118
119            exitCode = GitCommandLineUtils.execute(gitPull, stdout, stderr);
120            if (exitCode != 0) {
121                return new CheckOutScmResult(
122                        gitPull.toString(), "The git pull command failed.", stderr.getOutput(), false);
123            }
124
125            // and now let's do the git-checkout itself
126            Commandline gitCheckout = createCommandLine(repository, fileSet.getBasedir(), version);
127
128            exitCode = GitCommandLineUtils.execute(gitCheckout, stdout, stderr);
129            if (exitCode != 0) {
130                return new CheckOutScmResult(
131                        gitCheckout.toString(), "The git checkout command failed.", stderr.getOutput(), false);
132            }
133            lastCommandLine = gitCheckout.toString();
134        }
135
136        // and now search for the files
137        GitListConsumer listConsumer = new GitListConsumer(fileSet.getBasedir(), ScmFileStatus.CHECKED_IN);
138
139        Commandline gitList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());
140
141        exitCode = GitCommandLineUtils.execute(gitList, listConsumer, stderr);
142        if (exitCode != 0) {
143            return new CheckOutScmResult(
144                    gitList.toString(), "The git ls-files command failed.", stderr.getOutput(), false);
145        }
146
147        return new CheckOutScmResult(lastCommandLine, listConsumer.getListedFiles());
148    }
149
150    // ----------------------------------------------------------------------
151    //
152    // ----------------------------------------------------------------------
153
154    public static Commandline createCommandLine(
155            GitScmProviderRepository repository, File workingDirectory, ScmVersion version) {
156        Commandline gitCheckout = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "checkout");
157
158        if (version != null && StringUtils.isNotEmpty(version.getName())) {
159            gitCheckout.createArg().setValue(version.getName());
160        }
161
162        return gitCheckout;
163    }
164
165    /**
166     * Create a git-clone repository command.
167     */
168    private Commandline createCloneCommand(
169            GitScmProviderRepository repository,
170            File workingDirectory,
171            ScmVersion version,
172            boolean binary,
173            boolean shallow) {
174        Commandline gitClone = GitCommandLineUtils.getBaseGitCommandLine(
175                workingDirectory.getParentFile(), "clone", repository, environmentVariables);
176
177        forceBinary(gitClone, binary);
178
179        if (shallow) {
180            gitClone.createArg().setValue("--depth");
181
182            gitClone.createArg().setValue("1");
183        }
184
185        if (version instanceof ScmBranch) {
186
187            gitClone.createArg().setValue("--branch");
188
189            gitClone.createArg().setValue(version.getName());
190        }
191
192        gitClone.createArg().setValue(repository.getFetchUrl());
193
194        gitClone.createArg().setValue(workingDirectory.getName());
195
196        return gitClone;
197    }
198
199    private void forceBinary(Commandline commandLine, boolean binary) {
200        if (binary) {
201            commandLine.createArg().setValue("-c");
202            commandLine.createArg().setValue("core.autocrlf=false");
203        }
204    }
205
206    /**
207     * Create a git fetch or git pull repository command.
208     */
209    private Commandline createPullCommand(
210            GitScmProviderRepository repository, File workingDirectory, ScmVersion version) {
211
212        if (version != null && StringUtils.isNotEmpty(version.getName())) {
213            if (version instanceof ScmTag) {
214                // A tag will not be pulled but we only fetch all the commits from the upstream repo
215                // This is done because checking out a tag might not happen on the current branch
216                // but create a 'detached HEAD'.
217                // In fact, a tag in git may be in multiple branches. This occurs if
218                // you create a branch after the tag has been created
219                Commandline gitFetch = GitCommandLineUtils.getBaseGitCommandLine(
220                        workingDirectory, "fetch", repository, environmentVariables);
221
222                gitFetch.createArg().setValue(repository.getFetchUrl());
223                return gitFetch;
224            } else {
225                Commandline gitPull = GitCommandLineUtils.getBaseGitCommandLine(
226                        workingDirectory, "pull", repository, environmentVariables);
227                gitPull.createArg().setValue(repository.getFetchUrl());
228                gitPull.createArg().setValue(version.getName() + ":" + version.getName());
229                return gitPull;
230            }
231        } else {
232            Commandline gitPull = GitCommandLineUtils.getBaseGitCommandLine(
233                    workingDirectory, "pull", repository, environmentVariables);
234            gitPull.createArg().setValue(repository.getFetchUrl());
235            gitPull.createArg().setValue("master");
236            return gitPull;
237        }
238    }
239
240    /**
241     * The overridden {@link #executeCommand(ScmProviderRepository, ScmFileSet, CommandParameters)} in this class will
242     * not call this method!
243     * <p>
244     * {@inheritDoc}
245     */
246    @Override
247    protected CheckOutScmResult executeCheckOutCommand(
248            ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive, boolean shallow)
249            throws ScmException {
250        throw new UnsupportedOperationException("Should not get here");
251    }
252}