From 5eeed89abb02bb1d35c113b38b3c0081c4adc869 Mon Sep 17 00:00:00 2001 From: Austin Kregel Date: Sat, 20 Mar 2021 15:22:01 -0400 Subject: [PATCH 1/3] Add blame finder call, and reorder the class... And clean up some of the docs --- src/Codeframe.php | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/Codeframe.php b/src/Codeframe.php index ab423b4..6a8d2a0 100644 --- a/src/Codeframe.php +++ b/src/Codeframe.php @@ -28,36 +28,27 @@ class Codeframe */ public $code; - /** - * @return string - */ + public function __construct(string $file, int $line, array $code, string $frame) + { + $this->setFile($file); + $this->line = $line; + $this->code = $code; + $this->frame = $frame; + } + public function getFile(): string { return $this->file; } - /** - * @param string $file - * @return Codeframe - */ public function setFile(string $file): Codeframe { $this->file = $file; return $this; } - /** - * StackAndCode constructor. - * @param string $file - * @param int $line - * @param array $code - * @param string $frame - */ - public function __construct(string $file, int $line, array $code, string $frame) + public function blame(): Blame { - $this->setFile($file); - $this->line = $line; - $this->code = $code; - $this->frame = $frame; + return (new GitBlameFinder)->blame($this->file)[$this->line]; } } From f2e1362dac5c3474fc72b1ecc1cab8eddb00c438 Mon Sep 17 00:00:00 2001 From: Austin Kregel Date: Sat, 20 Mar 2021 15:23:31 -0400 Subject: [PATCH 2/3] Add blame class --- src/Blame.php | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/Blame.php diff --git a/src/Blame.php b/src/Blame.php new file mode 100644 index 0000000..aa1a6be --- /dev/null +++ b/src/Blame.php @@ -0,0 +1,131 @@ +hash = $hash; + return $this; + } + + public function setAuthor($author) + { + $this->author = $author; + return $this; + } + + public function setAuthorEmail($authorEmail) + { + $this->authorEmail = trim($authorEmail, '<>'); + return $this; + } + + public function setAuthorTimezone($authorTimezone) + { + $this->authorTimezone = $authorTimezone; + return $this; + } + + public function setAuthorTime($authorTime) + { + $this->authorTime = $authorTime; + return $this; + } + + public function setCommitterTime($committerTime) + { + $this->committerTime = $committerTime; + return $this; + } + + public function setCommitter($committer) + { + $this->committer = $committer; + return $this; + } + + public function setCommitterEmail($committerEmail) + { + $this->committerEmail = trim($committerEmail, '<>');; + return $this; + } + + public function setCommitterTimezone($committerTimezone) + { + $this->committerTimezone = $committerTimezone; + return $this; + } + + public function setSummary($summary) + { + $this->summary = $summary; + return $this; + } + + public function setFilename($filename) + { + $this->filename = $filename; + return $this; + } + + public function setContents($contents) + { + $this->contents = $contents; + return $this; + } + + public function setPrevious($previous) + { + $this->previous = $previous; + return $this; + } + + public function toArray(): array + { + try { + return [ + 'date' => new \DateTime(date('Y-m-d H:i:s', $this->authorTime), new \DateTimeZone((int)trim($this->authorTimezone, '0'))), + 'author' => [ + 'name' => $this->author, + 'email' => $this->authorEmail, + ], + 'summary' => $this->summary, + 'contents' => $this->contents, + 'previous' => $this->previous, + ]; + } catch (\Throwable $e) { + return [ + 'date' => new \DateTime(date('Y-m-d H:i:s', $this->authorTime)), + 'tz' => (int)trim($this->authorTimezone, '0'), + 'author' => [ + 'name' => $this->author, + 'email' => $this->authorEmail, + ], + 'summary' => $this->summary, + 'contents' => $this->contents, + 'previous' => $this->previous, + ]; + } + } +} From be4a6972f91a6f3ab7b19d60d8b24d3b575fcf36 Mon Sep 17 00:00:00 2001 From: Austin Kregel Date: Sat, 20 Mar 2021 15:24:04 -0400 Subject: [PATCH 3/3] Add git blame finder service to process the output of the git poreclain format --- src/GitBlameFinder.php | 95 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/GitBlameFinder.php diff --git a/src/GitBlameFinder.php b/src/GitBlameFinder.php new file mode 100644 index 0000000..cc033c2 --- /dev/null +++ b/src/GitBlameFinder.php @@ -0,0 +1,95 @@ + $line) { + // We're only going to do logic based off the location of the `author` header. + if (stripos($line, 'author ') !== false) { + $indexes[] = $index - 1; + } + } + + foreach ($indexes as $index => $startingCommitLine) { + if(!isset($items[$index])) { + $items[$index] = []; + } + + $isFileContents = false; + + $blameObject = new Blame(); + for($i = $startingCommitLine; $i < ($indexes[$index + 1] ?? $count); $i ++) { + if ($isFileContents) { + $blameObject->setContents($blame[$i]); + continue; + } + + [$start, $end] = explode(' ', $blame[$i], 2); + + switch ($start) { + case 'author': + $blameObject->setAuthor($end); + break; + case 'author-mail': + $blameObject->setAuthorEmail($end); + break; + case 'author-time': + $blameObject->setAuthorTime($end); + break; + case 'author-tz': + $blameObject->setAuthorTimezone($end); + break; + case 'committer': + $blameObject->setCommitter($end); + break; + case 'committer-time': + $blameObject->setCommitterTime($end); + break; + case 'committer-tz': + $blameObject->setCommitterTimezone($end); + break; + case 'committer-mail': + $blameObject->setCommitterEmail($end); + break; + case 'summary': + $blameObject->setSummary($end); + break; + case 'filename': + $blameObject->setFilename($end); + break; + case 'previous': + $blameObject->setPrevious($end); + break; + default: + if (strlen($start) === 40) { + $blameObject->setHash($blame[$i]); + } + } + + if ($start === 'filename') { + $isFileContents = true; + } + } + $items[] = $blameObject; + } + + return $items; + } +}