-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add check for copyright headers (#7370)
- Loading branch information
1 parent
06bf664
commit b7ee7bd
Showing
5 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const {execSync} = require('child_process'); | ||
const isbinaryfile = require('isbinaryfile'); | ||
|
||
const getFileContents = path => fs.readFileSync(path, {encoding: 'utf-8'}); | ||
const isDirectory = path => fs.lstatSync(path).isDirectory(); | ||
const createRegExp = pattern => new RegExp(pattern); | ||
|
||
// Important: this patterns must be in sync with internal Facebook tools | ||
|
||
const GENERIC_IGNORED_EXTENSIONS = [ | ||
'lock', | ||
'patch', | ||
'exe', | ||
'bin', | ||
'cfg', | ||
'config', | ||
'conf', | ||
'html', | ||
'md', | ||
'markdown', | ||
'opam', | ||
'osm', | ||
'descr', | ||
'rst', | ||
'json', | ||
'key', | ||
'ini', | ||
'plist', | ||
'snap', | ||
'svg', | ||
'txt', | ||
'xcodeproj', | ||
'xcscheme', | ||
'xml', | ||
'yaml', | ||
'yml', | ||
'textile', | ||
'tsv', | ||
'csv', | ||
'pem', | ||
'csr', | ||
'der', | ||
'crt', | ||
'cert', | ||
'cer', | ||
'p7b', | ||
'iml', | ||
'org', | ||
'podspec', | ||
'modulemap', | ||
'pch', | ||
'lproj', | ||
'xcworkspace', | ||
'storyboard', | ||
'tvml', | ||
'xib', | ||
'pbxproj', | ||
'xcworkspacedata', | ||
'xccheckout', | ||
'xcsettings', | ||
'strings', | ||
'ipynb', | ||
'htm', | ||
'toml', | ||
].map(extension => createRegExp(`\.${extension}$`)); | ||
|
||
const GENERIC_IGNORED_PATTERNS = [ | ||
'(^|/)\\.[^/]+(/|$)', | ||
|
||
'third[_\\-. ]party/', | ||
'^node[_\\-. ]modules/', | ||
'gradlew\\.bat$', | ||
'gradlew$', | ||
'gradle/wrapper/', | ||
'.idea/', | ||
'__init__\\.py$', | ||
'^Setup.hs$', | ||
'^(readme|README|Readme)\\..*$', | ||
'Cargo\\.toml$', | ||
'^Cartfile.*$', | ||
'^.*\\.xcodeproj/$', | ||
'^.*\\.xcworkspace/$', | ||
'^.*\\.lproj/$', | ||
'^.*\\.bundle/$', | ||
'^MANIFEST\\.in$', | ||
].map(createRegExp); | ||
|
||
const CUSTOM_IGNORED_PATTERNS = [ | ||
'\\.(example|map)$', | ||
'^examples/.*', | ||
'^flow-typed/.*', | ||
'^packages/expect/src/jasmineUtils\\.js$', | ||
'^packages/jest-config/src/vendor/jsonlint\\.js$', | ||
].map(createRegExp); | ||
|
||
const IGNORED_PATTERNS = [ | ||
...GENERIC_IGNORED_EXTENSIONS, | ||
...GENERIC_IGNORED_PATTERNS, | ||
...CUSTOM_IGNORED_PATTERNS, | ||
]; | ||
|
||
const INCLUDED_PATTERNS = [ | ||
// Any file with an extension | ||
/\.[^/]+$/, | ||
]; | ||
|
||
const COPYRIGHT_HEADER_RE = /(Facebook, Inc(.|,)? and its affiliates)|([0-9]{4}-present(.|,)? Facebook)|([0-9]{4}(.|,)? Facebook)/; | ||
|
||
function needsCopyrightHeader(file) { | ||
const contents = getFileContents(file); | ||
return contents.trim().length > 0 && !COPYRIGHT_HEADER_RE.test(contents); | ||
} | ||
|
||
function check() { | ||
const allFiles = execSync('git ls-files', {encoding: 'utf-8'}) | ||
.trim() | ||
.split('\n'); | ||
|
||
const invalidFiles = allFiles.filter( | ||
file => | ||
INCLUDED_PATTERNS.some(pattern => pattern.test(file)) && | ||
!IGNORED_PATTERNS.some(pattern => pattern.test(file)) && | ||
!isDirectory(file) && | ||
!isbinaryfile.sync(file) && | ||
needsCopyrightHeader(file) | ||
); | ||
|
||
if (invalidFiles.length > 0) { | ||
console.log(`Facebook copyright header check failed for the following files: | ||
${invalidFiles.join('\n ')} | ||
Please include the header or blacklist the files in \`scripts/checkCopyrightHeaders.js\``); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
check(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters