Skip to content

Prevent resource leaks #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 38 additions & 36 deletions core/src/main/java/com/cosium/code/format/git/GitIndexEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,48 +132,50 @@ private void logObjectContent(ObjectLoader objectLoader, String virtualName)
}

private LineRanges computeLineRanges(DirCacheEntry dirCacheEntry) {
Git git = new Git(repository);
boolean partiallyStaged;
try {
partiallyStaged =
!git.status().addPath(dirCacheEntry.getPathString()).call().getModified().isEmpty();
} catch (GitAPIException e) {
throw new MavenGitCodeFormatException(e);
}
try (Git git = new Git(repository)) {
boolean partiallyStaged;
try {
partiallyStaged =
!git.status().addPath(dirCacheEntry.getPathString()).call().getModified().isEmpty();
} catch (GitAPIException e) {
throw new MavenGitCodeFormatException(e);
}

if (!partiallyStaged) {
return LineRanges.all();
}
if (!partiallyStaged) {
return LineRanges.all();
}

try {
return git
.diff()
.setPathFilter(PathFilter.create(dirCacheEntry.getPathString()))
.setCached(true)
.call()
.stream()
.map(this::computeLineRanges)
.reduce(LineRanges::concat)
.orElse(LineRanges.all());

} catch (GitAPIException e) {
throw new MavenGitCodeFormatException(e);
try {
return git
.diff()
.setPathFilter(PathFilter.create(dirCacheEntry.getPathString()))
.setCached(true)
.call()
.stream()
.map(this::computeLineRanges)
.reduce(LineRanges::concat)
.orElse(LineRanges.all());

} catch (GitAPIException e) {
throw new MavenGitCodeFormatException(e);
}
}
}

private LineRanges computeLineRanges(DiffEntry diffEntry) {
DiffFormatter diffFormatter = new DiffFormatter(NullOutputStream.INSTANCE);
diffFormatter.setRepository(repository);

try {
FileHeader fileHeader = diffFormatter.toFileHeader(diffEntry);
return fileHeader.getHunks().stream()
.map(HunkHeader.class::cast)
.map(this::computeLineRanges)
.reduce(LineRanges::concat)
.orElse(LineRanges.all());
} catch (IOException e) {
throw new MavenGitCodeFormatException(e);
try (DiffFormatter diffFormatter = new DiffFormatter(NullOutputStream.INSTANCE)) {
diffFormatter.setRepository(repository);

try {
FileHeader fileHeader = diffFormatter.toFileHeader(diffEntry);
return fileHeader.getHunks().stream()
.map(HunkHeader.class::cast)
.map(this::computeLineRanges)
.reduce(LineRanges::concat)
.orElse(LineRanges.all());
} catch (IOException e) {
throw new MavenGitCodeFormatException(e);
}
}
}

Expand Down
29 changes: 15 additions & 14 deletions core/src/main/java/com/cosium/code/format/git/GitStagedFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,21 @@ private GitStagedFiles(Log log, Repository repository, Set<String> filePaths) {

public static GitStagedFiles read(Log log, Repository repository, Predicate<Path> fileFilter)
throws GitAPIException {
Status gitStatus = new Git(repository).status().call();
Path workTree = repository.getWorkTree().toPath();
Set<String> filePaths =
Stream.concat(gitStatus.getChanged().stream(), gitStatus.getAdded().stream())
.filter(relativePath -> fileFilter.test(workTree.resolve(relativePath)))
.collect(Collectors.toSet());
log.debug("Staged files: " + filePaths.toString());
return new GitStagedFiles(log, repository, filePaths);
try (Git git = new Git(repository)) {
Status gitStatus = git.status().call();
Path workTree = repository.getWorkTree().toPath();
Set<String> filePaths =
Stream.concat(gitStatus.getChanged().stream(), gitStatus.getAdded().stream())
.filter(relativePath -> fileFilter.test(workTree.resolve(relativePath)))
.collect(Collectors.toSet());
log.debug("Staged files: " + filePaths.toString());
return new GitStagedFiles(log, repository, filePaths);
}
}

public void format(CodeFormatters formatters) throws IOException {
Git git = new Git(repository);

try (Index index = Index.lock(repository);
try (Git git = new Git(repository);
Index index = Index.lock(repository);
TemporaryFile temporaryDiffFile =
TemporaryFile.create(log, "diff-between-unformatted-and-formatted-files")) {
DirCacheEditor dirCacheEditor = index.editor();
Expand All @@ -78,10 +79,10 @@ public void format(CodeFormatters formatters) throws IOException {

index.write();

try (Repository autoCRLFRepository =
new AutoCRLFRepository(git.getRepository().getDirectory(), eolStreamType);
try (Git gitForAutoCRLFRepository =
new Git(new AutoCRLFRepository(git.getRepository().getDirectory(), eolStreamType));
OutputStream diffOutput = temporaryDiffFile.newOutputStream()) {
new Git(autoCRLFRepository)
gitForAutoCRLFRepository
.diff()
.setOutputStream(diffOutput)
.setOldTree(treeIterator(repository.readDirCache()))
Expand Down