001package org.apache.maven.scm.provider.svn.svnexe.command.mkdir;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.scm.ScmException;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.command.mkdir.AbstractMkdirCommand;
025import org.apache.maven.scm.command.mkdir.MkdirScmResult;
026import org.apache.maven.scm.provider.ScmProviderRepository;
027import org.apache.maven.scm.provider.svn.command.SvnCommand;
028import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
029import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
030import org.codehaus.plexus.util.FileUtils;
031import org.codehaus.plexus.util.Os;
032import org.codehaus.plexus.util.StringUtils;
033import org.codehaus.plexus.util.cli.CommandLineException;
034import org.codehaus.plexus.util.cli.CommandLineUtils;
035import org.codehaus.plexus.util.cli.Commandline;
036
037import java.io.File;
038import java.io.IOException;
039import java.util.Iterator;
040
041/**
042 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
043 *
044 */
045public class SvnMkdirCommand
046    extends AbstractMkdirCommand
047    implements SvnCommand
048{
049    /**
050     * {@inheritDoc}
051     */
052    protected MkdirScmResult executeMkdirCommand( ScmProviderRepository repository, ScmFileSet fileSet, String message,
053                                                  boolean createInLocal )
054        throws ScmException
055    {
056        File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
057
058        try
059        {
060            FileUtils.fileWrite( messageFile.getAbsolutePath(), "UTF-8", message );
061        }
062        catch ( IOException ex )
063        {
064            return new MkdirScmResult( null,
065                                       "Error while making a temporary file for the mkdir message: " + ex.getMessage(),
066                                       null, false );
067        }
068
069        Commandline cl =
070            createCommandLine( (SvnScmProviderRepository) repository, fileSet, messageFile, createInLocal );
071
072        SvnMkdirConsumer consumer = new SvnMkdirConsumer( getLogger() );
073
074        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
075
076        if ( getLogger().isInfoEnabled() )
077        {
078            getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
079
080            if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
081            {
082                getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
083            }
084        }
085
086        int exitCode;
087
088        try
089        {
090            exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
091        }
092        catch ( CommandLineException ex )
093        {
094            throw new ScmException( "Error while executing command.", ex );
095        }
096        finally
097        {
098            try
099            {
100                FileUtils.forceDelete( messageFile );
101            }
102            catch ( IOException ex )
103            {
104                // ignore
105            }
106        }
107
108        if ( exitCode != 0 )
109        {
110            return new MkdirScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
111        }
112
113        if ( createInLocal )
114        {
115            return new MkdirScmResult( cl.toString(), consumer.getCreatedDirs() );
116        }
117        else
118        {
119            return new MkdirScmResult( cl.toString(), Integer.toString( consumer.getRevision() ) );
120        }
121    }
122
123    protected static Commandline createCommandLine( SvnScmProviderRepository repository, ScmFileSet fileSet,
124                                                    File messageFile, boolean createInLocal )
125    {
126        // as we want to be able to create path remote only create this directory if not here
127
128        if ( !fileSet.getBasedir().exists() && !createInLocal )
129        {
130            fileSet.getBasedir().mkdirs();
131        }
132        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet.getBasedir(), repository );
133
134        cl.createArg().setValue( "mkdir" );
135
136        cl.createArg().setValue( "--parents" );
137
138        Iterator<File> it = fileSet.getFileList().iterator();
139        String dirPath = it.next().getPath();
140        // replacing \ with / for windauze
141        if ( dirPath != null && Os.isFamily( Os.FAMILY_WINDOWS ) )
142        {
143            dirPath = StringUtils.replace( dirPath, "\\", "/" );
144        }
145
146        if ( !createInLocal )
147        {
148            cl.createArg().setValue( repository.getUrl() + "/" + dirPath + "@" );
149
150            if ( messageFile != null )
151            {
152                cl.createArg().setValue( "--file" );
153                cl.createArg().setValue( messageFile.getAbsolutePath() );
154
155                cl.createArg().setValue( "--encoding" );
156                cl.createArg().setValue( "UTF-8" );
157            }
158        }
159        else
160        {
161            cl.createArg().setValue( dirPath );
162        }
163
164        return cl;
165    }
166}