Skip to content

Commit

Permalink
Added more type hinting & Fixed bug when parsing renamed files with s…
Browse files Browse the repository at this point in the history
…paces and/or non english-symbols (#194)

* changed repo name in the composer

* added path to the symlink RuntimeException exception-message

* added more and better typehints into PHPDocs above methods

* Regex didn't count for the rare case when the diff has quotation marks around the filenames (file names with non-english symbols for example)

* replaced uninitialized $entry variable with $element

* removed getRevisions() since revisions isn't a part of the Diff object

* removed unused $fullname in the getCommit() method

* - added more type hinting
- introduced data property to Tag
- added .idea to .gitignore

* - changes that were requested by "
continuous-integration/styleci/pr"
- fixed composer.json

* upped the number of characters that are shown to get a more verbose exception

* added type annotation for file in the Diff class

* - fixed parser to properly work with files
- with umlauts added a test
- made changes @lyrixx suggested

* - made fixes for @continuous-integration/styleci/pr

* - made one more fix for @continuous-integration/styleci/pr

* - switches quotes for @continuous-integration/styleci/pr

* added trailing \n to the gitignore
  • Loading branch information
tolik518 authored Aug 5, 2022
1 parent 793ffe5 commit 33ae0a2
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 57 deletions.
4 changes: 1 addition & 3 deletions src/Gitonomy/Git/Blame.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ public function getGroupedLines()
}

/**
* Returns all lines of the blame.
*
* @return array
* @return Line[] All lines of the blame.
*/
public function getLines()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Gitonomy/Git/Blob.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public function getHash()
}

