1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
42
43
44 public class SvnMkdirCommand extends AbstractMkdirCommand implements SvnCommand {
45
46
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
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
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
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 }