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.mkdir;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Iterator;
24  
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.maven.scm.ScmException;
27  import org.apache.maven.scm.ScmFileSet;
28  import org.apache.maven.scm.command.mkdir.AbstractMkdirCommand;
29  import org.apache.maven.scm.command.mkdir.MkdirScmResult;
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:oching@apache.org">Maria Odea Ching</a>
42   */
43  public class SvnMkdirCommand extends AbstractMkdirCommand implements SvnCommand {
44      private final boolean interactive;
45  
46      public SvnMkdirCommand(boolean interactive) {
47          this.interactive = interactive;
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      protected MkdirScmResult executeMkdirCommand(
54              ScmProviderRepository repository, ScmFileSet fileSet, String message, boolean createInLocal)
55              throws ScmException {
56          File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
57  
58          try {
59              FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8", message);
60          } catch (IOException ex) {
61              return new MkdirScmResult(
62                      null, "Error while making a temporary file for the mkdir message: " + ex.getMessage(), null, false);
63          }
64  
65          Commandline cl = createCommandLine((SvnScmProviderRepository) repository, fileSet, messageFile, createInLocal);
66  
67          SvnMkdirConsumer consumer = new SvnMkdirConsumer();
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 MkdirScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
95          }
96  
97          if (createInLocal) {
98              return new MkdirScmResult(cl.toString(), consumer.getCreatedDirs());
99          } else {
100             return new MkdirScmResult(cl.toString(), Integer.toString(consumer.getRevision()));
101         }
102     }
103 
104     protected Commandline createCommandLine(
105             SvnScmProviderRepository repository, ScmFileSet fileSet, File messageFile, boolean createInLocal) {
106         // as we want to be able to create path remote only create this directory if not here
107 
108         if (!fileSet.getBasedir().exists() && !createInLocal) {
109             fileSet.getBasedir().mkdirs();
110         }
111         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(fileSet.getBasedir(), repository, interactive);
112 
113         cl.createArg().setValue("mkdir");
114 
115         cl.createArg().setValue("--parents");
116 
117         Iterator<File> it = fileSet.getFileList().iterator();
118         String dirPath = it.next().getPath();
119         // replacing \ with / for windauze
120         if (Os.isFamily(Os.FAMILY_WINDOWS)) {
121             dirPath = StringUtils.replace(dirPath, "\\", "/");
122         }
123 
124         if (!createInLocal) {
125             cl.createArg().setValue(repository.getUrl() + "/" + dirPath + "@");
126 
127             if (messageFile != null) {
128                 cl.createArg().setValue("--file");
129                 cl.createArg().setValue(messageFile.getAbsolutePath());
130 
131                 cl.createArg().setValue("--encoding");
132                 cl.createArg().setValue("UTF-8");
133             }
134         } else {
135             cl.createArg().setValue(dirPath);
136         }
137 
138         return cl;
139     }
140 }