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