Skip to content

Support multiple year tokens in license headers - #3223

Open
toyeshhm wants to merge 2 commits into
diffplug:mainfrom
toyeshhm:fix-1270-multiple-year-tokens
Open

toyeshhm wants to merge 2 commits into
diffplug:mainfrom
toyeshhm:fix-1270-multiple-year-tokens

Conversation

@toyeshhm

@toyeshhm toyeshhm commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #1270.

Problem

A license header can only use the year token once. With a header like

/*
 * Copyright $YEAR ACME corp.
 * Parts copyright $YEAR Other corp.
 */

the first line gets the year and the second line is written out with a literal $YEAR. The step also never treats such a file as clean, since the literal token never matches what is in the file.

Root cause

LicenseHeaderStep.Runtime finds the first year token with indexOf and splits the header into beforeYear and afterYear. Any later $YEAR (or $today.year) stays inside afterYear as plain text. That text is written as-is, and it is also what replaceYear searches for to find where the existing year ends.

Fix

Every year token in the header gets the same year as the first one.

  • afterYearWith(year) fills any year tokens left in afterYear with the year being written. It is used everywhere the header is rebuilt, including SET_FROM_GIT mode.
  • To read the existing year, replaceYear now looks for the text after the first year up to the next token (afterYearUntilNextYear) instead of the whole afterYear. The no-change fast path also checks that the rest of the header matches.

Year detection, updateYearWithLatest and year ranges still go by the first token. For a header with a single token, afterYearUntilNextYear equals afterYear, so behavior there is unchanged.

Tests

  • LicenseHeaderStepTest.should_apply_license_containing_YEAR_token_multiple_times: adds the header to a file without one, leaves 2003, 1990-2015 and the current year alone, keeps the first year when the second one differs, and updates both slots to 2003-<now> with UPDATE_TO_TODAY.
  • LicenseHeaderStepTest.should_setFromGit_year_for_every_YEAR_token: same header in SET_FROM_GIT mode.
  • Both fail on main, where the second line comes out as Parts copyright $YEAR Other corp., and pass with the fix.
  • ./gradlew :testlib:test --tests 'com.diffplug.spotless.generic.*', :plugin-gradle:test --tests '*LicenseHeaderTest', :plugin-maven:test --tests 'com.diffplug.spotless.maven.generic.LicenseHeader*', :lib:spotlessCheck :testlib:spotlessCheck :lib:spotbugsMain all pass.

Changelog entries are in CHANGES.md, plugin-gradle/CHANGES.md and plugin-maven/CHANGES.md.

Copilot AI balanced review requested due to automatic review settings December 3, 2026 20:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lack of support for multiple occurances of year token in license header

2 participants