<commit_before><?php

/*
 * This file is part of Composer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Package\Version;

use Composer\Config;
use Composer\Repository\Vcs\HgDriver;
use Composer\IO\NullIO;
use Composer\Semver\VersionParser as SemverVersionParser;
use Composer\Util\Git as GitUtil;
use Composer\Util\ProcessExecutor;
use Composer\Util\Svn as SvnUtil;

/**
 * Try to guess the current version number based on different VCS configuration.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class VersionGuesser
{
    /**
     * @var Config
     */
    private $config;

    /**
     * @var ProcessExecutor
     */
    private $process;

    /**
     * @var SemverVersionParser
     */
    private $versionParser;

    /**
     * @param Config              $config
     * @param ProcessExecutor     $process
     * @param SemverVersionParser $versionParser
     */
    public function __construct(Config $config, ProcessExecutor $process, SemverVersionParser $versionParser)
    {
        $this->config = $config;
        $this->process = $process;
        $this->versionParser = $versionParser;
    }

    /**
     * @param array  $packageConfig
     * @param string $path          Path to guess into
     *
     * @return null|array versionData, 'version', 'pretty_version' and 'commit' keys
     */
    public function guessVersion(array $packageConfig, $path)
    {
        if (function_exists('proc_open')) {
            $versionData = $this->guessGitVersion($packageConfig, $path);
            if (null !== $versionData && null !== $versionData['version']) {
                return $this->postprocess($versionData);
            }

            $versionData = $this->guessHgVersion($packageConfig, $path);
            if (null !== $versionData && null !== $versionData['version']) {
                return $this->postprocess($versionData);
            }

            $versionData = $this->guessFossilVersion($packageConfig, $path);
            if (null !== $versionData && null !== $versionData['version']) {
                return $this->postprocess($versionData);
            }

            $versionData = $this->guessSvnVersion($packageConfig, $path);
            if (null !== $versionData && null !== $versionData['version']) {
                return $this->postprocess($versionData);
            }
        }
    }

    private function postprocess(array $versionData)
    {
        // make sure that e.g. dev-1.5 gets converted to 1.5.x-dev
        if ('dev-' !== substr($versionData['version'], 0, 4)) {
            $versionData['pretty_version'] = preg_replace('{(\.9{7})+}', '.x', $versionData['version']);
        }

        return $versionData;
    }

    private function guessGitVersion(array $packageConfig, $path)
    {
        GitUtil::cleanEnv();
        $commit = null;
        $version = null;
        $prettyVersion = null;
        $isDetached = false;
<commit_msg>Fix pretty version guessing when non-dev branch<commit_after><?php

/*
 * This file is part of Composer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Package\Version;

use Composer\Config;
use Composer\Repository\Vcs\HgDriver;
use Composer\IO\NullIO;
use Composer\Semver\VersionParser as SemverVersionParser;
use Composer\Util\Git as GitUtil;
use Composer\Util\ProcessExecutor;
use Composer\Util\Svn as SvnUtil;

/**
 * Try to guess the current version number based on different VCS configuration.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class VersionGuesser
{
    /**
     * @var Config
     */
    private $config;

    /**
     * @var ProcessExecutor
     */
    private $process;

    /**
     * @var SemverVersionParser
     */
    private $versionParser;

    /**
     * @param Config              $config
     * @param ProcessExecutor     $process
     * @param SemverVersionParser $versionParser
     */
    public function __construct(Config $config, ProcessExecutor $process, SemverVersionParser $versionParser)
    {
        $this->config = $config;
        $this->process = $process;
        $this->versionParser = $versionParser;
    }

    /**
     * @param array  $packageConfig
     * @param string $path          Path to guess into
     *
     * @return null|array versionData, 'version', 'pretty_version' and 'commit' keys
     */
    public function guessVersion(array $packageConfig, $path)
    {
        if (function_exists('proc_open')) {
            $versionData = $this->guessGitVersion($packageConfig, $path);
            if (null !== $versionData && null !== $versionData['version']) {
                return $this->postprocess($versionData);
            }

            $versionData = $this->guessHgVersion($packageConfig, $path);
            if (null !== $versionData && null !== $versionData['version']) {
                return $this->postprocess($versionData);
            }

            $versionData = $this->guessFossilVersion($packageConfig, $path);
            if (null !== $versionData && null !== $versionData['version']) {
                return $this->postprocess($versionData);
            }

            $versionData = $this->guessSvnVersion($packageConfig, $path);
            if (null !== $versionData && null !== $versionData['version']) {
                return $this->postprocess($versionData);
            }
        }
    }

    private function postprocess(array $versionData)
    {
        if ('-dev' === substr($versionData['version'], -4)) {
            $versionData['pretty_version'] = preg_replace('{(\.9{7})+}', '.x', $versionData['version']);
        }

        return $versionData;
    }

    private function guessGitVersion(array $packageConfig, $path)
    {
        GitUtil::cleanEnv();
        $commit = null;
        $version = null;
        $prettyVersion = null;
        $isDetached = false;

{'fraction_non_alphanumeric': 0.13619250286462597, 'fraction_numerical': 0.0018006220330659681, 'mean_word_length': 2.4696195343554797, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 49, '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': '5388003', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1693, 'n_words': 418}