View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013 - 2023, Tapio Rautonen
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in
14   * all copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22   * THE SOFTWARE.
23   */
24  package org.eluder.coveralls.maven.plugin.domain;
25  
26  import org.eclipse.jgit.lib.Config;
27  import org.eclipse.jgit.lib.Constants;
28  import org.eclipse.jgit.lib.ObjectId;
29  import org.eclipse.jgit.lib.Repository;
30  import org.eclipse.jgit.lib.RepositoryBuilder;
31  import org.eclipse.jgit.revwalk.RevCommit;
32  import org.eclipse.jgit.revwalk.RevWalk;
33  
34  import java.io.File;
35  import java.io.IOException;
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  public class GitRepository {
40  
41      private final File sourceDirectory;
42  
43      public GitRepository(final File sourceDirectory) {
44          this.sourceDirectory = sourceDirectory;
45      }
46  
47      public Git load() throws IOException {
48          try (Repository repository = new RepositoryBuilder().findGitDir(this.sourceDirectory).build()) {
49              Git.Head head = getHead(repository);
50              String branch = getBranch(repository);
51              List<Git.Remote> remotes = getRemotes(repository);
52              return new Git(repository.getWorkTree(), head, branch, remotes);
53          }
54      }
55  
56      private Git.Head getHead(final Repository repository) throws IOException {
57          ObjectId revision = repository.resolve(Constants.HEAD);
58          RevCommit commit = new RevWalk(repository).parseCommit(revision);
59          Git.Head head = new Git.Head(
60                  revision.getName(),
61                  commit.getAuthorIdent().getName(),
62                  commit.getAuthorIdent().getEmailAddress(),
63                  commit.getCommitterIdent().getName(),
64                  commit.getCommitterIdent().getEmailAddress(),
65                  commit.getFullMessage()
66          );
67          return head;
68      }
69  
70      private String getBranch(final Repository repository) throws IOException {
71          return repository.getBranch();
72      }
73  
74      private List<Git.Remote> getRemotes(final Repository repository) {
75          Config config = repository.getConfig();
76          List<Git.Remote> remotes = new ArrayList<>();
77          for (String remote : config.getSubsections("remote")) {
78              String url = config.getString("remote", remote, "url");
79              remotes.add(new Git.Remote(remote, url));
80          }
81          return remotes;
82      }
83  }