Skip to content
Snippets Groups Projects
Commit 049e3473 authored by Joas Schilling's avatar Joas Schilling
Browse files

Allow the script to run for different git roots

parent c6b24573
No related branches found
No related tags found
No related merge requests found
...@@ -48,17 +48,25 @@ EOD; ...@@ -48,17 +48,25 @@ EOD;
$this->licenseText = str_replace('@YEAR@', date("Y"), $this->licenseText); $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)) { if (is_array($folder)) {
foreach($folder as $f) { foreach($folder as $f) {
$this->exec($f); $this->exec($f, $gitRoot);
} }
return; return;
} }
if ($gitRoot !== false && substr($gitRoot, -1) !== '/') {
$gitRoot .= '/';
}
if (is_file($folder)) { if (is_file($folder)) {
$this->handleFile($folder); $this->handleFile($folder, $gitRoot);
return; return;
} }
...@@ -81,7 +89,7 @@ EOD; ...@@ -81,7 +89,7 @@ EOD;
foreach ($iterator as $file) { foreach ($iterator as $file) {
/** @var SplFileInfo $file */ /** @var SplFileInfo $file */
$this->handleFile($file); $this->handleFile($file, $gitRoot);
} }
} }
...@@ -103,14 +111,14 @@ With help from many libraries and frameworks including: ...@@ -103,14 +111,14 @@ With help from many libraries and frameworks including:
file_put_contents(__DIR__.'/../AUTHORS', $template); file_put_contents(__DIR__.'/../AUTHORS', $template);
} }
function handleFile($path) { function handleFile($path, $gitRoot) {
$source = file_get_contents($path); $source = file_get_contents($path);
if ($this->isMITLicensed($source)) { if ($this->isMITLicensed($source)) {
echo "MIT licensed file: $path" . PHP_EOL; echo "MIT licensed file: $path" . PHP_EOL;
return; return;
} }
$source = $this->eatOldLicense($source); $source = $this->eatOldLicense($source);
$authors = $this->getAuthors($path); $authors = $this->getAuthors($path, $gitRoot);
$license = str_replace('@AUTHORS@', $authors, $this->licenseText); $license = str_replace('@AUTHORS@', $authors, $this->licenseText);
$source = "<?php" . PHP_EOL . $license . PHP_EOL . $source; $source = "<?php" . PHP_EOL . $license . PHP_EOL . $source;
...@@ -136,6 +144,7 @@ With help from many libraries and frameworks including: ...@@ -136,6 +144,7 @@ With help from many libraries and frameworks including:
/** /**
* @param string $source * @param string $source
* @return string
*/ */
private function eatOldLicense($source) { private function eatOldLicense($source) {
$lines = explode(PHP_EOL, $source); $lines = explode(PHP_EOL, $source);
...@@ -167,10 +176,18 @@ With help from many libraries and frameworks including: ...@@ -167,10 +176,18 @@ With help from many libraries and frameworks including:
return implode(PHP_EOL, $lines); 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 // 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")); $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"); $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 = explode(PHP_EOL, $out);
$authors = array_filter($authors, function($author) { $authors = array_filter($authors, function($author) {
...@@ -189,7 +206,7 @@ With help from many libraries and frameworks including: ...@@ -189,7 +206,7 @@ With help from many libraries and frameworks including:
$licenses = new Licenses; $licenses = new Licenses;
if (isset($argv[1])) { if (isset($argv[1])) {
$licenses->exec($argv[1]); $licenses->exec($argv[1], isset($argv[2]) ? $argv[1] : false);
} else { } else {
$licenses->exec([ $licenses->exec([
'../apps/dav', '../apps/dav',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment