Skip to content

Allow for copyrights to be assigned to Google LLC #2665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 25, 2020
Merged
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
116 changes: 76 additions & 40 deletions tools/gitHooks/license.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
const { promisify } = require('util');
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const { resolve } = require('path');
const simpleGit = require('simple-git/promise');
const chalk = require('chalk');
const globRaw = require('glob');
const fs = require('mz/fs');
const ora = require('ora');

const glob = promisify(globRaw);
const root = resolve(__dirname, '../..');
const git = simpleGit(root);
const licenseHeader = `/**
* @license
* Copyright 2020 Google Inc.
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -28,49 +41,72 @@ const licenseHeader = `/**

`;

async function doLicenseCommit() {
const licenseSpinner = ora(' Validating License Headers').start();
const copyrightPattern = /Copyright \d{4} Google (Inc\.|LLC)/;
const oldCopyrightPattern = /(\s*\*\s*Copyright \d{4}) Google Inc\./;

const paths = await glob('**/*.+(ts|js)', {
ignore: ['**/node_modules/**', '**/dist/**']
async function readFiles(paths) {
const fileContents = await Promise.all(paths.map(path => fs.readFile(path)));
return fileContents.map((buffer, idx) => ({
contents: String(buffer),
path: paths[idx]
}));
}

function addLicenceTag(contents) {
const lines = contents.split('\n');
let newLines = [];
for (const line of lines) {
if (line.match(copyrightPattern)) {
const indent = line.split('*')[0]; // Get whitespace to match
newLines.push(indent + '* @license');
}
newLines.push(line);
}
return newLines.join('\n');
}

function rewriteCopyrightLine(contents) {
const lines = contents.split('\n');
let newLines = lines.map(line => {
return line.replace(oldCopyrightPattern, (_, leader) => {
return leader + ' Google LLC';
});
});
return newLines.join('\n');
}

// Files with no license block at all.
const fileContents = await Promise.all(paths.map(path => fs.readFile(path)));
const filesMissingLicensePaths = fileContents
.map((buffer, idx) => ({ buffer, path: paths[idx] }))
.filter(
({ buffer }) =>
String(buffer).match(/Copyright \d{4} Google Inc\./) == null
);
async function doLicenseCommit(changedFiles) {
const licenseSpinner = ora(' Validating License Headers').start();

await Promise.all(
filesMissingLicensePaths.map(({ buffer, path }) => {
const contents = Buffer.concat([new Buffer(licenseHeader), buffer]);
return fs.writeFile(path, contents, 'utf8');
})
);
const paths = changedFiles.filter(line => line.match(/(js|ts)$/));
if (paths.length === 0) return;

// Files with no @license tag.
const appendedFileContents = await Promise.all(
paths.map(path => fs.readFile(path))
);
const filesMissingTagPaths = appendedFileContents
.map((buffer, idx) => ({ buffer, path: paths[idx] }))
.filter(({ buffer }) => String(buffer).match(/@license/) == null);
const files = await readFiles(paths);

await Promise.all(
filesMissingTagPaths.map(({ buffer, path }) => {
const lines = String(buffer).split('\n');
let newLines = [];
for (const line of lines) {
if (line.match(/Copyright \d{4} Google Inc\./)) {
const indent = line.split('*')[0]; // Get whitespace to match
newLines.push(indent + '* @license');
}
newLines.push(line);
files.map(({ contents, path }) => {
let result = contents;

// Files with no license block at all.
if (result.match(copyrightPattern) == null) {
result = licenseHeader + result;
}

// Files with no @license tag.
if (result.match(/@license/) == null) {
result = addLicenceTag(result);
}

// Files with the old form of copyright notice.
if (result.match(oldCopyrightPattern) != null) {
result = rewriteCopyrightLine(result);
}

if (contents !== result) {
return fs.writeFile(path, result, 'utf8');
} else {
return Promise.resolve();
}
return fs.writeFile(path, newLines.join('\n'), 'utf8');
})
);

Expand Down
13 changes: 10 additions & 3 deletions tools/gitHooks/prepush.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017 Google Inc.
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,11 +44,18 @@ $ git stash pop
return process.exit(1);
}

const diff = await git.diff([
'--name-only',
'--diff-filter=d',
'origin/master...HEAD'
]);
const changedFiles = diff.split('\n');

// Style the code
await doPrettierCommit();
await doPrettierCommit(changedFiles);

// Validate License headers exist
await doLicenseCommit();
await doLicenseCommit(changedFiles);

console.log(chalk`
Pre-Push Validation Succeeded
Expand Down
13 changes: 4 additions & 9 deletions tools/gitHooks/prettier.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017 Google Inc.
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,21 +56,16 @@ function checkVersion() {
});
}

async function doPrettierCommit() {
async function doPrettierCommit(changedFiles) {
try {
await checkVersion();
} catch (e) {
console.error(e);
return process.exit(1);
}
const diff = await git.diff([
'--name-only',
'origin/master...HEAD',
'--diff-filter',
'd'
]);

// Only run on .js or .ts files.
const targetFiles = diff.split('\n').filter(line => line.match(/(js|ts)$/));
const targetFiles = changedFiles.filter(line => line.match(/(js|ts)$/));
if (targetFiles.length === 0) return;

const stylingSpinner = ora(
Expand Down