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.status;
020
021import java.net.URI;
022
023import org.apache.maven.scm.ScmException;
024import org.apache.maven.scm.ScmFileSet;
025import org.apache.maven.scm.command.status.AbstractStatusCommand;
026import org.apache.maven.scm.command.status.StatusScmResult;
027import org.apache.maven.scm.provider.ScmProviderRepository;
028import org.apache.maven.scm.provider.git.command.GitCommand;
029import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
030import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
031import org.codehaus.plexus.util.cli.CommandLineUtils;
032import org.codehaus.plexus.util.cli.Commandline;
033import org.slf4j.Logger;
034
035/**
036 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
037 */
038public class GitStatusCommand extends AbstractStatusCommand implements GitCommand {
039    /**
040     * {@inheritDoc}
041     */
042    protected StatusScmResult executeStatusCommand(ScmProviderRepository repo, ScmFileSet fileSet) throws ScmException {
043        int exitCode;
044        CommandLineUtils.StringStreamConsumer stderr;
045
046        URI relativeRepositoryPath = getRelativeCWD(logger, fileSet);
047
048        Commandline cl = createCommandLine((GitScmProviderRepository) repo, fileSet);
049
050        GitStatusConsumer consumer = new GitStatusConsumer(fileSet.getBasedir(), relativeRepositoryPath, fileSet);
051
052        stderr = new CommandLineUtils.StringStreamConsumer();
053
054        exitCode = GitCommandLineUtils.execute(cl, consumer, stderr);
055        if (exitCode != 0) {
056            // git-status returns non-zero if nothing to do
057            if (logger.isInfoEnabled()) {
058                logger.info("nothing added to commit but untracked files present (use \"git add\" to track)");
059            }
060        }
061
062        return new StatusScmResult(cl.toString(), consumer.getChangedFiles());
063    }
064
065    // ----------------------------------------------------------------------
066    //
067    // ----------------------------------------------------------------------
068
069    /**
070     * Get the dir relative to the repository root.
071     *
072     * @param logger the caller command logger
073     * @param fileSet in which subdir to execute
074     * @return the relative URI
075     * @throws ScmException if execute() fails
076     */
077    public static URI getRelativeCWD(Logger logger, ScmFileSet fileSet) throws ScmException {
078        Commandline clRevparse = createRevparseShowPrefix(fileSet);
079
080        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
081        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
082
083        URI relativeRepositoryPath = null;
084
085        int exitCode = GitCommandLineUtils.execute(clRevparse, stdout, stderr);
086        if (exitCode != 0) {
087            // git-status returns non-zero if nothing to do
088            if (logger.isInfoEnabled()) {
089                logger.info("Could not resolve prefix");
090            }
091        } else {
092            relativeRepositoryPath =
093                    GitStatusConsumer.uriFromPath(stdout.getOutput().trim());
094        }
095        return relativeRepositoryPath;
096    }
097
098    public static Commandline createCommandLine(GitScmProviderRepository repository, ScmFileSet fileSet) {
099        Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "status");
100        cl.addArguments(new String[] {"--porcelain", "."});
101        return cl;
102    }
103
104    public static Commandline createRevparseShowPrefix(ScmFileSet fileSet) {
105        Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "rev-parse");
106        cl.addArguments(new String[] {"--show-prefix"});
107        return cl;
108    }
109}