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.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
35
36
37
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
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
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
85
86
87
88
89
90 public static Commandline getBaseGitCommandLine( File workingDirectory, String command )
91 {
92 return getAnonymousBaseGitCommandLine( workingDirectory, command );
93 }
94
95
96
97
98
99
100
101
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 }