From c5446bac019b057c2d94dae6bca98149139ffcbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Fox?= Date: Sat, 21 Nov 2026 08:03:57 +0100 Subject: [PATCH] Keep `skipLinesMatching` lines when setting license years from git `setLicenseHeaderYearsFromGitHistory` built the new header from the whole file and cut everything before the delimiter, so lines matched by `skipLinesMatching` (like a shebang) were dropped. Run it through the same skip-lines handling that the regular formatting path uses. --- CHANGES.md | 1 + .../spotless/generic/LicenseHeaderStep.java | 13 +++++++--- plugin-gradle/CHANGES.md | 1 + plugin-maven/CHANGES.md | 1 + .../generic/LicenseHeaderStepTest.java | 25 +++++++++++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 5a3c98ad78..4660291f3d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed - Fix npm/node executable discovery treating a directory (e.g. an `npm` folder inside `NODE_PATH`) as an executable. ([#3190](https://github.com/diffplug/spotless/issues/3190)) +- `LicenseHeaderStep` no longer drops the lines matched by `skipLinesMatching` (such as a shebang) when the years are set from git history. ([#2270](https://github.com/diffplug/spotless/issues/2270)) ## [4.12.0] - 2026-11-21 ### Changes diff --git a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java index 70d524641c..b2c1fbcdc3 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java @@ -132,7 +132,7 @@ private static class SetLicenseHeaderYearsFromGitHistory implements SerializedFu @Override public FormatterFunc apply(Runtime input) throws Exception { - return FormatterFunc.needsFile(input::setLicenseHeaderYearsFromGitHistory); + return FormatterFunc.needsFile((raw, file) -> input.formatAfterSkippedLines(raw, file, input::setLicenseHeaderYearsFromGitHistory)); } } @@ -286,9 +286,14 @@ private static Optional getYearToken(String licenseHeader) { } /** Formats the given string. */ - private String format(String raw, File file) { + private String format(String raw, File file) throws Exception { + return formatAfterSkippedLines(raw, file, this::addOrUpdateLicenseHeader); + } + + /** Applies the given formatter to the content which follows the lines matching {@code skipLinesMatching}. */ + private String formatAfterSkippedLines(String raw, File file, FormatterFunc.NeedsFile formatter) throws Exception { if (skipLinesMatching == null) { - return addOrUpdateLicenseHeader(raw, file); + return formatter.applyWithFile(raw, file); } else { String[] lines = raw.split("\n"); StringBuilder skippedLinesBuilder = new StringBuilder(); @@ -307,7 +312,7 @@ private String format(String raw, File file) { remainingLinesBuilder.append(line).append('\n'); } } - return skippedLinesBuilder + addOrUpdateLicenseHeader(remainingLinesBuilder.toString(), file); + return skippedLinesBuilder + formatter.applyWithFile(remainingLinesBuilder.toString(), file); } } diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 1151abc9ae..3b9f884473 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed - Fix npm/node executable discovery treating a directory (e.g. an `npm` folder inside `NODE_PATH`) as an executable. ([#3190](https://github.com/diffplug/spotless/issues/3190)) +- `licenseHeader` no longer drops the lines matched by `skipLinesMatching` (such as a shebang) when run with `-PspotlessSetLicenseHeaderYearsFromGitHistory=true`. ([#2270](https://github.com/diffplug/spotless/issues/2270)) ## [8.12.0] - 2026-11-21 ### Added diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 626f743508..b6c004d9d0 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed - Fix npm/node executable discovery treating a directory (e.g. an `npm` folder inside `NODE_PATH`) as an executable. ([#3190](https://github.com/diffplug/spotless/issues/3190)) +- `` no longer drops the lines matched by `` (such as a shebang) when `` is enabled. ([#2270](https://github.com/diffplug/spotless/issues/2270)) ## [3.12.0] - 2026-11-21 ### Changes diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java index 783ca6b94e..62fec65d7f 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -336,4 +336,29 @@ void should_setFromGit_year_when_singleCommit() throws Exception { StepHarnessWithFile.forStep(this, step) .test("Foo.java", getTestResource(FILE_NO_LICENSE), hasHeaderYear(currentYear())); } + + @Test + @ClearGitConfig + void should_setFromGit_year_after_skipped_lines() throws Exception { + try (Git git = Git.init().setDirectory(rootFolder()).call()) { + setFile("run.sh").toContent("#!/bin/bash\necho hello\n"); + git.add() + .addFilepattern("run.sh") + .call(); + git.commit() + .setMessage("add") + .setAuthor("Test User", "test@example.com") + .setCommitter("Test User", "test@example.com") + .setSign(false) + .call(); + } + + FormatterStep step = LicenseHeaderStep.headerDelimiter("# Copyright $YEAR ACME corp.\n", "echo") + .withYearMode(YearMode.SET_FROM_GIT) + .withSkipLinesMatching("^#!") + .build(); + + StepHarnessWithFile.forStep(this, step) + .test("run.sh", "#!/bin/bash\necho hello\n", "#!/bin/bash\n# Copyright " + currentYear() + " ACME corp.\necho hello\n"); + } }