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      public static Commandline getBaseGitCommandLine( File workingDirectory, String command )
85      {
86          if ( command == null || command.length() == 0 )
87          {
88              return null;
89          }
90  
91          Commandline cl = new Commandline();
92  
93          cl.setExecutable( "git" );
94  
95          cl.createArg().setValue( command );
96  
97          if ( workingDirectory != null )
98          {
99              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 }