<commit_before><?php

namespace Platformsh\Cli\Local;

use Platformsh\Cli\Helper\GitHelper;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;

class LocalProject
{

    const ARCHIVE_DIR = '.build-archives';
    const BUILD_DIR = 'builds';
    const PROJECT_CONFIG = '.platform-project';
    const REPOSITORY_DIR = 'repository';
    const SHARED_DIR = 'shared';
    const WEB_ROOT = 'www';

    /**
     * Create the default files for a project.
     *
     * @param string $projectRoot
     * @param string $projectId
     */
    public function createProjectFiles($projectRoot, $projectId)
    {
        mkdir($projectRoot . '/' . self::BUILD_DIR);
        mkdir($projectRoot . '/' . self::SHARED_DIR);

        // Create the .platform-project file.
        $projectConfig = array(
          'id' => $projectId,
        );
        $dumper = new Dumper();
        file_put_contents($projectRoot . '/' . self::PROJECT_CONFIG, $dumper->dump($projectConfig));
    }

    /**
     * Initialize a project in a directory.
     *
     * @param string $dir
     *
     * @throws \RuntimeException
     *
     * @return string The absolute path to the project.
     */
    public function initialize($dir)
    {
        $realPath = realpath($dir);
        if (!$realPath) {
            throw new \RuntimeException("Directory not readable: $dir");
        }

        $dir = $realPath;

        if (file_exists($dir . '/../' . self::PROJECT_CONFIG)) {
            throw new \RuntimeException("The project is already initialized");
        }

        // Get the project ID from the Git repository.
        $remoteUrl = $this->getGitRemote($dir);
        $projectId = $this->getProjectId($remoteUrl);

        // Move the directory into a 'repository' subdirectory.
        $backupDir = $this->getBackupDir($dir);

        $fs = new Filesystem();
        $fs->rename($dir, $backupDir);
        $fs->mkdir($dir, 0755);
        $fs->rename($backupDir, $dir . '/' . LocalProject::REPOSITORY_DIR);

        $this->createProjectFiles($dir, $projectId);
        $this->ensureGitRemote($dir, $remoteUrl);

        return $dir;
    }

    /**
     * @throws \RuntimeException If the URL is not a Platform.sh Git URL.
     *
     * @param string $gitUrl
     */
    protected function getProjectId($gitUrl)
    {
        if (!preg_match('/^([a-z][a-z0-9]{12})@git\.[a-z\-]+\.platform\.sh:\1\.git$/', $gitUrl, $matches)) {
            throw new \RuntimeException("Not a Platform.sh Git URL: $gitUrl");
        }

        return $matches[1];
    }

    /**
     * @param string $dir
     *
     * @throws \RuntimeException
     *   If no remote can be found.
     *
     * @return string
     *   The Git remote URL.
     */<commit_msg>Fix: 'platform init' reports 'The directory is not a Git repository'<commit_after><?php

namespace Platformsh\Cli\Local;

use Platformsh\Cli\Helper\GitHelper;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;

class LocalProject
{

    const ARCHIVE_DIR = '.build-archives';
    const BUILD_DIR = 'builds';
    const PROJECT_CONFIG = '.platform-project';
    const REPOSITORY_DIR = 'repository';
    const SHARED_DIR = 'shared';
    const WEB_ROOT = 'www';

    /**
     * Create the default files for a project.
     *
     * @param string $projectRoot
     * @param string $projectId
     */
    public function createProjectFiles($projectRoot, $projectId)
    {
        mkdir($projectRoot . '/' . self::BUILD_DIR);
        mkdir($projectRoot . '/' . self::SHARED_DIR);

        // Create the .platform-project file.
        $projectConfig = array(
          'id' => $projectId,
        );
        $dumper = new Dumper();
        file_put_contents($projectRoot . '/' . self::PROJECT_CONFIG, $dumper->dump($projectConfig));
    }

    /**
     * Initialize a project in a directory.
     *
     * @param string $dir
     *
     * @throws \RuntimeException
     *
     * @return string The absolute path to the project.
     */
    public function initialize($dir)
    {
        $realPath = realpath($dir);
        if (!$realPath) {
            throw new \RuntimeException("Directory not readable: $dir");
        }

        $dir = $realPath;

        if (file_exists($dir . '/../' . self::PROJECT_CONFIG)) {
            throw new \RuntimeException("The project is already initialized");
        }

        // Get the project ID from the Git repository.
        $remoteUrl = $this->getGitRemote($dir);
        $projectId = $this->getProjectId($remoteUrl);

        // Move the directory into a 'repository' subdirectory.
        $backupDir = $this->getBackupDir($dir);

        $fs = new Filesystem();
        $fs->rename($dir, $backupDir);
        $fs->mkdir($dir, 0755);
        $fs->rename($backupDir, $dir . '/' . LocalProject::REPOSITORY_DIR);

        $this->createProjectFiles($dir, $projectId);
        $this->ensureGitRemote($dir . '/' . LocalProject::REPOSITORY_DIR, $gitUrl);

        return $dir;
    }

    /**
     * @throws \RuntimeException If the URL is not a Platform.sh Git URL.
     *
     * @param string $gitUrl
     */
    protected function getProjectId($gitUrl)
    {
        if (!preg_match('/^([a-z][a-z0-9]{12})@git\.[a-z\-]+\.platform\.sh:\1\.git$/', $gitUrl, $matches)) {
            throw new \RuntimeException("Not a Platform.sh Git URL: $gitUrl");
        }

        return $matches[1];
    }

    /**
     * @param string $dir
     *
     * @throws \RuntimeException
     *   If no remote can be found.
     *
     * @return string
     *   The Git remote URL.
     */
{'fraction_non_alphanumeric': 0.1420726306465899, 'fraction_numerical': 0.00354295837023915, 'mean_word_length': 2.259815242494226, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2457938', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1700, 'n_words': 401}