View Javadoc

1   package org.apache.maven.scm.provider.vss.commands.update;
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  
23  import org.apache.maven.scm.ScmException;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmVersion;
26  import org.apache.maven.scm.command.changelog.ChangeLogCommand;
27  import org.apache.maven.scm.command.update.AbstractUpdateCommand;
28  import org.apache.maven.scm.command.update.UpdateScmResult;
29  import org.apache.maven.scm.provider.ScmProviderRepository;
30  import org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils;
31  import org.apache.maven.scm.provider.vss.commands.VssConstants;
32  import org.apache.maven.scm.provider.vss.commands.changelog.VssHistoryCommand;
33  import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
34  import org.codehaus.plexus.util.cli.CommandLineUtils;
35  import org.codehaus.plexus.util.cli.Commandline;
36  
37  /**
38   * @author <a href="mailto:triek@thrx.de">Thorsten Riek</a>
39   *
40   */
41  public class VssUpdateCommand
42      extends AbstractUpdateCommand
43  {
44      // TODO handle deleted files from VSS
45      /** {@inheritDoc} */
46      protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repository, ScmFileSet fileSet,
47                                                      ScmVersion version )
48          throws ScmException
49      {
50          if ( getLogger().isDebugEnabled() )
51          {
52              getLogger().debug( "executing update command..." );
53          }
54  
55          VssScmProviderRepository repo = (VssScmProviderRepository) repository;
56  
57          Commandline cl = buildCmdLine( repo, fileSet, version );
58  
59          VssUpdateConsumer consumer = new VssUpdateConsumer( repo, getLogger() );
60  
61          // TODO handle deleted files from VSS
62          // TODO identify local files
63          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
64  
65          int exitCode;
66  
67          if ( getLogger().isDebugEnabled() )
68          {
69              getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
70          }
71  
72          exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
73  
74          if ( exitCode != 0 )
75          {
76              String error = stderr.getOutput();
77  
78              if ( getLogger().isDebugEnabled() )
79              {
80                  getLogger().debug( "VSS returns error: [" + error + "] return code: [" + exitCode + "]" );
81              }
82              if ( error.indexOf( "A writable copy of" ) < 0 )
83              {
84                  return new UpdateScmResult( cl.toString(), "The vss command failed.", error, false );
85              }
86              // print out the writable copy for manual handling
87              if ( getLogger().isWarnEnabled() )
88              {
89                  getLogger().warn( error );
90              }
91          }
92  
93          return new UpdateScmResult( cl.toString(), consumer.getUpdatedFiles() );
94      }
95  
96      public Commandline buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
97          throws ScmException
98      {
99  
100         Commandline command = new Commandline();
101 
102         command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
103 
104         try
105         {
106             command.addSystemEnvironment();
107         }
108         catch ( Exception e )
109         {
110             throw new ScmException( "Can't add system environment.", e );
111         }
112 
113         command.addEnvironment( "SSDIR", repo.getVssdir() );
114 
115         String ssDir = VssCommandLineUtils.getSsDir();
116 
117         command.setExecutable( ssDir + VssConstants.SS_EXE );
118 
119         command.createArg().setValue( VssConstants.COMMAND_GET );
120 
121         command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
122 
123         //User identification to get access to vss repository
124         if ( repo.getUserPassword() != null )
125         {
126             command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
127         }
128 
129         //Display the history of an entire project list
130         command.createArg().setValue( VssConstants.FLAG_RECURSION );
131 
132         //Ignore: Do not ask for input under any circumstances.
133         command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
134 
135         // FIXME Update command only works if there is no file checked out
136         // or no file is dirty locally. It's better than overwriting
137         // checked out files
138         //Ignore: Do not touch local writable files.
139         command.createArg().setValue( VssConstants.FLAG_SKIP_WRITABLE );
140 
141         if ( version != null )
142         {
143             command.createArg().setValue( VssConstants.FLAG_VERSION_LABEL + version );
144         }
145 
146         return command;
147     }
148 
149     /** {@inheritDoc} */
150     protected ChangeLogCommand getChangeLogCommand()
151     {
152         VssHistoryCommand command = new VssHistoryCommand();
153 
154         command.setLogger( getLogger() );
155 
156         return command;
157     }
158 
159 }