View Javadoc
1   package org.apache.maven.scm.provider.clearcase.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.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.clearcase.command.ClearCaseCommand;
30  import org.apache.maven.scm.provider.clearcase.util.ClearCaseUtil;
31  import org.apache.maven.scm.providers.clearcase.settings.Settings;
32  import org.codehaus.plexus.util.StringUtils;
33  import org.codehaus.plexus.util.cli.CommandLineException;
34  import org.codehaus.plexus.util.cli.CommandLineUtils;
35  import org.codehaus.plexus.util.cli.Commandline;
36  
37  import java.io.File;
38  import java.text.SimpleDateFormat;
39  import java.util.Date;
40  import java.util.Locale;
41  
42  /**
43   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
44   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
45   * @author <a href="mailto:frederic.mura@laposte.net">Frederic Mura</a>
46   * @author <a href="mailto:m.holster@anva.nl">Mark Holster</a>
47   * @author Olivier Lamy
48   *
49   */
50  public class ClearCaseChangeLogCommand
51      extends AbstractChangeLogCommand
52      implements ClearCaseCommand
53  {
54      // ----------------------------------------------------------------------
55      // AbstractChangeLogCommand Implementation
56      // ----------------------------------------------------------------------
57  
58      /** {@inheritDoc} */
59      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repository, ScmFileSet fileSet,
60                                                            Date startDate, Date endDate, ScmBranch branch,
61                                                            String datePattern )
62          throws ScmException
63      {
64          if ( getLogger().isDebugEnabled() )
65          {
66              getLogger().debug( "executing changelog command..." );
67          }
68          Commandline cl = createCommandLine( fileSet.getBasedir(), branch, startDate );
69  
70          ClearCaseChangeLogConsumer consumer = new ClearCaseChangeLogConsumer( getLogger(), datePattern );
71  
72          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
73  
74          int exitCode;
75  
76          try
77          {
78              if ( getLogger().isDebugEnabled() )
79              {
80                  getLogger().debug(
81                                     "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>"
82                                         + cl.toString() );
83              }
84              exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
85          }
86          catch ( CommandLineException ex )
87          {
88              throw new ScmException( "Error while executing cvs command.", ex );
89          }
90  
91          if ( exitCode != 0 )
92          {
93              return new ChangeLogScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
94          }
95  
96          return new ChangeLogScmResult( cl.toString(),
97                                         new ChangeLogSet( consumer.getModifications(), startDate, endDate ) );
98      }
99  
100     // ----------------------------------------------------------------------
101     //
102     // ----------------------------------------------------------------------
103 
104     /**
105      * ClearCase LT version doesn't support the attribut -fmt and -since for command lhistory.
106      *
107      * @param workingDirectory
108      * @param branch
109      * @param startDate
110      * @return The command line
111      */
112     public static Commandline createCommandLine( File workingDirectory, ScmBranch branch, Date startDate )
113     {
114         Commandline command = new Commandline();
115         command.setExecutable( "cleartool" );
116         command.createArg().setValue( "lshistory" );
117 
118         command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
119 
120         Settings settings = ClearCaseUtil.getSettings();
121         String userFormat =
122             StringUtils.isEmpty( settings.getChangelogUserFormat() ) ? "" : settings.getChangelogUserFormat();
123 
124         StringBuilder format = new StringBuilder();
125         format.append( "NAME:%En\\n" );
126         format.append( "DATE:%Nd\\n" );
127         format.append( "COMM:%-12.12o - %o - %c - Activity: %[activity]p\\n" );
128         format.append( "USER:%" + userFormat + "u\\n" );
129         format.append( "REVI:%Ln\\n" );
130 
131         command.createArg().setValue( "-fmt" );
132         command.createArg().setValue( format.toString() );
133         command.createArg().setValue( "-recurse" );
134         command.createArg().setValue( "-nco" );
135 
136         if ( startDate != null )
137         {
138             SimpleDateFormat sdf = new SimpleDateFormat( "dd-MMM-yyyy", Locale.ENGLISH );
139 
140             String start = sdf.format( startDate );
141 
142             command.createArg().setValue( "-since" );
143 
144             command.createArg().setValue( start );
145         }
146 
147         // TODO: End date?
148 
149         if ( branch != null && StringUtils.isNotEmpty( branch.getName() ) )
150         {
151             command.createArg().setValue( "-branch" );
152 
153             command.createArg().setValue( branch.getName() );
154         }
155 
156         return command;
157     }
158 }