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 public static Commandline getBaseGitCommandLine( File workingDirectory, String command )
85 {
86 if ( command == null || command.length() == 0 )
87 {
88 return null;
89 }
90
91 Commandline cl = new Commandline();
92
93 cl.setExecutable( "git" );
94
95 cl.createArg().setValue( command );
96
97 if ( workingDirectory != null )
98 {
99 cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
100 }
101
102 return cl;
103 }
104
105 public static int execute( Commandline cl, StreamConsumer consumer, CommandLineUtils.StringStreamConsumer stderr,
106 ScmLogger logger )
107 throws ScmException
108 {
109 if ( logger.isInfoEnabled() )
110 {
111 logger.info( "Executing: " + cl );
112 logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
113 }
114
115 int exitCode;
116 try
117 {
118 exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
119 }
120 catch ( CommandLineException ex )
121 {
122 throw new ScmException( "Error while executing command.", ex );
123 }
124
125 return exitCode;
126 }
127
128 public static int execute( Commandline cl, CommandLineUtils.StringStreamConsumer stdout,
129 CommandLineUtils.StringStreamConsumer stderr, ScmLogger logger )
130 throws ScmException
131 {
132 if ( logger.isInfoEnabled() )
133 {
134 logger.info( "Executing: " + cl );
135 logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
136 }
137
138 int exitCode;
139 try
140 {
141 exitCode = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
142 }
143 catch ( CommandLineException ex )
144 {
145 throw new ScmException( "Error while executing command.", ex );
146 }
147
148 return exitCode;
149 }
150
151 }