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.commons.lang3.StringUtils;
027import org.apache.maven.plugin.MojoExecutionException;
028import org.apache.maven.plugins.annotations.Mojo;
029import org.apache.maven.scm.ScmException;
030import org.apache.maven.scm.ScmFile;
031import org.apache.maven.scm.command.status.StatusScmResult;
032import org.apache.maven.scm.manager.ScmManager;
033import org.apache.maven.scm.repository.ScmRepository;
034import org.apache.maven.settings.crypto.SettingsDecrypter;
035
036/**
037 * Display the modification status of the files in the configured scm url.
038 *
039 * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
040 * @author Olivier Lamy
041 */
042@Mojo(name = "status", aggregator = true)
043public class StatusMojo extends AbstractScmMojo {
044
045    @Inject
046    public StatusMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
047        super(manager, settingsDecrypter);
048    }
049
050    /** {@inheritDoc} */
051    public void execute() throws MojoExecutionException {
052        super.execute();
053
054        try {
055            ScmRepository repository = getScmRepository();
056
057            StatusScmResult result = getScmManager().status(repository, getFileSet());
058
059            checkResult(result);
060
061            File baseDir = getFileSet().getBasedir();
062
063            // Determine the maximum length of the status column
064            int maxLen = 0;
065
066            for (ScmFile file : result.getChangedFiles()) {
067                maxLen = Math.max(maxLen, file.getStatus().toString().length());
068            }
069
070            for (ScmFile file : result.getChangedFiles()) {
071                // right align all of the statuses
072                getLog().info(StringUtils.leftPad(file.getStatus().toString(), maxLen) + " status for "
073                        + getRelativePath(baseDir, file.getPath()));
074            }
075        } catch (IOException | ScmException e) {
076            throw new MojoExecutionException("Cannot run status command : ", e);
077        }
078    }
079
080    /**
081     * Formats the filename so that it is a relative directory from the base.
082     *
083     * @param baseDir
084     * @param path
085     * @return The relative path
086     */
087    protected String getRelativePath(File baseDir, String path) {
088        if (path.equals(baseDir.getAbsolutePath())) {
089            return ".";
090        } else if (path.indexOf(baseDir.getAbsolutePath()) == 0) {
091            // the + 1 gets rid of a leading file separator
092            return path.substring(baseDir.getAbsolutePath().length() + 1);
093        } else {
094            return path;
095        }
096    }
097}