View Javadoc
1   package org.apache.maven.scm.provider.git.gitexe.command;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.io.FilenameUtils;
23  
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.log.ScmLogger;
26  import org.apache.maven.scm.provider.git.util.GitUtil;
27  import org.apache.maven.scm.providers.gitlib.settings.Settings;
28  import org.codehaus.plexus.util.cli.CommandLineException;
29  import org.codehaus.plexus.util.cli.CommandLineUtils;
30  import org.codehaus.plexus.util.cli.Commandline;
31  import org.codehaus.plexus.util.cli.StreamConsumer;
32  
33  import java.io.File;
34  import java.io.IOException;
35  import java.util.List;
36  
37  /**
38   * Command line construction utility.
39   *
40   * @author Brett Porter
41   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
42   *
43   */
44  public final class GitCommandLineUtils
45  {
46  
47      private GitCommandLineUtils()
48      {
49      }
50  
51      public static void addTarget( Commandline cl, List<File> files )
52      {
53          if ( files == null || files.isEmpty() )
54          {
55              return;
56          }
57          final File workingDirectory = cl.getWorkingDirectory();
58          try
59          {
60              final String canonicalWorkingDirectory = workingDirectory.getCanonicalPath();
61              for ( File file : files )
62              {
63                  String relativeFile = file.getPath();
64  
65                  final String canonicalFile = file.getCanonicalPath();
66                  if ( canonicalFile.startsWith( canonicalWorkingDirectory ) )
67                  {
68                      // so we can omit the starting characters
69                      relativeFile = canonicalFile.substring( canonicalWorkingDirectory.length() );
70  
71                      if ( relativeFile.startsWith( File.separator ) )
72                      {
73                          relativeFile = relativeFile.substring( File.separator.length() );
74                      }
75                  }
76  
77                  // no setFile() since this screws up the working directory!
78                  cl.createArg().setValue( FilenameUtils.separatorsToUnix( relativeFile ) );
79              }
80          }
81          catch ( IOException ex )
82          {
83              throw new IllegalArgumentException( "Could not get canonical paths for workingDirectory = "
84                  + workingDirectory + " or files=" + files, ex );
85          }
86      }
87  
88      /**
89       *
90       * @param workingDirectory
91       * @param command
92       * @return
93       */
94      public static Commandline getBaseGitCommandLine( File workingDirectory, String command )
95      {
96          return getAnonymousBaseGitCommandLine( workingDirectory, command );
97      }
98  
99      /**
100      * Creates a {@link Commandline} for which the toString() do not display
101      * password.
102      *
103      * @param workingDirectory
104      * @param command
105      * @return CommandLine with anonymous output.
106      */
107     private static Commandline getAnonymousBaseGitCommandLine( File workingDirectory, String command )
108     {
109         if ( command == null || command.length() == 0 )
110         {
111             return null;
112         }
113 
114         Commandline cl = new AnonymousCommandLine();
115 
116         composeCommand( workingDirectory, command, cl );
117 
118         return cl;
119     }
120 
121     private static void composeCommand( File workingDirectory, String command, Commandline cl )
122     {
123         Settings settings = GitUtil.getSettings();
124 
125         cl.setExecutable( settings.getGitCommand() );
126 
127         cl.createArg().setValue( command );
128 
129         if ( workingDirectory != null )
130         {
131             cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
132         }
133     }
134 
135     public static int execute( Commandline cl, StreamConsumer consumer, CommandLineUtils.StringStreamConsumer stderr,
136                                ScmLogger logger )
137         throws ScmException
138     {
139         if ( logger.isInfoEnabled() )
140         {
141             logger.info( "Executing: " + cl );
142             logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
143         }
144 
145         int exitCode;
146         try
147         {
148             exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
149         }
150         catch ( CommandLineException ex )
151         {
152             throw new ScmException( "Error while executing command.", ex );
153         }
154 
155         return exitCode;
156     }
157 
158     public static int execute( Commandline cl, CommandLineUtils.StringStreamConsumer stdout,
159                                CommandLineUtils.StringStreamConsumer stderr, ScmLogger logger )
160         throws ScmException
161     {
162         if ( logger.isInfoEnabled() )
163         {
164             logger.info( "Executing: " + cl );
165             logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
166         }
167 
168         int exitCode;
169         try
170         {
171             exitCode = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
172         }
173         catch ( CommandLineException ex )
174         {
175             throw new ScmException( "Error while executing command.", ex );
176         }
177 
178         return exitCode;
179     }
180 
181 }