001package 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 022import org.apache.maven.scm.ScmException; 023import org.apache.maven.scm.log.ScmLogger; 024import org.apache.maven.scm.provider.git.util.GitUtil; 025import org.apache.maven.scm.providers.gitlib.settings.Settings; 026import org.codehaus.plexus.util.cli.CommandLineException; 027import org.codehaus.plexus.util.cli.CommandLineUtils; 028import org.codehaus.plexus.util.cli.Commandline; 029import org.codehaus.plexus.util.cli.StreamConsumer; 030 031import java.io.File; 032import java.io.IOException; 033import java.util.List; 034 035/** 036 * Command line construction utility. 037 * 038 * @author Brett Porter 039 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a> 040 * 041 */ 042public final class GitCommandLineUtils 043{ 044 045 private GitCommandLineUtils() 046 { 047 } 048 049 public static void addTarget( Commandline cl, List<File> files ) 050 { 051 if ( files == null || files.isEmpty() ) 052 { 053 return; 054 } 055 final File workingDirectory = cl.getWorkingDirectory(); 056 try 057 { 058 final String canonicalWorkingDirectory = workingDirectory.getCanonicalPath(); 059 for ( File file : files ) 060 { 061 String relativeFile = file.getPath(); 062 063 final String canonicalFile = file.getCanonicalPath(); 064 if ( canonicalFile.startsWith( canonicalWorkingDirectory ) ) 065 { 066 // so we can omit the starting characters 067 relativeFile = canonicalFile.substring( canonicalWorkingDirectory.length() ); 068 069 if ( relativeFile.startsWith( File.separator ) ) 070 { 071 relativeFile = relativeFile.substring( File.separator.length() ); 072 } 073 } 074 075 // no setFile() since this screws up the working directory! 076 cl.createArg().setValue( relativeFile ); 077 } 078 } 079 catch ( IOException ex ) 080 { 081 throw new IllegalArgumentException( "Could not get canonical paths for workingDirectory = " 082 + workingDirectory + " or files=" + files, ex ); 083 } 084 } 085 086 /** 087 * 088 * @param workingDirectory 089 * @param command 090 * @return 091 */ 092 public static Commandline getBaseGitCommandLine( File workingDirectory, String command ) 093 { 094 return getAnonymousBaseGitCommandLine( workingDirectory, command ); 095 } 096 097 /** 098 * Creates a {@link Commandline} for which the toString() do not display 099 * 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}