1 package org.apache.maven.scm.provider.git.gitexe.command;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.io.FilenameUtils;
23
24 import org.apache.maven.scm.ScmException;
25 import org.apache.maven.scm.log.ScmLogger;
26 import org.apache.maven.scm.provider.git.util.GitUtil;
27 import org.apache.maven.scm.providers.gitlib.settings.Settings;
28 import org.codehaus.plexus.util.cli.CommandLineException;
29 import org.codehaus.plexus.util.cli.CommandLineUtils;
30 import org.codehaus.plexus.util.cli.Commandline;
31 import org.codehaus.plexus.util.cli.StreamConsumer;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.List;
36
37
38
39
40
41
42
43
44 public final class GitCommandLineUtils
45 {
46
47 private GitCommandLineUtils()
48 {
49 }
50
51 public static void addTarget( Commandline cl, List<File> files )
52 {
53 if ( files == null || files.isEmpty() )
54 {
55 return;
56 }
57 final File workingDirectory = cl.getWorkingDirectory();
58 try
59 {
60 final String canonicalWorkingDirectory = workingDirectory.getCanonicalPath();
61 for ( File file : files )
62 {
63 String relativeFile = file.getPath();
64
65 final String canonicalFile = file.getCanonicalPath();
66 if ( canonicalFile.startsWith( canonicalWorkingDirectory ) )
67 {
68
69 relativeFile = canonicalFile.substring( canonicalWorkingDirectory.length() );
70
71 if ( relativeFile.startsWith( File.separator ) )
72 {
73 relativeFile = relativeFile.substring( File.separator.length() );
74 }
75 }
76
77
78 cl.createArg().setValue( FilenameUtils.separatorsToUnix( relativeFile ) );
79 }
80 }
81 catch ( IOException ex )
82 {
83 throw new IllegalArgumentException( "Could not get canonical paths for workingDirectory = "
84 + workingDirectory + " or files=" + files, ex );
85 }
86 }
87
88
89
90
91
92
93
94 public static Commandline getBaseGitCommandLine( File workingDirectory, String command )
95 {
96 return getAnonymousBaseGitCommandLine( workingDirectory, command );
97 }
98
99
100
101
102
103
104
105
106
107 private static Commandline getAnonymousBaseGitCommandLine( File workingDirectory, String command )
108 {
109 if ( command == null || command.length() == 0 )
110 {
111 return null;
112 }
113
114 Commandline cl = new AnonymousCommandLine();
115
116 composeCommand( workingDirectory, command, cl );
117
118 return cl;
119 }
120
121 private static void composeCommand( File workingDirectory, String command, Commandline cl )
122 {
123 Settings settings = GitUtil.getSettings();
124
125 cl.setExecutable( settings.getGitCommand() );
126
127 cl.createArg().setValue( command );
128
129 if ( workingDirectory != null )
130 {
131 cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
132 }
133 }
134
135 public static int execute( Commandline cl, StreamConsumer consumer, CommandLineUtils.StringStreamConsumer stderr,
136 ScmLogger logger )
137 throws ScmException
138 {
139 if ( logger.isInfoEnabled() )
140 {
141 logger.info( "Executing: " + cl );
142 logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
143 }
144
145 int exitCode;
146 try
147 {
148 exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
149 }
150 catch ( CommandLineException ex )
151 {
152 throw new ScmException( "Error while executing command.", ex );
153 }
154
155 return exitCode;
156 }
157
158 public static int execute( Commandline cl, CommandLineUtils.StringStreamConsumer stdout,
159 CommandLineUtils.StringStreamConsumer stderr, ScmLogger logger )
160 throws ScmException
161 {
162 if ( logger.isInfoEnabled() )
163 {
164 logger.info( "Executing: " + cl );
165 logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
166 }
167
168 int exitCode;
169 try
170 {
171 exitCode = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
172 }
173 catch ( CommandLineException ex )
174 {
175 throw new ScmException( "Error while executing command.", ex );
176 }
177
178 return exitCode;
179 }
180
181 }