001 package org.apache.maven.scm.provider.clearcase.command.tag;
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
022 import java.io.File;
023 import java.util.List;
024
025 import org.apache.maven.scm.ScmException;
026 import org.apache.maven.scm.ScmFileSet;
027 import org.apache.maven.scm.ScmResult;
028 import org.apache.maven.scm.ScmTagParameters;
029 import org.apache.maven.scm.command.tag.AbstractTagCommand;
030 import org.apache.maven.scm.command.tag.TagScmResult;
031 import org.apache.maven.scm.provider.ScmProviderRepository;
032 import org.apache.maven.scm.provider.clearcase.command.ClearCaseCommand;
033 import org.apache.maven.scm.provider.clearcase.command.checkin.ClearCaseCheckInConsumer;
034 import org.codehaus.plexus.util.cli.CommandLineException;
035 import org.codehaus.plexus.util.cli.CommandLineUtils;
036 import org.codehaus.plexus.util.cli.Commandline;
037
038 /**
039 * @author <a href="mailto:wim.deblauwe@gmail.com">Wim Deblauwe</a>
040 * @author Olivier Lamy
041 *
042 */
043 public class ClearCaseTagCommand
044 extends AbstractTagCommand
045 implements ClearCaseCommand
046 {
047
048 protected ScmResult executeTagCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String tag,
049 String message )
050 throws ScmException
051 {
052 return executeTagCommand( scmProviderRepository, fileSet, tag, new ScmTagParameters( message ) );
053 }
054
055 /** {@inheritDoc} */
056 protected ScmResult executeTagCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String tag,
057 ScmTagParameters scmTagParameters )
058 throws ScmException
059 {
060 if ( getLogger().isDebugEnabled() )
061 {
062 getLogger().debug( "executing tag command..." );
063 }
064 Commandline cl = createCommandLine( fileSet, tag );
065
066 ClearCaseCheckInConsumer consumer = new ClearCaseCheckInConsumer( getLogger() );
067
068 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
069
070 int exitCode;
071
072 try
073 {
074 if ( getLogger().isDebugEnabled() )
075 {
076 getLogger().debug( "Creating label: " + tag );
077 }
078 Commandline newLabelCommandLine = createNewLabelCommandLine( fileSet, tag );
079 if ( getLogger().isDebugEnabled() )
080 {
081 getLogger().debug(
082 "Executing: " + newLabelCommandLine.getWorkingDirectory().getAbsolutePath()
083 + ">>" + newLabelCommandLine.toString() );
084 }
085 exitCode = CommandLineUtils.executeCommandLine( newLabelCommandLine,
086 new CommandLineUtils.StringStreamConsumer(), stderr );
087
088 if ( exitCode == 0 )
089 {
090 getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
091 exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
092 }
093 }
094 catch ( CommandLineException ex )
095 {
096 throw new ScmException( "Error while executing clearcase command.", ex );
097 }
098
099 if ( exitCode != 0 )
100 {
101 return new TagScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
102 }
103
104 return new TagScmResult( cl.toString(), consumer.getCheckedInFiles() );
105 }
106
107 // ----------------------------------------------------------------------
108 //
109 // ----------------------------------------------------------------------
110
111 public static Commandline createCommandLine( ScmFileSet scmFileSet, String tag )
112 {
113 Commandline command = new Commandline();
114
115 File workingDirectory = scmFileSet.getBasedir();
116
117 command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
118
119 command.setExecutable( "cleartool" );
120
121 command.createArg().setValue( "mklabel" );
122 List<File> files = scmFileSet.getFileList();
123 if ( files.isEmpty() )
124 {
125 command.createArg().setValue( "-recurse" );
126 }
127 command.createArg().setValue( tag );
128
129 if ( files.size() > 0 )
130 {
131 for ( File file : files )
132 {
133 command.createArg().setValue( file.getName() );
134 }
135 }
136 else
137 {
138 command.createArg().setValue( "." );
139 }
140
141 return command;
142 }
143
144 private static Commandline createNewLabelCommandLine( ScmFileSet scmFileSet, String tag )
145 {
146 Commandline command = new Commandline();
147
148 File workingDirectory = scmFileSet.getBasedir();
149
150 command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
151
152 command.setExecutable( "cleartool" );
153
154 command.createArg().setValue( "mklbtype" );
155 command.createArg().setValue( "-nc" );
156 command.createArg().setValue( tag );
157
158 return command;
159 }
160 }