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