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