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 *
043 */
044public class SvnMkdirCommand extends AbstractMkdirCommand implements SvnCommand {
045    /**
046     * {@inheritDoc}
047     */
048    protected MkdirScmResult executeMkdirCommand(
049            ScmProviderRepository repository, ScmFileSet fileSet, String message, boolean createInLocal)
050            throws ScmException {
051        File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
052
053        try {
054            FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8", message);
055        } catch (IOException ex) {
056            return new MkdirScmResult(
057                    null, "Error while making a temporary file for the mkdir message: " + ex.getMessage(), null, false);
058        }
059
060        Commandline cl = createCommandLine((SvnScmProviderRepository) repository, fileSet, messageFile, createInLocal);
061
062        SvnMkdirConsumer consumer = new SvnMkdirConsumer();
063
064        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
065
066        if (logger.isInfoEnabled()) {
067            logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
068
069            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
070                logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
071            }
072        }
073
074        int exitCode;
075
076        try {
077            exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
078        } catch (CommandLineException ex) {
079            throw new ScmException("Error while executing command.", ex);
080        } finally {
081            try {
082                FileUtils.forceDelete(messageFile);
083            } catch (IOException ex) {
084                // ignore
085            }
086        }
087
088        if (exitCode != 0) {
089            return new MkdirScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
090        }
091
092        if (createInLocal) {
093            return new MkdirScmResult(cl.toString(), consumer.getCreatedDirs());
094        } else {
095            return new MkdirScmResult(cl.toString(), Integer.toString(consumer.getRevision()));
096        }
097    }
098
099    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}