View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.svn.svnexe.command.checkin;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFileSet;
27  import org.apache.maven.scm.ScmVersion;
28  import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
29  import org.apache.maven.scm.command.checkin.CheckInScmResult;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.svn.command.SvnCommand;
32  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
33  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
34  import org.codehaus.plexus.util.FileUtils;
35  import org.codehaus.plexus.util.Os;
36  import org.codehaus.plexus.util.cli.CommandLineException;
37  import org.codehaus.plexus.util.cli.CommandLineUtils;
38  import org.codehaus.plexus.util.cli.Commandline;
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 extends AbstractCheckInCommand implements SvnCommand {
46      /** {@inheritDoc} */
47      protected CheckInScmResult executeCheckInCommand(
48              ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion version) throws ScmException {
49          if (version != null && StringUtils.isNotEmpty(version.getName())) {
50              throw new ScmException("This provider command can't handle tags.");
51          }
52  
53          File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
54  
55          try {
56              FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8", message);
57          } catch (IOException ex) {
58              return new CheckInScmResult(
59                      null,
60                      "Error while making a temporary file for the commit message: " + ex.getMessage(),
61                      null,
62                      false);
63          }
64  
65          Commandline cl = createCommandLine((SvnScmProviderRepository) repo, fileSet, messageFile);
66  
67          SvnCheckInConsumer consumer = new SvnCheckInConsumer(fileSet.getBasedir());
68  
69          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
70  
71          if (logger.isInfoEnabled()) {
72              logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
73  
74              if (Os.isFamily(Os.FAMILY_WINDOWS)) {
75                  logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
76              }
77          }
78  
79          int exitCode;
80  
81          try {
82              exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
83          } catch (CommandLineException ex) {
84              throw new ScmException("Error while executing command.", ex);
85          } finally {
86              try {
87                  FileUtils.forceDelete(messageFile);
88              } catch (IOException ex) {
89                  // ignore
90              }
91          }
92  
93          if (exitCode != 0) {
94              return new CheckInScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
95          }
96  
97          return new CheckInScmResult(
98                  cl.toString(), consumer.getCheckedInFiles(), Integer.toString(consumer.getRevision()));
99      }
100 
101     // ----------------------------------------------------------------------
102     //
103     // ----------------------------------------------------------------------
104 
105     public static Commandline createCommandLine(
106             SvnScmProviderRepository repository, ScmFileSet fileSet, File messageFile) throws ScmException {
107         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(fileSet.getBasedir(), repository);
108 
109         cl.createArg().setValue("commit");
110 
111         cl.createArg().setValue("--file");
112 
113         cl.createArg().setValue(messageFile.getAbsolutePath());
114 
115         cl.createArg().setValue("--encoding");
116 
117         cl.createArg().setValue("UTF-8");
118 
119         try {
120             SvnCommandLineUtils.addTarget(cl, fileSet.getFileList());
121         } catch (IOException e) {
122             throw new ScmException("Can't create the targets file", e);
123         }
124 
125         return cl;
126     }
127 }