001package org.apache.maven.scm.provider.git.gitexe.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.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.ScmResult; 027import org.apache.maven.scm.command.add.AbstractAddCommand; 028import org.apache.maven.scm.command.add.AddScmResult; 029import org.apache.maven.scm.provider.ScmProviderRepository; 030import org.apache.maven.scm.provider.git.command.GitCommand; 031import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils; 032import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusCommand; 033import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer; 034import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository; 035import org.codehaus.plexus.util.Os; 036import org.codehaus.plexus.util.cli.CommandLineUtils; 037import org.codehaus.plexus.util.cli.Commandline; 038 039import java.io.File; 040import java.net.URI; 041import java.util.ArrayList; 042import java.util.Collections; 043import java.util.List; 044 045/** 046 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a> 047 */ 048public class GitAddCommand 049 extends AbstractAddCommand 050 implements GitCommand 051{ 052 /** 053 * {@inheritDoc} 054 */ 055 protected ScmResult executeAddCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message, 056 boolean binary ) 057 throws ScmException 058 { 059 GitScmProviderRepository repository = (GitScmProviderRepository) repo; 060 061 if ( fileSet.getFileList().isEmpty() ) 062 { 063 throw new ScmException( "You must provide at least one file/directory to add" ); 064 } 065 066 AddScmResult result = executeAddFileSet( fileSet ); 067 068 if ( result != null ) 069 { 070 return result; 071 } 072 073 // SCM-709: statusCommand uses repositoryRoot instead of workingDirectory, adjust it with relativeRepositoryPath 074 Commandline clRevparse = GitStatusCommand.createRevparseShowToplevelCommand( fileSet ); 075 076 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer(); 077 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 078 079 URI relativeRepositoryPath = null; 080 081 int exitCode; 082 083 exitCode = GitCommandLineUtils.execute( clRevparse, stdout, stderr, getLogger() ); 084 if ( exitCode != 0 ) 085 { 086 // git-status returns non-zero if nothing to do 087 if ( getLogger().isInfoEnabled() ) 088 { 089 getLogger().info( "Could not resolve toplevel" ); 090 } 091 } 092 else 093 { 094 relativeRepositoryPath = 095 GitStatusConsumer.resolveURI( stdout.getOutput().trim(), fileSet.getBasedir().toURI() ); 096 } 097 098 // git-add doesn't show single files, but only summary :/ 099 // so we must run git-status and consume the output 100 // borrow a few things from the git-status command 101 Commandline clStatus = GitStatusCommand.createCommandLine( repository, fileSet ); 102 103 GitStatusConsumer statusConsumer = 104 new GitStatusConsumer( getLogger(), fileSet.getBasedir(), relativeRepositoryPath ); 105 stderr = new CommandLineUtils.StringStreamConsumer(); 106 exitCode = GitCommandLineUtils.execute( clStatus, statusConsumer, stderr, getLogger() ); 107 if ( exitCode != 0 ) 108 { 109 // git-status returns non-zero if nothing to do 110 if ( getLogger().isInfoEnabled() ) 111 { 112 getLogger().info( "nothing added to commit but untracked files present (use \"git add\" to track)" ); 113 } 114 } 115 116 List<ScmFile> changedFiles = new ArrayList<ScmFile>(); 117 118 // rewrite all detected files to now have status 'checked_in' 119 for ( ScmFile scmfile : statusConsumer.getChangedFiles() ) 120 { 121 // if a specific fileSet is given, we have to check if the file is really tracked 122 for ( File f : fileSet.getFileList() ) 123 { 124 if ( FilenameUtils.separatorsToUnix( f.getPath() ).equals( scmfile.getPath() ) ) 125 { 126 changedFiles.add( scmfile ); 127 } 128 } 129 } 130 131 Commandline cl = createCommandLine( fileSet.getBasedir(), fileSet.getFileList() ); 132 return new AddScmResult( cl.toString(), changedFiles ); 133 } 134 135 public static Commandline createCommandLine( File workingDirectory, List<File> files ) 136 throws ScmException 137 { 138 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "add" ); 139 140 // use this separator to make clear that the following parameters are files and not revision info. 141 cl.createArg().setValue( "--" ); 142 143 GitCommandLineUtils.addTarget( cl, files ); 144 145 return cl; 146 } 147 148 private AddScmResult executeAddFileSet( ScmFileSet fileSet ) 149 throws ScmException 150 { 151 File workingDirectory = fileSet.getBasedir(); 152 List<File> files = fileSet.getFileList(); 153 154 // command line can be too long for windows so add files individually (see SCM-697) 155 if ( Os.isFamily( Os.FAMILY_WINDOWS ) ) 156 { 157 for ( File file : files ) 158 { 159 AddScmResult result = executeAddFiles( workingDirectory, Collections.singletonList( file ) ); 160 161 if ( result != null ) 162 { 163 return result; 164 } 165 } 166 } 167 else 168 { 169 AddScmResult result = executeAddFiles( workingDirectory, files ); 170 171 if ( result != null ) 172 { 173 return result; 174 } 175 } 176 177 return null; 178 } 179 180 private AddScmResult executeAddFiles( File workingDirectory, List<File> files ) 181 throws ScmException 182 { 183 Commandline cl = createCommandLine( workingDirectory, files ); 184 185 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 186 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer(); 187 188 int exitCode = GitCommandLineUtils.execute( cl, stdout, stderr, getLogger() ); 189 190 if ( exitCode != 0 ) 191 { 192 return new AddScmResult( cl.toString(), "The git-add command failed.", stderr.getOutput(), false ); 193 } 194 195 return null; 196 } 197}