Skip to content
This repository was archived by the owner on Dec 14, 2020. It is now read-only.

Commit ef1b80a

Browse files
committed
fix: close AutoCloseable resource by try-with-resource
1 parent 98c40f3 commit ef1b80a

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

src/main/java/com/worksap/tools/spotbugs/maven/incremental/GitUpdatedJavaCodeDetector.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,29 @@ boolean detectDifference(Path projectRoot, String target, String source) throws
4040
throw new IllegalArgumentException("Git repository not found at " + projectRoot);
4141
}
4242
Path gitRoot = optionalGitRoot.get();
43-
Git git = Git.open(gitRoot.toFile());
44-
Repository repository = git.getRepository();
45-
Ref targetRef = repository.exactRef(target);
46-
if (targetRef == null) {
47-
throw new IllegalArgumentException(target + " does not exist in this Git repo");
48-
}
49-
Ref sourceRef = repository.exactRef(source);
50-
if (sourceRef == null) {
51-
throw new IllegalArgumentException(source + " does not exist in this Git repo");
43+
try (Git git = Git.open(gitRoot.toFile())) {
44+
Repository repository = git.getRepository();
45+
Ref targetRef = repository.exactRef(target);
46+
if (targetRef == null) {
47+
throw new IllegalArgumentException(target + " does not exist in this Git repo");
48+
}
49+
Ref sourceRef = repository.exactRef(source);
50+
if (sourceRef == null) {
51+
throw new IllegalArgumentException(source + " does not exist in this Git repo");
52+
}
53+
return !sourceRef.equals(targetRef);
5254
}
53-
return !sourceRef.equals(targetRef);
5455
}
5556

56-
Stream<Path> detectUpdatedCode(Path projectRoot, String target, String source) throws IOException {
57+
Stream<Path> detectUpdatedCode(Path projectRoot, String target, String source)
58+
throws IOException {
5759
Optional<Path> optionalGitRoot = findGitRoot(projectRoot);
5860
if (!optionalGitRoot.isPresent()) {
5961
throw new IllegalArgumentException("Git repository not found at " + projectRoot);
6062
}
6163

62-
try {
63-
Path gitRoot = optionalGitRoot.get();
64-
Git git = Git.open(gitRoot.toFile());
64+
Path gitRoot = optionalGitRoot.get();
65+
try (Git git = Git.open(gitRoot.toFile())) {
6566
Repository repository = git.getRepository();
6667
List<DiffEntry> updated =
6768
git.diff()

0 commit comments

Comments
 (0)