Coverage Report - org.apache.maven.shared.release.phase.ScmCheckModificationsPhase
 
Classes in this File Line Coverage Branch Coverage Complexity
ScmCheckModificationsPhase
97%
44/45
90%
18/20
10
 
 1  
 package org.apache.maven.shared.release.phase;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.maven.project.MavenProject;
 23  
 import org.apache.maven.scm.ScmException;
 24  
 import org.apache.maven.scm.ScmFile;
 25  
 import org.apache.maven.scm.ScmFileSet;
 26  
 import org.apache.maven.scm.command.status.StatusScmResult;
 27  
 import org.apache.maven.scm.manager.NoSuchScmProviderException;
 28  
 import org.apache.maven.scm.provider.ScmProvider;
 29  
 import org.apache.maven.scm.repository.ScmRepository;
 30  
 import org.apache.maven.scm.repository.ScmRepositoryException;
 31  
 import org.apache.maven.shared.release.ReleaseExecutionException;
 32  
 import org.apache.maven.shared.release.ReleaseFailureException;
 33  
 import org.apache.maven.shared.release.ReleaseResult;
 34  
 import org.apache.maven.shared.release.config.ReleaseDescriptor;
 35  
 import org.apache.maven.shared.release.env.ReleaseEnvironment;
 36  
 import org.apache.maven.shared.release.scm.ReleaseScmCommandException;
 37  
 import org.apache.maven.shared.release.scm.ReleaseScmRepositoryException;
 38  
 import org.apache.maven.shared.release.scm.ScmRepositoryConfigurator;
 39  
 import org.apache.maven.shared.release.scm.ScmTranslator;
 40  
 import org.codehaus.plexus.util.SelectorUtils;
 41  
 import org.codehaus.plexus.util.StringUtils;
 42  
 
 43  
 import java.io.File;
 44  
 import java.util.Arrays;
 45  
 import java.util.HashSet;
 46  
 import java.util.Iterator;
 47  
 import java.util.List;
 48  
 import java.util.Map;
 49  
 import java.util.Set;
 50  
 
 51  
 /**
 52  
  * See if there are any local modifications to the files before proceeding with SCM operations and the release.
 53  
  * 
 54  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 55  
  * @plexus.component role="org.apache.maven.shared.release.phase.ReleasePhase" role-hint="scm-check-modifications"
 56  
  */
 57  80
 public class ScmCheckModificationsPhase
 58  
     extends AbstractReleasePhase
 59  
 {
 60  
     /**
 61  
      * Tool that gets a configured SCM repository from release configuration.
 62  
      * 
 63  
      * @plexus.requirement
 64  
      */
 65  
     private ScmRepositoryConfigurator scmRepositoryConfigurator;
 66  
     
 67  
     /**
 68  
      * SCM URL translators mapped by provider name.
 69  
      *
 70  
      * @plexus.requirement role="org.apache.maven.shared.release.scm.ScmTranslator"
 71  
      */
 72  
     private Map<String, ScmTranslator> scmTranslators;
 73  
 
 74  
     /**
 75  
      * The filepatterns to exclude from the status check.
 76  
      * 
 77  
      * @todo proper construction of filenames, especially release properties
 78  
      */
 79  80
     private Set<String> exclusionPatterns = new HashSet<String>( Arrays.asList( new String[] {
 80  
         "**" + File.separator + "pom.xml.backup", "**" + File.separator + "pom.xml.tag",
 81  
         "**" + File.separator + "pom.xml.next", "**" + File.separator + "pom.xml.branch",
 82  
         "**" + File.separator + "release.properties", "**" + File.separator + "pom.xml.releaseBackup" } ) );
 83  
 
 84  
     public ReleaseResult execute( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
 85  
                                   List<MavenProject> reactorProjects )
 86  
         throws ReleaseExecutionException, ReleaseFailureException
 87  
     {
 88  44
         ReleaseResult relResult = new ReleaseResult();
 89  
 
 90  44
         List<String> additionalExcludes = releaseDescriptor.getCheckModificationExcludes();
 91  
 
 92  44
         if ( additionalExcludes != null )
 93  
         {
 94  
             // SelectorUtils expects OS-specific paths and patterns
 95  44
             for ( String additionalExclude : additionalExcludes )
 96  
             {
 97  8
                 exclusionPatterns.add( additionalExclude.replace( "\\", File.separator ).replace( "/", File.separator ) );
 98  
             }
 99  
         }
 100  
 
 101  44
         logInfo( relResult, "Verifying that there are no local modifications..." );
 102  44
         logInfo( relResult, "  ignoring changes on: " + StringUtils.join( exclusionPatterns.toArray(), ", " ) );
 103  
 
 104  
         ScmRepository repository;
 105  
         ScmProvider provider;
 106  
         try
 107  
         {
 108  44
             repository =
 109  
                 scmRepositoryConfigurator.getConfiguredRepository( releaseDescriptor, releaseEnvironment.getSettings() );
 110  
 
 111  36
             provider = scmRepositoryConfigurator.getRepositoryProvider( repository );
 112  
         }
 113  4
         catch ( ScmRepositoryException e )
 114  
         {
 115  4
             throw new ReleaseScmRepositoryException( e.getMessage() + " for URL: "
 116  
                 + releaseDescriptor.getScmSourceUrl(), e.getValidationMessages() );
 117  
         }
 118  4
         catch ( NoSuchScmProviderException e )
 119  
         {
 120  4
             throw new ReleaseExecutionException( "Unable to configure SCM repository: " + e.getMessage(), e );
 121  36
         }
 122  
 
 123  
         StatusScmResult result;
 124  
         try
 125  
         {
 126  36
             result =
 127  
                 provider.status( repository, new ScmFileSet( new File( releaseDescriptor.getWorkingDirectory() ) ) );
 128  
         }
 129  4
         catch ( ScmException e )
 130  
         {
 131  4
             throw new ReleaseExecutionException( "An error occurred during the status check process: " + e.getMessage(),
 132  
                                                  e );
 133  32
         }
 134  
 
 135  32
         if ( !result.isSuccess() )
 136  
         {
 137  4
             throw new ReleaseScmCommandException( "Unable to check for local modifications", result );
 138  
         }
 139  
 
 140  28
         List<ScmFile> changedFiles = result.getChangedFiles();
 141  
 
 142  28
         if ( !changedFiles.isEmpty() )
 143  
         {
 144  18
             ScmTranslator scmTranslator = scmTranslators.get( repository.getProvider() );
 145  
             
 146  
             // TODO: would be nice for SCM status command to do this for me.
 147  18
             for ( Iterator<ScmFile> i = changedFiles.iterator(); i.hasNext(); )
 148  
             {
 149  50
                 ScmFile f = i.next();
 150  
 
 151  
                 String path;
 152  50
                 if ( scmTranslator != null )
 153  
                 {
 154  0
                     path = scmTranslator.toRelativePath( f.getPath() );
 155  
                 }
 156  
                 else
 157  
                 {
 158  50
                     path = f.getPath();
 159  
                 }
 160  
 
 161  
                 // SelectorUtils expects File.separator, don't standardize!
 162  50
                 String fileName = path.replace( "\\", File.separator ).replace( "/", File.separator );
 163  
 
 164  50
                 for ( String exclusionPattern : exclusionPatterns )
 165  
                 {
 166  316
                     if ( SelectorUtils.matchPath( exclusionPattern, fileName ) )
 167  
                     {
 168  30
                         logDebug( relResult, "Ignoring changed file: " + fileName );
 169  30
                         i.remove();
 170  
                     }
 171  
                 }
 172  50
             }
 173  
         }
 174  
 
 175  28
         if ( !changedFiles.isEmpty() )
 176  
         {
 177  12
             StringBuilder message = new StringBuilder();
 178  
 
 179  12
             for ( ScmFile file : changedFiles )
 180  
             {
 181  20
                 message.append( file.toString() );
 182  20
                 message.append( "\n" );
 183  
             }
 184  
 
 185  12
             throw new ReleaseFailureException( "Cannot prepare the release because you have local modifications : \n"
 186  
                 + message );
 187  
         }
 188  
 
 189  16
         relResult.setResultCode( ReleaseResult.SUCCESS );
 190  
 
 191  16
         return relResult;
 192  
     }
 193  
 
 194  
     public ReleaseResult simulate( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
 195  
                                    List<MavenProject> reactorProjects )
 196  
         throws ReleaseExecutionException, ReleaseFailureException
 197  
     {
 198  
         // It makes no modifications, so simulate is the same as execute
 199  22
         return execute( releaseDescriptor, releaseEnvironment, reactorProjects );
 200  
     }
 201  
 }