Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -286,9 +286,14 @@ private static Optional<String> 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();
Expand All @@ -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);
}
}

Expand Down
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<setLicenseHeaderYearsFromGitHistory>` is enabled. ([#2270](https://github.com/diffplug/spotless/issues/2270))

## [3.12.0] - 2026-11-21
### Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Loading