View Javadoc
1   package org.apache.maven.scm.provider.git.jgit.command.status;
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.ScmFileStatus;
26  import org.apache.maven.scm.command.status.AbstractStatusCommand;
27  import org.apache.maven.scm.command.status.StatusScmResult;
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  import org.eclipse.jgit.api.Status;
33  
34  import java.util.ArrayList;
35  import java.util.Collection;
36  import java.util.List;
37  
38  /**
39   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
40   * @author Dominik Bartholdi (imod)
41   * @since 1.9
42   */
43  public class JGitStatusCommand
44      extends AbstractStatusCommand
45      implements GitCommand
46  {
47      /**
48       * {@inheritDoc}
49       */
50      protected StatusScmResult executeStatusCommand( ScmProviderRepository repo, ScmFileSet fileSet )
51          throws ScmException
52      {
53          Git git = null;
54          try
55          {
56              git = Git.open( fileSet.getBasedir() );
57              Status status = git.status().call();
58              List<ScmFile> changedFiles = getFileStati( status );
59  
60              return new StatusScmResult( "JGit status", changedFiles );
61          }
62          catch ( Exception e )
63          {
64              throw new ScmException( "JGit status failure!", e );
65          }
66          finally
67          {
68              JGitUtils.closeRepo( git );
69          }
70      }
71  
72      private List<ScmFile> getFileStati( Status status )
73      {
74          List<ScmFile> all = new ArrayList<ScmFile>();
75          addAsScmFiles( all, status.getAdded(), ScmFileStatus.ADDED );
76          addAsScmFiles( all, status.getChanged(), ScmFileStatus.UPDATED );
77          addAsScmFiles( all, status.getConflicting(), ScmFileStatus.CONFLICT );
78          addAsScmFiles( all, status.getModified(), ScmFileStatus.MODIFIED );
79          addAsScmFiles( all, status.getRemoved(), ScmFileStatus.DELETED );
80          return all;
81      }
82  
83      private void addAsScmFiles( Collection<ScmFile> all, Collection<String> files, ScmFileStatus status )
84      {
85          for ( String f : files )
86          {
87              all.add( new ScmFile( f, status ) );
88          }
89      }
90  }