001package org.apache.maven.scm.provider.local.command.update; 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 org.apache.maven.scm.ScmException; 023import org.apache.maven.scm.ScmFile; 024import org.apache.maven.scm.ScmFileSet; 025import org.apache.maven.scm.ScmFileStatus; 026import org.apache.maven.scm.ScmVersion; 027import org.apache.maven.scm.command.changelog.ChangeLogCommand; 028import org.apache.maven.scm.command.update.AbstractUpdateCommand; 029import org.apache.maven.scm.command.update.UpdateScmResult; 030import org.apache.maven.scm.provider.ScmProviderRepository; 031import org.apache.maven.scm.provider.local.command.LocalCommand; 032import org.apache.maven.scm.provider.local.command.changelog.LocalChangeLogCommand; 033import org.apache.maven.scm.provider.local.metadata.LocalScmMetadata; 034import org.apache.maven.scm.provider.local.metadata.LocalScmMetadataUtils; 035import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository; 036import org.codehaus.plexus.util.FileUtils; 037 038import java.io.File; 039import java.io.IOException; 040import java.util.ArrayList; 041import java.util.Iterator; 042import java.util.List; 043 044/** 045 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a> 046 * @author Olivier Lamy 047 * 048 */ 049public class LocalUpdateCommand 050 extends AbstractUpdateCommand 051 implements LocalCommand 052{ 053 /** {@inheritDoc} */ 054 protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version ) 055 throws ScmException 056 { 057 LocalScmProviderRepository repository = (LocalScmProviderRepository) repo; 058 059 if ( version != null ) 060 { 061 throw new ScmException( "The local scm doesn't support tags." ); 062 } 063 064 File root = new File( repository.getRoot() ); 065 066 String module = repository.getModule(); 067 068 File source = new File( root, module ); 069 070 File baseDestination = fileSet.getBasedir(); 071 072 if ( !baseDestination.exists() ) 073 { 074 throw new ScmException( 075 "The working directory doesn't exist (" + baseDestination.getAbsolutePath() + ")." ); 076 } 077 078 if ( !root.exists() ) 079 { 080 throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." ); 081 } 082 083 if ( !source.exists() ) 084 { 085 throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." ); 086 } 087 088 if ( !baseDestination.exists() && !baseDestination.isDirectory() ) 089 { 090 throw new ScmException( "The destination directory isn't a directory or doesn't exist (" 091 + baseDestination.getAbsolutePath() + ")." ); 092 } 093 094 List<ScmFile> updatedFiles; 095 096 try 097 { 098 if ( getLogger().isInfoEnabled() ) 099 { 100 getLogger().info( 101 "Updating '" + baseDestination.getAbsolutePath() + "' from '" 102 + source.getAbsolutePath() + "'." ); 103 } 104 105 @SuppressWarnings( "unchecked" ) 106 List<File> fileList = FileUtils.getFiles( source.getAbsoluteFile(), "**", null ); 107 updatedFiles = update( source, baseDestination, fileList ); 108 109 // process deletions in repository 110 LocalScmMetadataUtils metadataUtils = new LocalScmMetadataUtils( getLogger() ); 111 LocalScmMetadata originalMetadata = metadataUtils.readMetadata( baseDestination ); 112 if ( originalMetadata != null ) 113 { 114 LocalScmMetadata newMetadata = metadataUtils.buildMetadata( source ); 115 for ( Iterator<String> it = originalMetadata.getRepositoryFileNames().iterator(); it.hasNext(); ) 116 { 117 String filename = it.next(); 118 if ( !newMetadata.getRepositoryFileNames().contains( filename ) ) 119 { 120 File localFile = new File( baseDestination, filename ); 121 if ( localFile.exists() ) 122 { 123 localFile.delete(); 124 updatedFiles.add( new ScmFile( "/" + filename, ScmFileStatus.UPDATED ) ); 125 } 126 } 127 } 128 } 129 130 // rewrite metadata file 131 metadataUtils.writeMetadata( baseDestination, metadataUtils.buildMetadata( source ) ); 132 133 } 134 catch ( IOException ex ) 135 { 136 throw new ScmException( "Error while checking out the files.", ex ); 137 } 138 139 return new LocalUpdateScmResult( null, updatedFiles ); 140 } 141 142 private List<ScmFile> update( File source, File baseDestination, List<File> files ) 143 throws ScmException, IOException 144 { 145 String sourcePath = source.getAbsolutePath(); 146 147 List<ScmFile> updatedFiles = new ArrayList<ScmFile>(); 148 149 for ( Iterator<File> i = files.iterator(); i.hasNext(); ) 150 { 151 File repositoryFile = i.next(); 152 153 File repositoryDirectory = repositoryFile.getParentFile(); 154 155 // TODO: Add more excludes here 156 if ( repositoryDirectory != null && repositoryDirectory.getName().equals( "CVS" ) ) 157 { 158 continue; 159 } 160 161 String dest = repositoryFile.getAbsolutePath().substring( sourcePath.length() + 1 ); 162 163 File destinationFile = new File( baseDestination, dest ); 164 165 String repositoryFileContents = FileUtils.fileRead( repositoryFile ); 166 167 if ( destinationFile.exists() ) 168 { 169 String destionationFileContents = FileUtils.fileRead( destinationFile ); 170 171 if ( repositoryFileContents.equals( destionationFileContents ) ) 172 { 173 continue; 174 } 175 } 176 177 File destinationDirectory = destinationFile.getParentFile(); 178 179 if ( !destinationDirectory.exists() && !destinationDirectory.mkdirs() ) 180 { 181 throw new ScmException( 182 "Could not create destination directory '" + destinationDirectory.getAbsolutePath() + "'." ); 183 } 184 185 ScmFileStatus status; 186 187 if ( destinationFile.exists() ) 188 { 189 status = ScmFileStatus.UPDATED; 190 } 191 else 192 { 193 status = ScmFileStatus.ADDED; 194 } 195 196 FileUtils.copyFileToDirectory( repositoryFile, destinationDirectory ); 197 198 int chop = baseDestination.getAbsolutePath().length(); 199 200 String fileName = "/" + destinationFile.getAbsolutePath().substring( chop + 1 ); 201 202 updatedFiles.add( new ScmFile( fileName, status ) ); 203 } 204 205 return updatedFiles; 206 } 207 208 /** {@inheritDoc} */ 209 protected ChangeLogCommand getChangeLogCommand() 210 { 211 return new LocalChangeLogCommand(); 212 } 213}