diff --git a/build/license.php b/build/license.php
index ce6fceb8160a2d905abc38b1ceaf110651321d4c..d7ac4e0a143ac7f68974195e2a5841c74b2cbf7e 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',