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    /**
051     * {@inheritDoc}
052     */
053    public void execute() throws MojoExecutionException {
054        super.execute();
055
056        try {
057            ScmRepository repository = getScmRepository();
058
059            StatusScmResult result = getScmManager().status(repository, getFileSet());
060
061            checkResult(result);
062
063            File baseDir = getFileSet().getBasedir();
064
065            // Determine the maximum length of the status column
066            int maxLen = 0;
067
068            for (ScmFile file : result.getChangedFiles()) {
069                maxLen = Math.max(maxLen, file.getStatus().toString().length());
070            }
071
072            for (ScmFile file : result.getChangedFiles()) {
073                // right align all of the statuses
074                getLog().info(StringUtils.leftPad(file.getStatus().toString(), maxLen) + " status for "
075                        + getRelativePath(baseDir, file.getPath()));
076            }
077        } catch (IOException | ScmException e) {
078            throw new MojoExecutionException("Cannot run status command : ", e);
079        }
080    }
081
082    /**
083     * Formats the filename so that it is a relative directory from the base.
084     *
085     * @param baseDir
086     * @param path
087     * @return the relative path
088     */
089    protected String getRelativePath(File baseDir, String path) {
090        if (path.equals(baseDir.getAbsolutePath())) {
091            return ".";
092        } else if (path.indexOf(baseDir.getAbsolutePath()) == 0) {
093            // the + 1 gets rid of a leading file separator
094            return path.substring(baseDir.getAbsolutePath().length() + 1);
095        } else {
096            return path;
097        }
098    }
099}