<commit_before><?php
/* (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Deployer;

require __DIR__ . '/common/config.php';

use Deployer\Type\Csv;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

/**
 * Configuration
 */

set('keep_releases', 5);

set('repository', ''); // Repository to deploy.
set('branch', ''); // Branch to deploy.

set('shared_dirs', []);
set('shared_files', []);

set('copy_dirs', []);

set('writable_dirs', []);
set('writable_mode', 'chgrp'); // chgrp or acl.
set('writable_use_sudo', false); // Using sudo in writable commands?

set('http_user', false);

set('clear_paths', []);         // Relative path from deploy_path
set('clear_use_sudo', false);    // Using sudo in clean commands?

set('use_relative_symlink', true);

set('composer_action', 'install');
set('composer_options', '{{composer_action}} --verbose --prefer-dist --no-progress --no-interaction --no-dev --optimize-autoloader');

set('env_vars', ''); // Variable assignment before cmds (for example, SYMFONY_ENV={{set}})

set('git_cache', function () { //whether to use git cache - faster cloning by borrowing objects from existing clones.
    $gitVersion = run('{{bin/git}} version');
    $regs       = [];
    if (preg_match('/((\d+\.?)+)/', $gitVersion, $regs)) {
        $version = $regs[1];
    } else {
        $version = "1.0.0";
    }
    return version_compare($version, '2.3', '>=');
});

set('release_name', function () {
    $list = get('releases_list');

    // Filter out anything that does not look like a release.
    $list = array_filter($list, function ($release) {
        return preg_match('/^[\d\.]+$/', $release);
    });

    $nextReleaseNumber = 1;
    if (count($list) > 0) {
        $nextReleaseNumber = (int)max($list) + 1;
    }

    return (string)$nextReleaseNumber;
}); // name of folder in releases

/**
 * Return list of releases on server.
 */
set('releases_list', function () {
    cd('{{deploy_path}}');

    // If there is no releases return empty list.
    if (!run('[ -d releases ] && [ "$(ls -A releases)" ] && echo "true" || echo "false"')->toBool()) {
        return [];
    }

    // Will list only dirs in releases.
    $list = run('cd releases && ls -t -d */')->toArray();

    // Prepare list.
    $list = array_map(function ($release) {
        return basename(rtrim($release, '/'));
    }, $list);

    $releases = []; // Releases list.

    if (run('if [ -f .dep/releases ]; then echo "true"; fi')->toBool()) {
        $keepReleases = get('keep_releases');
        if ($keepReleases === -1) {
            $csv = run('cat .dep/releases');
        } else {
            $csv = run("tail -n " . ($keepReleases + 5) . " .dep/releases");
        }

        $metainfo = Csv::parse($csv);

        for ($i = count($metainfo) - 1; $i >= 0; --$i) {
            if (is_array($metainfo[$i]) && count($metainfo[$i]) >= 2) {
                list($date, $release) = $metainfo[$i];
                if (in_array($release, $list, true)) {
                    $releases[] = $release;
                }
            }
        }
    }

    return $releases;
});

/**
 * Return release path.
 */
set('release_path', function () {
    $releaseExists = run("if [ -h {{deploy_path}}/release ]; then echo 'true'; fi")->toBool();
    if (!$releaseExists) {
        throw new \RuntimeException(
            "Release path does not found.\n" .
            "Run deploy:release to create new release."
        );
    }

    $link = run("readlink {{deploy_path}}/release")->toString();
    return substr($link, 0, 1) === '/' ? $link : get('deploy_path') . '/' . $link;
});


/**
 * Return current release path.
 */
set('current_path', function () {
    $link = run("readlink {{deploy_path}}/current")->toString();<commit_msg>Fix doubling releases.<commit_after><?php
/* (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Deployer;

require __DIR__ . '/common/config.php';

use Deployer\Type\Csv;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

/**
 * Configuration
 */

set('keep_releases', 5);

set('repository', ''); // Repository to deploy.
set('branch', ''); // Branch to deploy.

set('shared_dirs', []);
set('shared_files', []);

set('copy_dirs', []);

set('writable_dirs', []);
set('writable_mode', 'chgrp'); // chgrp or acl.
set('writable_use_sudo', false); // Using sudo in writable commands?

set('http_user', false);

set('clear_paths', []);         // Relative path from deploy_path
set('clear_use_sudo', false);    // Using sudo in clean commands?

set('use_relative_symlink', true);

set('composer_action', 'install');
set('composer_options', '{{composer_action}} --verbose --prefer-dist --no-progress --no-interaction --no-dev --optimize-autoloader');

set('env_vars', ''); // Variable assignment before cmds (for example, SYMFONY_ENV={{set}})

set('git_cache', function () { //whether to use git cache - faster cloning by borrowing objects from existing clones.
    $gitVersion = run('{{bin/git}} version');
    $regs       = [];
    if (preg_match('/((\d+\.?)+)/', $gitVersion, $regs)) {
        $version = $regs[1];
    } else {
        $version = "1.0.0";
    }
    return version_compare($version, '2.3', '>=');
});

set('release_name', function () {
    $list = get('releases_list');

    // Filter out anything that does not look like a release.
    $list = array_filter($list, function ($release) {
        return preg_match('/^[\d\.]+$/', $release);
    });

    $nextReleaseNumber = 1;
    if (count($list) > 0) {
        $nextReleaseNumber = (int)max($list) + 1;
    }

    return (string)$nextReleaseNumber;
}); // name of folder in releases

/**
 * Return list of releases on server.
 */
set('releases_list', function () {
    cd('{{deploy_path}}');

    // If there is no releases return empty list.
    if (!run('[ -d releases ] && [ "$(ls -A releases)" ] && echo "true" || echo "false"')->toBool()) {
        return [];
    }

    // Will list only dirs in releases.
    $list = run('cd releases && ls -t -d */')->toArray();

    // Prepare list.
    $list = array_map(function ($release) {
        return basename(rtrim($release, '/'));
    }, $list);

    $releases = []; // Releases list.

    if (run('if [ -f .dep/releases ]; then echo "true"; fi')->toBool()) {
        $keepReleases = get('keep_releases');
        if ($keepReleases === -1) {
            $csv = run('cat .dep/releases');
        } else {
            $csv = run("tail -n " . ($keepReleases + 5) . " .dep/releases");
        }

        $metainfo = Csv::parse($csv);

        for ($i = count($metainfo) - 1; $i >= 0; --$i) {
            if (is_array($metainfo[$i]) && count($metainfo[$i]) >= 2) {
                list($date, $release) = $metainfo[$i];
                $index = array_search($release, $list, true);
                if ($index !== false) {
                    $releases[] = $release;
                    unset($list[$index]);
                }
            }
        }
    }

    return $releases;
});

/**
 * Return release path.
 */
set('release_path', function () {
    $releaseExists = run("if [ -h {{deploy_path}}/release ]; then echo 'true'; fi")->toBool();
    if (!$releaseExists) {
        throw new \RuntimeException(
            "Release path does not found.\n" .
            "Run deploy:release to create new release."
        );
    }

    $link = run("readlink {{deploy_path}}/release")->toString();
    return substr($link, 0, 1) === '/' ? $link : get('deploy_path') . '/' . $link;
});


/**
 * Return current release path.
 */
set('current_path', function () {
    $link = run("readlink {{deploy_path}}/current")->toString();
{'fraction_non_alphanumeric': 0.19516210739614995, 'fraction_numerical': 0.004305977710233029, 'mean_word_length': 2.7285174693106704, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, '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': '1953460', 'n_tokens_mistral': 2609, 'n_tokens_neox': 2476, 'n_words': 625}