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.provider.local.command.list; 020 021import java.io.File; 022import java.util.ArrayList; 023import java.util.List; 024 025import org.apache.commons.lang3.StringUtils; 026import org.apache.maven.scm.ScmException; 027import org.apache.maven.scm.ScmFile; 028import org.apache.maven.scm.ScmFileSet; 029import org.apache.maven.scm.ScmFileStatus; 030import org.apache.maven.scm.ScmVersion; 031import org.apache.maven.scm.command.list.AbstractListCommand; 032import org.apache.maven.scm.command.list.ListScmResult; 033import org.apache.maven.scm.provider.ScmProviderRepository; 034import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository; 035 036/** 037 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a> 038 * 039 */ 040public class LocalListCommand extends AbstractListCommand { 041 /** {@inheritDoc} */ 042 protected ListScmResult executeListCommand( 043 ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive, ScmVersion version) throws ScmException { 044 if (version != null) { 045 throw new ScmException("The local scm doesn't support tags."); 046 } 047 048 LocalScmProviderRepository repository = (LocalScmProviderRepository) repo; 049 050 File root = new File(repository.getRoot()); 051 052 String module = repository.getModule(); 053 054 File source = new File(root, module); 055 056 if (!root.exists()) { 057 throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ")."); 058 } 059 060 if (!source.exists()) { 061 throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ")."); 062 } 063 064 if (logger.isInfoEnabled()) { 065 logger.info("Listing files of '" + source.getAbsolutePath() + "'."); 066 } 067 068 try { 069 if (fileSet.getFileList() == null || fileSet.getFileList().isEmpty()) { 070 return new LocalListScmResult(null, getFiles(source, source, recursive)); 071 } else { 072 List<ScmFile> files = new ArrayList<>(); 073 074 for (File file : fileSet.getFileList()) { 075 files.addAll(getFiles(source, new File(source, file.getPath()), recursive)); 076 } 077 078 return new LocalListScmResult(null, files); 079 } 080 } catch (Exception e) { 081 return new ListScmResult(null, "The svn command failed.", e.getMessage(), false); 082 } 083 } 084 085 private List<ScmFile> getFiles(File source, File directory, boolean recursive) throws Exception { 086 if (!directory.exists()) { 087 throw new Exception("Directory '" + directory.getAbsolutePath() + "' doesn't exist."); 088 } 089 090 List<ScmFile> files = new ArrayList<>(); 091 092 File[] filesArray = directory.listFiles(); 093 094 if (filesArray != null) { 095 for (int i = 0; i < filesArray.length; i++) { 096 File f = filesArray[i]; 097 098 String path = 099 f.getAbsolutePath().substring(source.getAbsolutePath().length()); 100 path = StringUtils.replace(path, "\\", "/"); 101 path = StringUtils.replace(path, "/./", "/"); 102 103 files.add(new ScmFile(path, ScmFileStatus.CHECKED_IN)); 104 105 if (f.isDirectory() && recursive) { 106 files.addAll(getFiles(source, f, recursive)); 107 } 108 } 109 } 110 111 return files; 112 } 113}