Skip to content

Commit

Permalink
feat: add commit parent class with raw metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocesarato committed Jan 24, 2021
1 parent 849f43d commit 962e0f7
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function generate(InputInterface $input, SymfonyStyle $output): int
// Get all commits information
$commits = [];
foreach ($commitsRaw as $commitRaw) {
$commit = new Commit\Conventional($commitRaw);
$commit = Commit\Conventional::fromCommit($commitRaw);

// Not a conventional commit
if (!$commit->isValid()) {
Expand All @@ -259,7 +259,7 @@ public function generate(InputInterface $input, SymfonyStyle $output): int
}
// Add commit
if (!$ignore) {
$commits[] = new Commit\Conventional($commit);
$commits[] = $commit;
}
}

Expand Down
218 changes: 218 additions & 0 deletions src/Commit/Commit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?php

namespace ConventionalChangelog\Commit;

use ConventionalChangelog\Helper\Formatter;
use ConventionalChangelog\Type\Stringable;
use DateTime;

class Commit implements Stringable
{
/**
* Raw content.
*
* @var string
*/
protected $raw;

/**
* Sha hash.
*
* @var string
*/
protected $hash;

/**
* @var DateTime
*/
protected $authorDate;

/**
* @var string
*/
protected $authorName;

/**
* @var string
*/
protected $authorEmail;

/**
* @var DateTime
*/
protected $committerDate;

/**
* @var string
*/
protected $committerName;

/**
* @var string
*/
protected $committerEmail;

public function __construct(string $commit = null)
{
// New commit or empty commit
if (empty($commit)) {
return;
}

$raw = Formatter::clean($commit);
$this->setRaw($raw);
}

/**
* From array.
*
* @throws \Exception
*/
public function fromArray(array $array)
{
if (isset($array['raw'])) {
$this->setRaw($array['raw']);
}
if (isset($array['hash'])) {
$this->setHash($array['hash']);
}
if (isset($array['authorName'])) {
$this->setAuthorName($array['authorName']);
}
if (isset($array['authorEmail'])) {
$this->setAuthorEmail($array['authorEmail']);
}
if (isset($array['authorDate'])) {
$date = new DateTime($array['authorDate']);
$this->setAuthorDate($date);
}
if (isset($array['committerName'])) {
$this->setCommitterName($array['committerName']);
}
if (isset($array['committerEmail'])) {
$this->setCommitterEmail($array['committerEmail']);
}
if (isset($array['committerDate'])) {
$date = new DateTime($array['committerDate']);
$this->setCommitterDate($date);
}

return $this;
}

/**
* Check if is valid SHA-1.
*/
protected function isValidHash(string $hash)
{
return (bool)preg_match('/^[0-9a-f]{40}$/i', $hash);
}

public function setRaw(string $raw): self
{
$this->raw = $raw;

return $this;
}

public function setHash(string $hash): self
{
if ($this->isValidHash($hash)) {
$this->hash = $hash;
}

return $this;
}

public function getRaw(): ?string
{
return $this->raw;
}

public function getHash(): ?string
{
return $this->hash;
}

public function getShortHash(): string
{
return substr($this->hash, 0, 6);
}

public function getAuthorDate(): DateTime
{
return $this->authorDate;
}

public function setAuthorDate(DateTime $authorDate): Commit
{
$this->authorDate = $authorDate;

return $this;
}

public function getAuthorName(): string
{
return $this->authorName;
}

public function setAuthorName(string $authorName): Commit
{
$this->authorName = $authorName;

return $this;
}

public function getAuthorEmail(): string
{
return $this->authorEmail;
}

public function setAuthorEmail(string $authorEmail): Commit
{
$this->authorEmail = $authorEmail;

return $this;
}

public function getCommitterDate(): DateTime
{
return $this->committerDate;
}

public function setCommitterDate(DateTime $committerDate): Commit
{
$this->committerDate = $committerDate;

return $this;
}

public function getCommitterName(): string
{
return $this->committerName;
}

public function setCommitterName(string $committerName): Commit
{
$this->committerName = $committerName;

return $this;
}

public function getCommitterEmail(): string
{
return $this->committerEmail;
}

public function setCommitterEmail(string $committerEmail): Commit
{
$this->committerEmail = $committerEmail;

return $this;
}

public function __toString(): string
{
return $this->getRaw();
}
}
97 changes: 32 additions & 65 deletions src/Commit/Conventional.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,12 @@
namespace ConventionalChangelog\Commit;

