View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5    *     https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   */
25  package org.eluder.coveralls.maven.plugin.domain;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import org.eclipse.jgit.lib.Config;
33  import org.eclipse.jgit.lib.Constants;
34  import org.eclipse.jgit.lib.Repository;
35  import org.eclipse.jgit.lib.RepositoryBuilder;
36  import org.eclipse.jgit.revwalk.RevWalk;
37  
38  /**
39   * The Class GitRepository.
40   */
41  public class GitRepository {
42  
43      /** The source directory. */
44      private final File sourceDirectory;
45  
46      /**
47       * Instantiates a new git repository.
48       *
49       * @param sourceDirectory
50       *            the source directory
51       */
52      public GitRepository(final File sourceDirectory) {
53          this.sourceDirectory = sourceDirectory;
54      }
55  
56      /**
57       * Load.
58       *
59       * @return the git
60       *
61       * @throws IOException
62       *             Signals that an I/O exception has occurred.
63       */
64      public Git load() throws IOException {
65          try (var repository = new RepositoryBuilder().findGitDir(this.sourceDirectory).build()) {
66              final var head = this.getHead(repository);
67              final var branch = this.getBranch(repository);
68              final var remotes = this.getRemotes(repository);
69              return new Git(repository.getWorkTree(), head, branch, remotes);
70          }
71      }
72  
73      /**
74       * Gets the head.
75       * <p>
76       * Resource is closed in load().
77       *
78       * @param repository
79       *            the repository
80       *
81       * @return the head
82       *
83       * @throws IOException
84       *             Signals that an I/O exception has occurred.
85       */
86      @SuppressWarnings("resource")
87      private Git.Head getHead(final Repository repository) throws IOException {
88          final var revision = repository.resolve(Constants.HEAD);
89          final var commit = new RevWalk(repository).parseCommit(revision);
90          if (revision == null) {
91              throw new IllegalStateException("Cannot resolve HEAD");
92          }
93          return new Git.Head(revision.getName(), commit.getAuthorIdent().getName(),
94                  commit.getAuthorIdent().getEmailAddress(), commit.getCommitterIdent().getName(),
95                  commit.getCommitterIdent().getEmailAddress(), commit.getFullMessage());
96      }
97  
98      /**
99       * Gets the branch.
100      *
101      * @param repository
102      *            the repository
103      *
104      * @return the branch
105      *
106      * @throws IOException
107      *             Signals that an I/O exception has occurred.
108      */
109     private String getBranch(final Repository repository) throws IOException {
110         return repository.getBranch();
111     }
112 
113     /**
114      * Gets the remotes.
115      *
116      * @param repository
117      *            the repository
118      *
119      * @return the remotes
120      */
121     private List<Git.Remote> getRemotes(final Repository repository) {
122         final Config config = repository.getConfig();
123         final List<Git.Remote> remotes = new ArrayList<>();
124         for (final String remote : config.getSubsections("remote")) {
125             final var url = config.getString("remote", remote, "url");
126             remotes.add(new Git.Remote(remote, url));
127         }
128         return remotes;
129     }
130 }