View Javadoc
1   package org.apache.maven.scm.provider.git.jgit.command.add;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmFile;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmResult;
26  import org.apache.maven.scm.command.add.AbstractAddCommand;
27  import org.apache.maven.scm.command.add.AddScmResult;
28  import org.apache.maven.scm.provider.ScmProviderRepository;
29  import org.apache.maven.scm.provider.git.command.GitCommand;
30  import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
31  import org.eclipse.jgit.api.Git;
32  
33  import java.util.List;
34  
35  /**
36   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
37   * @author Dominik Bartholdi (imod)
38   * @since 1.9
39   */
40  public class JGitAddCommand
41      extends AbstractAddCommand
42      implements GitCommand
43  {
44      /**
45       * {@inheritDoc}
46       */
47      protected ScmResult executeAddCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
48                                             boolean binary )
49          throws ScmException
50      {
51  
52          if ( fileSet.getFileList().isEmpty() )
53          {
54              throw new ScmException( "You must provide at least one file/directory to add (e.g. -Dincludes=...)" );
55          }
56          Git git = null;
57          try
58          {
59              git = Git.open( fileSet.getBasedir() );
60  
61              List<ScmFile> addedFiles = JGitUtils.addAllFiles( git, fileSet );
62  
63              if ( getLogger().isDebugEnabled() )
64              {
65                  for ( ScmFile scmFile : addedFiles )
66                  {
67                      getLogger().info( "added file: " + scmFile );
68                  }
69              }
70  
71              return new AddScmResult( "JGit add", addedFiles );
72  
73          }
74          catch ( Exception e )
75          {
76              throw new ScmException( "JGit add failure!", e );
77          }
78          finally
79          {
80              JGitUtils.closeRepo( git );
81          }
82  
83      }
84  
85  }