001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.local.command.changelog;
020
021import java.io.File;
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.Date;
025import java.util.List;
026
027import org.apache.maven.scm.ChangeFile;
028import org.apache.maven.scm.ChangeSet;
029import org.apache.maven.scm.ScmBranch;
030import org.apache.maven.scm.ScmException;
031import org.apache.maven.scm.ScmFileSet;
032import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
033import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
034import org.apache.maven.scm.command.changelog.ChangeLogSet;
035import org.apache.maven.scm.provider.ScmProviderRepository;
036import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
037import org.codehaus.plexus.util.FileUtils;
038
039/**
040 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
041 * @author Olivier Lamy
042 */
043public class LocalChangeLogCommand extends AbstractChangeLogCommand {
044    /**
045     * {@inheritDoc}
046     */
047    protected ChangeLogScmResult executeChangeLogCommand(
048            ScmProviderRepository repository,
049            ScmFileSet fileSet,
050            Date startDate,
051            Date endDate,
052            ScmBranch branch,
053            String datePattern)
054            throws ScmException {
055        LocalScmProviderRepository repo = (LocalScmProviderRepository) repository;
056
057        if (branch != null) {
058            throw new ScmException("The local scm doesn't support tags.");
059        }
060
061        File root = new File(repo.getRoot());
062
063        String module = repo.getModule();
064
065        File source = new File(root, module);
066
067        File baseDestination = fileSet.getBasedir();
068
069        if (!baseDestination.exists()) {
070            throw new ScmException("The working directory doesn't exist (" + baseDestination.getAbsolutePath() + ").");
071        }
072
073        if (!root.exists()) {
074            throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
075        }
076
077        if (!source.exists()) {
078            throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
079        }
080
081        List<ChangeSet> changeLogList = new ArrayList<>();
082
083        try {
084            File repoRoot = new File(repo.getRoot(), repo.getModule());
085
086            List<File> files = fileSet.getFileList();
087
088            if (files.isEmpty()) {
089                files = FileUtils.getFiles(baseDestination, "**", null, false);
090            }
091
092            for (File file : files) {
093
094                String path = file.getPath().replace('\\', '/');
095
096                File repoFile = new File(repoRoot, path);
097
098                file = new File(baseDestination, path);
099
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}