001package org.apache.maven.scm.provider.clearcase.command.changelog;
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 org.apache.maven.scm.ScmBranch;
023import org.apache.maven.scm.ScmException;
024import org.apache.maven.scm.ScmFileSet;
025import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
026import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
027import org.apache.maven.scm.command.changelog.ChangeLogSet;
028import org.apache.maven.scm.provider.ScmProviderRepository;
029import org.apache.maven.scm.provider.clearcase.command.ClearCaseCommand;
030import org.apache.maven.scm.provider.clearcase.util.ClearCaseUtil;
031import org.apache.maven.scm.providers.clearcase.settings.Settings;
032import org.codehaus.plexus.util.StringUtils;
033import org.codehaus.plexus.util.cli.CommandLineException;
034import org.codehaus.plexus.util.cli.CommandLineUtils;
035import org.codehaus.plexus.util.cli.Commandline;
036
037import java.io.File;
038import java.text.SimpleDateFormat;
039import java.util.Date;
040import java.util.Locale;
041
042/**
043 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
044 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
045 * @author <a href="mailto:frederic.mura@laposte.net">Frederic Mura</a>
046 * @author <a href="mailto:m.holster@anva.nl">Mark Holster</a>
047 * @author Olivier Lamy
048 *
049 */
050public class ClearCaseChangeLogCommand
051    extends AbstractChangeLogCommand
052    implements ClearCaseCommand
053{
054    // ----------------------------------------------------------------------
055    // AbstractChangeLogCommand Implementation
056    // ----------------------------------------------------------------------
057
058    /** {@inheritDoc} */
059    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repository, ScmFileSet fileSet,
060                                                          Date startDate, Date endDate, ScmBranch branch,
061                                                          String datePattern )
062        throws ScmException
063    {
064        if ( getLogger().isDebugEnabled() )
065        {
066            getLogger().debug( "executing changelog command..." );
067        }
068        Commandline cl = createCommandLine( fileSet.getBasedir(), branch, startDate );
069
070        ClearCaseChangeLogConsumer consumer = new ClearCaseChangeLogConsumer( getLogger(), datePattern );
071
072        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
073
074        int exitCode;
075
076        try
077        {
078            if ( getLogger().isDebugEnabled() )
079            {
080                getLogger().debug(
081                                   "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>"
082                                       + cl.toString() );
083            }
084            exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
085        }
086        catch ( CommandLineException ex )
087        {
088            throw new ScmException( "Error while executing cvs command.", ex );
089        }
090
091        if ( exitCode != 0 )
092        {
093            return new ChangeLogScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
094        }
095
096        return new ChangeLogScmResult( cl.toString(),
097                                       new ChangeLogSet( consumer.getModifications(), startDate, endDate ) );
098    }
099
100    // ----------------------------------------------------------------------
101    //
102    // ----------------------------------------------------------------------
103
104    /**
105     * ClearCase LT version doesn't support the attribut -fmt and -since for command lhistory.
106     *
107     * @param workingDirectory
108     * @param branch
109     * @param startDate
110     * @return The command line
111     */
112    public static Commandline createCommandLine( File workingDirectory, ScmBranch branch, Date startDate )
113    {
114        Commandline command = new Commandline();
115        command.setExecutable( "cleartool" );
116        command.createArg().setValue( "lshistory" );
117
118        command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
119
120        Settings settings = ClearCaseUtil.getSettings();
121        String userFormat =
122            StringUtils.isEmpty( settings.getChangelogUserFormat() ) ? "" : settings.getChangelogUserFormat();
123
124        StringBuilder format = new StringBuilder();
125        format.append( "NAME:%En\\n" );
126        format.append( "DATE:%Nd\\n" );
127        format.append( "COMM:%-12.12o - %o - %c - Activity: %[activity]p\\n" );
128        format.append( "USER:%" + userFormat + "u\\n" );
129        format.append( "REVI:%Ln\\n" );
130
131        command.createArg().setValue( "-fmt" );
132        command.createArg().setValue( format.toString() );
133        command.createArg().setValue( "-recurse" );
134        command.createArg().setValue( "-nco" );
135
136        if ( startDate != null )
137        {
138            SimpleDateFormat sdf = new SimpleDateFormat( "dd-MMM-yyyy", Locale.ENGLISH );
139
140            String start = sdf.format( startDate );
141
142            command.createArg().setValue( "-since" );
143
144            command.createArg().setValue( start );
145        }
146
147        // TODO: End date?
148
149        if ( branch != null && StringUtils.isNotEmpty( branch.getName() ) )
150        {
151            command.createArg().setValue( "-branch" );
152
153            command.createArg().setValue( branch.getName() );
154        }
155
156        return command;
157    }
158}