Skip to content

Commit 58afdb5

Browse files
committed
feat: Abstract Git log calls
1 parent 3ece9b2 commit 58afdb5

2 files changed

Lines changed: 67 additions & 10 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.variantsync.diffdetective.datasets;
2+
3+
import java.io.IOException;
4+
import java.util.Iterator;
5+
6+
import org.eclipse.jgit.api.errors.GitAPIException;
7+
import org.eclipse.jgit.revwalk.RevCommit;
8+
import org.tinylog.Logger;
9+
10+
/**
11+
* A functional interface for listing the commits in a {@link Repository}.
12+
* Apart from listing a fixed set of commits, this is mainly useful to configure a {@link org.eclipse.jgit.api.Git#log() Git log} call.
13+
*<p>
14+
* This is mainly intended to be used as an argument for the {@code listCommits} argument of
15+
* {@link Repository#Repository(RepositoryLocationType, Path, URI, String, Function<Repository, Iterator<RevCommit>>, PatchDiffParseOptions, DiffFilter)}.
16+
*/
17+
@FunctionalInterface
18+
public interface CommitLister {
19+
Iterator<RevCommit> listCommits(Repository repository);
20+
21+
/**
22+
* List all commits reachable by the current {@code HEAD} of the repository.
23+
*/
24+
public static final CommitLister TraverseHEAD =
25+
(Repository repository) -> {
26+
try {
27+
return repository.getGitRepo().log().call().iterator();
28+
} catch (GitAPIException e) {
29+
Logger.warn("Could not get log for git repository {}", repository.getRepositoryName());
30+
throw new RuntimeException(e);
31+
}
32+
};
33+
34+
/**
35+
* List all commits reachable from all branches of the repository.
36+
*/
37+
public static final CommitLister AllCommits =
38+
(Repository repository) -> {
39+
try {
40+
return repository.getGitRepo().log().all().call().iterator();
41+
} catch (GitAPIException | IOException e) {
42+
Logger.warn("Could not get log for git repository {}", repository.getRepositoryName());
43+
throw new RuntimeException(e);
44+
}
45+
};
46+
}

src/main/java/org/variantsync/diffdetective/datasets/Repository.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.variantsync.diffdetective.datasets;
22

33
import org.eclipse.jgit.api.Git;
4-
import org.eclipse.jgit.api.errors.GitAPIException;
54
import org.eclipse.jgit.lib.ObjectId;
65
import org.eclipse.jgit.revwalk.RevCommit;
76
import org.tinylog.Logger;
@@ -44,6 +43,11 @@ public class Repository {
4443
*/
4544
private final String repositoryName;
4645

46+
/**
47+
* A function that extracts the list of commits that are represented by this repository instance.
48+
*/
49+
private CommitLister commitLister;
50+
4751
/**
4852
* Filter determining which files and commits to consider for diffs.
4953
*/
@@ -55,14 +59,15 @@ public class Repository {
5559
private PatchDiffParseOptions parseOptions;
5660

5761
private final Lazy<Git> git = Lazy.of(this::load);
58-
62+
5963
/**
6064
* Creates a repository.
6165
*
6266
* @param repoLocation {@link RepositoryLocationType} From which location the repository is read from
6367
* @param localPath The local path where the repository can be found or should be cloned to.
6468
* @param remote The remote url of the repository. May be <code>null</code> if local.
6569
* @param repositoryName Name of the cloned repository (<code>null</code> if local)
70+
* @param commitLister extracts the commits from {@link #getGitRepo()} that should be represented.
6671
* @param parseOptions Omit some debug data to save RAM.
6772
* @param diffFilter Filter determining which files and commits to consider for diffs.
6873
*/
@@ -71,27 +76,38 @@ public Repository(
7176
final Path localPath,
7277
final URI remote,
7378
final String repositoryName,
79+
final CommitLister commitLister,
7480
final PatchDiffParseOptions parseOptions,
7581
final DiffFilter diffFilter) {
7682
this.repoLocation = repoLocation;
7783
this.localPath = localPath;
7884
this.remote = remote;
7985
this.repositoryName = repositoryName;
86+
this.commitLister = commitLister;
8087
this.parseOptions = parseOptions;
8188
this.diffFilter = diffFilter;
8289
}
8390

8491
/**
8592
* Creates repository of the given source and with all other settings set to default values.
8693
* @see Repository
94+
* <p>
95+
* Defaults to {@link CommitLister#TraverseHEAD}, {@link PatchDiffParseOptions#Default} and {@link DiffFilter#ALLOW_ALL}.
8796
*/
8897
public Repository(
8998
final RepositoryLocationType repoLocation,
9099
final Path localPath,
91100
final URI remote,
92101
final String repositoryName) {
93-
this(repoLocation, localPath, remote, repositoryName,
94-
PatchDiffParseOptions.Default, DiffFilter.ALLOW_ALL);
102+
this(
103+
repoLocation,
104+
localPath,
105+
remote,
106+
repositoryName,
107+
CommitLister.TraverseHEAD,
108+
PatchDiffParseOptions.Default,
109+
DiffFilter.ALLOW_ALL
110+
);
95111
}
96112

97113
/**
@@ -251,12 +267,7 @@ public RevCommit getCommit(String commitHash) throws IOException {
251267
* Returns all commits in the repository's history.
252268
*/
253269
public Iterator<RevCommit> getCommits() {
254-
try {
255-
return getGitRepo().log().call().iterator();
256-
} catch (GitAPIException e) {
257-
Logger.warn("Could not get log for git repository {}", getRepositoryName());
258-
throw new RuntimeException(e);
259-
}
270+
return commitLister.listCommits(this);
260271
}
261272

262273
/**

0 commit comments

Comments
 (0)