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