001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.svn.svnexe.command.mkdir;
020
021import java.io.File;
022import java.io.IOException;
023import java.util.Iterator;
024
025import org.apache.commons.lang3.StringUtils;
026import org.apache.maven.scm.ScmException;
027import org.apache.maven.scm.ScmFileSet;
028import org.apache.maven.scm.command.mkdir.AbstractMkdirCommand;
029import org.apache.maven.scm.command.mkdir.MkdirScmResult;
030import org.apache.maven.scm.provider.ScmProviderRepository;
031import org.apache.maven.scm.provider.svn.command.SvnCommand;
032import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
033import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
034import org.codehaus.plexus.util.FileUtils;
035import org.codehaus.plexus.util.Os;
036import org.codehaus.plexus.util.cli.CommandLineException;
037import org.codehaus.plexus.util.cli.CommandLineUtils;
038import org.codehaus.plexus.util.cli.Commandline;
039
040/**
041 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
042 */
043public class SvnMkdirCommand extends AbstractMkdirCommand implements SvnCommand {
044    private final boolean interactive;
045
046    public SvnMkdirCommand(boolean interactive) {
047        this.interactive = interactive;
048    }
049
050    /**
051     * {@inheritDoc}
052     */
053    protected MkdirScmResult executeMkdirCommand(
054            ScmProviderRepository repository, ScmFileSet fileSet, String message, boolean createInLocal)
055            throws ScmException {
056        File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
057
058        try {
059            FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8", message);
060        } catch (IOException ex) {
061            return new MkdirScmResult(
062                    null, "Error while making a temporary file for the mkdir message: " + ex.getMessage(), null, false);
063        }
064
065        Commandline cl = createCommandLine((SvnScmProviderRepository) repository, fileSet, messageFile, createInLocal);
066
067        SvnMkdirConsumer consumer = new SvnMkdirConsumer();
068
069        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
070
071        if (logger.isInfoEnabled()) {
072            logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
073
074            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
075                logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
076            }
077        }
078
079        int exitCode;
080
081        try {
082            exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
083        } catch (CommandLineException ex) {
084            throw new ScmException("Error while executing command.", ex);
085        } finally {
086            try {
087                FileUtils.forceDelete(messageFile);
088            } catch (IOException ex) {
089                // ignore
090            }
091        }
092
093        if (exitCode != 0) {
094            return new MkdirScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
095        }
096
097        if (createInLocal) {
098            return new MkdirScmResult(cl.toString(), consumer.getCreatedDirs());
099        } 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}