View Javadoc

1   package org.apache.maven.scm.provider.jazz.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 org.apache.maven.scm.ChangeSet;
23  import org.apache.maven.scm.ScmBranch;
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
27  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
28  import org.apache.maven.scm.command.changelog.ChangeLogSet;
29  import org.apache.maven.scm.provider.ScmProviderRepository;
30  import org.apache.maven.scm.provider.jazz.command.JazzConstants;
31  import org.apache.maven.scm.provider.jazz.command.JazzScmCommand;
32  import org.apache.maven.scm.provider.jazz.command.consumer.ErrorConsumer;
33  import org.apache.maven.scm.provider.jazz.repository.JazzScmProviderRepository;
34  import org.codehaus.plexus.util.StringUtils;
35  
36  import java.util.ArrayList;
37  import java.util.Date;
38  import java.util.List;
39  
40  // To get a changelog, we need to get a list of changesets (scm history), and then for each changeset listed,
41  // get the details of each changeset (scm list changesets X, Y, Z).
42  // 
43  // We do not appear to be able to get a list of changes between a range of dates, so all of them are returned.
44  //
45  // See the following links for additional information on the RTC "history" command:
46  // RTC 2.0.0.2:
47  // http://publib.boulder.ibm.com/infocenter/rtc/v2r0m0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_history.html
48  // RTC 3.0:
49  // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_history.html
50  // RTC 3.0.1:
51  // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0m1/topic/com.ibm.team.scm.doc/topics/r_scm_cli_history.html
52  //
53  //
54  // See the following links for additional information on the RTC "list changesets" command:
55  // RTC 2.0.0.2:
56  // http://publib.boulder.ibm.com/infocenter/rtc/v2r0m0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_list.html
57  // RTC 3.0:
58  // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_list.html
59  // RTC 3.0.1:
60  // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0m1/topic/com.ibm.team.scm.doc/topics/r_scm_cli_list.html
61  //
62  
63  /**
64   * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a>
65   */
66  public class JazzChangeLogCommand
67      extends AbstractChangeLogCommand
68  {
69      /**
70       * {@inheritDoc}
71       */
72      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
73                                                            Date startDate, Date endDate, ScmBranch branch,
74                                                            String datePattern )
75          throws ScmException
76      {
77          if ( branch != null && StringUtils.isNotEmpty( branch.getName() ) )
78          {
79              throw new ScmException( "This SCM provider doesn't support branches." );
80          }
81  
82          if ( getLogger().isDebugEnabled() )
83          {
84              getLogger().debug( "Executing changelog command..." );
85          }
86  
87          // This acts as a two phase operation.
88          // The first pass is to call the "scm history" command to get a list
89          // of the changeSets from Jazz SCM. It is stored in the revision of the
90          // changeSets array.
91          List<ChangeSet> changeSets = new ArrayList<ChangeSet>();
92          JazzScmCommand historyCommand = createHistoryCommand( repo, fileSet );
93          JazzHistoryConsumer changeLogConsumer = new JazzHistoryConsumer( repo, getLogger(), changeSets );
94          ErrorConsumer errConsumer = new ErrorConsumer( getLogger() );
95          int status = historyCommand.execute( changeLogConsumer, errConsumer );
96          if ( status != 0 || errConsumer.hasBeenFed() )
97          {
98              return new ChangeLogScmResult( historyCommand.getCommandString(),
99                                             "Error code for Jazz SCM history command - " + status,
100                                            errConsumer.getOutput(), false );
101         }
102 
103         // Now, call the "scm list changesets" command, passing in the list of changesets from the first pass.
104         JazzScmCommand listChangesetsCommand = createListChangesetCommand( repo, fileSet, changeSets );
105         JazzListChangesetConsumer listChangesetConsumer =
106             new JazzListChangesetConsumer( repo, getLogger(), changeSets, datePattern );
107         errConsumer = new ErrorConsumer( getLogger() );
108         status = listChangesetsCommand.execute( listChangesetConsumer, errConsumer );
109         if ( status != 0 || errConsumer.hasBeenFed() )
110         {
111             return new ChangeLogScmResult( listChangesetsCommand.getCommandString(),
112                                            "Error code for Jazz SCM list changesets command - " + status,
113                                            errConsumer.getOutput(), false );
114         }
115 
116         // Build the result and return it.
117         ChangeLogSet changeLogSet = new ChangeLogSet( changeSets, startDate, endDate );
118 
119         // Return the "main" command used, namely "scm history"
120         return new ChangeLogScmResult( historyCommand.getCommandString(), changeLogSet );
121     }
122 
123     protected JazzScmCommand createHistoryCommand( ScmProviderRepository repo, ScmFileSet fileSet )
124     {
125         JazzScmCommand command = new JazzScmCommand( JazzConstants.CMD_HISTORY, repo, fileSet, getLogger() );
126         command.addArgument( JazzConstants.ARG_MAXIMUM );
127         command.addArgument( "10000000" );      // Beyond me as to why they didn't make 0 = all.
128         // And just to really annoy us, it defaults to 10.
129         // So we put something stupidly large in there instead.
130 
131         return command;
132     }
133 
134     protected JazzScmCommand createListChangesetCommand( ScmProviderRepository repo, ScmFileSet fileSet,
135                                                          List<ChangeSet> changeSets )
136     {
137         JazzScmProviderRepository jazzRepo = (JazzScmProviderRepository) repo;
138         JazzScmCommand command =
139             new JazzScmCommand( JazzConstants.CMD_LIST, JazzConstants.CMD_SUB_CHANGESETS, repo, fileSet, getLogger() );
140         command.addArgument( JazzConstants.ARG_WORKSPACE );
141         command.addArgument( jazzRepo.getWorkspace() );
142         for ( int i = 0; i < changeSets.size(); i++ )
143         {
144             command.addArgument( changeSets.get( i ).getRevision() );
145         }
146         return command;
147     }
148 }