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  public class LocalChangeLogCommand extends AbstractChangeLogCommand {
44      /**
45       * {@inheritDoc}
46       */
47      protected ChangeLogScmResult executeChangeLogCommand(
48              ScmProviderRepository repository,
49              ScmFileSet fileSet,
50              Date startDate,
51              Date endDate,
52              ScmBranch branch,
53              String datePattern)
54              throws ScmException {
55          LocalScmProviderRepository repo = (LocalScmProviderRepository) repository;
56  
57          if (branch != null) {
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              throw new ScmException("The working directory doesn't exist (" + baseDestination.getAbsolutePath() + ").");
71          }
72  
73          if (!root.exists()) {
74              throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
75          }
76  
77          if (!source.exists()) {
78              throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
79          }
80  
81          List<ChangeSet> changeLogList = new ArrayList<>();
82  
83          try {
84              File repoRoot = new File(repo.getRoot(), repo.getModule());
85  
86              List<File> files = fileSet.getFileList();
87  
88              if (files.isEmpty()) {
89                  files = FileUtils.getFiles(baseDestination, "**", null, false);
90              }
91  
92              for (File file : files) {
93  
94                  String path = file.getPath().replace('\\', '/');
95  
96                  File repoFile = new File(repoRoot, path);
97  
98                  file = new File(baseDestination, path);
99  
100                 ChangeSet changeSet = new ChangeSet();
101 
102                 int chop = repoRoot.getAbsolutePath().length();
103 
104                 String fileName = "/" + repoFile.getAbsolutePath().substring(chop + 1);
105 
106                 changeSet.addFile(new ChangeFile(fileName, null));
107 
108                 if (repoFile.exists()) {
109                     long lastModified = repoFile.lastModified();
110 
111                     Date modifiedDate = new Date(lastModified);
112 
113                     if (startDate != null) {
114                         if (startDate.before(modifiedDate) || startDate.equals(modifiedDate)) {
115                             if (endDate != null) {
116                                 if (endDate.after(modifiedDate) || endDate.equals(modifiedDate)) {
117                                     // nop
118                                 } else {
119                                     continue;
120                                 }
121                             }
122                         } else {
123                             continue;
124                         }
125                     }
126 
127                     changeSet.setDate(modifiedDate);
128 
129                     changeLogList.add(changeSet);
130                 } else {
131                     // This file is deleted
132                     changeLogList.add(changeSet);
133                 }
134             }
135         } catch (IOException ex) {
136             throw new ScmException("Error while getting change logs.", ex);
137         }
138 
139         return new ChangeLogScmResult(null, new ChangeLogSet(changeLogList, startDate, endDate));
140     }
141 }