001package org.apache.maven.scm.plugin;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.io.IOException;
024
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;
031import org.codehaus.plexus.util.StringUtils;
032
033/**
034 * Display the modification status of the files in the configured scm url.
035 *
036 * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
037 * @author Olivier Lamy
038 */
039@Mojo( name = "status", aggregator = true )
040public class StatusMojo
041    extends AbstractScmMojo
042{
043    /** {@inheritDoc} */
044    public void execute()
045        throws MojoExecutionException
046    {
047        super.execute();
048
049        try
050        {
051            ScmRepository repository = getScmRepository();
052
053            StatusScmResult result = getScmManager().status( repository, getFileSet() );
054
055            checkResult( result );
056
057            File baseDir = getFileSet().getBasedir();
058
059            // Determine the maximum length of the status column
060            int maxLen = 0;
061
062            for ( ScmFile file : result.getChangedFiles() )
063            {
064                maxLen = Math.max( maxLen, file.getStatus().toString().length() );
065            }
066
067            for ( ScmFile file : result.getChangedFiles() )
068            {
069                // right align all of the statuses
070                getLog().info(
071                               StringUtils.leftPad( file.getStatus().toString(), maxLen ) + " status for "
072                                   + getRelativePath( baseDir, file.getPath() ) );
073            }
074        }
075        catch ( IOException e )
076        {
077            throw new MojoExecutionException( "Cannot run status command : ", e );
078        }
079        catch ( ScmException e )
080        {
081            throw new MojoExecutionException( "Cannot run status command : ", e );
082        }
083    }
084
085    /**
086     * Formats the filename so that it is a relative directory from the base.
087     *
088     * @param baseDir
089     * @param path
090     * @return The relative path
091     */
092    protected String getRelativePath( File baseDir, String path )
093    {
094        if ( path.equals( baseDir.getAbsolutePath() ) )
095        {
096            return ".";
097        }
098        else if ( path.indexOf( baseDir.getAbsolutePath() ) == 0 )
099        {
100            // the + 1 gets rid of a leading file separator
101            return path.substring( baseDir.getAbsolutePath().length() + 1 );
102        }
103        else
104        {
105            return path;
106        }
107    }
108}