diff --git a/CHANGES.md b/CHANGES.md index b4bdc4479e..a59f0c8570 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed +* Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426)) ## [2.31.0] - 2022-11-24 ### Added @@ -17,6 +19,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Fixed * Don't treat `@Value` as a type annotation [#1367](https://github.com/diffplug/spotless/pull/1367) * Support `ktlint_disabled_rules` in `ktlint` 0.47.x [#1378](https://github.com/diffplug/spotless/pull/1378) +* Share git repositories across projects when using ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426)) ### Changes * Bump default `ktfmt` version to latest `0.40` -> `0.41` ([#1340](https://github.com/diffplug/spotless/pull/1340)) * Bump default `scalafmt` version to latest `3.5.9` -> `3.6.1` ([#1373](https://github.com/diffplug/spotless/pull/1373)) diff --git a/build.gradle b/build.gradle index 5766e6b85e..f9e3d0de21 100644 --- a/build.gradle +++ b/build.gradle @@ -18,4 +18,4 @@ spotless { trimTrailingWhitespace() endWithNewline() } -} +} \ No newline at end of file diff --git a/lib-extra/src/main/java/com/diffplug/spotless/extra/GitRatchet.java b/lib-extra/src/main/java/com/diffplug/spotless/extra/GitRatchet.java index 6b8d5a9f41..31f2f597f9 100644 --- a/lib-extra/src/main/java/com/diffplug/spotless/extra/GitRatchet.java +++ b/lib-extra/src/main/java/com/diffplug/spotless/extra/GitRatchet.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2021 DiffPlug + * Copyright 2020-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,6 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -import java.util.Objects; import java.util.Optional; import javax.annotation.Nullable; @@ -137,7 +136,7 @@ private static boolean worktreeIsCleanCheckout(TreeWalk treeWalk) { private final static int INDEX = 1; private final static int WORKDIR = 2; - Map gitRoots = new HashMap<>(); + Map gitRoots = new HashMap<>(); Table rootTreeShaCache = HashBasedTable.create(); Map subtreeShaCache = new HashMap<>(); @@ -147,25 +146,14 @@ private static boolean worktreeIsCleanCheckout(TreeWalk treeWalk) { * We cache the Repository for every Project in {@code gitRoots}, and use dynamic programming to populate it. */ protected Repository repositoryFor(Project project) throws IOException { - Repository repo = gitRoots.get(project); + File projectGitDir = GitWorkarounds.getDotGitDir(getDir(project)); + if (projectGitDir == null || !RepositoryCache.FileKey.isGitRepository(projectGitDir, FS.DETECTED)) { + throw new IllegalArgumentException("Cannot find git repository in any parent directory"); + } + Repository repo = gitRoots.get(projectGitDir); if (repo == null) { - if (isGitRoot(getDir(project))) { - repo = createRepo(getDir(project)); - } else { - Project parentProj = getParent(project); - if (parentProj == null) { - repo = traverseParentsUntil(getDir(project).getParentFile(), null); - if (repo == null) { - throw new IllegalArgumentException("Cannot find git repository in any parent directory"); - } - } else { - repo = traverseParentsUntil(getDir(project).getParentFile(), getDir(parentProj)); - if (repo == null) { - repo = repositoryFor(parentProj); - } - } - } - gitRoots.put(project, repo); + repo = FileRepositoryBuilder.create(projectGitDir); + gitRoots.put(projectGitDir, repo); } return repo; } @@ -174,26 +162,6 @@ protected Repository repositoryFor(Project project) throws IOException { protected abstract @Nullable Project getParent(Project project); - private static @Nullable Repository traverseParentsUntil(File startWith, @Nullable File file) throws IOException { - while (startWith != null && !Objects.equals(startWith, file)) { - if (isGitRoot(startWith)) { - return createRepo(startWith); - } else { - startWith = startWith.getParentFile(); - } - } - return null; - } - - private static boolean isGitRoot(File dir) { - File dotGit = GitWorkarounds.getDotGitDir(dir); - return dotGit != null && RepositoryCache.FileKey.isGitRepository(dotGit, FS.DETECTED); - } - - static Repository createRepo(File dir) throws IOException { - return FileRepositoryBuilder.create(GitWorkarounds.getDotGitDir(dir)); - } - /** * Fast way to return treeSha of the given ref against the git repository which stores the given project. * Because of parallel project evaluation, there may be races here, so we synchronize on ourselves. However, this method diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 2060e5e90c..ebe7d0ceea 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Fixed +* Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426)) ## [6.12.0] - 2022-11-24 ### Added diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 9baefc2ec8..07903bfae1 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed +* Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426)) ## [2.28.0] - 2022-11-24 ### Added @@ -10,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Fixed * Don't treat `@Value` as a type annotation [#1367](https://github.com/diffplug/spotless/pull/1367) * Support `ktlint_disabled_rules` in `ktlint` 0.47.x [#1378](https://github.com/diffplug/spotless/pull/1378) +* Share git repositories across projects when using ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426)) ### Changes * Bump default `ktfmt` version to latest `0.40` -> `0.41` ([#1340](https://github.com/diffplug/spotless/pull/1340)) * Bump default `scalafmt` version to latest `3.5.9` -> `3.6.1` ([#1373](https://github.com/diffplug/spotless/pull/1373))