diff --git a/core/src/main/java/com/cosium/code/format/git/GitIndexEntry.java b/core/src/main/java/com/cosium/code/format/git/GitIndexEntry.java index 3c2323c..65f05b6 100644 --- a/core/src/main/java/com/cosium/code/format/git/GitIndexEntry.java +++ b/core/src/main/java/com/cosium/code/format/git/GitIndexEntry.java @@ -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); + } } } diff --git a/core/src/main/java/com/cosium/code/format/git/GitStagedFiles.java b/core/src/main/java/com/cosium/code/format/git/GitStagedFiles.java index f343255..fd42339 100644 --- a/core/src/main/java/com/cosium/code/format/git/GitStagedFiles.java +++ b/core/src/main/java/com/cosium/code/format/git/GitStagedFiles.java @@ -53,20 +53,21 @@ private GitStagedFiles(Log log, Repository repository, Set filePaths) { public static GitStagedFiles read(Log log, Repository repository, Predicate fileFilter) throws GitAPIException { - Status gitStatus = new Git(repository).status().call(); - Path workTree = repository.getWorkTree().toPath(); - Set 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 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(); @@ -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()))