001    package 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    
022    import org.apache.maven.scm.CommandParameter;
023    import org.apache.maven.scm.CommandParameters;
024    import org.apache.maven.scm.ScmException;
025    import org.apache.maven.scm.ScmFileSet;
026    import org.apache.maven.scm.ScmResult;
027    import org.apache.maven.scm.command.AbstractCommand;
028    import org.apache.maven.scm.command.info.InfoScmResult;
029    import org.apache.maven.scm.provider.ScmProviderRepository;
030    import org.apache.maven.scm.provider.git.command.GitCommand;
031    import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
032    import org.codehaus.plexus.util.cli.CommandLineUtils;
033    import org.codehaus.plexus.util.cli.Commandline;
034    
035    /**
036     * @author Olivier Lamy
037     * @since 1.5
038     */
039    public 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
073                > NO_REVISION_LENGTH )// set the --short key only if revision length parameter is passed and 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/> and otherwise - the length to be applied for the revision formatting
087         * @throws ScmException
088         * @since 1.7
089         */
090        private static int getRevisionLength( final CommandParameters parameters )
091            throws ScmException
092        {
093            if ( parameters == null )
094            {
095                return NO_REVISION_LENGTH;
096            }
097            else
098            {
099                return parameters.getInt( CommandParameter.SCM_SHORT_REVISION_LENGTH, NO_REVISION_LENGTH );
100            }
101        }
102    
103    
104    }