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 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
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
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
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
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 }