use ConventionalChangelog\Helper\Formatter;
use ConventionalChangelog\Type\Stringable;

class Conventional implements Stringable
class Conventional extends Commit
{
protected const PATTERN_HEADER = "/^(?'type'[a-z]+)(\((?'scope'.+)\))?(?'important'[!]?)[:][[:blank:]](?'description'.+)/iums";
protected const PATTERN_FOOTER = "/(?'token'^([a-z0-9_-]+|BREAKING[[:blank:]]CHANGES?))(?'value'([:][[:blank:]]|[:]?[[:blank:]][#](?=\w)).*?)$/iums";

/**
* Raw content.
*
* @var string
*/
protected $raw;

/**
* Sha hash.
*
Expand Down Expand Up @@ -64,38 +56,21 @@ class Conventional implements Stringable
*/
protected $footers = [];

public function __construct(string $commit = '')
public function __construct(?string $commit = null)
{
parent::__construct($commit);

// New commit or empty commit
if (empty($commit)) {
return;
}

$raw = Formatter::clean($commit);
$this->setRaw($raw);

// Not parsable
if (!$this->isValid()) {
return;
}

$rows = explode("\n", $this->raw);
$count = count($rows);
// Commit info
$hash = trim($rows[$count - 1]);
if ($this->isValidHash($hash)) {
$this->hash = $hash;
}
$header = $rows[0];
$message = '';
// Get message
foreach ($rows as $i => $row) {
if ($i !== 0 && $i !== $count) {
$message .= $row . "\n";
}
}
$this->parseHeader($header);
$this->parseMessage($message);
$this->__wakeup();
}

/**
Expand Down Expand Up @@ -132,11 +107,15 @@ protected function parseMessage(string $message)
}

/**
* Check if is valid SHA-1.
* From commit.
*
* @return self
*/
protected function isValidHash(string $hash)
public static function fromCommit(Commit $commit)
{
return (bool)preg_match('/^[0-9a-f]{40}$/i', $hash);
return unserialize(
preg_replace('/^O:\d+:"[^"]++"/', 'O:' . strlen(__CLASS__) . ':"' . __CLASS__ . '"', serialize($commit))
);
}

/**
Expand All @@ -147,21 +126,6 @@ public function isValid(): bool
return (bool)preg_match(self::PATTERN_HEADER, $this->raw);
}

public function getRaw(): ?string
{
return $this->raw;
}

public function getHash(): ?string
{
return $this->hash;
}

public function getShortHash(): string
{
return substr($this->hash, 0, 6);
}

public function getType(): Type
{
return $this->type;
Expand Down Expand Up @@ -246,23 +210,6 @@ public function getMessage()
return $this->body . "\n\n" . $footer;
}

/**
* @return Conventional
*/
public function setRaw(string $raw): self
{
$this->raw = $raw;

return $this;
}

public function setHash(string $hash): self
{
$this->hash = $hash;

return $this;
}

public function setType(string $type): self
{
$this->type = new Type($type);
Expand Down Expand Up @@ -305,6 +252,26 @@ public function setFooters(array $footers): self
return $this;
}

public function __wakeup()
{
$rows = explode("\n", $this->raw);
$count = count($rows);
// Commit info
$hash = trim($rows[$count - 1]);
$this->setHash($hash);

$header = $rows[0];
$message = '';
// Get message
foreach ($rows as $i => $row) {
if ($i !== 0 && $i !== $count) {
$message .= $row . "\n";
}
}
$this->parseHeader($header);
$this->parseMessage($message);
}

public function __toString(): string
{
$header = $this->getHeader();
Expand Down
Loading

0 comments on commit 962e0f7

Please sign in to comment.