View Javadoc

1   package org.apache.maven.scm.provider.cvslib.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.ScmVersion;
26  import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
27  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
28  import org.apache.maven.scm.provider.ScmProviderRepository;
29  import org.apache.maven.scm.provider.cvslib.command.CvsCommand;
30  import org.apache.maven.scm.provider.cvslib.command.CvsCommandUtils;
31  import org.apache.maven.scm.provider.cvslib.repository.CvsScmProviderRepository;
32  import org.apache.maven.scm.provider.cvslib.util.CvsUtil;
33  import org.codehaus.plexus.util.Os;
34  import org.codehaus.plexus.util.StringUtils;
35  import org.codehaus.plexus.util.cli.Commandline;
36  
37  import java.text.SimpleDateFormat;
38  import java.util.Date;
39  
40  /**
41   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse </a>
42   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
43   *
44   */
45  public abstract class AbstractCvsChangeLogCommand
46      extends AbstractChangeLogCommand
47      implements CvsCommand
48  {
49      /** {@inheritDoc} */
50      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
51                                                            ScmVersion startVersion, ScmVersion endVersion,
52                                                            String datePattern )
53          throws ScmException
54      {
55          return executeChangeLogCommand( repo, fileSet, null, null, null, startVersion, endVersion, datePattern );
56      }
57  
58      /** {@inheritDoc} */
59      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
60                                                            Date startDate, Date endDate, ScmBranch branch,
61                                                            String datePattern )
62          throws ScmException
63      {
64          return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, null, null, datePattern );
65      }
66  
67      private ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet, Date startDate,
68                                                          Date endDate, ScmBranch branch, ScmVersion startVersion,
69                                                          ScmVersion endVersion, String datePattern )
70          throws ScmException
71      {
72          CvsScmProviderRepository repository = (CvsScmProviderRepository) repo;
73  
74          Commandline cl = CvsCommandUtils.getBaseCommand( "log", repository, fileSet );
75  
76          if ( startDate != null )
77          {
78              SimpleDateFormat outputDate = new SimpleDateFormat( getDateFormat() );
79  
80              String dateRange;
81  
82              if ( endDate == null )
83              {
84                  dateRange = ">" + outputDate.format( startDate );
85              }
86              else
87              {
88                  dateRange = outputDate.format( startDate ) + "<" + outputDate.format( endDate );
89              }
90  
91              cl.createArg().setValue( "-d" );
92  
93              addDateRangeParameter( cl, dateRange );
94          }
95  
96          if ( branch != null && StringUtils.isNotEmpty( branch.getName() ) )
97          {
98              cl.createArg().setValue( "-r" + branch.getName() );
99          }
100 
101         if ( startVersion != null  || endVersion != null )
102         {
103             StringBuilder sb = new StringBuilder();
104             sb.append( "-r" );
105             if ( startVersion != null && StringUtils.isNotEmpty( startVersion.getName() ) )
106             {
107                 sb.append( startVersion.getName() );
108             }
109             sb.append( "::" );
110             if ( endVersion != null && StringUtils.isNotEmpty( endVersion.getName() ) )
111             {
112                 sb.append( endVersion.getName() );
113             }
114 
115             cl.createArg().setValue( sb.toString() );
116         }
117 
118         if ( getLogger().isInfoEnabled() )
119         {
120             getLogger().info( "Executing: " + cl );
121             getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
122         }
123 
124         return executeCvsCommand( cl, startDate, endDate, startVersion, endVersion, datePattern );
125     }
126 
127     protected abstract ChangeLogScmResult executeCvsCommand( Commandline cl, Date startDate, Date endDate,
128                                                              ScmVersion startVersion, ScmVersion endVersion,
129                                                              String datePattern )
130         throws ScmException;
131 
132     protected String getDateFormat()
133     {
134         return CvsUtil.getSettings().getChangeLogCommandDateFormat();
135     }
136 
137     protected void addDateRangeParameter( Commandline cl, String dateRange )
138     {
139         // There's a difference between UNIX-like OS and Windows
140         // See http://jira.codehaus.org/browse/SCM-187
141         if ( Os.isFamily( "windows" ) )
142         {
143             cl.createArg().setValue( "\"" + dateRange + "\"" );
144         }
145         else
146         {
147             cl.createArg().setValue( dateRange );
148         }
149     }
150 }