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.maven.scm.ScmException;
23 import org.apache.maven.scm.log.ScmLogger;
24 import org.apache.maven.scm.provider.git.util.GitUtil;
25 import org.apache.maven.scm.providers.gitlib.settings.Settings;
26 import org.codehaus.plexus.util.cli.CommandLineException;
27 import org.codehaus.plexus.util.cli.CommandLineUtils;
28 import org.codehaus.plexus.util.cli.Commandline;
29 import org.codehaus.plexus.util.cli.StreamConsumer;
30
31 import java.io.File;
32 import java.io.IOException;
33 import java.util.List;
34
35
36
37
38
39
40
41
42 public final class GitCommandLineUtils
43 {
44
45 private GitCommandLineUtils()
46 {
47 }
48
49 public static void addTarget( Commandline cl, List<File> files )
50 {
51 if ( files == null || files.isEmpty() )
52 {
53 return;
54 }
55 final File workingDirectory = cl.getWorkingDirectory();
56 try
57 {
58 final String canonicalWorkingDirectory = workingDirectory.getCanonicalPath();
59 for ( File file : files )
60 {
61 String relativeFile = file.getPath();
62
63 final String canonicalFile = file.getCanonicalPath();
64 if ( canonicalFile.startsWith( canonicalWorkingDirectory ) )
65 {
66
67 relativeFile = canonicalFile.substring( canonicalWorkingDirectory.length() );
68
69 if ( relativeFile.startsWith( File.separator ) )
70 {
71 relativeFile = relativeFile.substring( File.separator.length() );
72 }
73 }
74
75
76 cl.createArg().setValue( relativeFile );
77 }
78 }
79 catch ( IOException ex )
80 {
81 throw new IllegalArgumentException( "Could not get canonical paths for workingDirectory = "
82 + workingDirectory + " or files=" + files, ex );
83 }
84 }
85
86
87
88
89
90
91
92 public static Commandline getBaseGitCommandLine( File workingDirectory, String command )
93 {
94 return getAnonymousBaseGitCommandLine( workingDirectory, command );
95 }
96
97
98
99
100
101
102
103
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 }