View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.local.command.changelog;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Date;
25  import java.util.List;
26  
27  import org.apache.maven.scm.ChangeFile;
28  import org.apache.maven.scm.ChangeSet;
29  import org.apache.maven.scm.ScmBranch;
30  import org.apache.maven.scm.ScmException;
31  import org.apache.maven.scm.ScmFileSet;
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.local.repository.LocalScmProviderRepository;
37  import org.codehaus.plexus.util.FileUtils;
38  
39  /**
40   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
41   * @author Olivier Lamy
42   *
43   */
44  public class LocalChangeLogCommand extends AbstractChangeLogCommand {
45      /** {@inheritDoc} */
46      protected ChangeLogScmResult executeChangeLogCommand(
47              ScmProviderRepository repository,
48              ScmFileSet fileSet,
49              Date startDate,
50              Date endDate,
51              ScmBranch branch,
52              String datePattern)
53              throws ScmException {
54          LocalScmProviderRepository repo = (LocalScmProviderRepository) repository;
55  
56          if (branch != null) {
57              throw new ScmException("The local scm doesn't support tags.");
58          }
59  
60          File root = new File(repo.getRoot());
61  
62          String module = repo.getModule();
63  
64          File source = new File(root, module);
65  
66          File baseDestination = fileSet.getBasedir();
67  
68          if (!baseDestination.exists()) {
69              throw new ScmException("The working directory doesn't exist (" + baseDestination.getAbsolutePath() + ").");
70          }
71  
72          if (!root.exists()) {
73              throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
74          }
75  
76          if (!source.exists()) {
77              throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
78          }
79  
80          List<ChangeSet> changeLogList = new ArrayList<>();
81  
82          try {
83              File repoRoot = new File(repo.getRoot(), repo.getModule());
84  
85              List<File> files = fileSet.getFileList();
86  
87              if (files.isEmpty()) {
88                  files = FileUtils.getFiles(baseDestination, "**", null, false);
89              }
90  
91              for (File file : files) {
92  
93                  String path = file.getPath().replace('\\', '/');
94  
95                  File repoFile = new File(repoRoot, path);
96  
97                  file = new File(baseDestination, path);
98  
99                  ChangeSet changeSet = new ChangeSet();
100 
101                 int chop = repoRoot.getAbsolutePath().length();
102 
103                 String fileName = "/" + repoFile.getAbsolutePath().substring(chop + 1);
104 
105                 changeSet.addFile(new ChangeFile(fileName, null));
106 
107                 if (repoFile.exists()) {
108                     long lastModified = repoFile.lastModified();
109 
110                     Date modifiedDate = new Date(lastModified);
111 
112                     if (startDate != null) {
113                         if (startDate.before(modifiedDate) || startDate.equals(modifiedDate)) {
114                             if (endDate != null) {
115                                 if (endDate.after(modifiedDate) || endDate.equals(modifiedDate)) {
116                                     // nop
117                                 } else {
118                                     continue;
119                                 }
120                             }
121                         } else {
122                             continue;
123                         }
124                     }
125 
126                     changeSet.setDate(modifiedDate);
127 
128                     changeLogList.add(changeSet);
129                 } else {
130                     // This file is deleted
131                     changeLogList.add(changeSet);
132                 }
133             }
134         } catch (IOException ex) {
135             throw new ScmException("Error while getting change logs.", ex);
136         }
137 
138         return new ChangeLogScmResult(null, new ChangeLogSet(changeLogList, startDate, endDate));
139     }
140 }