if ( fileSet.getFileList().isEmpty() )
{
throw new ScmException( "You must provide at least one file/directory to add" );
}
AddScmResult result = executeAddFileSet( fileSet );
if ( result != null )
{
return result;
}
// SCM-709: statusCommand uses repositoryRoot instead of workingDirectory, adjust it with relativeRepositoryPath
Commandline clRevparse = GitStatusCommand.createRevparseShowToplevelCommand( fileSet );
CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
URI relativeRepositoryPath = null;
int exitCode;
exitCode = GitCommandLineUtils.execute( clRevparse, stdout, stderr, getLogger() );
if ( exitCode != 0 )
{
// git-status returns non-zero if nothing to do
if ( getLogger().isInfoEnabled() )
{
getLogger().info( "Could not resolve toplevel" );
}
}
else
{
relativeRepositoryPath = GitStatusConsumer.resolveURI( stdout.getOutput().trim() , fileSet.getBasedir().toURI() );
}
// git-add doesn't show single files, but only summary :/
// so we must run git-status and consume the output
// borrow a few things from the git-status command
Commandline clStatus = GitStatusCommand.createCommandLine( repository, fileSet );
GitStatusConsumer statusConsumer = new GitStatusConsumer( getLogger(), fileSet.getBasedir(), relativeRepositoryPath );
stderr = new CommandLineUtils.StringStreamConsumer();
exitCode = GitCommandLineUtils.execute( clStatus, statusConsumer, stderr, getLogger() );
if ( exitCode != 0 )
{
// git-status returns non-zero if nothing to do
if ( getLogger().isInfoEnabled() )
{
getLogger().info( "nothing added to commit but untracked files present (use \"git add\" to track)" );
}
}
List<ScmFile> changedFiles = new ArrayList<ScmFile>();
// rewrite all detected files to now have status 'checked_in'
for ( ScmFile scmfile : statusConsumer.getChangedFiles() )
{
// if a specific fileSet is given, we have to check if the file is really tracked
for ( File f : fileSet.getFileList() )
{
if ( FilenameUtils.separatorsToUnix( f.getPath() ).equals( scmfile.getPath() ) )
{
changedFiles.add( scmfile );
}
}
}
Commandline cl = createCommandLine( fileSet.getBasedir(), fileSet.getFileList() );
return new AddScmResult( cl.toString(), changedFiles );
}