/**
* Returns content of the blob.
*
* @throws ProcessException Error occurred while getting content of blob
*
* @return string Content of the blob.
*/
public function getContent()
{
Expand Down
18 changes: 12 additions & 6 deletions src/Gitonomy/Git/Commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Gitonomy\Git\Exception\InvalidArgumentException;
use Gitonomy\Git\Exception\ProcessException;
use Gitonomy\Git\Exception\ReferenceNotFoundException;
use Gitonomy\Git\Reference\Branch;
use Gitonomy\Git\Util\StringHelper;

/**
Expand All @@ -35,8 +36,8 @@ class Commit extends Revision
/**
* Constructor.
*
* @param Gitonomy\Git\Repository $repository Repository of the commit
* @param string $hash Hash of the commit
* @param Repository $repository Repository of the commit
* @param string $hash Hash of the commit
*/
public function __construct(Repository $repository, $hash, array $data = [])
{
Expand Down Expand Up @@ -91,6 +92,8 @@ public function getShortHash()

/**
* Returns a fixed-with short hash.
*
* @return string Short hash
*/
public function getFixedShortHash($length = 6)
{
Expand All @@ -100,7 +103,7 @@ public function getFixedShortHash($length = 6)
/**
* Returns parent hashes.
*
* @return array An array of SHA1 hashes
* @return string[] An array of SHA1 hashes
*/
public function getParentHashes()
{
Expand All @@ -110,7 +113,7 @@ public function getParentHashes()
/**
* Returns the parent commits.
*
* @return array An array of Commit objects
* @return Commit[] An array of Commit objects
*/
public function getParents()
{
Expand All @@ -132,6 +135,9 @@ public function getTreeHash()
return $this->getData('treeHash');
}

/**
* @return Tree
*/
public function getTree()
{
return $this->getData('tree');
Expand Down Expand Up @@ -184,7 +190,7 @@ public function getShortMessage($length = 50, $preserve = false, $separator = '.
/**
* Resolves all references associated to this commit.
*
* @return array An array of references (Branch, Tag, Squash)
* @return Reference[] An array of references (Branch, Tag, Squash)
*/
public function resolveReferences()
{
Expand All @@ -197,7 +203,7 @@ public function resolveReferences()
* @param bool $local set true to try to locate a commit on local repository
* @param bool $remote set true to try to locate a commit on remote repository
*
* @return array An array of Reference\Branch
* @return Reference[]|Branch[] An array of Reference\Branch
*/
public function getIncludingBranches($local = true, $remote = true)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Gitonomy/Git/CommitReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

class CommitReference
{
/**
* @var string
*/
private $hash;

public function __construct($hash)
Expand Down
12 changes: 2 additions & 10 deletions src/Gitonomy/Git/Diff/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
class Diff
{
/**
* @var array
* @var File[]
*/
protected $files;

Expand Down Expand Up @@ -62,18 +62,10 @@ public function setRepository(Repository $repository)
}
}

/**
* @return array
*/
public function getRevisions()
{
return $this->revisions;
}

/**
* Get list of files modified in the diff's revision.
*
* @return array An array of Diff\File objects
* @return File[] An array of Diff\File objects
*/
public function getFiles()
{
Expand Down
8 changes: 7 additions & 1 deletion src/Gitonomy/Git/Diff/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class File
protected $isBinary;

/**
* @var array An array of FileChange objects
* @var FileChange[] An array of FileChange objects
*/
protected $changes;

Expand Down Expand Up @@ -215,6 +215,9 @@ public function isBinary()
return $this->isBinary;
}

/**
* @return FileChange[]
*/
public function getChanges()
{
return $this->changes;
Expand All @@ -236,6 +239,9 @@ public function toArray()
];
}

/**
* @return File
*/
public static function fromArray(array $array)
{
$file = new self($array['old_name'], $array['new_name'], $array['old_mode'], $array['new_mode'], $array['old_index'], $array['new_index'], $array['is_binary']);
Expand Down
8 changes: 6 additions & 2 deletions src/Gitonomy/Git/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Gitonomy\Git\Exception\InvalidArgumentException;
use Gitonomy\Git\Exception\LogicException;
use Gitonomy\Git\Exception\RuntimeException;

/**
* Hooks collection, aggregated by repository.
Expand All @@ -23,7 +24,7 @@
class Hooks
{
/**
* @var Gitonomy\Git\Repository
* @var \Gitonomy\Git\Repository
*/
protected $repository;

Expand Down Expand Up @@ -82,7 +83,7 @@ public function setSymlink($name, $file)

$path = $this->getPath($name);
if (false === symlink($file, $path)) {
throw new RuntimeException(sprintf('Unable to create hook "%s"', $name, $path));
throw new RuntimeException(sprintf('Unable to create hook "%s" (%s)', $name, $path));
}
}

Expand Down Expand Up @@ -121,6 +122,9 @@ public function remove($name)
unlink($this->getPath($name));
}

/**
* @return string
*/
protected function getPath($name)
{
return $this->repository->getGitDir().'/hooks/'.$name;
Expand Down
6 changes: 5 additions & 1 deletion src/Gitonomy/Git/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Gitonomy\Git;

use Gitonomy\Git\Diff\Diff;
use Gitonomy\Git\Exception\ProcessException;
use Gitonomy\Git\Exception\ReferenceNotFoundException;
use Gitonomy\Git\Util\StringHelper;
Expand Down Expand Up @@ -136,6 +137,9 @@ public function setLimit($limit)
return $this;
}

/**
* @return Commit
*/
public function getSingleCommit()
{
$limit = $this->limit;
Expand All @@ -151,7 +155,7 @@ public function getSingleCommit()
}

/**
* @return array
* @return Commit[]
*/
public function getCommits()
{
Expand Down
10 changes: 6 additions & 4 deletions src/Gitonomy/Git/Parser/DiffParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function doParse()

while (!$this->isFinished()) {
// 1. title
$vars = $this->consumeRegexp('/diff --git (a\/.*) (b\/.*)\n/');
$vars = $this->consumeRegexp("/diff --git \"?(a\/.*?)\"? \"?(b\/.*?)\"?\n/");
$oldName = $vars[1];
$newName = $vars[2];
$oldIndex = null;
Expand Down Expand Up @@ -74,14 +74,15 @@ protected function doParse()
}
$this->consumeNewLine();

//verifying if the file was deleted or created
if ($this->expects('--- ')) {
$oldName = $this->consumeTo("\n");
$oldName = $this->consumeTo("\n") === '/dev/null' ? '/dev/null' : $oldName;
$this->consumeNewLine();
$this->consume('+++ ');
$newName = $this->consumeTo("\n");
$newName = $this->consumeTo("\n") === '/dev/null' ? '/dev/null' : $newName;
$this->consumeNewLine();
} elseif ($this->expects('Binary files ')) {
$vars = $this->consumeRegexp('/(.*) and (.*) differ\n/');
$vars = $this->consumeRegexp('/"?(.*?)"? and "?(.*?)"? differ\n/');
$isBinary = true;
$oldName = $vars[1];
$newName = $vars[2];
Expand All @@ -90,6 +91,7 @@ protected function doParse()

$oldName = $oldName === '/dev/null' ? null : substr($oldName, 2);
$newName = $newName === '/dev/null' ? null : substr($newName, 2);

$oldIndex = $oldIndex !== null ?: '';
$newIndex = $newIndex !== null ?: '';
$oldIndex = preg_match('/^0+$/', $oldIndex) ? null : $oldIndex;
Expand Down
2 changes: 1 addition & 1 deletion src/Gitonomy/Git/Parser/ParserBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected function consumeHash()
protected function consumeRegexp($regexp)
{
if (!preg_match($regexp.'A', $this->content, $vars, 0, $this->cursor)) {
throw new RuntimeException('No match for regexp '.$regexp.' Upcoming: '.substr($this->content, $this->cursor, 30));
throw new RuntimeException('No match for regexp '.$regexp.' Upcoming: '.substr($this->content, $this->cursor, 500));
}

$this->cursor += strlen($vars[0]);
Expand Down
9 changes: 8 additions & 1 deletion src/Gitonomy/Git/PushReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class PushReference
{
const ZERO = '0000000000000000000000000000000000000000';

/**
* @var Repository
*/
protected $repository;
/**
* @var string
*/
Expand Down Expand Up @@ -86,7 +90,7 @@ public function getAfter()
}

/**
* @return array
* @return Log
*/
public function getLog($excludes = [])
{
Expand All @@ -98,6 +102,9 @@ public function getLog($excludes = [])
));
}

/**
* @return string
*/
public function getRevision()
{
if ($this->isDelete()) {
Expand Down
19 changes: 16 additions & 3 deletions src/Gitonomy/Git/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

namespace Gitonomy\Git;

use Gitonomy\Git\Exception\ProcessException;
use Gitonomy\Git\Exception\ReferenceNotFoundException;

/**
* Reference in a Git repository.
*
Expand All @@ -29,16 +32,25 @@ public function __construct(Repository $repository, $revision, $commitHash = nul
$this->commitHash = $commitHash;
}

/**
* @return string
*/
public function getFullname()
{
return $this->revision;
}

/**
* @return void
*/
public function delete()
{
$this->repository->getReferences()->delete($this->getFullname());
}

/**
* @return string
*/
public function getCommitHash()
{
if (null !== $this->commitHash) {
Expand All @@ -55,15 +67,16 @@ public function getCommitHash()
}

/**
* Returns the commit associated to the reference.
*
* @return Commit
* @return Commit Commit associated to the reference.
*/
public function getCommit()
{
return $this->repository->getCommit($this->getCommitHash());
}

/**
* @return Commit
*/
public function getLastModification($path = null)
{
return $this->getCommit()->getLastModification($path);
Expand Down
8 changes: 5 additions & 3 deletions src/Gitonomy/Git/Reference/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
class Tag extends Reference
{
protected $data;

public function getName()
{
if (!preg_match('#^refs/tags/(.*)$#', $this->revision, $vars)) {
Expand Down Expand Up @@ -65,8 +67,8 @@ public function getCommit()
$parser = new ReferenceParser();
$parser->parse($output);

foreach ($parser->references as $row) {
list($commitHash, $fullname) = $row;
foreach ($parser->references as list($row)) {
$commitHash = $row;
}

return $this->repository->getCommit($commitHash);
Expand Down Expand Up @@ -101,7 +103,7 @@ public function getTaggerEmail()
/**
* Returns the authoring date.
*
* @return DateTime A time object
* @return \DateTime A time object
*/
public function getTaggerDate()
{
Expand Down
Loading

0 comments on commit 33ae0a2

Please sign in to comment.