001    package org.apache.maven.scm.provider.git.gitexe.command;
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.ScmException;
023    import org.apache.maven.scm.log.ScmLogger;
024    import org.codehaus.plexus.util.cli.CommandLineException;
025    import org.codehaus.plexus.util.cli.CommandLineUtils;
026    import org.codehaus.plexus.util.cli.Commandline;
027    import org.codehaus.plexus.util.cli.StreamConsumer;
028    
029    import java.io.File;
030    import java.io.IOException;
031    import java.util.List;
032    
033    /**
034     * Command line construction utility.
035     * 
036     * @author Brett Porter
037     * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
038     * @version $Id: GitCommandLineUtils.java 1303172 2012-03-20 22:05:02Z rfscholte $
039     */
040    public final class GitCommandLineUtils
041    {
042    
043        private GitCommandLineUtils()
044        {
045        }
046    
047        public static void addTarget( Commandline cl, List<File> files )
048        {
049            if ( files == null || files.isEmpty() )
050            {
051                return;
052            }
053            final File workingDirectory = cl.getWorkingDirectory();
054            try
055            {
056                final String canonicalWorkingDirectory = workingDirectory.getCanonicalPath();
057                for ( File file : files )
058                {
059                    String relativeFile = file.getPath();
060                    
061                    final String canonicalFile = file.getCanonicalPath();
062                    if ( canonicalFile.startsWith( canonicalWorkingDirectory ) )
063                    {
064                        // so we can omit the starting characters
065                        relativeFile = canonicalFile.substring( canonicalWorkingDirectory.length() );
066    
067                        if ( relativeFile.startsWith( File.separator ) )
068                        {
069                            relativeFile = relativeFile.substring( File.separator.length() );
070                        }
071                    }
072    
073                    // no setFile() since this screws up the working directory!
074                    cl.createArg().setValue( relativeFile );
075                }
076            }
077            catch ( IOException ex )
078            {
079                throw new IllegalArgumentException( "Could not get canonical paths for workingDirectory = "
080                    + workingDirectory + " or files=" + files, ex );
081            }
082        }
083    
084        public static Commandline getBaseGitCommandLine( File workingDirectory, String command )
085        {
086            if ( command == null || command.length() == 0 )
087            {
088                return null;
089            }
090    
091            Commandline cl = new Commandline();
092    
093            cl.setExecutable( "git" );
094    
095            cl.createArg().setValue( command );
096    
097            if ( workingDirectory != null )
098            {
099                cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
100            }
101    
102            return cl;
103        }
104    
105        public static int execute( Commandline cl, StreamConsumer consumer, CommandLineUtils.StringStreamConsumer stderr,
106                                   ScmLogger logger )
107            throws ScmException
108        {
109            if ( logger.isInfoEnabled() )
110            {
111                logger.info( "Executing: " + cl );
112                logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
113            }
114    
115            int exitCode;
116            try
117            {
118                exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
119            }
120            catch ( CommandLineException ex )
121            {
122                throw new ScmException( "Error while executing command.", ex );
123            }
124    
125            return exitCode;
126        }
127    
128        public static int execute( Commandline cl, CommandLineUtils.StringStreamConsumer stdout,
129                                   CommandLineUtils.StringStreamConsumer stderr, ScmLogger logger )
130            throws ScmException
131        {
132            if ( logger.isInfoEnabled() )
133            {
134                logger.info( "Executing: " + cl );
135                logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
136            }
137    
138            int exitCode;
139            try
140            {
141                exitCode = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
142            }
143            catch ( CommandLineException ex )
144            {
145                throw new ScmException( "Error while executing command.", ex );
146            }
147    
148            return exitCode;
149        }
150    
151    }