Skip to content
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

Allow license.php for app repos aswell #22340

Merged
merged 2 commits into from
Feb 17, 2016
Merged
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
62 changes: 53 additions & 9 deletions build/license.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*/
class Licenses
{
protected $paths = array();
protected $paths = [];
protected $mailMap = [];
public $authors = [];

public function __construct() {
Expand Down Expand Up @@ -48,17 +49,25 @@ public function __construct() {
$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;
}

Expand All @@ -81,7 +90,7 @@ function exec($folder) {

foreach ($iterator as $file) {
/** @var SplFileInfo $file */
$this->handleFile($file);
$this->handleFile($file, $gitRoot);
}
}

Expand All @@ -103,14 +112,14 @@ function writeAuthorsFile() {
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;
Expand All @@ -136,6 +145,7 @@ private function isMITLicensed($source) {

/**
* @param string $source
* @return string
*/
private function eatOldLicense($source) {
$lines = explode(PHP_EOL, $source);
Expand Down Expand Up @@ -167,10 +177,18 @@ private function eatOldLicense($source) {
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) {
Expand All @@ -179,17 +197,43 @@ private function getAuthors($file) {
'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',
Expand Down