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