001package org.apache.maven.scm.provider.jazz.command.add; 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.ScmException; 023import org.apache.maven.scm.ScmFile; 024import org.apache.maven.scm.ScmFileSet; 025import org.apache.maven.scm.ScmFileStatus; 026import org.apache.maven.scm.ScmResult; 027import org.apache.maven.scm.command.add.AbstractAddCommand; 028import org.apache.maven.scm.command.add.AddScmResult; 029import org.apache.maven.scm.command.status.StatusScmResult; 030import org.apache.maven.scm.provider.ScmProviderRepository; 031import org.apache.maven.scm.provider.jazz.command.JazzConstants; 032import org.apache.maven.scm.provider.jazz.command.JazzScmCommand; 033import org.apache.maven.scm.provider.jazz.command.consumer.ErrorConsumer; 034import org.apache.maven.scm.provider.jazz.command.status.JazzStatusCommand; 035 036import java.io.File; 037import java.util.ArrayList; 038import java.util.List; 039 040// RTC does not have the equivalent of the "add" goal. The closest we have is the "share" command, however 041// that only shares directories (not individual or specific files), and it is recursive (which can not be 042// switched off!). 043// 044// The Maven SCM plugin "add" goal's job is to add files to source control, but not commit them. 045// The SVN equivalent of this is "svn add", which places a file under source control (Working Copy in svn terms) 046// but does not commit them (svn commit). 047// So, this provider will use the RTC "checkin" command for the implementation of the "add" goal. 048// This will checkin the code into a remote repository workspace. It will not deliver. 049// 050// Additionally, "svn add" does not take a message, whereas commit does. Under RTC, the only way we can preserve 051// the message, is to create a changeset. So we do that in the "checkin" goal, not the "add" goal. 052// 053// The Maven SCM plugin "add" goal is roughly equivalent to the RTC "checkin" command. 054// 055// See the following links for additional information on the RTC "checkin" command. 056// RTC 2.0.0.2: 057// http://publib.boulder.ibm.com/infocenter/rtc/v2r0m0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_checkin.html 058// RTC 3.0: 059// http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_checkin.html 060// RTC 3.0.1: 061// http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0m1/topic/com.ibm.team.scm.doc/topics/r_scm_cli_checkin.html 062// 063// Currently this implementation does not use the comment message. 064// Perhaps in the future this method can be used to deliver a change-set with the given comment message. 065// However some users may want the checkin goal to only check in to the desired repository workspace. 066// While some users may want the checkin goal to both check in to the desired repository workspace and deliver it to a 067// stream. 068// Currently this implementation only checks in the unresolved changes to the repository workspace. 069// 070// See the following links for additional information on the RTC "deliver" command. 071// RTC 2.0.0.2: 072// http://publib.boulder.ibm.com/infocenter/rtc/v2r0m0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_deliver.html 073// RTC 3.0: 074// http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_deliver.html 075// RTC 3.0.1: 076// http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0m1/topic/com.ibm.team.scm.doc/topics/r_scm_cli_deliver.html 077// 078 079/** 080 * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a> 081 */ 082public class JazzAddCommand 083 extends AbstractAddCommand 084{ 085 /** 086 * {@inheritDoc} 087 */ 088 protected ScmResult executeAddCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message, 089 boolean binary ) 090 throws ScmException 091 { 092 if ( getLogger().isDebugEnabled() ) 093 { 094 getLogger().debug( "Executing add command..." ); 095 } 096 097 // The message can only be used when we create a change set, which is only done on the checkin command. 098 // So that can be ignored. 099 // The binary flag, is not needed for RTC. 100 return executeAddCommand( repo, fileSet ); 101 } 102 103 public AddScmResult executeAddCommand( ScmProviderRepository repo, ScmFileSet fileSet ) 104 throws ScmException 105 { 106 // NOTE: THIS IS ALSO CALLED DIRECTLY FROM THE CHECKIN COMMAND. 107 // 108 // The "checkin" command does not produce consumable output as to which individual files were checked in. (in 109 // 2.0.0.2 at least). Since only "locally modified" changes get checked in, we call a "status" command to 110 // generate a list of these files. 111 File baseDir = fileSet.getBasedir(); 112 File parentFolder = ( baseDir.getParentFile() != null ) ? baseDir.getParentFile() : baseDir; 113 114 List<ScmFile> changedScmFiles = new ArrayList<ScmFile>(); 115 List<File> changedFiles = new ArrayList<File>(); 116 List<ScmFile> commitedFiles = new ArrayList<ScmFile>(); 117 118 JazzStatusCommand statusCmd = new JazzStatusCommand(); 119 statusCmd.setLogger( getLogger() ); 120 StatusScmResult statusCmdResult = statusCmd.executeStatusCommand( repo, fileSet ); 121 List<ScmFile> statusScmFiles = statusCmdResult.getChangedFiles(); 122 123 for ( ScmFile file : statusScmFiles ) 124 { 125 getLogger().debug( "Iterating over statusScmFiles: " + file ); 126 if ( file.getStatus() == ScmFileStatus.ADDED || file.getStatus() == ScmFileStatus.DELETED 127 || file.getStatus() == ScmFileStatus.MODIFIED ) 128 { 129 changedScmFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) ); 130 changedFiles.add( new File( parentFolder, file.getPath() ) ); 131 } 132 } 133 134 List<File> files = fileSet.getFileList(); 135 if ( files.size() == 0 ) 136 { 137 // Either commit all local changes 138 commitedFiles = changedScmFiles; 139 } 140 else 141 { 142 // Or commit specific files 143 for ( File file : files ) 144 { 145 if ( fileExistsInFileList( file, changedFiles ) ) 146 { 147 commitedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) ); 148 } 149 } 150 } 151 152 // Now that we have a list of files to process, we can "add" (scm checkin) them. 153 JazzAddConsumer addConsumer = new JazzAddConsumer( repo, getLogger() ); 154 ErrorConsumer errConsumer = new ErrorConsumer( getLogger() ); 155 JazzScmCommand command = createAddCommand( repo, fileSet ); 156 157 int status = command.execute( addConsumer, errConsumer ); 158 if ( status != 0 || errConsumer.hasBeenFed() ) 159 { 160 return new AddScmResult( command.getCommandString(), 161 "Error code for Jazz SCM add (checkin) command - " + status, 162 errConsumer.getOutput(), false ); 163 } 164 165 return new AddScmResult( command.getCommandString(), addConsumer.getFiles() ); 166 } 167 168 public JazzScmCommand createAddCommand( ScmProviderRepository repo, ScmFileSet fileSet ) 169 { 170 JazzScmCommand command = 171 new JazzScmCommand( JazzConstants.CMD_CHECKIN, null, repo, false, fileSet, getLogger() ); 172 173 List<File> files = fileSet.getFileList(); 174 if ( files != null && !files.isEmpty() ) 175 { 176 for ( File file : files ) 177 { 178 command.addArgument( file.getPath() ); // Check in only the files specified 179 } 180 } 181 else 182 { 183 command.addArgument( "." ); // This will check in all local changes 184 } 185 186 return command; 187 } 188 189 private boolean fileExistsInFileList( File file, List<File> fileList ) 190 { 191 boolean exists = false; 192 for ( File changedFile : fileList ) 193 { 194 if ( changedFile.compareTo( file ) == 0 ) 195 { 196 exists = true; 197 break; 198 } 199 } 200 return exists; 201 } 202 203}