diff --git a/src/Git/Commit.php b/src/Git/Commit.php index 973e645..46d61d0 100644 --- a/src/Git/Commit.php +++ b/src/Git/Commit.php @@ -4,6 +4,7 @@ use ConventionalChangelog\Git\Commit\Body; use ConventionalChangelog\Git\Commit\Footer; +use ConventionalChangelog\Git\Commit\Mention; use ConventionalChangelog\Git\Commit\Subject; use ConventionalChangelog\Helper\Formatter; use ConventionalChangelog\Type\Stringable; @@ -12,6 +13,7 @@ class Commit implements Stringable { protected const PATTERN_FOOTER = "/(?^([a-z0-9_-]+|BREAKING[[:blank:]]CHANGES?))(?([:][[:blank:]]|[:]?[[:blank:]][#](?=\w)).*?)$/iums"; + protected const PATTERN_MENTION = "/(?:^|\s+)(?@(?[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}))(?:$|\s+)/smi"; /** * Raw content. @@ -41,6 +43,13 @@ class Commit implements Stringable */ protected $footers = []; + /** + * User Mentions. + * + * @var Mention[] + */ + protected $mentions = []; + /** * Sha hash. * @@ -246,6 +255,7 @@ public function getBody(): Body public function setBody(string $body): self { + $body = Formatter::clean($body); $footers = []; if (preg_match_all(self::PATTERN_FOOTER, $body, $matches, PREG_SET_ORDER, 0)) { foreach ($matches as $match) { @@ -257,6 +267,8 @@ public function setBody(string $body): self } } $this->setFooters($footers); + + $body = Formatter::clean($body); $this->body = new Body($body); return $this; @@ -289,6 +301,30 @@ public function getFooters(): array return $this->footers; } + /** + * Set mentions. + * + * @param Mention[] $mentions + * + * @return $this + */ + public function setMentions(array $mentions): self + { + $this->mentions = array_unique($mentions); + + return $this; + } + + /** + * Get mentions. + * + * @return Mention[] + */ + public function getMentions(): array + { + return $this->mentions; + } + /** * Parse raw commit. */ @@ -297,16 +333,23 @@ protected function parse() $rows = explode("\n", $this->raw); $subject = $rows[0]; - $this->setSubject($subject); - $body = ''; - // Get message foreach ($rows as $i => $row) { if ($i !== 0) { $body .= $row . "\n"; } } - $this->setBody($body); + + $mentions = []; + if (preg_match_all(self::PATTERN_MENTION, $this->raw, $matches)) { + foreach ($matches['user'] as $match) { + $mentions[] = new Mention($match); + } + } + + $this->setSubject($subject) + ->setBody($body) + ->setMentions($mentions); } public function __wakeup() diff --git a/src/Git/ConventionalCommit.php b/src/Git/ConventionalCommit.php index 08c2d88..53054c4 100644 --- a/src/Git/ConventionalCommit.php +++ b/src/Git/ConventionalCommit.php @@ -4,23 +4,14 @@ use ConventionalChangelog\Configuration; use ConventionalChangelog\Git\Commit\Description; -use ConventionalChangelog\Git\Commit\Mention; use ConventionalChangelog\Git\Commit\Reference; use ConventionalChangelog\Git\Commit\Scope; use ConventionalChangelog\Git\Commit\Type; -use ConventionalChangelog\Helper\Formatter; class ConventionalCommit extends Commit { protected const PATTERN_HEADER = "/^(?[a-z]+)(?[!]?)(\((?.+)\))?(?[!]?)[:][[:blank:]](?.+)/iums"; - /** - * Sha hash. - * - * @var string - */ - protected $hash; - /** * Type. * @@ -49,57 +40,12 @@ class ConventionalCommit extends Commit */ protected $description; - /** - * User Mentions. - * - * @var Mention[] - */ - protected $mentions = []; - public function __construct(?string $commit = null) { parent::__construct($commit); $this->parse(); } - /** - * Parse header. - */ - protected function parseHeader(string $header) - { - preg_match(self::PATTERN_HEADER, $header, $matches); - $this->setType((string)$matches['type']) - ->setScope((string)$matches['scope']) - ->setBreakingChange(!empty($matches['breaking_before'] || !empty($matches['breaking_after'])) ? true : false) - ->setDescription((string)$matches['description']); - } - - /** - * Parse message. - */ - protected function parseMessage(string $message) - { - $body = Formatter::clean($message); - - $mentions = []; - if (preg_match_all('/(?:^|\s+)(?@(?[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}))(?:$|\s+)/smi', $this->raw, $matches)) { - foreach ($matches['user'] as $match) { - $mentions[] = new Mention($match); - } - } - - if (preg_match_all(self::PATTERN_FOOTER, $body, $matches, PREG_SET_ORDER, 0)) { - foreach ($matches as $match) { - $footer = $match[0]; - $body = str_replace($footer, '', $body); - } - } - - $body = Formatter::clean($body); - $this->setBody($body) - ->setMentions($mentions); - } - /** * From commit. */ @@ -176,30 +122,6 @@ public function getReferences(): array return array_unique($refs); } - /** - * Set mentions. - * - * @param Mention[] $mentions - * - * @return $this - */ - public function setMentions(array $mentions): self - { - $this->mentions = array_unique($mentions); - - return $this; - } - - /** - * Get mentions. - * - * @return Mention[] - */ - public function getMentions(): array - { - return $this->mentions; - } - public function getHeader(): string { $header = $this->type; @@ -265,11 +187,20 @@ protected function parse() if (!$this->isValid()) { return; } - $header = $this->getSubject(); - $message = $this->getBody(); $this->parseHeader($header); - $this->parseMessage($message); + } + + /** + * Parse header. + */ + protected function parseHeader(string $header) + { + preg_match(self::PATTERN_HEADER, $header, $matches); + $this->setType((string)$matches['type']) + ->setScope((string)$matches['scope']) + ->setBreakingChange(!empty($matches['breaking_before'] || !empty($matches['breaking_after'])) ? true : false) + ->setDescription((string)$matches['description']); } }