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.maven.scm.ScmException;
23  import org.apache.maven.scm.log.ScmLogger;
24  import org.apache.maven.scm.provider.git.util.GitUtil;
25  import org.apache.maven.scm.providers.gitlib.settings.Settings;
26  import org.codehaus.plexus.util.cli.CommandLineException;
27  import org.codehaus.plexus.util.cli.CommandLineUtils;
28  import org.codehaus.plexus.util.cli.Commandline;
29  import org.codehaus.plexus.util.cli.StreamConsumer;
30  
31  import java.io.File;
32  import java.io.IOException;
33  import java.util.List;
34  
35  /**
36   * Command line construction utility.
37   * 
38   * @author Brett Porter
39   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
40   *
41   */
42  public final class GitCommandLineUtils
43  {
44  
45      private GitCommandLineUtils()
46      {
47      }
48  
49      public static void addTarget( Commandline cl, List<File> files )
50      {
51          if ( files == null || files.isEmpty() )
52          {
53              return;
54          }
55          final File workingDirectory = cl.getWorkingDirectory();
56          try
57          {
58              final String canonicalWorkingDirectory = workingDirectory.getCanonicalPath();
59              for ( File file : files )
60              {
61                  String relativeFile = file.getPath();
62  
63                  final String canonicalFile = file.getCanonicalPath();
64                  if ( canonicalFile.startsWith( canonicalWorkingDirectory ) )
65                  {
66                      // so we can omit the starting characters
67                      relativeFile = canonicalFile.substring( canonicalWorkingDirectory.length() );
68  
69                      if ( relativeFile.startsWith( File.separator ) )
70                      {
71                          relativeFile = relativeFile.substring( File.separator.length() );
72                      }
73                  }
74  
75                  // no setFile() since this screws up the working directory!
76                  cl.createArg().setValue( relativeFile );
77              }
78          }
79          catch ( IOException ex )
80          {
81              throw new IllegalArgumentException( "Could not get canonical paths for workingDirectory = "
82                  + workingDirectory + " or files=" + files, ex );
83          }
84      }
85  
86      /**
87       * 
88       * @param workingDirectory
89       * @param command
90       * @return
91       */
92      public static Commandline getBaseGitCommandLine( File workingDirectory, String command )
93      {
94          return getAnonymousBaseGitCommandLine( workingDirectory, command );
95      }
96  
97      /**
98       * Creates a {@link Commandline} for which the toString() do not display
99       * password.
100      * 
101      * @param workingDirectory
102      * @param command
103      * @return CommandLine with anonymous output.
104      */
105     private static Commandline getAnonymousBaseGitCommandLine( File workingDirectory, String command )
106     {
107         if ( command == null || command.length() == 0 )
108         {
109             return null;
110         }
111 
112         Commandline cl = new AnonymousCommandLine();
113 
114         composeCommand( workingDirectory, command, cl );
115 
116         return cl;
117     }
118 
119     private static void composeCommand( File workingDirectory, String command, Commandline cl )
120     {
121         Settings settings = GitUtil.getSettings();
122 
123         cl.setExecutable( settings.getGitCommand() );
124 
125         cl.createArg().setValue( command );
126 
127         if ( workingDirectory != null )
128         {
129             cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
130         }
131     }
132 
133     public static int execute( Commandline cl, StreamConsumer consumer, CommandLineUtils.StringStreamConsumer stderr,
134                                ScmLogger logger )
135         throws ScmException
136     {
137         if ( logger.isInfoEnabled() )
138         {
139             logger.info( "Executing: " + cl );
140             logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
141         }
142 
143         int exitCode;
144         try
145         {
146             exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
147         }
148         catch ( CommandLineException ex )
149         {
150             throw new ScmException( "Error while executing command.", ex );
151         }
152 
153         return exitCode;
154     }
155 
156     public static int execute( Commandline cl, CommandLineUtils.StringStreamConsumer stdout,
157                                CommandLineUtils.StringStreamConsumer stderr, ScmLogger logger )
158         throws ScmException
159     {
160         if ( logger.isInfoEnabled() )
161         {
162             logger.info( "Executing: " + cl );
163             logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
164         }
165 
166         int exitCode;
167         try
168         {
169             exitCode = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
170         }
171         catch ( CommandLineException ex )
172         {
173             throw new ScmException( "Error while executing command.", ex );
174         }
175 
176         return exitCode;
177     }
178 
179 }