001package org.apache.maven.scm.provider.git.gitexe.command.checkin; 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.commons.io.FilenameUtils; 023import org.apache.maven.scm.ScmException; 024import org.apache.maven.scm.ScmFile; 025import org.apache.maven.scm.ScmFileSet; 026import org.apache.maven.scm.ScmFileStatus; 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.log.ScmLogger; 031import org.apache.maven.scm.provider.ScmProviderRepository; 032import org.apache.maven.scm.provider.git.command.GitCommand; 033import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository; 034import org.apache.maven.scm.provider.git.util.GitUtil; 035import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils; 036import org.apache.maven.scm.provider.git.gitexe.command.add.GitAddCommand; 037import org.apache.maven.scm.provider.git.gitexe.command.branch.GitBranchCommand; 038import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusCommand; 039import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer; 040import org.codehaus.plexus.util.FileUtils; 041import org.codehaus.plexus.util.Os; 042import org.codehaus.plexus.util.cli.CommandLineUtils; 043import org.codehaus.plexus.util.cli.Commandline; 044 045import java.io.File; 046import java.io.IOException; 047import java.net.URI; 048import java.util.ArrayList; 049import java.util.Collections; 050import java.util.List; 051 052/** 053 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a> 054 * @author Olivier Lamy 055 * 056 */ 057public class GitCheckInCommand 058 extends AbstractCheckInCommand 059 implements GitCommand 060{ 061 /** {@inheritDoc} */ 062 protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message, 063 ScmVersion version ) 064 throws ScmException 065 { 066 GitScmProviderRepository repository = (GitScmProviderRepository) repo; 067 068 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 069 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer(); 070 071 int exitCode = -1; 072 073 File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null ); 074 try 075 { 076 FileUtils.fileWrite( messageFile.getAbsolutePath(), "UTF-8", message ); 077 } 078 catch ( IOException ex ) 079 { 080 return new CheckInScmResult( null, "Error while making a temporary file for the commit message: " 081 + ex.getMessage(), null, false ); 082 } 083 084 try 085 { 086 if ( !fileSet.getFileList().isEmpty() ) 087 { 088 // if specific fileSet is given, we have to git-add them first 089 // otherwise we will use 'git-commit -a' later 090 091 Commandline clAdd = null; 092 093 //SCM-714: Workaround for the Windows terminal command limit 094 if ( Os.isFamily( Os.FAMILY_WINDOWS ) ) 095 { 096 for ( File file: fileSet.getFileList() ) 097 { 098 clAdd = GitAddCommand.createCommandLine( fileSet.getBasedir(), 099 Collections.singletonList( file ) ); 100 exitCode = GitCommandLineUtils.execute( clAdd, stdout, stderr, getLogger() ); 101 102 if ( exitCode != 0 ) 103 { 104 break; 105 } 106 } 107 } 108 else 109 { 110 clAdd = GitAddCommand.createCommandLine( fileSet.getBasedir(), fileSet.getFileList() ); 111 exitCode = GitCommandLineUtils.execute( clAdd, stdout, stderr, getLogger() ); 112 } 113 114 if ( exitCode != 0 ) 115 { 116 return new CheckInScmResult( clAdd.toString(), "The git-add command failed.", 117 stderr.getOutput(), false ); 118 } 119 120 } 121 122 // SCM-709: statusCommand uses repositoryRoot instead of workingDirectory, adjust it with 123 // relativeRepositoryPath 124 URI relativeRepositoryPath = GitStatusCommand.getRelativeCWD( this, fileSet ); 125 126 // git-commit doesn't show single files, but only summary :/ 127 // so we must run git-status and consume the output 128 // borrow a few things from the git-status command 129 Commandline clStatus = GitStatusCommand.createCommandLine( repository, fileSet ); 130 131 GitStatusConsumer statusConsumer = 132 new GitStatusConsumer( getLogger(), fileSet.getBasedir(), relativeRepositoryPath, fileSet ); 133 exitCode = GitCommandLineUtils.execute( clStatus, statusConsumer, stderr, getLogger() ); 134 if ( exitCode != 0 ) 135 { 136 // git-status returns non-zero if nothing to do 137 if ( getLogger().isInfoEnabled() ) 138 { 139 getLogger().info( "nothing added to commit but untracked files present (use \"git add\" to " 140 + "track)" ); 141 } 142 } 143 144 if ( statusConsumer.getChangedFiles().isEmpty() ) 145 { 146 return new CheckInScmResult( null, statusConsumer.getChangedFiles() ); 147 } 148 149 Commandline clCommit = createCommitCommandLine( repository, fileSet, messageFile ); 150 151 exitCode = GitCommandLineUtils.execute( clCommit, stdout, stderr, getLogger() ); 152 if ( exitCode != 0 ) 153 { 154 return new CheckInScmResult( clCommit.toString(), "The git-commit command failed.", stderr.getOutput(), 155 false ); 156 } 157 158 if ( repo.isPushChanges() ) 159 { 160 Commandline cl = createPushCommandLine( getLogger(), repository, fileSet, version ); 161 162 exitCode = GitCommandLineUtils.execute( cl, stdout, stderr, getLogger() ); 163 if ( exitCode != 0 ) 164 { 165 return new CheckInScmResult( cl.toString(), "The git-push command failed.", stderr.getOutput(), 166 false ); 167 } 168 } 169 170 List<ScmFile> checkedInFiles = new ArrayList<ScmFile>( statusConsumer.getChangedFiles().size() ); 171 172 // rewrite all detected files to now have status 'checked_in' 173 for ( ScmFile changedFile : statusConsumer.getChangedFiles() ) 174 { 175 ScmFile scmfile = new ScmFile( changedFile.getPath(), ScmFileStatus.CHECKED_IN ); 176 177 if ( fileSet.getFileList().isEmpty() ) 178 { 179 checkedInFiles.add( scmfile ); 180 } 181 else 182 { 183 // if a specific fileSet is given, we have to check if the file is really tracked 184 for ( File f : fileSet.getFileList() ) 185 { 186 if ( FilenameUtils.separatorsToUnix( f.getPath() ).equals( scmfile.getPath() ) ) 187 { 188 checkedInFiles.add( scmfile ); 189 } 190 191 } 192 } 193 } 194 195 return new CheckInScmResult( clCommit.toString(), checkedInFiles ); 196 } 197 finally 198 { 199 try 200 { 201 FileUtils.forceDelete( messageFile ); 202 } 203 catch ( IOException ex ) 204 { 205 // ignore 206 } 207 } 208 209 } 210 211 // ---------------------------------------------------------------------- 212 // 213 // ---------------------------------------------------------------------- 214 215 public static Commandline createPushCommandLine( ScmLogger logger, GitScmProviderRepository repository, 216 ScmFileSet fileSet, ScmVersion version ) 217 throws ScmException 218 { 219 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "push" ); 220 221 String branch = GitBranchCommand.getCurrentBranch( logger, repository, fileSet ); 222 223 if ( branch == null || branch.length() == 0 ) 224 { 225 throw new ScmException( "Could not detect the current branch. Don't know where I should push to!" ); 226 } 227 228 cl.createArg().setValue( repository.getPushUrl() ); 229 230 cl.createArg().setValue( "refs/heads/" + branch + ":" + "refs/heads/" + branch ); 231 232 return cl; 233 } 234 235 public static Commandline createCommitCommandLine( GitScmProviderRepository repository, ScmFileSet fileSet, 236 File messageFile ) 237 throws ScmException 238 { 239 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "commit" ); 240 241 cl.createArg().setValue( "--verbose" ); 242 243 cl.createArg().setValue( "-F" ); 244 245 cl.createArg().setValue( messageFile.getAbsolutePath() ); 246 247 if ( fileSet.getFileList().isEmpty() ) 248 { 249 // commit all tracked files 250 cl.createArg().setValue( "-a" ); 251 } 252 253 if ( GitUtil.getSettings().isCommitNoVerify() ) 254 { 255 cl.createArg().setValue( "--no-verify" ); 256 } 257 258 return cl; 259 } 260 261}