Skip to content

Commit ad45412

Browse files
committed
feat: Abstract Git log calls
1 parent f569d29 commit ad45412

1 file changed

Lines changed: 52 additions & 9 deletions

File tree

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

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.nio.file.Path;
1616
import java.util.Iterator;
1717
import java.util.Optional;
18+
import java.util.function.Function;
1819

1920
/**
2021
* Representation of git repositories used as datasets for DiffDetective.
@@ -44,6 +45,11 @@ public class Repository {
4445
*/
4546
private final String repositoryName;
4647

48+
/**
49+
* A function that extracts the list of commits that are represented by this repository instance.
50+
*/
51+
private Function<Repository, Iterator<RevCommit>> listCommits;
52+
4753
/**
4854
* Filter determining which files and commits to consider for diffs.
4955
*/
@@ -55,14 +61,45 @@ public class Repository {
5561
private PatchDiffParseOptions parseOptions;
5662

5763
private final Lazy<Git> git = Lazy.of(this::load);
58-
64+
65+
/**
66+
* List all commits reachable by the current {@code HEAD} of the repository.
67+
* This is intended to be used as an argument for the {@code listCommits} argument of
68+
* {@link #Repository(RepositoryLocationType, Path, URI, String, Function<Repository, Iterator<RevCommit>>, PatchDiffParseOptions, DiffFilter)}.
69+
*/
70+
public static final Function<Repository, Iterator<RevCommit>> TraverseHEAD =
71+
(Repository repository) -> {
72+
try {
73+
return repository.getGitRepo().log().call().iterator();
74+
} catch (GitAPIException e) {
75+
Logger.warn("Could not get log for git repository {}", repository.getRepositoryName());
76+
throw new RuntimeException(e);
77+
}
78+
};
79+
80+
/**
81+
* List all commits reachable from a branch of the repository.
82+
* This is intended to be used as an argument for the {@code listCommits} argument of
83+
* {@link #Repository(RepositoryLocationType, Path, URI, String, Function<Repository, Iterator<RevCommit>>, PatchDiffParseOptions, DiffFilter)}.
84+
*/
85+
public static final Function<Repository, Iterator<RevCommit>> AllCommits =
86+
(Repository repository) -> {
87+
try {
88+
return repository.getGitRepo().log().all().call().iterator();
89+
} catch (GitAPIException | IOException e) {
90+
Logger.warn("Could not get log for git repository {}", repository.getRepositoryName());
91+
throw new RuntimeException(e);
92+
}
93+
};
94+
5995
/**
6096
* Creates a repository.
6197
*
6298
* @param repoLocation {@link RepositoryLocationType} From which location the repository is read from
6399
* @param localPath The local path where the repository can be found or should be cloned to.
64100
* @param remote The remote url of the repository. May be <code>null</code> if local.
65101
* @param repositoryName Name of the cloned repository (<code>null</code> if local)
102+
* @param listCommits extracts the commits from {@link #getGitRepo()} that should be represented.
66103
* @param parseOptions Omit some debug data to save RAM.
67104
* @param diffFilter Filter determining which files and commits to consider for diffs.
68105
*/
@@ -71,27 +108,38 @@ public Repository(
71108
final Path localPath,
72109
final URI remote,
73110
final String repositoryName,
111+
final Function<Repository, Iterator<RevCommit>> listCommits,
74112
final PatchDiffParseOptions parseOptions,
75113
final DiffFilter diffFilter) {
76114
this.repoLocation = repoLocation;
77115
this.localPath = localPath;
78116
this.remote = remote;
79117
this.repositoryName = repositoryName;
118+
this.listCommits = listCommits;
80119
this.parseOptions = parseOptions;
81120
this.diffFilter = diffFilter;
82121
}
83122

84123
/**
85124
* Creates repository of the given source and with all other settings set to default values.
86125
* @see Repository
126+
* <p>
127+
* Defaults to {@link #TraverseHEAD}, {@link PatchDiffParseOptions#Default} and {@link DiffFilter#ALLOW_ALL}.
87128
*/
88129
public Repository(
89130
final RepositoryLocationType repoLocation,
90131
final Path localPath,
91132
final URI remote,
92133
final String repositoryName) {
93-
this(repoLocation, localPath, remote, repositoryName,
94-
PatchDiffParseOptions.Default, DiffFilter.ALLOW_ALL);
134+
this(
135+
repoLocation,
136+
localPath,
137+
remote,
138+
repositoryName,
139+
TraverseHEAD,
140+
PatchDiffParseOptions.Default,
141+
DiffFilter.ALLOW_ALL
142+
);
95143
}
96144

97145
/**
@@ -251,12 +299,7 @@ public RevCommit getCommit(String commitHash) throws IOException {
251299
* Returns all commits in the repository's history.
252300
*/
253301
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-
}
302+
return listCommits.apply(this);
260303
}
261304

262305
/**

0 commit comments

Comments
 (0)