From 849923fb0e90ffa9b28ae4c69b82da3be37d6c55 Mon Sep 17 00:00:00 2001 From: Duke Date: Sat, 11 Mar 2023 22:41:48 +0100 Subject: [PATCH 1/2] Use try-with-resources to prevent resource leaks --- .../cosium/code/format/git/GitIndexEntry.java | 74 ++++++++++--------- .../code/format/git/GitStagedFiles.java | 26 ++++--- 2 files changed, 52 insertions(+), 48 deletions(-) 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..b89a9e2 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(); @@ -80,8 +81,9 @@ public void format(CodeFormatters formatters) throws IOException { try (Repository autoCRLFRepository = new AutoCRLFRepository(git.getRepository().getDirectory(), eolStreamType); + Git gitForAutoCRLFRepository = new Git(autoCRLFRepository); OutputStream diffOutput = temporaryDiffFile.newOutputStream()) { - new Git(autoCRLFRepository) + gitForAutoCRLFRepository .diff() .setOutputStream(diffOutput) .setOldTree(treeIterator(repository.readDirCache())) From ce610580e23441b35ef17a6db87319c6b417609b Mon Sep 17 00:00:00 2001 From: Duke Date: Fri, 31 Mar 2023 17:27:59 +0200 Subject: [PATCH 2/2] Inline variable --- .../main/java/com/cosium/code/format/git/GitStagedFiles.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 b89a9e2..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 @@ -79,9 +79,8 @@ public void format(CodeFormatters formatters) throws IOException { index.write(); - try (Repository autoCRLFRepository = - new AutoCRLFRepository(git.getRepository().getDirectory(), eolStreamType); - Git gitForAutoCRLFRepository = new Git(autoCRLFRepository); + try (Git gitForAutoCRLFRepository = + new Git(new AutoCRLFRepository(git.getRepository().getDirectory(), eolStreamType)); OutputStream diffOutput = temporaryDiffFile.newOutputStream()) { gitForAutoCRLFRepository .diff()