<commit_before><?php
namespace Guywithnose\ReleaseNotes;

use Fhaculty\Graph\Graph;

class GithubCommitGraph
{
    /** @type \Fhaculty\Graph\Graph The commit graph by sha. */
    private $_graph;

    /**
     * Initialize the commit graph using the github API commit representation.
     *
     * @param array $commits The commits as returned from the github API.
     */
    public function __construct(array $commits)
    {
        $this->_graph = $this->_buildGraph($commits);
    }

    /**
     * Filter the commits to just the first parents (i.e., merge commits).
     *
     * @return array The commits along the left-most line of the commit graph.
     */
    public function firstParents()
    {
        $result = [];

        $current = $this->_baseCommitNode();
        while ($current->getAttribute('commit')) {
            $result[] = $current->getAttribute('commit');
            $current = $current->getVerticesEdgeTo()->getVertexFirst();
        }

        return $result;
    }

    /**
     * Creates the graph from the commits.
     *
     * The graph represents the parent-child relationship.  Each node in the graph has the id of the commit hash.
     *
     * @param array $commits The commits.
     * @return \Fhaculty\Graph\Graph The graph of commits.
     */
    private function _buildGraph(array $commits)
    {
        $graph = new Graph();

        foreach ($commits as $commit) {
            $vertex = $graph->createVertex($commit['sha'], true);
            $vertex->setAttribute('commit', $commit);
            foreach ($commit['parents'] as $parent) {
                $vertex->createEdgeTo($graph->createVertex($parent['sha'], true));
            }
        }<commit_msg>Don't die on the first commit to a repository.<commit_after><?php
namespace Guywithnose\ReleaseNotes;

use Fhaculty\Graph\Graph;

class GithubCommitGraph
{
    /** @type \Fhaculty\Graph\Graph The commit graph by sha. */
    private $_graph;

    /**
     * Initialize the commit graph using the github API commit representation.
     *
     * @param array $commits The commits as returned from the github API.
     */
    public function __construct(array $commits)
    {
        $this->_graph = $this->_buildGraph($commits);
    }

    /**
     * Filter the commits to just the first parents (i.e., merge commits).
     *
     * @return array The commits along the left-most line of the commit graph.
     */
    public function firstParents()
    {
        $result = [];

        $current = $this->_baseCommitNode();
        while ($current->getAttribute('commit')) {
            $result[] = $current->getAttribute('commit');
            $parents = $current->getVerticesEdgeTo();
            if ($parents->isEmpty()) {
                break;
            }

            $current = $parents->getVertexFirst();
        }

        return $result;
    }

    /**
     * Creates the graph from the commits.
     *
     * The graph represents the parent-child relationship.  Each node in the graph has the id of the commit hash.
     *
     * @param array $commits The commits.
     * @return \Fhaculty\Graph\Graph The graph of commits.
     */
    private function _buildGraph(array $commits)
    {
        $graph = new Graph();

        foreach ($commits as $commit) {
            $vertex = $graph->createVertex($commit['sha'], true);
            $vertex->setAttribute('commit', $commit);
            foreach ($commit['parents'] as $parent) {
                $vertex->createEdgeTo($graph->createVertex($parent['sha'], true));
            }
        }
{'fraction_non_alphanumeric': 0.12341504649196956, 'fraction_numerical': 0.0, 'mean_word_length': 2.227272727272727, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7188986', 'n_tokens_mistral': 979, 'n_tokens_neox': 950, 'n_words': 287}