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.checkin;
020
021import java.io.File;
022import java.io.IOException;
023
024import org.apache.commons.lang3.StringUtils;
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.ScmFileSet;
027import org.apache.maven.scm.ScmVersion;
028import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
029import org.apache.maven.scm.command.checkin.CheckInScmResult;
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.FileUtils;
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 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
042 * @author Olivier Lamy
043 *
044 */
045public class SvnCheckInCommand extends AbstractCheckInCommand implements SvnCommand {
046    /** {@inheritDoc} */
047    protected CheckInScmResult executeCheckInCommand(
048            ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion version) throws ScmException {
049        if (version != null && StringUtils.isNotEmpty(version.getName())) {
050            throw new ScmException("This provider command can't handle tags.");
051        }
052
053        File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
054
055        try {
056            FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8", message);
057        } catch (IOException ex) {
058            return new CheckInScmResult(
059                    null,
060                    "Error while making a temporary file for the commit message: " + ex.getMessage(),
061                    null,
062                    false);
063        }
064
065        Commandline cl = createCommandLine((SvnScmProviderRepository) repo, fileSet, messageFile);
066
067        SvnCheckInConsumer consumer = new SvnCheckInConsumer(fileSet.getBasedir());
068
069        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
070
071        if (logger.isInfoEnabled()) {
072            logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
073
074            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
075                logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
076            }
077        }
078
079        int exitCode;
080
081        try {
082            exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
083        } catch (CommandLineException ex) {
084            throw new ScmException("Error while executing command.", ex);
085        } finally {
086            try {
087                FileUtils.forceDelete(messageFile);
088            } catch (IOException ex) {
089                // ignore
090            }
091        }
092
093        if (exitCode != 0) {
094            return new CheckInScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
095        }
096
097        return new CheckInScmResult(
098                cl.toString(), consumer.getCheckedInFiles(), Integer.toString(consumer.getRevision()));
099    }
100
101    // ----------------------------------------------------------------------
102    //
103    // ----------------------------------------------------------------------
104
105    public static Commandline createCommandLine(
106            SvnScmProviderRepository repository, ScmFileSet fileSet, File messageFile) throws ScmException {
107        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(fileSet.getBasedir(), repository);
108
109        cl.createArg().setValue("commit");
110
111        cl.createArg().setValue("--file");
112
113        cl.createArg().setValue(messageFile.getAbsolutePath());
114
115        cl.createArg().setValue("--encoding");
116
117        cl.createArg().setValue("UTF-8");
118
119        try {
120            SvnCommandLineUtils.addTarget(cl, fileSet.getFileList());
121        } catch (IOException e) {
122            throw new ScmException("Can't create the targets file", e);
123        }
124
125        return cl;
126    }
127}