diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2016-02-17 15:09:30 +0100 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2016-02-17 15:09:30 +0100 |
commit | 9c1248521a76d7d8e5c6c893cf73b149fdf80df1 (patch) | |
tree | 079f815df3f61eeb823c57283538d1dcfa5eb07a | |
parent | 7af7d18cfa2f1fab239e9a21e989bd8061cf23bb (diff) | |
parent | 100b357c688868dc4104c05dba3677cf2b650b2e (diff) | |
download | nextcloud-server-9c1248521a76d7d8e5c6c893cf73b149fdf80df1.tar.gz nextcloud-server-9c1248521a76d7d8e5c6c893cf73b149fdf80df1.zip |
Merge pull request #22340 from owncloud/allow-licensephp-for-app-repos-aswell
Allow license.php for app repos aswell
-rw-r--r-- | build/license.php | 62 |
1 files changed, 53 insertions, 9 deletions
diff --git a/build/license.php b/build/license.php index ce6fceb8160..d7ac4e0a143 100644 --- a/build/license.php +++ b/build/license.php @@ -20,7 +20,8 @@ */ class Licenses { - protected $paths = array(); + protected $paths = []; + protected $mailMap = []; public $authors = []; public function __construct() { @@ -48,17 +49,25 @@ EOD; $this->licenseText = str_replace('@YEAR@', date("Y"), $this->licenseText); } - function exec($folder) { + /** + * @param string|string[] $folder + * @param string|bool $gitRoot + */ + function exec($folder, $gitRoot = false) { if (is_array($folder)) { foreach($folder as $f) { - $this->exec($f); + $this->exec($f, $gitRoot); } return; } + if ($gitRoot !== false && substr($gitRoot, -1) !== '/') { + $gitRoot .= '/'; + } + if (is_file($folder)) { - $this->handleFile($folder); + $this->handleFile($folder, $gitRoot); return; } @@ -81,7 +90,7 @@ EOD; foreach ($iterator as $file) { /** @var SplFileInfo $file */ - $this->handleFile($file); + $this->handleFile($file, $gitRoot); } } @@ -103,14 +112,14 @@ With help from many libraries and frameworks including: file_put_contents(__DIR__.'/../AUTHORS', $template); } - function handleFile($path) { + function handleFile($path, $gitRoot) { $source = file_get_contents($path); if ($this->isMITLicensed($source)) { echo "MIT licensed file: $path" . PHP_EOL; return; } $source = $this->eatOldLicense($source); - $authors = $this->getAuthors($path); + $authors = $this->getAuthors($path, $gitRoot); $license = str_replace('@AUTHORS@', $authors, $this->licenseText); $source = "<?php" . PHP_EOL . $license . PHP_EOL . $source; @@ -136,6 +145,7 @@ With help from many libraries and frameworks including: /** * @param string $source + * @return string */ private function eatOldLicense($source) { $lines = explode(PHP_EOL, $source); @@ -167,10 +177,18 @@ With help from many libraries and frameworks including: return implode(PHP_EOL, $lines); } - private function getAuthors($file) { + private function getAuthors($file, $gitRoot) { // only add authors that changed code and not the license header $licenseHeaderEndsAtLine = trim(shell_exec("grep -n '*/' $file | head -n 1 | cut -d ':' -f 1")); + $buildDir = getcwd(); + if ($gitRoot) { + chdir($gitRoot); + $file = substr($file, strlen($gitRoot)); + } $out = shell_exec("git blame --line-porcelain -L $licenseHeaderEndsAtLine, $file | sed -n 's/^author //p;s/^author-mail //p' | sed 'N;s/\\n/ /' | sort -f | uniq"); + if ($gitRoot) { + chdir($buildDir); + } $authors = explode(PHP_EOL, $out); $authors = array_filter($authors, function($author) { @@ -179,17 +197,43 @@ With help from many libraries and frameworks including: 'Not Committed Yet <not.committed.yet>', 'Jenkins for ownCloud <owncloud-bot@tmit.eu>']); }); + + if ($gitRoot) { + $authors = array_map([$this, 'checkCoreMailMap'], $authors); + $authors = array_unique($authors); + } + $authors = array_map(function($author){ $this->authors[$author] = $author; return " * @author $author"; }, $authors); return implode(PHP_EOL, $authors); } + + private function checkCoreMailMap($author) { + if (empty($this->mailMap)) { + $content = file_get_contents(__DIR__ . '/../.mailmap'); + $entries = explode("\n", $content); + foreach ($entries as $entry) { + if (strpos($entry, '> ') === false) { + $this->mailMap[$entry] = $entry; + } else { + list($use, $actual) = explode('> ', $entry); + $this->mailMap[$actual] = $use . '>'; + } + } + } + + if (isset($this->mailMap[$author])) { + return $this->mailMap[$author]; + } + return $author; + } } $licenses = new Licenses; if (isset($argv[1])) { - $licenses->exec($argv[1]); + $licenses->exec($argv[1], isset($argv[2]) ? $argv[1] : false); } else { $licenses->exec([ '../apps/dav', |