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.info;
020
021import java.io.File;
022import java.util.Iterator;
023
024import org.apache.maven.scm.CommandParameters;
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.ScmFileSet;
027import org.apache.maven.scm.ScmResult;
028import org.apache.maven.scm.command.AbstractCommand;
029import org.apache.maven.scm.command.info.InfoScmResult;
030import org.apache.maven.scm.provider.ScmProviderRepository;
031import org.apache.maven.scm.provider.svn.command.SvnCommand;
032import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
033import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
034import org.codehaus.plexus.util.Os;
035import org.codehaus.plexus.util.cli.CommandLineException;
036import org.codehaus.plexus.util.cli.CommandLineUtils;
037import org.codehaus.plexus.util.cli.Commandline;
038
039/**
040 * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
041 *
042 */
043public class SvnInfoCommand extends AbstractCommand implements SvnCommand {
044
045    /** {@inheritDoc} */
046    protected ScmResult executeCommand(
047            ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
048        return executeInfoCommand((SvnScmProviderRepository) repository, fileSet, parameters, false, null);
049    }
050
051    public InfoScmResult executeInfoCommand(
052            SvnScmProviderRepository repository,
053            ScmFileSet fileSet,
054            CommandParameters parameters,
055            boolean recursive,
056            String revision)
057            throws ScmException {
058        Commandline cl = createCommandLine(repository, fileSet, recursive, revision);
059
060        SvnInfoConsumer consumer = new SvnInfoConsumer();
061
062        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
063
064        if (logger.isInfoEnabled()) {
065            logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
066
067            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
068                logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
069            }
070        }
071
072        int exitCode;
073
074        try {
075            exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
076        } catch (CommandLineException ex) {
077            throw new ScmException("Error while executing command.", ex);
078        }
079
080        if (exitCode != 0) {
081            return new InfoScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
082        }
083
084        return new InfoScmResult(cl.toString(), consumer.getInfoItems());
085    }
086
087    // set scope to protected to allow test to call it directly
088    protected static Commandline createCommandLine(
089            SvnScmProviderRepository repository, ScmFileSet fileSet, boolean recursive, String revision) {
090        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(fileSet.getBasedir(), repository);
091
092        cl.createArg().setValue("info");
093
094        if (recursive) {
095            cl.createArg().setValue("--recursive");
096        }
097
098        if (revision != null && !revision.isEmpty()) {
099            cl.createArg().setValue("-r");
100
101            cl.createArg().setValue(revision);
102        }
103
104        Iterator<File> it = fileSet.getFileList().iterator();
105
106        while (it.hasNext()) {
107            File file = (File) it.next();
108
109            if (repository == null) {
110                cl.createArg().setValue(file.getPath());
111            } else {
112                cl.createArg()
113                        .setValue(repository.getUrl() + "/" + file.getPath().replace('\\', '/') + "@");
114            }
115        }
116
117        return cl;
118    }
119}