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(), 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        Iterator<File> it = fileSet.getFileList().iterator();
137        String dirPath = it.next().getPath();
138        // replacing \ with / for windauze
139        if ( dirPath != null && Os.isFamily( Os.FAMILY_DOS ) )
140        {
141            dirPath = StringUtils.replace( dirPath, "\\", "/" );
142        }
143
144        if ( !createInLocal )
145        {
146            cl.createArg().setValue( repository.getUrl() + "/" + dirPath );
147
148            if ( messageFile != null )
149            {
150                cl.createArg().setValue( "--file" );
151                cl.createArg().setValue( messageFile.getAbsolutePath() );
152            }
153        }
154        else
155        {
156            cl.createArg().setValue( dirPath );
157        }
158
159        return cl;
160    }
161}