View Javadoc

1   package org.apache.maven.scm.provider.perforce.command.checkin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author Mike Perham
44   *
45   */
46  public class PerforceCheckInCommand
47      extends AbstractCheckInCommand
48      implements PerforceCommand
49  {
50      /**
51       * {@inheritDoc}
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             // Multiple jobs are not handled with this implementation
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                 // XXX Submit requires the canonical repository path for each
150                 // file.
151                 // It is unclear how to get that from a File object.
152                 // We assume the repo object has the relative prefix
153                 // "//depot/some/project"
154                 // and canfile has the relative path "src/foo.xml" to be added
155                 // to that prefix.
156                 // "//depot/some/project/src/foo.xml"
157                 String canfile = file.getCanonicalPath();
158                 if ( dupes.contains( canfile ) )
159                 {
160                     // XXX I am seeing duplicate files in the ScmFileSet.
161                     // I don't know why this is but we have to weed them out
162                     // or Perforce will barf
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 }