001package org.apache.maven.scm.provider.clearcase.command.unedit;
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 java.io.File;
023import java.util.List;
024
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.ScmFileSet;
027import org.apache.maven.scm.ScmResult;
028import org.apache.maven.scm.command.status.StatusScmResult;
029import org.apache.maven.scm.command.unedit.AbstractUnEditCommand;
030import org.apache.maven.scm.log.ScmLogger;
031import org.apache.maven.scm.provider.ScmProviderRepository;
032import org.apache.maven.scm.provider.clearcase.command.ClearCaseCommand;
033import org.codehaus.plexus.util.cli.CommandLineException;
034import org.codehaus.plexus.util.cli.CommandLineUtils;
035import org.codehaus.plexus.util.cli.Commandline;
036
037/**
038 * @author <a href="mailto:wim.deblauwe@gmail.com">Wim Deblauwe</a>
039 * @author Olivier Lamy
040 *
041 */
042public class ClearCaseUnEditCommand
043    extends AbstractUnEditCommand
044    implements ClearCaseCommand
045{
046    /** {@inheritDoc} */
047    protected ScmResult executeUnEditCommand( ScmProviderRepository repository, ScmFileSet fileSet )
048        throws ScmException
049    {
050        if ( getLogger().isDebugEnabled() )
051        {
052            getLogger().debug( "executing unedit command..." );
053        }
054        Commandline cl = createCommandLine( getLogger(), fileSet );
055
056        ClearCaseUnEditConsumer consumer = new ClearCaseUnEditConsumer( getLogger() );
057
058        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
059
060        int exitCode;
061
062        try
063        {
064            if ( getLogger().isDebugEnabled() )
065            {
066                getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
067            }
068            exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
069        }
070        catch ( CommandLineException ex )
071        {
072            throw new ScmException( "Error while executing clearcase command.", ex );
073        }
074
075        if ( exitCode != 0 )
076        {
077            return new StatusScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
078        }
079
080        return new StatusScmResult( cl.toString(), consumer.getUnEditFiles() );
081    }
082
083    // ----------------------------------------------------------------------
084    //
085    // ----------------------------------------------------------------------
086
087    public static Commandline createCommandLine( ScmLogger logger, ScmFileSet scmFileSet )
088    {
089        Commandline command = new Commandline();
090
091        File workingDirectory = scmFileSet.getBasedir();
092
093        command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
094
095        command.setExecutable( "cleartool" );
096
097        command.createArg().setValue( "unco" );
098        command.createArg().setValue( "-keep" );
099
100        List<File> files = scmFileSet.getFileList();
101        for ( File file : files )
102        {
103            command.createArg().setValue( file.getName() );
104        }
105
106        return command;
107    }
108}