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.svn.svnexe.command.list;
020
021import java.io.File;
022import java.util.Iterator;
023
024import org.apache.commons.lang3.StringUtils;
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.ScmFileSet;
027import org.apache.maven.scm.ScmRevision;
028import org.apache.maven.scm.ScmVersion;
029import org.apache.maven.scm.command.list.AbstractListCommand;
030import org.apache.maven.scm.command.list.ListScmResult;
031import org.apache.maven.scm.provider.ScmProviderRepository;
032import org.apache.maven.scm.provider.svn.command.SvnCommand;
033import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
034import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
035import org.codehaus.plexus.util.Os;
036import org.codehaus.plexus.util.cli.CommandLineException;
037import org.codehaus.plexus.util.cli.CommandLineUtils;
038import org.codehaus.plexus.util.cli.Commandline;
039
040/**
041 * Command to list files in SVN ( <code>svn list</code> command )
042 *
043 * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
044 *
045 */
046public class SvnListCommand extends AbstractListCommand implements SvnCommand {
047    private static final File TMP_DIR = new File(System.getProperty("java.io.tmpdir"));
048
049    /** {@inheritDoc} */
050    protected ListScmResult executeListCommand(
051            ScmProviderRepository repository, ScmFileSet fileSet, boolean recursive, ScmVersion version)
052            throws ScmException {
053        Commandline cl = createCommandLine((SvnScmProviderRepository) repository, fileSet, recursive, version);
054
055        SvnListConsumer consumer = new SvnListConsumer();
056
057        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
058
059        if (logger.isInfoEnabled()) {
060            logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
061
062            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
063                logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
064            }
065        }
066
067        int exitCode;
068
069        try {
070            exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
071        } catch (CommandLineException ex) {
072            throw new ScmException("Error while executing command.", ex);
073        }
074
075        if (exitCode != 0) {
076            return new ListScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
077        }
078
079        return new ListScmResult(cl.toString(), consumer.getFiles());
080    }
081
082    static Commandline createCommandLine(
083            SvnScmProviderRepository repository, ScmFileSet fileSet, boolean recursive, ScmVersion version) {
084        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(TMP_DIR, repository);
085
086        cl.createArg().setValue("list");
087
088        if (recursive) {
089            cl.createArg().setValue("--recursive");
090        }
091
092        if (version != null && StringUtils.isNotEmpty(version.getName())) {
093            if (version instanceof ScmRevision) {
094                cl.createArg().setValue("-r");
095
096                cl.createArg().setValue(version.getName());
097            }
098        }
099
100        Iterator<File> it = fileSet.getFileList().iterator();
101
102        while (it.hasNext()) {
103            File file = it.next();
104
105            cl.createArg().setValue(repository.getUrl() + "/" + file.getPath().replace('\\', '/') + "@");
106        }
107
108        return cl;
109    }
110}