001    package 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    
022    import org.apache.maven.scm.ScmException;
023    import org.apache.maven.scm.ScmFileSet;
024    import org.apache.maven.scm.command.mkdir.AbstractMkdirCommand;
025    import org.apache.maven.scm.command.mkdir.MkdirScmResult;
026    import org.apache.maven.scm.provider.ScmProviderRepository;
027    import org.apache.maven.scm.provider.svn.command.SvnCommand;
028    import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
029    import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
030    import org.codehaus.plexus.util.FileUtils;
031    import org.codehaus.plexus.util.Os;
032    import org.codehaus.plexus.util.StringUtils;
033    import org.codehaus.plexus.util.cli.CommandLineException;
034    import org.codehaus.plexus.util.cli.CommandLineUtils;
035    import org.codehaus.plexus.util.cli.Commandline;
036    
037    import java.io.File;
038    import java.io.IOException;
039    import java.util.Iterator;
040    
041    /**
042     * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
043     * @version $Id$
044     */
045    public 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(), 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                getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
080            }
081    
082            int exitCode;
083    
084            try
085            {
086                exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
087            }
088            catch ( CommandLineException ex )
089            {
090                throw new ScmException( "Error while executing command.", ex );
091            }
092            finally
093            {
094                try
095                {
096                    FileUtils.forceDelete( messageFile );
097                }
098                catch ( IOException ex )
099                {
100                    // ignore
101                }
102            }
103    
104            if ( exitCode != 0 )
105            {
106                return new MkdirScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
107            }
108    
109            if ( createInLocal )
110            {
111                return new MkdirScmResult( cl.toString(), consumer.getCreatedDirs() );
112            }
113            else
114            {
115                return new MkdirScmResult( cl.toString(), Integer.toString( consumer.getRevision() ) );
116            }
117        }
118    
119        protected static Commandline createCommandLine( SvnScmProviderRepository repository, ScmFileSet fileSet,
120                                                        File messageFile, boolean createInLocal )
121        {
122            // as we want to be able to create path remote only create this directory if not here
123    
124            if ( !fileSet.getBasedir().exists() && !createInLocal )
125            {
126                fileSet.getBasedir().mkdirs();
127            }
128            Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet.getBasedir(), repository );
129    
130            cl.createArg().setValue( "mkdir" );
131    
132            Iterator<File> it = fileSet.getFileList().iterator();
133            String dirPath = it.next().getPath();
134            // replacing \ with / for windauze
135            if ( dirPath != null && Os.isFamily( Os.FAMILY_DOS ) )
136            {
137                dirPath = StringUtils.replace( dirPath, "\\", "/" );
138            }
139    
140            if ( !createInLocal )
141            {
142                cl.createArg().setValue( repository.getUrl() + "/" + dirPath );
143    
144                if ( messageFile != null )
145                {
146                    cl.createArg().setValue( "--file" );
147                    cl.createArg().setValue( messageFile.getAbsolutePath() );
148                }
149            }
150            else
151            {
152                cl.createArg().setValue( dirPath );
153            }
154    
155            return cl;
156        }
157    }