View Javadoc
1   package org.apache.maven.scm.provider.svn.svnexe.command.mkdir;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmFileSet;
24  import org.apache.maven.scm.command.mkdir.AbstractMkdirCommand;
25  import org.apache.maven.scm.command.mkdir.MkdirScmResult;
26  import org.apache.maven.scm.provider.ScmProviderRepository;
27  import org.apache.maven.scm.provider.svn.command.SvnCommand;
28  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
29  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
30  import org.codehaus.plexus.util.FileUtils;
31  import org.codehaus.plexus.util.Os;
32  import org.codehaus.plexus.util.StringUtils;
33  import org.codehaus.plexus.util.cli.CommandLineException;
34  import org.codehaus.plexus.util.cli.CommandLineUtils;
35  import org.codehaus.plexus.util.cli.Commandline;
36  
37  import java.io.File;
38  import java.io.IOException;
39  import java.util.Iterator;
40  
41  /**
42   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
43   *
44   */
45  public class SvnMkdirCommand
46      extends AbstractMkdirCommand
47      implements SvnCommand
48  {
49      /**
50       * {@inheritDoc}
51       */
52      protected MkdirScmResult executeMkdirCommand( ScmProviderRepository repository, ScmFileSet fileSet, String message,
53                                                    boolean createInLocal )
54          throws ScmException
55      {
56          File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
57  
58          try
59          {
60              FileUtils.fileWrite( messageFile.getAbsolutePath(), message );
61          }
62          catch ( IOException ex )
63          {
64              return new MkdirScmResult( null,
65                                         "Error while making a temporary file for the mkdir message: " + ex.getMessage(),
66                                         null, false );
67          }
68  
69          Commandline cl =
70              createCommandLine( (SvnScmProviderRepository) repository, fileSet, messageFile, createInLocal );
71  
72          SvnMkdirConsumer consumer = new SvnMkdirConsumer( getLogger() );
73  
74          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
75  
76          if ( getLogger().isInfoEnabled() )
77          {
78              getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
79  
80              if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
81              {
82                  getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
83              }
84          }
85  
86          int exitCode;
87  
88          try
89          {
90              exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
91          }
92          catch ( CommandLineException ex )
93          {
94              throw new ScmException( "Error while executing command.", ex );
95          }
96          finally
97          {
98              try
99              {
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 }