View Javadoc
1   package org.apache.maven.scm.provider.svn.svnexe.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.svn.command.SvnCommand;
29  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
30  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
31  import org.codehaus.plexus.util.FileUtils;
32  import org.codehaus.plexus.util.StringUtils;
33  import org.codehaus.plexus.util.cli.CommandLineException;
34  import org.codehaus.plexus.util.cli.CommandLineUtils;
35  import org.codehaus.plexus.util.cli.Commandline;
36  
37  import java.io.File;
38  import java.io.IOException;
39  
40  /**
41   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
42   * @author Olivier Lamy
43   *
44   */
45  public class SvnCheckInCommand
46      extends AbstractCheckInCommand
47      implements SvnCommand
48  {
49      /** {@inheritDoc} */
50      protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
51                                                        ScmVersion version )
52          throws ScmException
53      {
54          if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
55          {
56              throw new ScmException( "This provider command can't handle tags." );
57          }
58  
59          File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
60  
61          try
62          {
63              FileUtils.fileWrite( messageFile.getAbsolutePath(), message );
64          }
65          catch ( IOException ex )
66          {
67              return new CheckInScmResult( null, "Error while making a temporary file for the commit message: "
68                  + ex.getMessage(), null, false );
69          }
70  
71          Commandline cl = createCommandLine( (SvnScmProviderRepository) repo, fileSet, messageFile );
72  
73          SvnCheckInConsumer consumer = new SvnCheckInConsumer( getLogger(), fileSet.getBasedir() );
74  
75          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
76  
77          if ( getLogger().isInfoEnabled() )
78          {
79              getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
80              getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
81          }
82  
83          int exitCode;
84  
85          try
86          {
87              exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
88          }
89          catch ( CommandLineException ex )
90          {
91              throw new ScmException( "Error while executing command.", ex );
92          }
93          finally
94          {
95              try
96              {
97                  FileUtils.forceDelete( messageFile );
98              }
99              catch ( IOException ex )
100             {
101                 // ignore
102             }
103         }
104 
105         if ( exitCode != 0 )
106         {
107             return new CheckInScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
108         }
109 
110         return new CheckInScmResult( cl.toString(), consumer.getCheckedInFiles(), Integer.toString( consumer.getRevision() ) );
111     }
112 
113     // ----------------------------------------------------------------------
114     //
115     // ----------------------------------------------------------------------
116 
117     public static Commandline createCommandLine( SvnScmProviderRepository repository, ScmFileSet fileSet,
118                                                  File messageFile )
119         throws ScmException
120     {
121         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet.getBasedir(), repository );
122 
123         cl.createArg().setValue( "commit" );
124 
125         cl.createArg().setValue( "--file" );
126 
127         cl.createArg().setValue( messageFile.getAbsolutePath() );
128 
129         try
130         {
131             SvnCommandLineUtils.addTarget( cl, fileSet.getFileList() );
132         }
133         catch ( IOException e )
134         {
135             throw new ScmException( "Can't create the targets file", e );
136         }
137 
138         return cl;
139     }
140 }