Example #1
0
 public function getCommit($commit)
 {
     $logs = $this->getClient()->run($this, 'show --pretty=format:\'{"hash": "%H", "short_hash": "%h", "tree": "%T", "parent": "%P", "author": "%an", "author_email": "%ae", "date": "%at", "commiter": "%cn", "commiter_email": "%ce", "commiter_date": "%ct", "message": "%f"}\' ' . $commit);
     if (empty($logs)) {
         throw new \RuntimeException('No commit log available');
     }
     $logs = explode("\n", $logs);
     // Read commit metadata
     $data = json_decode($logs[0], true);
     $data['message'] = str_replace('-', ' ', $data['message']);
     $commit = new Commit();
     $commit->importData($data);
     unset($logs[0]);
     if (empty($logs[1])) {
         throw new \RuntimeException('No commit log available');
     }
     // Read diff logs
     foreach ($logs as $log) {
         if ('diff' === substr($log, 0, 4)) {
             if (isset($diff)) {
                 $diffs[] = $diff;
             }
             $diff = new Diff();
             preg_match('/^diff --[\\S]+ (a\\/)?([\\S]+)( b\\/)?/', $log, $name);
             $diff->setFile($name[2]);
             continue;
         }
         if ('index' === substr($log, 0, 5)) {
             $diff->setIndex($log);
             continue;
         }
         if ('---' === substr($log, 0, 3)) {
             $diff->setOld($log);
             continue;
         }
         if ('+++' === substr($log, 0, 3)) {
             $diff->setNew($log);
             continue;
         }
         // Handle binary files properly.
         if ('Binary' === substr($log, 0, 6)) {
             $m = array();
             if (preg_match('/Binary files (.+) and (.+) differ/', $log, $m)) {
                 $diff->setOld($m[1]);
                 $diff->setNew("    {$m[2]}");
             }
         }
         $diff->addLine($log);
     }
     if (isset($diff)) {
         $diffs[] = $diff;
     }
     $commit->setDiffs($diffs);
     return $commit;
 }
Example #2
0
 public function getCommit($commitHash)
 {
     $logs = $this->getClient()->run($this, 'show --pretty=format:"{\\"hash\\": \\"%H\\", \\"short_hash\\": \\"%h\\", \\"tree\\": \\"%T\\", \\"parent\\": \\"%P\\", \\"author\\": \\"%an\\", \\"author_email\\": \\"%ae\\", \\"date\\": \\"%at\\", \\"commiter\\": \\"%cn\\", \\"commiter_email\\": \\"%ce\\", \\"commiter_date\\": \\"%ct\\", \\"message\\": \\"%f\\"}" ' . $commitHash);
     if (empty($logs)) {
         throw new \RuntimeException('No commit log available');
     }
     $logs = explode("\n", $logs);
     // Read commit metadata
     $data = json_decode($logs[0], true);
     $data['message'] = str_replace('-', ' ', $data['message']);
     $commit = new Commit();
     $commit->importData($data);
     unset($logs[0]);
     if (empty($logs[1])) {
         $logs = explode("\n", $this->getClient()->run($this, 'diff ' . $commitHash . '~1..' . $commitHash));
     }
     // Read diff logs
     $lineNumOld = 0;
     $lineNumNew = 0;
     foreach ($logs as $log) {
         if ('diff' === substr($log, 0, 4)) {
             if (isset($diff)) {
                 $diffs[] = $diff;
             }
             $diff = new Diff();
             preg_match('/^diff --[\\S]+ (a\\/)?([\\S]+)( b\\/)?/', $log, $name);
             $diff->setFile($name[2]);
             continue;
         }
         if ('index' === substr($log, 0, 5)) {
             $diff->setIndex($log);
             continue;
         }
         if ('---' === substr($log, 0, 3)) {
             $diff->setOld($log);
             continue;
         }
         if ('+++' === substr($log, 0, 3)) {
             $diff->setNew($log);
             continue;
         }
         // Handle binary files properly.
         if ('Binary' === substr($log, 0, 6)) {
             $m = array();
             if (preg_match('/Binary files (.+) and (.+) differ/', $log, $m)) {
                 $diff->setOld($m[1]);
                 $diff->setNew("    {$m[2]}");
             }
         }
         if (!empty($log)) {
             switch ($log[0]) {
                 case "@":
                     // Set the line numbers
                     preg_match('/@@ -([0-9]+)/', $log, $matches);
                     $lineNumOld = $matches[1] - 1;
                     $lineNumNew = $matches[1] - 1;
                     break;
                 case "-":
                     $lineNumOld++;
                     break;
                 case "+":
                     $lineNumNew++;
                     break;
                 default:
                     $lineNumOld++;
                     $lineNumNew++;
             }
         } else {
             $lineNumOld++;
             $lineNumNew++;
         }
         $diff->addLine($log, $lineNumOld, $lineNumNew);
     }
     if (isset($diff)) {
         $diffs[] = $diff;
     }
     $commit->setDiffs($diffs);
     return $commit;
 }