Skip to content

Commit

Permalink
GitRepository: fixed parameters in git log commands (closes #70)
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed Jun 15, 2021
1 parent dc084fc commit 3250512
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
18 changes: 9 additions & 9 deletions src/GitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public function commit($message, $params = NULL)
*/
public function getLastCommitId()
{
$result = $this->run('log', '--pretty=format:"%H"', '-n', '1');
$result = $this->run('log', '--pretty=format:%H', '-n', '1');
$lastLine = $result->getOutputLastLine();
return new CommitId((string) $lastLine);
}
Expand All @@ -377,39 +377,39 @@ public function getCommit($commitId)
}

// subject
$result = $this->run('log', '-1', $commitId, '--format="%s"');
$result = $this->run('log', '-1', $commitId, '--format=%s');
$subject = rtrim($result->getOutputAsString());

// body
$result = $this->run('log', '-1', $commitId, '--format="%b"');
$result = $this->run('log', '-1', $commitId, '--format=%b');
$body = rtrim($result->getOutputAsString());

// author email
$result = $this->run('log', '-1', $commitId, '--format="%ae"');
$result = $this->run('log', '-1', $commitId, '--format=%ae');
$authorEmail = rtrim($result->getOutputAsString());

// author name
$result = $this->run('log', '-1', $commitId, '--format="%an"');
$result = $this->run('log', '-1', $commitId, '--format=%an');
$authorName = rtrim($result->getOutputAsString());

// author date
$result = $this->run('log', '-1', $commitId, '--pretty="format:%ad"', '--date=iso-strict');
$result = $this->run('log', '-1', $commitId, '--pretty=format:%ad', '--date=iso-strict');
$authorDate = \DateTimeImmutable::createFromFormat(\DateTime::ATOM, (string) $result->getOutputLastLine());

if (!($authorDate instanceof \DateTimeImmutable)) {
throw new GitException('Failed fetching of commit author date.', 0, NULL, $result);
}

// committer email
$result = $this->run('log', '-1', $commitId, '--format="%ce"');
$result = $this->run('log', '-1', $commitId, '--format=%ce');
$committerEmail = rtrim($result->getOutputAsString());

// committer name
$result = $this->run('log', '-1', $commitId, '--format="%cn"');
$result = $this->run('log', '-1', $commitId, '--format=%cn');
$committerName = rtrim($result->getOutputAsString());

// committer date
$result = $this->run('log', '-1', $commitId, '--pretty="format:%cd"', '--date=iso-strict');
$result = $this->run('log', '-1', $commitId, '--pretty=format:%cd', '--date=iso-strict');
$committerDate = \DateTimeImmutable::createFromFormat(\DateTime::ATOM, (string) $result->getOutputLastLine());

if (!($committerDate instanceof \DateTimeImmutable)) {
Expand Down
34 changes: 17 additions & 17 deletions tests/GitPhp/GitRepository.getCommit.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,63 +12,63 @@ test(function () {

// last commit ID
$runner->assert(
['log', '--pretty=format:"%H"', '-n', '1'],
['log', '--pretty=format:%H', '-n', '1'],
[],
['734713bc047d87bf7eac9674765ae793478c50d3']
);

// commit subject
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format="%s"'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format=%s'],
[],
['init commit', '', '']
);

// commit body
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format="%b"'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format=%b'],
[],
['', '', ''] // 3 empty lines
);

// author email
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format="%ae"'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format=%ae'],
[],
['[email protected]']
);

// author name
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format="%an"'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format=%an'],
[],
['John Example']
);

// author date
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--pretty="format:%ad"', '--date=iso-strict'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--pretty=format:%ad', '--date=iso-strict'],
[],
["2021-04-29T15:55:09+00:00"]
);

// committer email
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format="%ce"'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format=%ce'],
[],
["[email protected]"]
);

// committer name
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format="%cn"'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format=%cn'],
[],
["John Committer"]
);

// committter date
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--pretty="format:%cd"', '--date=iso-strict'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--pretty=format:%cd', '--date=iso-strict'],
[],
['2021-04-29T17:55:09+02:00']
);
Expand Down Expand Up @@ -98,56 +98,56 @@ test(function () {

// commit subject
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format="%s"'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format=%s'],
[],
['init commit', '', '']
);

// commit body
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format="%b"'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format=%b'],
[],
['first line', 'second line', '', ''] // + 2 empty lines
);

// author email
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format="%ae"'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format=%ae'],
[],
['[email protected]']
);

// author name
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format="%an"'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format=%an'],
[],
['John Example']
);

// author date
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--pretty="format:%ad"', '--date=iso-strict'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--pretty=format:%ad', '--date=iso-strict'],
[],
["2021-04-29T15:55:09+00:00"]
);

// committer email
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format="%ce"'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format=%ce'],
[],
["[email protected]"]
);

// committer name
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format="%cn"'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--format=%cn'],
[],
["John Committer"]
);

// committter date
$runner->assert(
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--pretty="format:%cd"', '--date=iso-strict'],
['log', '-1', '734713bc047d87bf7eac9674765ae793478c50d3', '--pretty=format:%cd', '--date=iso-strict'],
[],
['2021-04-29T17:55:09+02:00']
);
Expand Down
2 changes: 1 addition & 1 deletion tests/GitPhp/GitRepository.getLastCommitId.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ $runner = new AssertRunner(__DIR__);
$git = new Git($runner);

$runner->assert(
['log', '--pretty=format:"%H"', '-n', '1'],
['log', '--pretty=format:%H', '-n', '1'],
[],
['734713bc047d87bf7eac9674765ae793478c50d3']
);
Expand Down

0 comments on commit 3250512

Please sign in to comment.