View Javadoc
1   package org.apache.maven.scm.provider.vss.commands.checkin;
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 java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFile;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmVersion;
31  import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
32  import org.apache.maven.scm.command.checkin.CheckInScmResult;
33  import org.apache.maven.scm.provider.ScmProviderRepository;
34  import org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils;
35  import org.apache.maven.scm.provider.vss.commands.VssConstants;
36  import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
37  import org.codehaus.plexus.util.cli.CommandLineUtils;
38  import org.codehaus.plexus.util.cli.Commandline;
39  
40  /**
41   * @author <a href="mailto:matpimenta@gmail.com">Mateus Pimenta</a>
42   * @author Olivier Lamy
43   * @since 1.3
44   * 
45   */
46  public class VssCheckInCommand
47      extends AbstractCheckInCommand
48  {
49  
50      /**
51       * (non-Javadoc)
52       */
53      protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repository, ScmFileSet fileSet,
54                                                        String message, ScmVersion scmVersion )
55          throws ScmException
56      {
57          if ( getLogger().isDebugEnabled() )
58          {
59              getLogger().debug( "executing checkin command..." );
60          }
61  
62          VssScmProviderRepository repo = (VssScmProviderRepository) repository;
63  
64          List<Commandline> commandLines = buildCmdLine( repo, fileSet, scmVersion );
65  
66          VssCheckInConsumer consumer = new VssCheckInConsumer( repo, getLogger() );
67  
68          //      TODO handle deleted files from VSS
69          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
70  
71          int exitCode;
72  
73          StringBuilder sb = new StringBuilder();
74          for ( Commandline cl : commandLines )
75          {
76  
77              if ( getLogger().isDebugEnabled() )
78              {
79                  getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
80              }
81  
82              exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
83  
84              if ( exitCode != 0 )
85              {
86                  String error = stderr.getOutput();
87  
88                  if ( getLogger().isDebugEnabled() )
89                  {
90                      getLogger().debug( "VSS returns error: [" + error + "] return code: [" + exitCode + "]" );
91                  }
92                  if ( error.indexOf( "A writable copy of" ) < 0 )
93                  {
94                      return new CheckInScmResult( cl.toString(), "The vss command failed.", error, false );
95                  }
96                  // print out the writable copy for manual handling
97                  if ( getLogger().isWarnEnabled() )
98                  {
99                      getLogger().warn( error );
100                 }
101             }
102 
103         }
104         return new CheckInScmResult( sb.toString(), new ArrayList<ScmFile>() );
105     }
106 
107     public List<Commandline> buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
108         throws ScmException
109     {
110 
111         List<File> files = fileSet.getFileList();
112         List<Commandline> commands = new ArrayList<Commandline>();
113 
114         if ( files.size() > 0 )
115         {
116 
117             String base;
118             try
119             {
120                 base = fileSet.getBasedir().getCanonicalPath();
121             }
122             catch ( IOException e )
123             {
124                 throw new ScmException( "Invalid canonical path", e );
125             }
126 
127             for ( File file : files )
128             {
129 
130                 Commandline command = new Commandline();
131 
132                 try
133                 {
134                     command.addSystemEnvironment();
135                 }
136                 catch ( Exception e )
137                 {
138                     throw new ScmException( "Can't add system environment.", e );
139                 }
140 
141                 command.addEnvironment( "SSDIR", repo.getVssdir() );
142 
143                 String ssDir = VssCommandLineUtils.getSsDir();
144 
145                 command.setExecutable( ssDir + VssConstants.SS_EXE );
146 
147                 command.createArg().setValue( VssConstants.COMMAND_CHECKIN );
148 
149                 String absolute;
150                 try
151                 {
152                     absolute = file.getCanonicalPath();
153                     String relative;
154                     int index = absolute.indexOf( base );
155                     if ( index >= 0 )
156                     {
157                         relative = absolute.substring( index + base.length() );
158                     }
159                     else
160                     {
161                         relative = file.getPath();
162                     }
163 
164                     relative = relative.replace( '\\', '/' );
165 
166                     if ( !relative.startsWith( "/" ) )
167                     {
168                         relative = '/' + relative;
169                     }
170 
171                     String relativeFolder = relative.substring( 0, relative.lastIndexOf( '/' ) );
172 
173                     command.setWorkingDirectory( new File( fileSet.getBasedir().getAbsolutePath() + File.separatorChar
174                         + relativeFolder ).getCanonicalPath() );
175 
176                     command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() + relative );
177                 }
178                 catch ( IOException e )
179                 {
180                     throw new ScmException( "Invalid canonical path", e );
181                 }
182 
183                 //User identification to get access to vss repository
184                 if ( repo.getUserPassword() != null )
185                 {
186                     command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
187                 }
188 
189                 // Ignore: Do not ask for input under any circumstances.
190                 command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
191 
192                 // Ignore: Do not touch local writable files.
193                 command.createArg().setValue( VssConstants.FLAG_REPLACE_WRITABLE );
194 
195                 commands.add( command );
196 
197             }
198 
199         }
200         else
201         {
202             Commandline command = new Commandline();
203 
204             command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
205 
206             try
207             {
208                 command.addSystemEnvironment();
209             }
210             catch ( Exception e )
211             {
212                 throw new ScmException( "Can't add system environment.", e );
213             }
214 
215             command.addEnvironment( "SSDIR", repo.getVssdir() );
216 
217             String ssDir = VssCommandLineUtils.getSsDir();
218 
219             command.setExecutable( ssDir + VssConstants.SS_EXE );
220 
221             command.createArg().setValue( VssConstants.COMMAND_CHECKIN );
222 
223             command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
224             //Display the history of an entire project list
225             command.createArg().setValue( VssConstants.FLAG_RECURSION );
226 
227             //User identification to get access to vss repository
228             if ( repo.getUserPassword() != null )
229             {
230                 command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
231             }
232 
233             // Ignore: Do not ask for input under any circumstances.
234             command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
235 
236             // Ignore: Do not touch local writable files.
237             command.createArg().setValue( VssConstants.FLAG_REPLACE_WRITABLE );
238 
239             commands.add( command );
240 
241         }
242 
243         return commands;
244 
245     }
246 }