1 package org.apache.maven.scm.provider.perforce.command.checkin;
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.ScmFileSet;
24 import org.apache.maven.scm.ScmVersion;
25 import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
26 import org.apache.maven.scm.command.checkin.CheckInScmResult;
27 import org.apache.maven.scm.provider.ScmProviderRepository;
28 import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
29 import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
30 import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
31 import org.codehaus.plexus.util.cli.CommandLineException;
32 import org.codehaus.plexus.util.cli.CommandLineUtils;
33 import org.codehaus.plexus.util.cli.Commandline;
34
35 import java.io.ByteArrayInputStream;
36 import java.io.File;
37 import java.io.IOException;
38 import java.util.HashSet;
39 import java.util.List;
40 import java.util.Set;
41
42
43
44
45
46 public class PerforceCheckInCommand
47 extends AbstractCheckInCommand
48 implements PerforceCommand
49 {
50
51
52
53 @Override
54 protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet files, String message,
55 ScmVersion version )
56 throws ScmException
57 {
58 Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir() );
59 PerforceCheckInConsumer consumer = new PerforceCheckInConsumer();
60 try
61 {
62 String jobs = System.getProperty( "maven.scm.jobs" );
63
64 if ( getLogger().isDebugEnabled() )
65 {
66 getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
67 }
68
69 PerforceScmProviderRepository prepo = (PerforceScmProviderRepository) repo;
70 String changes = createChangeListSpecification( prepo, files, message,
71 PerforceScmProvider.getRepoPath( getLogger(), prepo,
72 files.getBasedir() ),
73 jobs );
74
75 if ( getLogger().isDebugEnabled() )
76 {
77 getLogger().debug( "Sending changelist:\n" + changes );
78 }
79
80 CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
81 int exitCode =
82 CommandLineUtils.executeCommandLine( cl, new ByteArrayInputStream( changes.getBytes() ), consumer,
83 err );
84
85 if ( exitCode != 0 )
86 {
87 String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
88
89 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
90 msg.append( '\n' );
91 msg.append( "Command line was:" + cmdLine );
92
93 throw new CommandLineException( msg.toString() );
94 }
95 }
96 catch ( CommandLineException e )
97 {
98 if ( getLogger().isErrorEnabled() )
99 {
100 getLogger().error( "CommandLineException " + e.getMessage(), e );
101 }
102 }
103
104 return new CheckInScmResult( cl.toString(), consumer.isSuccess() ? "Checkin successful" : "Unable to submit",
105 consumer.getOutput(), consumer.isSuccess() );
106 }
107
108 public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory )
109 {
110 Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
111
112 command.createArg().setValue( "submit" );
113 command.createArg().setValue( "-i" );
114 return command;
115 }
116
117 private static final String NEWLINE = "\r\n";
118
119 public static String createChangeListSpecification( PerforceScmProviderRepository repo, ScmFileSet files,
120 String msg, String canonicalPath, String jobs )
121 {
122 StringBuilder buf = new StringBuilder();
123 buf.append( "Change: new" ).append( NEWLINE ).append( NEWLINE );
124 buf.append( "Description:" ).append( NEWLINE ).append( "\t" ).append( msg ).append( NEWLINE ).append( NEWLINE );
125 if ( jobs != null && jobs.length() != 0 )
126 {
127
128 buf.append( "Jobs:" ).append( NEWLINE ).append( "\t" ).append( jobs ).append( NEWLINE ).append( NEWLINE );
129 }
130
131 buf.append( "Files:" ).append( NEWLINE );
132 try
133 {
134 Set<String> dupes = new HashSet<String>();
135 File workingDir = files.getBasedir();
136 String candir = workingDir.getCanonicalPath();
137 List<File> fs = files.getFileList();
138 for ( int i = 0; i < fs.size(); i++ )
139 {
140 File file = null;
141 if ( fs.get( i ).isAbsolute() )
142 {
143 file = new File( fs.get( i ).getPath() );
144 }
145 else
146 {
147 file = new File( workingDir, fs.get( i ).getPath() );
148 }
149
150
151
152
153
154
155
156
157 String canfile = file.getCanonicalPath();
158 if ( dupes.contains( canfile ) )
159 {
160
161
162
163 System.err.println( "Skipping duplicate file: " + file );
164 continue;
165 }
166 dupes.add( canfile );
167 if ( canfile.startsWith( candir ) )
168 {
169 canfile = canfile.substring( candir.length() + 1 );
170 }
171 buf.append( "\t" ).append( canonicalPath ).append( "/" ).append( canfile.replace( '\\', '/' ) ).append(
172 NEWLINE );
173 }
174 }
175 catch ( IOException e )
176 {
177 e.printStackTrace();
178 }
179 return buf.toString();
180 }
181 }