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