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   */
44  public class SvnMkdirCommand extends AbstractMkdirCommand implements SvnCommand {
45      /**
46       * {@inheritDoc}
47       */
48      protected MkdirScmResult executeMkdirCommand(
49              ScmProviderRepository repository, ScmFileSet fileSet, String message, boolean createInLocal)
50              throws ScmException {
51          File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
52  
53          try {
54              FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8", message);
55          } catch (IOException ex) {
56              return new MkdirScmResult(
57                      null, "Error while making a temporary file for the mkdir message: " + ex.getMessage(), null, false);
58          }
59  
60          Commandline cl = createCommandLine((SvnScmProviderRepository) repository, fileSet, messageFile, createInLocal);
61  
62          SvnMkdirConsumer consumer = new SvnMkdirConsumer();
63  
64          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
65  
66          if (logger.isInfoEnabled()) {
67              logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
68  
69              if (Os.isFamily(Os.FAMILY_WINDOWS)) {
70                  logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
71              }
72          }
73  
74          int exitCode;
75  
76          try {
77              exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
78          } catch (CommandLineException ex) {
79              throw new ScmException("Error while executing command.", ex);
80          } finally {
81              try {
82                  FileUtils.forceDelete(messageFile);
83              } catch (IOException ex) {
84                  // ignore
85              }
86          }
87  
88          if (exitCode != 0) {
89              return new MkdirScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
90          }
91  
92          if (createInLocal) {
93              return new MkdirScmResult(cl.toString(), consumer.getCreatedDirs());
94          } else {
95              return new MkdirScmResult(cl.toString(), Integer.toString(consumer.getRevision()));
96          }
97      }
98  
99      protected static Commandline createCommandLine(
100             SvnScmProviderRepository repository, ScmFileSet fileSet, File messageFile, boolean createInLocal) {
101         // as we want to be able to create path remote only create this directory if not here
102 
103         if (!fileSet.getBasedir().exists() && !createInLocal) {
104             fileSet.getBasedir().mkdirs();
105         }
106         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(fileSet.getBasedir(), repository);
107 
108         cl.createArg().setValue("mkdir");
109 
110         cl.createArg().setValue("--parents");
111 
112         Iterator<File> it = fileSet.getFileList().iterator();
113         String dirPath = it.next().getPath();
114         // replacing \ with / for windauze
115         if (dirPath != null && Os.isFamily(Os.FAMILY_WINDOWS)) {
116             dirPath = StringUtils.replace(dirPath, "\\", "/");
117         }
118 
119         if (!createInLocal) {
120             cl.createArg().setValue(repository.getUrl() + "/" + dirPath + "@");
121 
122             if (messageFile != null) {
123                 cl.createArg().setValue("--file");
124                 cl.createArg().setValue(messageFile.getAbsolutePath());
125 
126                 cl.createArg().setValue("--encoding");
127                 cl.createArg().setValue("UTF-8");
128             }
129         } else {
130             cl.createArg().setValue(dirPath);
131         }
132 
133         return cl;
134     }
135 }