001package org.apache.maven.scm.provider.git.jgit.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.ScmResult;
026import org.apache.maven.scm.command.add.AbstractAddCommand;
027import org.apache.maven.scm.command.add.AddScmResult;
028import org.apache.maven.scm.provider.ScmProviderRepository;
029import org.apache.maven.scm.provider.git.command.GitCommand;
030import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
031import org.eclipse.jgit.api.Git;
032
033import java.util.List;
034
035/**
036 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
037 * @author Dominik Bartholdi (imod)
038 * @since 1.9
039 */
040public class JGitAddCommand
041    extends AbstractAddCommand
042    implements GitCommand
043{
044    /**
045     * {@inheritDoc}
046     */
047    protected ScmResult executeAddCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
048                                           boolean binary )
049        throws ScmException
050    {
051
052        if ( fileSet.getFileList().isEmpty() )
053        {
054            throw new ScmException( "You must provide at least one file/directory to add (e.g. -Dincludes=...)" );
055        }
056        Git git = null;
057        try
058        {
059            git = JGitUtils.openRepo( fileSet.getBasedir() );
060
061            List<ScmFile> addedFiles = JGitUtils.addAllFiles( git, fileSet );
062
063            if ( getLogger().isDebugEnabled() )
064            {
065                for ( ScmFile scmFile : addedFiles )
066                {
067                    getLogger().info( "added file: " + scmFile );
068                }
069            }
070
071            return new AddScmResult( "JGit add", addedFiles );
072
073        }
074        catch ( Exception e )
075        {
076            throw new ScmException( "JGit add failure!", e );
077        }
078        finally
079        {
080            JGitUtils.closeRepo( git );
081        }
082
083    }
084
085}