View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.git.jgit.command.status;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFile;
27  import org.apache.maven.scm.ScmFileSet;
28  import org.apache.maven.scm.ScmFileStatus;
29  import org.apache.maven.scm.command.status.AbstractStatusCommand;
30  import org.apache.maven.scm.command.status.StatusScmResult;
31  import org.apache.maven.scm.provider.ScmProviderRepository;
32  import org.apache.maven.scm.provider.git.command.GitCommand;
33  import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
34  import org.eclipse.jgit.api.Git;
35  import org.eclipse.jgit.api.Status;
36  
37  /**
38   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
39   * @author Dominik Bartholdi (imod)
40   * @since 1.9
41   */
42  public class JGitStatusCommand extends AbstractStatusCommand implements GitCommand {
43      /**
44       * {@inheritDoc}
45       */
46      protected StatusScmResult executeStatusCommand(ScmProviderRepository repo, ScmFileSet fileSet) throws ScmException {
47          Git git = null;
48          try {
49              git = JGitUtils.openRepo(fileSet.getBasedir());
50              Status status = git.status().call();
51              List<ScmFile> changedFiles = getFileStati(status);
52  
53              return new StatusScmResult("JGit status", changedFiles);
54          } catch (Exception e) {
55              throw new ScmException("JGit status failure!", e);
56          } finally {
57              JGitUtils.closeRepo(git);
58          }
59      }
60  
61      private List<ScmFile> getFileStati(Status status) {
62          List<ScmFile> all = new ArrayList<>();
63          addAsScmFiles(all, status.getAdded(), ScmFileStatus.ADDED);
64          addAsScmFiles(all, status.getChanged(), ScmFileStatus.UPDATED);
65          addAsScmFiles(all, status.getConflicting(), ScmFileStatus.CONFLICT);
66          addAsScmFiles(all, status.getModified(), ScmFileStatus.MODIFIED);
67          addAsScmFiles(all, status.getRemoved(), ScmFileStatus.DELETED);
68          return all;
69      }
70  
71      private void addAsScmFiles(Collection<ScmFile> all, Collection<String> files, ScmFileStatus status) {
72          for (String f : files) {
73              all.add(new ScmFile(f, status));
74          }
75      }
76  }