View Javadoc
1   package org.apache.maven.scm.provider.perforce.command.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 java.io.File;
23  import java.text.DateFormat;
24  import java.text.SimpleDateFormat;
25  import java.util.Date;
26  import java.util.List;
27  
28  import org.apache.maven.scm.ScmBranch;
29  import org.apache.maven.scm.ScmException;
30  import org.apache.maven.scm.ScmFileSet;
31  import org.apache.maven.scm.ScmVersion;
32  import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
33  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
34  import org.apache.maven.scm.command.changelog.ChangeLogSet;
35  import org.apache.maven.scm.provider.ScmProviderRepository;
36  import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
37  import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
38  import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
39  import org.codehaus.plexus.util.StringUtils;
40  import org.codehaus.plexus.util.cli.CommandLineException;
41  import org.codehaus.plexus.util.cli.CommandLineUtils;
42  import org.codehaus.plexus.util.cli.Commandline;
43  
44  /**
45   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
46   * @author Olivier Lamy
47   *
48   */
49  public class PerforceChangeLogCommand
50      extends AbstractChangeLogCommand
51      implements PerforceCommand
52  {
53      /** {@inheritDoc} */
54      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
55                                                            ScmVersion startVersion, ScmVersion endVersion,
56                                                            String datePattern )
57          throws ScmException
58      {
59          return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion );
60      }
61  
62      /** {@inheritDoc} */
63      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
64                                                            Date startDate, Date endDate, ScmBranch branch,
65                                                            String datePattern )
66          throws ScmException
67      {
68          if ( branch != null && StringUtils.isNotEmpty( branch.getName() ) )
69          {
70              throw new ScmException( "This SCM doesn't support branches." );
71          }
72  
73          return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null );
74      }
75  
76      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
77                                                            Date startDate, Date endDate, ScmBranch branch,
78                                                            String datePattern, ScmVersion startVersion,
79                                                            ScmVersion endVersion )
80          throws ScmException
81      {
82          PerforceScmProviderRepository p4repo = (PerforceScmProviderRepository) repo;
83          String clientspec = PerforceScmProvider.getClientspecName( getLogger(), p4repo, fileSet.getBasedir() );
84          Commandline cl = createCommandLine( p4repo, fileSet.getBasedir(), clientspec, null, startDate, endDate, startVersion, endVersion );
85  
86          String location = PerforceScmProvider.getRepoPath( getLogger(), p4repo, fileSet.getBasedir() );
87          PerforceChangesConsumer consumer =
88              new PerforceChangesConsumer( getLogger() );
89  
90          try
91          {
92              if ( getLogger().isDebugEnabled() )
93              {
94                  getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
95              }
96  
97              CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
98              int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
99  
100             if ( exitCode != 0 )
101             {
102                 String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
103 
104                 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
105                 msg.append( '\n' );
106                 msg.append( "Command line was:" + cmdLine );
107 
108                 throw new CommandLineException( msg.toString() );
109             }
110         }
111         catch ( CommandLineException e )
112         {
113             if ( getLogger().isErrorEnabled() )
114             {
115                 getLogger().error( "CommandLineException " + e.getMessage(), e );
116             }
117         }
118 
119         List<String> changes = consumer.getChanges();
120 
121         cl = PerforceScmProvider.createP4Command( p4repo, fileSet.getBasedir() );
122         cl.createArg().setValue( "describe" );
123         cl.createArg().setValue( "-s" );
124 
125         for( String change : changes )
126         {
127             cl.createArg().setValue( change );
128         }
129 
130         PerforceDescribeConsumer describeConsumer =
131             new PerforceDescribeConsumer( location, datePattern, getLogger() );
132 
133         try
134         {
135             if ( getLogger().isDebugEnabled() )
136             {
137                 getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
138             }
139 
140             CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
141             int exitCode = CommandLineUtils.executeCommandLine( cl, describeConsumer, err );
142 
143             if ( exitCode != 0 )
144             {
145                 String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
146 
147                 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
148                 msg.append( '\n' );
149                 msg.append( "Command line was:" + cmdLine );
150 
151                 throw new CommandLineException( msg.toString() );
152             }
153         }
154         catch ( CommandLineException e )
155         {
156             if ( getLogger().isErrorEnabled() )
157             {
158                 getLogger().error( "CommandLineException " + e.getMessage(), e );
159             }
160         }
161 
162         ChangeLogSet cls = new ChangeLogSet( describeConsumer.getModifications(), null, null );
163         cls.setStartVersion(startVersion);
164         cls.setEndVersion(endVersion);
165         return new ChangeLogScmResult( cl.toString(), cls );
166     }
167 
168     public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
169                                                  String clientspec,
170                                                  ScmBranch branch, Date startDate, Date endDate,
171                                                  ScmVersion startVersion, ScmVersion endVersion )
172     {
173         DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd:HH:mm:ss");
174         Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
175 
176         if ( clientspec != null )
177         {
178             command.createArg().setValue( "-c" );
179             command.createArg().setValue( clientspec );
180         }
181         command.createArg().setValue( "changes" );
182         command.createArg().setValue( "-t" );
183 
184         StringBuilder fileSpec = new StringBuilder("...");
185         if ( startDate != null )
186         {
187             fileSpec.append( "@" )
188                  .append( dateFormat.format(startDate) )
189                  .append( "," );
190 
191             if ( endDate != null )
192             {
193                 fileSpec.append( dateFormat.format(endDate) );
194             }
195             else
196             {
197                 fileSpec.append( "now" );
198             }
199         }
200 
201         if ( startVersion != null )
202         {
203             fileSpec.append("@").append(startVersion.getName()).append(",");
204 
205             if ( endVersion != null )
206             {
207                 fileSpec.append( endVersion.getName() );
208             }
209             else
210             {
211                 fileSpec.append("now");
212             }
213         }
214 
215         command.createArg().setValue( fileSpec.toString() );
216 
217         return command;
218     }
219 }