From e9f895fc9c66565b992932cf2155c7c316c01558 Mon Sep 17 00:00:00 2001 From: conderls Date: Wed, 23 Dec 2020 16:22:33 +0800 Subject: [PATCH] feat: 'ratchetFrom' in git submodule supported (#746) --- CHANGES.md | 1 + .../diffplug/spotless/extra/GitRatchet.java | 32 +++++++++++++++++-- plugin-gradle/CHANGES.md | 2 ++ plugin-maven/CHANGES.md | 2 ++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a1fbaee438..f370646487 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Changed +* Added support for git-submodule with `ratchetFrom` ([#746](https://github.com/diffplug/spotless/issues/746)) * Update default ktfmt from 0.16 to 0.18 ([#748](https://github.com/diffplug/spotless/issues/748)) * fix typo in javadoc comment for SQL\_FORMATTER\_INDENT\_TYPE ([#753](https://github.com/diffplug/spotless/pull/753)) 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 5130760091..2fbd568191 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 @@ -17,6 +17,8 @@ import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -183,13 +185,37 @@ protected Repository repositoryFor(Project project) throws IOException { return null; } + /** + * When populating a new submodule directory with "git submodule init", the $GIT_DIR meta-information directory + * for submodules is created inside $GIT_DIR/modules// directory of the super-project + * and referenced via the git-file mechanism. + */ + private static @Nullable File getDotGitDir(File dir, String dotGit) { + File dotGitPath = new File(dir, dotGit); + + if (dotGitPath.isDirectory()) { + return dotGitPath; + } else if (dotGitPath.isFile()) { + try { + String relativePath = new String(Files.readAllBytes(dotGitPath.toPath()), StandardCharsets.UTF_8) + .split(":")[1].trim(); + return getDotGitDir(dir, relativePath); + } catch (IOException e) { + System.err.println("failed to parse git meta: " + e.getMessage()); + return null; + } + } else { + return null; + } + } + private static boolean isGitRoot(File dir) { - File dotGit = new File(dir, Constants.DOT_GIT); - return dotGit.isDirectory() && RepositoryCache.FileKey.isGitRepository(dotGit, FS.DETECTED); + File dotGit = getDotGitDir(dir, Constants.DOT_GIT); + return dotGit != null && RepositoryCache.FileKey.isGitRepository(dotGit, FS.DETECTED); } static Repository createRepo(File dir) throws IOException { - return FileRepositoryBuilder.create(new File(dir, Constants.DOT_GIT)); + return FileRepositoryBuilder.create(getDotGitDir(dir, Constants.DOT_GIT)); } /** diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index e7074e9a34..8df2656a9c 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] +### Added +* Added support for git-submodule with `ratchetFrom` ([#746](https://github.com/diffplug/spotless/issues/746)) ## [5.8.2] - 2020-11-16 ### Fixed diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index b6e7611c7c..39aea53603 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] +### Added +* Added support for git-submodule with `ratchetFrom` ([#746](https://github.com/diffplug/spotless/issues/746)) ### Fixed * Fix broken test for spotlessFiles parameter on windows ([#737](https://github.com/diffplug/spotless/pull/737))