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              getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
80          }
81  
82          int exitCode;
83  
84          try
85          {
86              exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
87          }
88          catch ( CommandLineException ex )
89          {
90              throw new ScmException( "Error while executing command.", ex );
91          }
92          finally
93          {
94              try
95              {
96                  FileUtils.forceDelete( messageFile );
97              }
98              catch ( IOException ex )
99              {
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 }