Skip to content

Commit 559390b

Browse files
committed
Fixed Commit::getShortMessage
Do not cache result since the method took arguments
1 parent 7448f5d commit 559390b

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

src/Gitonomy/Git/Commit.php

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,6 @@ class Commit
111111
*/
112112
private $message;
113113

114-
/**
115-
* Short message of the commit.
116-
*
117-
* @var string
118-
*/
119-
private $shortMessage;
120-
121114
/**
122115
* Constructor.
123116
*
@@ -266,36 +259,47 @@ public function getLastModification($path, $lastHash = null)
266259
}
267260

268261
/**
269-
* Returns the first line of the commit, and the first 80 characters.
262+
* Returns the first line of the commit, and the first 50 characters.
263+
*
264+
* Ported from https://github.com/fabpot/Twig-extensions/blob/d67bc7e69788795d7905b52d31188bbc1d390e01/lib/Twig/Extensions/Extension/Text.php#L52-L109
265+
*
266+
* @param integer $length
267+
* @param boolean $preserve
268+
* @param string $separator
270269
*
271270
* @return string
272271
*/
273-
public function getShortMessage($limit = 50)
272+
public function getShortMessage($length = 50, $preserve = false, $separator = '...')
274273
{
275274
$this->initialize();
276275

277-
if (null !== $this->shortMessage) {
278-
return $this->shortMessage;
279-
}
276+
$message = $this->getSubjectMessage();
280277

281-
$pos = mb_strpos($this->message, "\n");
282-
$length = mb_strlen($this->message);
278+
if (function_exists('mb_substr')) {
279+
if (mb_strlen($message) > $length) {
280+
if ($preserve) {
281+
if (false !== ($breakpoint = mb_strpos($message, ' ', $length))) {
282+
$length = $breakpoint;
283+
}
284+
}
283285

284-
if (false === $pos) {
285-
if ($length < $limit) {
286-
$shortMessage = $this->message;
287-
} else {
288-
$shortMessage = mb_substr($this->message, 0, $limit).'...';
286+
return rtrim(mb_substr($message, 0, $length)) . $separator;
289287
}
288+
289+
return $message;
290290
} else {
291-
if ($pos < $limit) {
292-
$shortMessage = mb_substr($this->message, 0, $pos);
293-
} else {
294-
$shortMessage = mb_substr($this->message, 0, $limit).'...';
291+
if (strlen($message) > $length) {
292+
if ($preserve) {
293+
if (false !== ($breakpoint = strpos($message, ' ', $length))) {
294+
$length = $breakpoint;
295+
}
296+
}
297+
298+
return rtrim(substr($message, 0, $length)) . $separator;
295299
}
296-
}
297300

298-
return $this->shortMessage = $shortMessage;
301+
return $message;
302+
}
299303
}
300304

301305
/**

tests/Gitonomy/Git/Tests/CommitTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ public function testGetSubjectMessage($repository)
182182
{
183183
$commit = $repository->getCommit(self::LONGMESSAGE_COMMIT);
184184

185-
$this->assertEquals('Fixed permissions of test.sh. Basically I just run bash command.', $commit->getSubjectMessage());
185+
$this->assertEquals('Fixed perm!!!', $commit->getShortMessage(10, false, '!!!'));
186+
$this->assertEquals('Fixed permissions!!!', $commit->getShortMessage(10, true, '!!!'));
186187
}
187188

188189
/**

0 commit comments

Comments
 (0)