Remove end year from licenses

This commit is contained in:
Jamie Kyle 2023-01-03 11:55:46 -08:00 committed by GitHub
parent eafd02b6d2
commit 3705b959d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
766 changed files with 899 additions and 882 deletions

View file

@ -1,4 +1,4 @@
// Copyright 2018-2020 Signal Messenger, LLC
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { join } from 'path';

View file

@ -1,4 +1,4 @@
// Copyright 2020-2022 Signal Messenger, LLC
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
// This file doesn't check the format of license files, just the end year. See
@ -11,6 +11,8 @@ import * as fs from 'fs';
import { promisify } from 'util';
import * as childProcess from 'child_process';
import pMap from 'p-map';
// eslint-disable-next-line import/no-extraneous-dependencies
import chalk from 'chalk';
const exec = promisify(childProcess.exec);
@ -102,7 +104,10 @@ export function readFirstLines(
input: fs.createReadStream(file),
});
lineReader.on('line', line => {
lines.push(line);
// `lineReader.close()` does not guarantee 'line' won't be emitted again
if (lines.length < count) {
lines.push(line);
}
if (lines.length >= count) {
lineReader.close();
}
@ -113,15 +118,26 @@ export function readFirstLines(
});
}
async function getLatestCommitYearForFile(file: string): Promise<number> {
const dateString = (
async function getCommitFileWasAdded(
file: string
): Promise<{ commitYear: number; commitHash: string }> {
const logLine = (
await new Promise<string>((resolve, reject) => {
let result = '';
// We use the more verbose `spawn` to avoid command injection, in case the filename
// has strange characters.
const gitLog = childProcess.spawn(
'git',
['log', '-1', '--format=%as', file],
[
// From: https://stackoverflow.com/questions/11533199/how-to-find-the-commit-in-which-a-given-file-was-added
'log',
'-1', // limit number of lines to return to 1
'--diff-filter=A', // select only files that are added (A)
'--follow', // continue listing the history of a file beyond renames
'--find-renames=40%', // consider a delete/add pair to be a rename if less than 40% of the file has changed (default 50%)
'--format=%as %h', // display commit as date YYYY-MM-DD
file,
],
{
cwd: rootPath,
env: { PATH: process.env.PATH },
@ -140,27 +156,110 @@ async function getLatestCommitYearForFile(file: string): Promise<number> {
})
).trim();
const result = new Date(dateString).getFullYear();
assert(!Number.isNaN(result), `Could not read commit year for ${file}`);
return result;
const [dateString, commitHash] = logLine.split(' ');
const commitYear = new Date(dateString).getFullYear();
assert(!Number.isNaN(commitYear), `Could not read commit year for ${file}`);
return { commitYear, commitHash };
}
type Failure = {
file: string;
warnings: Array<string>;
};
function indent(text: string) {
return text
.split('\n')
.map(line => ` ${line}`)
.join('\n');
}
async function main() {
const currentYear = new Date().getFullYear() + 1;
const currentYear = new Date().getFullYear();
const failures: Array<Failure> = [];
await forEachRelevantFile(async file => {
const [firstLine] = await readFirstLines(file, 1);
const { groups = {} } =
firstLine.match(/(?:\d{4}-)?(?<endYearString>\d{4})/) || {};
const { endYearString } = groups;
const endYear = Number(endYearString);
let lines: Array<string>;
let firstLine: string | void;
let secondLine: string | void;
assert(
endYear === currentYear ||
endYear === (await getLatestCommitYearForFile(file)),
`${file} has an invalid end license year`
);
if (getExtension(file) === '.sh') {
lines = await readFirstLines(file, 3);
[, firstLine, secondLine] = lines;
} else {
lines = await readFirstLines(file, 2);
[firstLine, secondLine] = lines;
}
const warnings = [];
if (!/Copyright \d{4} Signal Messenger, LLC/.test(firstLine)) {
const commit = await getCommitFileWasAdded(file);
warnings.push(
chalk.red('Missing/Incorrect copyright line'),
indent(
chalk.green(
`Expected: "Copyright ${commit.commitYear} Signal Messenger, LLC"`
)
),
indent(chalk.yellow(`Actual: "${firstLine}"`)),
indent(
chalk.italic.dim(
`Tip: Looks like this file was added in ${commit.commitHash} in ${commit.commitYear}`
)
),
indent(
chalk.italic.dim(
`Tip: You can also use the current year (${currentYear})`
)
)
);
} else if (/\d{4}-\d{4}/.test(firstLine)) {
warnings.push(
chalk.red('Copyright should not include end year'),
indent(chalk.yellow(`Actual: "${firstLine}"`))
);
}
if (!secondLine.includes('SPDX-License-Identifier: AGPL-3.0-only')) {
warnings.push(
chalk.red('Missing/incorrect license line'),
indent(
chalk.green('Expected: "SPDX-License-Identifier: AGPL-3.0-only"')
),
indent(chalk.yellow(`Actual: "${secondLine}"`))
);
}
if (warnings.length) {
failures.push({ file, warnings });
}
});
const failed = failures.length > 0;
/* eslint-disable no-console */
if (failed) {
console.log();
console.log(
chalk.magenta.bold(
'Some files are missing/contain incorrect copyrights/licenses:'
)
);
console.log();
for (const failure of failures) {
console.log(chalk.bold(`${failure.file}:`));
console.log(indent(failure.warnings.join('\n')));
console.log();
}
console.log(chalk.magenta.bold('`yarn lint-license-comments` failed'));
console.log();
process.exit(1);
}
/* eslint-enable no-console */
}
// Note: this check will fail if we switch to ES modules. See

View file

@ -1,4 +1,4 @@
// Copyright 2018-2022 Signal Messenger, LLC
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-disable no-console */

View file

@ -1,4 +1,4 @@
// Copyright 2018-2022 Signal Messenger, LLC
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { join } from 'path';

View file

@ -1,4 +1,4 @@
// Copyright 2018-2021 Signal Messenger, LLC
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
// Tool requirements:

View file

@ -1,4 +1,4 @@
// Copyright 2018-2022 Signal Messenger, LLC
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { readJsonSync, writeJsonSync } from 'fs-extra';