001package org.apache.maven.scm.provider.git.gitexe.command.info;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.scm.CommandParameter;
023import org.apache.maven.scm.CommandParameters;
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.ScmResult;
027import org.apache.maven.scm.command.AbstractCommand;
028import org.apache.maven.scm.command.info.InfoScmResult;
029import org.apache.maven.scm.provider.ScmProviderRepository;
030import org.apache.maven.scm.provider.git.command.GitCommand;
031import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
032import org.codehaus.plexus.util.cli.CommandLineUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035/**
036 * @author Olivier Lamy
037 * @since 1.5
038 */
039public class GitInfoCommand
040    extends AbstractCommand
041    implements GitCommand
042{
043
044    public static final int NO_REVISION_LENGTH = -1;
045
046    @Override
047    protected ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet,
048                                        CommandParameters parameters )
049        throws ScmException
050    {
051
052        GitInfoConsumer consumer = new GitInfoConsumer( getLogger(), fileSet );
053        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
054
055        Commandline cli = createCommandLine( repository, fileSet, parameters );
056
057        int exitCode = GitCommandLineUtils.execute( cli, consumer, stderr, getLogger() );
058        if ( exitCode != 0 )
059        {
060            return new InfoScmResult( cli.toString(), "The git rev-parse command failed.", stderr.getOutput(), false );
061        }
062        return new InfoScmResult( cli.toString(), consumer.getInfoItems() );
063    }
064
065    public static Commandline createCommandLine( ScmProviderRepository repository, ScmFileSet fileSet,
066                                                 CommandParameters parameters )
067        throws ScmException
068    {
069        Commandline cli = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "rev-parse" );
070        cli.createArg().setValue( "--verify" );
071        final int revLength = getRevisionLength( parameters );
072        if ( revLength > NO_REVISION_LENGTH )// set the --short key only if revision length parameter is passed and
073                                             // different from -1
074        {
075            cli.createArg().setValue( "--short=" + revLength );
076        }
077        cli.createArg().setValue( "HEAD" );
078
079        return cli;
080    }
081
082    /**
083     * Get the revision length from the parameters
084     *
085     * @param parameters
086     * @return -1 if parameter {@link CommandParameter.SCM_SHORT_REVISION_LENGTH} is absent, <br/>
087     *         and otherwise - the length to be applied for the revision formatting
088     * @throws ScmException
089     * @since 1.7
090     */
091    private static int getRevisionLength( final CommandParameters parameters )
092        throws ScmException
093    {
094        if ( parameters == null )
095        {
096            return NO_REVISION_LENGTH;
097        }
098        else
099        {
100            return parameters.getInt( CommandParameter.SCM_SHORT_REVISION_LENGTH, NO_REVISION_LENGTH );
101        }
102    }
103
104
105}