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  public class SvnCheckInCommand extends AbstractCheckInCommand implements SvnCommand {
45      private final boolean interactive;
46  
47      public SvnCheckInCommand(boolean interactive) {
48          this.interactive = interactive;
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      protected CheckInScmResult executeCheckInCommand(
55              ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion version) throws ScmException {
56          if (version != null && StringUtils.isNotEmpty(version.getName())) {
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              FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8", message);
64          } catch (IOException ex) {
65              return new CheckInScmResult(
66                      null,
67                      "Error while making a temporary file for the commit message: " + ex.getMessage(),
68                      null,
69                      false);
70          }
71  
72          Commandline cl = createCommandLine((SvnScmProviderRepository) repo, fileSet, messageFile);
73  
74          SvnCheckInConsumer consumer = new SvnCheckInConsumer(fileSet.getBasedir());
75  
76          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
77  
78          if (logger.isInfoEnabled()) {
79              logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
80  
81              if (Os.isFamily(Os.FAMILY_WINDOWS)) {
82                  logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
83              }
84          }
85  
86          int exitCode;
87  
88          try {
89              exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
90          } catch (CommandLineException ex) {
91              throw new ScmException("Error while executing command.", ex);
92          } finally {
93              try {
94                  FileUtils.forceDelete(messageFile);
95              } catch (IOException ex) {
96                  // ignore
97              }
98          }
99  
100         if (exitCode != 0) {
101             return new CheckInScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
102         }
103 
104         return new CheckInScmResult(
105                 cl.toString(), consumer.getCheckedInFiles(), Integer.toString(consumer.getRevision()));
106     }
107 
108     // ----------------------------------------------------------------------
109     //
110     // ----------------------------------------------------------------------
111 
112     public static Commandline createCommandLine(
113             SvnScmProviderRepository repository, ScmFileSet fileSet, File messageFile) throws ScmException {
114         return createCommandLine(repository, fileSet, messageFile, true);
115     }
116 
117     public static Commandline createCommandLine(
118             SvnScmProviderRepository repository, ScmFileSet fileSet, File messageFile, boolean interactive)
119             throws ScmException {
120         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(fileSet.getBasedir(), repository, interactive);
121 
122         cl.createArg().setValue("commit");
123 
124         cl.createArg().setValue("--file");
125 
126         cl.createArg().setValue(messageFile.getAbsolutePath());
127 
128         cl.createArg().setValue("--encoding");
129 
130         cl.createArg().setValue("UTF-8");
131 
132         try {
133             SvnCommandLineUtils.addTarget(cl, fileSet.getFileList());
134         } catch (IOException e) {
135             throw new ScmException("Can't create the targets file", e);
136         }
137 
138         return cl;
139     }
140 }