Skip to content

Commit 273407b

Browse files
#39 Added Palantir formatter
Added Palantir formatter to make it easier for others to contribute to the project without needing to fuss over code style. Also performed initial formatting.
1 parent c1962e3 commit 273407b

File tree

20 files changed

+528
-496
lines changed

20 files changed

+528
-496
lines changed

change-proneness-ranker/src/main/java/org/hjug/git/ChangePronenessRanker.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package org.hjug.git;
22

3-
import lombok.extern.slf4j.Slf4j;
4-
import org.eclipse.jgit.api.errors.GitAPIException;
5-
import org.eclipse.jgit.lib.Repository;
6-
73
import java.io.IOException;
84
import java.util.Comparator;
95
import java.util.List;
106
import java.util.TreeMap;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.eclipse.jgit.api.errors.GitAPIException;
9+
import org.eclipse.jgit.lib.Repository;
1110

1211
@Slf4j
1312
public class ChangePronenessRanker {
@@ -32,7 +31,9 @@ public void rankChangeProneness(List<ScmLogInfo> scmLogInfos) {
3231

3332
for (ScmLogInfo scmLogInfo : scmLogInfos) {
3433
int commitsInRepositorySinceCreation =
35-
changeCountsByTimeStamps.tailMap(scmLogInfo.getEarliestCommit()).values().stream().mapToInt(i -> i).sum();
34+
changeCountsByTimeStamps.tailMap(scmLogInfo.getEarliestCommit()).values().stream()
35+
.mapToInt(i -> i)
36+
.sum();
3637

3738
scmLogInfo.setChangeProneness((float) scmLogInfo.getCommitCount() / commitsInRepositorySinceCreation);
3839
}

change-proneness-ranker/src/main/java/org/hjug/git/GitLogReader.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.hjug.git;
22

3+
import java.io.*;
4+
import java.util.*;
35
import lombok.extern.slf4j.Slf4j;
46
import org.eclipse.jgit.api.Git;
57
import org.eclipse.jgit.api.errors.GitAPIException;
@@ -12,24 +14,22 @@
1214
import org.eclipse.jgit.treewalk.TreeWalk;
1315
import org.eclipse.jgit.util.io.NullOutputStream;
1416

15-
import java.io.*;
16-
import java.util.*;
17-
1817
@Slf4j
1918
public class GitLogReader implements RepositoryLogReader {
2019

2120
static final String JAVA_FILE_TYPE = ".java";
2221

23-
//Based on https://github.com/Cosium/git-code-format-maven-plugin/blob/master/src/main/java/com/cosium/code/format/AbstractMavenGitCodeFormatMojo.java
24-
//MIT License
25-
//Move to a provider?
22+
// Based on
23+
// https://github.com/Cosium/git-code-format-maven-plugin/blob/master/src/main/java/com/cosium/code/format/AbstractMavenGitCodeFormatMojo.java
24+
// MIT License
25+
// Move to a provider?
2626
@Override
2727
public Repository gitRepository(File basedir) throws IOException {
2828
Repository gitRepository;
29-
FileRepositoryBuilder repositoryBuilder =
30-
new FileRepositoryBuilder().findGitDir(basedir);
29+
FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder().findGitDir(basedir);
3130
String gitIndexFileEnvVariable = System.getenv("GIT_INDEX_FILE");
32-
if (Objects.nonNull(gitIndexFileEnvVariable) && !gitIndexFileEnvVariable.trim().isEmpty()) {
31+
if (Objects.nonNull(gitIndexFileEnvVariable)
32+
&& !gitIndexFileEnvVariable.trim().isEmpty()) {
3333
log.debug("Setting Index File based on Env Variable GIT_INDEX_FILE {}", gitIndexFileEnvVariable);
3434
repositoryBuilder = repositoryBuilder.setIndexFile(new File(gitIndexFileEnvVariable));
3535
}
@@ -39,14 +39,13 @@ public Repository gitRepository(File basedir) throws IOException {
3939
}
4040

4141
public File getGitDir(File basedir) {
42-
FileRepositoryBuilder repositoryBuilder =
43-
new FileRepositoryBuilder().findGitDir(basedir);
42+
FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder().findGitDir(basedir);
4443
return repositoryBuilder.getGitDir();
4544
}
4645

47-
//https://stackoverflow.com/a/19950970/346247
48-
//and
49-
//https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/api/ReadFileFromCommit.java
46+
// https://stackoverflow.com/a/19950970/346247
47+
// and
48+
// https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/api/ReadFileFromCommit.java
5049
@Override
5150
public Map<String, ByteArrayOutputStream> listRepositoryContentsAtHEAD(Repository repository) throws IOException {
5251
Ref head = repository.exactRef("HEAD");
@@ -59,7 +58,7 @@ public Map<String, ByteArrayOutputStream> listRepositoryContentsAtHEAD(Repositor
5958
treeWalk.addTree(tree);
6059
treeWalk.setRecursive(false);
6160

62-
//TODO: extract rest of this method to test it
61+
// TODO: extract rest of this method to test it
6362
Map<String, ByteArrayOutputStream> fileContentsCollection = new HashMap<>();
6463
while (treeWalk.next()) {
6564
if (treeWalk.isSubtree()) {
@@ -77,9 +76,8 @@ public Map<String, ByteArrayOutputStream> listRepositoryContentsAtHEAD(Repositor
7776
return fileContentsCollection;
7877
}
7978

80-
81-
//log --follow implementation may be worth adopting in the future
82-
//https://github.com/spearce/jgit/blob/master/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevWalkTextBuiltin.java
79+
// log --follow implementation may be worth adopting in the future
80+
// https://github.com/spearce/jgit/blob/master/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevWalkTextBuiltin.java
8381

8482
/**
8583
* Returns the number of commits and earliest commit for a given path
@@ -105,21 +103,24 @@ public ScmLogInfo fileLog(Repository repository, String path) throws GitAPIExcep
105103
commitCount++;
106104
}
107105

108-
//based on https://stackoverflow.com/a/59274329/346247
109-
int mostRecentCommit =
110-
new Git(repository).log()
111-
.add(branchId)
112-
.addPath(path)
113-
.setMaxCount(1)
114-
.call().iterator().next()
115-
.getCommitTime();
106+
// based on https://stackoverflow.com/a/59274329/346247
107+
int mostRecentCommit = new Git(repository)
108+
.log()
109+
.add(branchId)
110+
.addPath(path)
111+
.setMaxCount(1)
112+
.call()
113+
.iterator()
114+
.next()
115+
.getCommitTime();
116116

117117
return new ScmLogInfo(path, earliestCommit, mostRecentCommit, commitCount);
118118
}
119119

120-
//based on https://stackoverflow.com/questions/27361538/how-to-show-changes-between-commits-with-jgit
120+
// based on https://stackoverflow.com/questions/27361538/how-to-show-changes-between-commits-with-jgit
121121
@Override
122-
public TreeMap<Integer, Integer> captureChangeCountByCommitTimestamp(Repository repository) throws IOException, GitAPIException {
122+
public TreeMap<Integer, Integer> captureChangeCountByCommitTimestamp(Repository repository)
123+
throws IOException, GitAPIException {
123124

124125
TreeMap<Integer, Integer> changesByCommitTimestamp = new TreeMap<>();
125126

@@ -149,7 +150,7 @@ public TreeMap<Integer, Integer> captureChangeCountByCommitTimestamp(Repository
149150
changesByCommitTimestamp.put(newCommit.getCommitTime(), count);
150151
}
151152

152-
//Handle first / initial commit
153+
// Handle first / initial commit
153154
if (!iterator.hasNext()) {
154155
changesByCommitTimestamp.putAll(walkFirstCommit(repository, oldCommit));
155156
}
@@ -199,5 +200,4 @@ Map<Integer, Integer> walkFirstCommit(Repository repository, RevCommit firstComm
199200

200201
return changesByCommitTimestamp;
201202
}
202-
203203
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package org.hjug.git;
22

3-
import org.eclipse.jgit.api.errors.GitAPIException;
4-
import org.eclipse.jgit.lib.Repository;
5-
63
import java.io.ByteArrayOutputStream;
74
import java.io.File;
85
import java.io.IOException;
96
import java.util.Map;
107
import java.util.TreeMap;
8+
import org.eclipse.jgit.api.errors.GitAPIException;
9+
import org.eclipse.jgit.lib.Repository;
1110

1211
public interface RepositoryLogReader {
1312

@@ -17,5 +16,6 @@ public interface RepositoryLogReader {
1716

1817
ScmLogInfo fileLog(Repository repository, String path) throws GitAPIException, IOException;
1918

20-
TreeMap<Integer, Integer> captureChangeCountByCommitTimestamp(Repository repository) throws IOException, GitAPIException;
19+
TreeMap<Integer, Integer> captureChangeCountByCommitTimestamp(Repository repository)
20+
throws IOException, GitAPIException;
2121
}
Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package org.hjug.git;
22

3-
import org.eclipse.jgit.api.errors.GitAPIException;
4-
import org.junit.Assert;
5-
import org.junit.Before;
6-
import org.junit.Test;
7-
8-
import java.io.IOException;
9-
import java.util.*;
10-
113
import static org.mockito.ArgumentMatchers.any;
124
import static org.mockito.Mockito.mock;
135
import static org.mockito.Mockito.when;
146

7+
import java.io.IOException;
8+
import java.util.*;
9+
import org.eclipse.jgit.api.errors.GitAPIException;
10+
import org.junit.Assert;
11+
import org.junit.Before;
12+
import org.junit.Test;
1513

1614
public class ChangePronenessRankerTest {
1715

@@ -24,24 +22,24 @@ public void setUp() {
2422
changePronenessRanker = new ChangePronenessRanker(null, repositoryLogReader);
2523
}
2624

27-
//TODO: this should probably be a cucumber test
25+
// TODO: this should probably be a cucumber test
2826
@Test
2927
public void testChangePronenessCalculation() throws IOException, GitAPIException {
3028
ScmLogInfo scmLogInfo = new ScmLogInfo("path", 1595275997, 0, 1);
3129

3230
TreeMap<Integer, Integer> commitsWithChangeCounts = new TreeMap<>();
3331
commitsWithChangeCounts.put(scmLogInfo.getEarliestCommit(), scmLogInfo.getCommitCount());
34-
commitsWithChangeCounts.put(scmLogInfo.getEarliestCommit() + 5*60, 3);
35-
commitsWithChangeCounts.put(scmLogInfo.getEarliestCommit() + 10*60, 3);
32+
commitsWithChangeCounts.put(scmLogInfo.getEarliestCommit() + 5 * 60, 3);
33+
commitsWithChangeCounts.put(scmLogInfo.getEarliestCommit() + 10 * 60, 3);
3634

3735
when(repositoryLogReader.captureChangeCountByCommitTimestamp(any())).thenReturn(commitsWithChangeCounts);
3836

3937
List<ScmLogInfo> scmLogInfos = new ArrayList<>();
4038
scmLogInfos.add(scmLogInfo);
4139
changePronenessRanker.rankChangeProneness(scmLogInfos);
4240

43-
//1 commit of a class we're interested in, 6 commits of other files after it
44-
Assert.assertEquals((float) 1/7, scmLogInfo.getChangeProneness(), 0.1);
41+
// 1 commit of a class we're interested in, 6 commits of other files after it
42+
Assert.assertEquals((float) 1 / 7, scmLogInfo.getChangeProneness(), 0.1);
4543
}
4644

4745
@Test
@@ -50,14 +48,14 @@ public void testRankChangeProneness() throws IOException, GitAPIException {
5048

5149
TreeMap<Integer, Integer> commitsWithChangeCounts = new TreeMap<>();
5250
commitsWithChangeCounts.put(scmLogInfo.getEarliestCommit(), scmLogInfo.getCommitCount());
53-
commitsWithChangeCounts.put(scmLogInfo.getEarliestCommit() + 5*60, 3);
54-
commitsWithChangeCounts.put(scmLogInfo.getEarliestCommit() + 10*60, 3);
51+
commitsWithChangeCounts.put(scmLogInfo.getEarliestCommit() + 5 * 60, 3);
52+
commitsWithChangeCounts.put(scmLogInfo.getEarliestCommit() + 10 * 60, 3);
5553

5654
ScmLogInfo scmLogInfo2 = new ScmLogInfo("file2", 1595175997, 0, 1);
5755

5856
commitsWithChangeCounts.put(scmLogInfo2.getEarliestCommit(), scmLogInfo2.getCommitCount());
59-
commitsWithChangeCounts.put(scmLogInfo2.getEarliestCommit() + 5*60, 5);
60-
commitsWithChangeCounts.put(scmLogInfo2.getEarliestCommit() + 10*60, 5);
57+
commitsWithChangeCounts.put(scmLogInfo2.getEarliestCommit() + 5 * 60, 5);
58+
commitsWithChangeCounts.put(scmLogInfo2.getEarliestCommit() + 10 * 60, 5);
6159

6260
when(repositoryLogReader.captureChangeCountByCommitTimestamp(any())).thenReturn(commitsWithChangeCounts);
6361

@@ -71,5 +69,4 @@ public void testRankChangeProneness() throws IOException, GitAPIException {
7169
// ranks lower since there have been more commits since initial commit
7270
Assert.assertEquals(1, scmLogInfo2.getChangePronenessRank());
7371
}
74-
75-
}
72+
}

change-proneness-ranker/src/test/java/org/hjug/git/GitLogReaderTest.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package org.hjug.git;
22

3+
import static java.nio.charset.StandardCharsets.UTF_8;
4+
5+
import java.io.*;
6+
import java.util.*;
37
import org.eclipse.jgit.api.Git;
48
import org.eclipse.jgit.api.errors.GitAPIException;
59
import org.eclipse.jgit.lib.Repository;
610
import org.eclipse.jgit.revwalk.RevCommit;
711
import org.junit.*;
812
import org.junit.rules.TemporaryFolder;
913

10-
import java.io.*;
11-
import java.util.*;
12-
13-
import static java.nio.charset.StandardCharsets.UTF_8;
14-
1514
public class GitLogReaderTest {
16-
//Borrowed bits and pieces from
17-
//https://gist.github.com/rherrmann/0c682ea327862cb6847704acf90b1d5d
15+
// Borrowed bits and pieces from
16+
// https://gist.github.com/rherrmann/0c682ea327862cb6847704acf90b1d5d
1817

1918
@Rule
20-
public TemporaryFolder tempFolder= new TemporaryFolder();
19+
public TemporaryFolder tempFolder = new TemporaryFolder();
2120

2221
private Git git;
2322
private Repository repository;
@@ -33,11 +32,10 @@ public void tearDown() {
3332
repository.close();
3433
}
3534

36-
3735
@Test
3836
public void testFileLog() throws IOException, GitAPIException, InterruptedException {
39-
//This path works when referencing the full Tobago repository
40-
//String filePath = "tobago-core/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java";
37+
// This path works when referencing the full Tobago repository
38+
// String filePath = "tobago-core/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java";
4139

4240
GitLogReader gitLogReader = new GitLogReader();
4341

@@ -48,10 +46,10 @@ public void testFileLog() throws IOException, GitAPIException, InterruptedExcept
4846
git.add().addFilepattern(".").call();
4947
RevCommit firstCommit = git.commit().setMessage("message").call();
5048

51-
//Sleeping for one second to guarantee commits have different time stamps
52-
Thread.sleep(1000) ;
49+
// Sleeping for one second to guarantee commits have different time stamps
50+
Thread.sleep(1000);
5351

54-
//write contents of updated file to original file
52+
// write contents of updated file to original file
5553
InputStream resourceAsStream2 = getClass().getClassLoader().getResourceAsStream("AttributeHandler2.java");
5654
writeFile(attributeHandler, convertInputStreamToString(resourceAsStream2));
5755

@@ -92,10 +90,10 @@ public void testCaptureChangCountByCommitTimestamp() throws Exception {
9290
git.add().addFilepattern(".").call();
9391
RevCommit firstCommit = git.commit().setMessage("message").call();
9492

95-
//Sleeping for one second to guarantee commits have different time stamps
96-
Thread.sleep(1000) ;
93+
// Sleeping for one second to guarantee commits have different time stamps
94+
Thread.sleep(1000);
9795

98-
//write contents of updated file to original file
96+
// write contents of updated file to original file
9997
InputStream resourceAsStream2 = getClass().getClassLoader().getResourceAsStream("AttributeHandler2.java");
10098
writeFile(attributeHandler, convertInputStreamToString(resourceAsStream2));
10199

@@ -111,7 +109,6 @@ public void testCaptureChangCountByCommitTimestamp() throws Exception {
111109
Assert.assertEquals(2, commitCounts.get(secondCommit.getCommitTime()).intValue());
112110
}
113111

114-
115112
private void writeFile(String name, String content) throws IOException {
116113
File file = new File(git.getRepository().getWorkTree(), name);
117114
try (FileOutputStream outputStream = new FileOutputStream(file)) {

0 commit comments

Comments
 (0)