From 70d499a25ab0b273a6d49e66d5acd51a9c036c09 Mon Sep 17 00:00:00 2001 From: "Sven Carstens [UDG]" Date: Thu, 16 Mar 2017 14:24:08 +0100 Subject: [PATCH 1/2] [FEATURE] Ask github api for commits if we have no local .git directory. --- src/BaseCommand.php | 57 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/src/BaseCommand.php b/src/BaseCommand.php index 8eedb8c..a2ccf19 100644 --- a/src/BaseCommand.php +++ b/src/BaseCommand.php @@ -33,7 +33,7 @@ protected function packagePaths() /** * Return an array with lockFrom, lockTo, and rangeArg */ - protected function getGitArguments(InputInterface $input, OutputInterface $output) + protected function getGitArguments(InputInterface $input, OutputInterface $output) { $from = $input->getArgument('sha-from'); $to = $input->getArgument('sha-to'); @@ -69,7 +69,7 @@ protected function getGitArguments(InputInterface $input, OutputInterface $outpu $rangeArg = escapeshellarg("$from..$to"); } else { $rangeArg = escapeshellarg("$from"); - } + } return array($lockFrom, $lockTo, $rangeArg); } @@ -105,6 +105,7 @@ protected function reposFromLockfile($lockContent) protected function exec($argument, InputInterface $input, OutputInterface $output) { list($lockFrom, $lockTo, $rangeArg) = $this->getGitArguments($input, $output); + $output->writeln("Project differences"); $cmdProcess = new Process("git $argument $rangeArg"); @@ -126,15 +127,47 @@ protected function exec($argument, InputInterface $input, OutputInterface $outpu $path = $packagePaths[$package]; $output->writeln("$package in $path"); - if (!is_dir("$path/.git")) { - $output->writeln('Not a .git repo'.PHP_EOL); - continue; - } - - $gitDirArg = escapeshellarg("--git-dir=$path/.git"); - $shaArg = escapeshellarg($reposFrom[$package]['reference'] . '..' . $info['reference']); - $cmd = "git $gitDirArg $argument $shaArg"; - $this->runCommand($cmd, $gitDirArg, $output); + if (!is_dir("$path/.git")) { + // check for github + if (preg_match('|https://github.com/([^/]+)/([^/]+).git|',$info['url'],$match)) { + // take commit message from the github api + $compareUrl = 'https://api.github.com/repos/'.$match[1].'/'.$match[2].'/compare/'.$reposFrom[$package]['reference'].'...'. $info['reference']; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/vnd.github.v3+json','User-Agent: https://github.com/mtheunissen82/composer-diff')); + #curl_setopt($ch, CURLOPT_USERPWD, 'test:test' ); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_URL, $compareUrl ); + $result = curl_exec($ch); + + $compare = json_decode($result); + if (isset($compare->commits) && is_array($compare->commits)) { + foreach ($compare->commits as $commit) { + // take first line from commit message + $output->writeln(strtok($commit->commit->message,"\n")); + } + $output->writeln(''); + } else { + $output->writeln("Error reading commits from github " . $compareUrl . ""); + if ($compare && isset($compare->message)) { + // error message from github (authentication, rate limiting) + $output->writeln("" . trim($compare->message) . ""); + } else { + // general error + $output->writeln("" . trim($result) . ""); + } + } + } else { + // @TODO maybe ask packagist + $output->writeln('Not a .git repo'.PHP_EOL); + continue; + } + } else { + $gitDirArg = escapeshellarg("--git-dir=$path/.git"); + $shaArg = escapeshellarg($reposFrom[$package]['reference'] . '..' . $info['reference']); + $cmd = "git $gitDirArg $argument $shaArg"; + $this->runCommand($cmd, $gitDirArg, $output); + } } } } @@ -166,4 +199,4 @@ protected function runCommand($cmd, $gitDirArg, OutputInterface $output) } $output->writeln(trim($diff->getOutput()) . PHP_EOL); } -} \ No newline at end of file +} From c74ae01b419d7c966c5b421a0bebc089acf5fbea Mon Sep 17 00:00:00 2001 From: "Sven Carstens [UDG]" Date: Thu, 16 Mar 2017 14:37:57 +0100 Subject: [PATCH 2/2] [FEATURE] Add newline for more readable output --- src/BaseCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BaseCommand.php b/src/BaseCommand.php index a2ccf19..51eaeb6 100644 --- a/src/BaseCommand.php +++ b/src/BaseCommand.php @@ -151,10 +151,10 @@ protected function exec($argument, InputInterface $input, OutputInterface $outpu $output->writeln("Error reading commits from github " . $compareUrl . ""); if ($compare && isset($compare->message)) { // error message from github (authentication, rate limiting) - $output->writeln("" . trim($compare->message) . ""); + $output->writeln("" . trim($compare->message) . "" . PHP_EOL); } else { // general error - $output->writeln("" . trim($result) . ""); + $output->writeln("" . trim($result) . "" . PHP_EOL); } } } else {