From d1c1b88e695c54fdd3978a85cedb8c2e3ffed4b5 Mon Sep 17 00:00:00 2001 From: toyeshhm Date: Tue, 1 Dec 2026 21:09:32 -0600 Subject: [PATCH 1/2] Support multiple year tokens in license headers --- .../spotless/generic/LicenseHeaderStep.java | 27 ++++++++++--- .../generic/LicenseHeaderStepTest.java | 39 +++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) 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..78b0b83d0f 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java @@ -226,6 +226,7 @@ private static final class Runtime implements Serializable { private final @Nullable String yearToday; private final @Nullable String beforeYear; private final @Nullable String afterYear; + private final @Nullable String afterYearUntilNextYear; private final boolean updateYearWithLatest; private final boolean licenseHeaderWithRange; private final boolean hasFileToken; @@ -253,6 +254,8 @@ private Runtime(String licenseHeader, String delimiter, String yearSeparator, bo int yearTokenIndex = licenseHeader.indexOf(yearToken.orElseThrow()); this.beforeYear = licenseHeader.substring(0, yearTokenIndex); this.afterYear = licenseHeader.substring(yearTokenIndex + yearToken.orElseThrow().length()); + // later year tokens get the same year, so an existing year is followed by the text up to the next token + this.afterYearUntilNextYear = getYearToken(afterYear).map(token -> afterYear.substring(0, afterYear.indexOf(token))).orElse(afterYear); this.yearSepOrFull = yearSeparator; this.updateYearWithLatest = updateYearWithLatest; @@ -268,6 +271,7 @@ private Runtime(String licenseHeader, String delimiter, String yearSeparator, bo this.yearToday = null; this.beforeYear = null; this.afterYear = null; + this.afterYearUntilNextYear = null; this.yearSepOrFull = licenseHeader; this.updateYearWithLatest = false; this.licenseHeaderWithRange = false; @@ -343,29 +347,39 @@ private String replaceYear(String raw) { } else { // the yes year case is a bit harder int beforeYearIdx = raw.indexOf(beforeYear); - int afterYearIdx = raw.indexOf(afterYear, beforeYearIdx + beforeYear.length() + 1); + int afterYearIdx = raw.indexOf(afterYearUntilNextYear, beforeYearIdx + beforeYear.length() + 1); - if (beforeYearIdx >= 0 && afterYearIdx >= 0 && afterYearIdx + afterYear.length() <= contentMatcher.start()) { + if (beforeYearIdx >= 0 && afterYearIdx >= 0 && afterYearIdx + afterYearUntilNextYear.length() <= contentMatcher.start()) { // and also ends with exactly the right header, so it's easy to parse the existing year String existingYear = raw.substring(beforeYearIdx + beforeYear.length(), afterYearIdx); String newYear = formatYearStr(calculateYearExact(existingYear)); + String afterNewYear = afterYearWith(newYear); if (existingYear.equals(newYear)) { // fastpath where we don't need to make any changes at all - boolean noPadding = beforeYearIdx == 0 && afterYearIdx + afterYear.length() == contentMatcher.start(); // allows fastpath return raw + boolean noPadding = beforeYearIdx == 0 && raw.startsWith(afterNewYear, afterYearIdx) && afterYearIdx + afterNewYear.length() == contentMatcher.start(); // allows fastpath return raw if (noPadding) { return raw; } } - return beforeYear + newYear + afterYear + content; + return beforeYear + newYear + afterNewYear + content; } else { String newYear = formatYearStr(calculateYearBySearching(raw.substring(0, contentMatcher.start()))); // at worst, we just say that it was made today - return beforeYear + newYear + afterYear + content; + return beforeYear + newYear + afterYearWith(newYear) + content; } } } } + /** The header after the first year token, with any later year tokens set to the same year. */ + private String afterYearWith(String year) { + String result = afterYear; + for (String yearToken : YEAR_TOKENS) { + result = result.replace(yearToken, year); + } + return result; + } + private static final Pattern YYYY = Pattern.compile("\\d{4}"); /** Calculates the year to inject. */ @@ -465,7 +479,8 @@ private String setLicenseHeaderYearsFromGitHistory(String raw, File file) throws } else { yearRange = oldYear + yearSepOrFull + newYear; } - return beforeYear + formatYearStr(yearRange) + afterYear + raw.substring(contentMatcher.start()); + String formattedYearRange = formatYearStr(yearRange); + return beforeYear + formattedYearRange + afterYearWith(formattedYearRange) + raw.substring(contentMatcher.start()); } private String replaceFileName(String raw, File file) { 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..20a183636c 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -96,6 +96,20 @@ void updateYearWithLatest() throws Throwable { .test(hasHeaderYear("1990-2015"), hasHeaderYear("1990-" + currentYear())); } + @Test + void should_apply_license_containing_YEAR_token_multiple_times() throws Throwable { + String license = "This is a fake license, $YEAR. ACME corp.\n * Parts copyright $YEAR Other corp."; + StepHarness.forStep(LicenseHeaderStep.headerDelimiter(header(license), PACKAGE_).build()) + .test(getTestResource(FILE_NO_LICENSE), hasHeaderYear(license, currentYear())) + .testUnaffected(hasHeaderYear(license, currentYear())) + .testUnaffected(hasHeaderYear(license, "2003")) + .testUnaffected(hasHeaderYear(license, "1990-2015")) + .test(hasHeader("This is a fake license, 2003. ACME corp.\n * Parts copyright 2007 Other corp."), hasHeaderYear(license, "2003")); + StepHarness.forStep(LicenseHeaderStep.headerDelimiter(header(license), PACKAGE_).withYearMode(YearMode.UPDATE_TO_TODAY).build()) + .testUnaffected(hasHeaderYear(license, currentYear())) + .test(hasHeaderYear(license, "2003"), hasHeaderYear(license, "2003-" + currentYear())); + } + @Test void should_apply_license_containing_YEAR_token_with_non_default_year_separator() throws Throwable { StepHarness.forStep(LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$YEAR), PACKAGE_).withYearSeparator(", ").build()) @@ -336,4 +350,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_for_every_YEAR_token() throws Exception { + try (Git git = Git.init().setDirectory(rootFolder()).call()) { + setFile("Foo.java").toContent("package foo; // v1\n"); + git.add() + .addFilepattern("Foo.java") + .call(); + git.commit() + .setMessage("add") + .setAuthor("Test User", "test@example.com") + .setCommitter("Test User", "test@example.com") + .setSign(false) + .call(); + } + + String license = "This is a fake license, $YEAR. ACME corp.\n * Parts copyright $YEAR Other corp."; + FormatterStep step = LicenseHeaderStep.headerDelimiter(header(license), PACKAGE_) + .withYearMode(YearMode.SET_FROM_GIT) + .build(); + + StepHarnessWithFile.forStep(this, step) + .test("Foo.java", getTestResource(FILE_NO_LICENSE), hasHeaderYear(license, currentYear())); + } } From aedab32be025ecca564b8bba01864c33de4275b9 Mon Sep 17 00:00:00 2001 From: toyeshhm Date: Tue, 1 Dec 2026 21:09:32 -0600 Subject: [PATCH 2/2] Add changelog entries for #1270 --- CHANGES.md | 2 ++ plugin-gradle/CHANGES.md | 2 ++ plugin-maven/CHANGES.md | 2 ++ 3 files changed, 6 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 5a3c98ad78..19c0b36a2b 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] +### Added +- License header: a `$YEAR` (or `$today.year`) token can now appear more than once in the header; every occurrence gets the same year. ([#1270](https://github.com/diffplug/spotless/issues/1270)) ### 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)) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 67e6658fad..721dcceb0e 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 +- License header: a `$YEAR` (or `$today.year`) token can now appear more than once in the header; every occurrence gets the same year. ([#1270](https://github.com/diffplug/spotless/issues/1270)) ### 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)) - Fix `Cannot set the value of task ... property 'taskService'` when Spotless is applied to sibling projects but not to their parent, a regression since `8.3.0`. Applying it to the root project with `apply false` is no longer necessary. ([#2877](https://github.com/diffplug/spotless/issues/2877)) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index ff78402409..c93de8c58c 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 +- License header: a `$YEAR` (or `$today.year`) token can now appear more than once in the header; every occurrence gets the same year. ([#1270](https://github.com/diffplug/spotless/issues/1270)) ### 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)) ### Changes