View Javadoc
1   package org.apache.maven.scm.provider.local.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.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Date;
26  import java.util.List;
27  
28  import org.apache.maven.scm.ChangeFile;
29  import org.apache.maven.scm.ChangeSet;
30  import org.apache.maven.scm.ScmBranch;
31  import org.apache.maven.scm.ScmException;
32  import org.apache.maven.scm.ScmFileSet;
33  import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
34  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
35  import org.apache.maven.scm.command.changelog.ChangeLogSet;
36  import org.apache.maven.scm.provider.ScmProviderRepository;
37  import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
38  import org.codehaus.plexus.util.FileUtils;
39  
40  /**
41   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
42   * @author Olivier Lamy
43   *
44   */
45  public class LocalChangeLogCommand
46      extends AbstractChangeLogCommand
47  {
48      /** {@inheritDoc} */
49      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repository, ScmFileSet fileSet,
50                                                            Date startDate, Date endDate, ScmBranch branch,
51                                                            String datePattern )
52          throws ScmException
53      {
54          LocalScmProviderRepository repo = (LocalScmProviderRepository) repository;
55  
56          if ( branch != null )
57          {
58              throw new ScmException( "The local scm doesn't support tags." );
59          }
60  
61          File root = new File( repo.getRoot() );
62  
63          String module = repo.getModule();
64  
65          File source = new File( root, module );
66  
67          File baseDestination = fileSet.getBasedir();
68  
69          if ( !baseDestination.exists() )
70          {
71              throw new ScmException(
72                  "The working directory doesn't exist (" + baseDestination.getAbsolutePath() + ")." );
73          }
74  
75          if ( !root.exists() )
76          {
77              throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." );
78          }
79  
80          if ( !source.exists() )
81          {
82              throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." );
83          }
84  
85          List<ChangeSet> changeLogList = new ArrayList<ChangeSet>();
86  
87          try
88          {
89              File repoRoot = new File( repo.getRoot(), repo.getModule() );
90  
91              List<File> files = fileSet.getFileList();
92  
93              if ( files.isEmpty() )
94              {
95                  @SuppressWarnings( "unchecked" )
96                  List<File> fileList = FileUtils.getFiles( baseDestination, "**", null, false );
97                  files = fileList;
98              }
99  
100             for ( File file : files )
101             {
102 
103                 String path = file.getPath().replace( '\\', '/' );
104 
105                 File repoFile = new File( repoRoot, path );
106 
107                 file = new File( baseDestination, path );
108 
109                 ChangeSet changeSet = new ChangeSet();
110 
111                 int chop = repoRoot.getAbsolutePath().length();
112 
113                 String fileName = "/" + repoFile.getAbsolutePath().substring( chop + 1 );
114 
115                 changeSet.addFile( new ChangeFile( fileName, null ) );
116 
117                 if ( repoFile.exists() )
118                 {
119                     long lastModified = repoFile.lastModified();
120 
121                     Date modifiedDate = new Date( lastModified );
122 
123                     if ( startDate != null )
124                     {
125                         if ( startDate.before( modifiedDate ) || startDate.equals( modifiedDate ) )
126                         {
127                             if ( endDate != null )
128                             {
129                                 if ( endDate.after( modifiedDate ) || endDate.equals( modifiedDate ) )
130                                 {
131                                     // nop
132                                 }
133                                 else
134                                 {
135                                     continue;
136                                 }
137                             }
138                         }
139                         else
140                         {
141                             continue;
142                         }
143                     }
144 
145                     changeSet.setDate( modifiedDate );
146 
147                     changeLogList.add( changeSet );
148                 }
149                 else
150                 {
151                     // This file is deleted
152                     changeLogList.add( changeSet );
153                 }
154             }
155         }
156         catch ( IOException ex )
157         {
158             throw new ScmException( "Error while getting change logs.", ex );
159         }
160 
161         return new ChangeLogScmResult( null, new ChangeLogSet( changeLogList, startDate, endDate ) );
162     }
163 }