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.plugin;
020
021import javax.inject.Inject;
022
023import java.io.File;
024import java.io.IOException;
025
026import org.apache.maven.plugin.MojoExecutionException;
027import org.apache.maven.plugins.annotations.Mojo;
028import org.apache.maven.plugins.annotations.Parameter;
029import org.apache.maven.scm.ScmException;
030import org.apache.maven.scm.command.diff.DiffScmResult;
031import org.apache.maven.scm.manager.ScmManager;
032import org.apache.maven.scm.repository.ScmRepository;
033import org.apache.maven.settings.crypto.SettingsDecrypter;
034import org.codehaus.plexus.util.FileUtils;
035
036/**
037 * Display the difference of the working copy with the latest copy in the configured scm url.
038 *
039 * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
040 */
041@Mojo(name = "diff", aggregator = true)
042public class DiffMojo extends AbstractScmMojo {
043    /**
044     * The version type (branch/tag/revision) of scmVersion.
045     */
046    @Parameter(property = "startScmVersionType")
047    private String startScmVersionType;
048
049    /**
050     * The version (revision number/branch name/tag name).
051     */
052    @Parameter(property = "startScmVersion")
053    private String startScmVersion;
054
055    /**
056     * The version type (branch/tag/revision) of scmVersion.
057     */
058    @Parameter(property = "endScmVersionType")
059    private String endScmVersionType;
060
061    /**
062     * The version (revision number/branch name/tag name).
063     */
064    @Parameter(property = "endScmVersion")
065    private String endScmVersion;
066
067    /**
068     * Output file name.
069     */
070    @Parameter(property = "outputFile", defaultValue = "${project.artifactId}.diff")
071    private File outputFile;
072
073    @Inject
074    public DiffMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
075        super(manager, settingsDecrypter);
076    }
077
078    /** {@inheritDoc} */
079    public void execute() throws MojoExecutionException {
080        super.execute();
081
082        try {
083            ScmRepository repository = getScmRepository();
084
085            DiffScmResult result = getScmManager()
086                    .diff(
087                            repository,
088                            getFileSet(),
089                            getScmVersion(startScmVersionType, startScmVersion),
090                            getScmVersion(endScmVersionType, endScmVersion));
091
092            checkResult(result);
093
094            getLog().info(result.getPatch());
095
096            try {
097                if (outputFile != null) {
098                    FileUtils.fileWrite(outputFile.getAbsolutePath(), result.getPatch());
099                }
100            } catch (IOException e) {
101                throw new MojoExecutionException("Can't write patch file.", e);
102            }
103        } catch (IOException | ScmException e) {
104            throw new MojoExecutionException("Cannot run diff command : ", e);
105        }
106    }
107}