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 *
043 */
044public class LocalChangeLogCommand extends AbstractChangeLogCommand {
045    /** {@inheritDoc} */
046    protected ChangeLogScmResult executeChangeLogCommand(
047            ScmProviderRepository repository,
048            ScmFileSet fileSet,
049            Date startDate,
050            Date endDate,
051            ScmBranch branch,
052            String datePattern)
053            throws ScmException {
054        LocalScmProviderRepository repo = (LocalScmProviderRepository) repository;
055
056        if (branch != null) {
057            throw new ScmException("The local scm doesn't support tags.");
058        }
059
060        File root = new File(repo.getRoot());
061
062        String module = repo.getModule();
063
064        File source = new File(root, module);
065
066        File baseDestination = fileSet.getBasedir();
067
068        if (!baseDestination.exists()) {
069            throw new ScmException("The working directory doesn't exist (" + baseDestination.getAbsolutePath() + ").");
070        }
071
072        if (!root.exists()) {
073            throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
074        }
075
076        if (!source.exists()) {
077            throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
078        }
079
080        List<ChangeSet> changeLogList = new ArrayList<>();
081
082        try {
083            File repoRoot = new File(repo.getRoot(), repo.getModule());
084
085            List<File> files = fileSet.getFileList();
086
087            if (files.isEmpty()) {
088                files = FileUtils.getFiles(baseDestination, "**", null, false);
089            }
090
091            for (File file : files) {
092
093                String path = file.getPath().replace('\\', '/');
094
095                File repoFile = new File(repoRoot, path);
096
097                file = new File(baseDestination, path);
098
099                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}