View Javadoc

1   package org.apache.maven.scm.provider.vss.commands.changelog;
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.ScmBranch;
23  import org.apache.maven.scm.ScmException;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
26  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
27  import org.apache.maven.scm.command.changelog.ChangeLogSet;
28  import org.apache.maven.scm.provider.ScmProviderRepository;
29  import org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils;
30  import org.apache.maven.scm.provider.vss.commands.VssConstants;
31  import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
32  import org.codehaus.plexus.util.cli.CommandLineUtils;
33  import org.codehaus.plexus.util.cli.Commandline;
34  
35  import java.text.SimpleDateFormat;
36  import java.util.Date;
37  import java.util.Locale;
38  
39  /**
40   * @author <a href="mailto:triek@thrx.de">Thorsten Riek</a>
41   * @author Olivier Lamy
42   *
43   */
44  public class VssHistoryCommand
45      extends AbstractChangeLogCommand
46  {
47      /** {@inheritDoc} */
48      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repository, ScmFileSet fileSet,
49                                                            Date startDate, Date endDate, ScmBranch branch,
50                                                            String datePattern )
51          throws ScmException
52      {
53          VssScmProviderRepository repo = (VssScmProviderRepository) repository;
54  
55          Commandline cl = buildCmdLine( repo, fileSet, startDate, endDate );
56  
57          if ( getLogger().isInfoEnabled() )
58          {
59              getLogger().info( "Executing: " + cl );
60              getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
61          }
62  
63          VssChangeLogConsumer consumer = new VssChangeLogConsumer( repo, datePattern, getLogger() );
64  
65          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
66  
67          int exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
68  
69          if ( exitCode != 0 )
70          {
71              return new ChangeLogScmResult( cl.toString(), "The vss command failed.", stderr.getOutput(), false );
72          }
73  
74          return new ChangeLogScmResult( cl.toString(),
75                                         new ChangeLogSet( consumer.getModifications(), startDate, endDate ) );
76      }
77  
78      public Commandline buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet, Date startDate, Date endDate )
79          throws ScmException
80      {
81          Commandline command = new Commandline();
82  
83          command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
84  
85          try
86          {
87              command.addSystemEnvironment();
88          }
89          catch ( Exception e )
90          {
91              throw new ScmException( "Can't add system environment.", e );
92          }
93  
94          command.addEnvironment( "SSDIR", repo.getVssdir() );
95  
96          String ssDir = VssCommandLineUtils.getSsDir();
97  
98          command.setExecutable( ssDir + VssConstants.SS_EXE );
99  
100         command.createArg().setValue( VssConstants.COMMAND_HISTORY );
101 
102         command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
103 
104         //User identification to get access to vss repository
105         if ( repo.getUserPassword() != null )
106         {
107             command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
108         }
109 
110         //Display the history of an entire project list
111         command.createArg().setValue( VssConstants.FLAG_RECURSION );
112 
113         //Ignore: Do not ask for input under any circumstances.
114         command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
115 
116         //Display only versions that fall within specified data range.
117         if ( startDate != null )
118         {
119             if ( endDate == null )
120             {
121                 endDate = new Date(); // = now
122             }
123 
124             SimpleDateFormat sdf = new SimpleDateFormat( "dd/MM/yyyy", Locale.ENGLISH );
125             String dateRange = sdf.format( endDate ) + "~" + sdf.format( startDate );
126             command.createArg().setValue( VssConstants.FLAG_VERSION_DATE + dateRange );
127         }
128         return command;
129     }
130 }