Ejemplo n.º 1
0
/**
 * The main function of the hook.
 *
 * Expects the following arguments:
 *
 *   - $argv[1] - The path of the configuration file, xgit-config.php.
 *   - $argv[2] - The name of the ref being stored.
 *   - $argv[3] - The old object name stored in the ref.
 *   - $argv[4] - The new objectname to be stored in the ref.
 *
 * @param argc
 *   The number of arguments on the command line.
 *
 * @param argv
 *   Array of the arguments.
 */
function xgit_init($argc, $argv)
{
    $this_file = array_shift($argv);
    // argv[0]
    if ($argc != 5) {
        xgit_help($this_file, STDERR);
        exit(VERSIONCONTROL_GIT_ERROR_WRONG_ARGC);
    }
    $config_file = array_shift($argv);
    // argv[1]
    $ref = array_shift($argv);
    // argv[2]
    $old_obj = array_shift($argv);
    // argv[3]
    $new_obj = array_shift($argv);
    // argv[4]
    // Load the configuration file and bootstrap Drupal.
    if (!file_exists($config_file)) {
        fwrite(STDERR, t('Error: failed to load configuration file.') . "\n");
        exit(VERSIONCONTROL_GIT_ERROR_NO_CONFIG);
    }
    include_once $config_file;
    global $xgit;
    // Admins and other privileged users don't need to go through any checks.
    if (!in_array($xgit['uid'], $xgit['allowed_users'])) {
        // Do a full Drupal bootstrap.
        xgit_bootstrap();
        $ref_type = xgit_ref_type($ref);
        if ($ref_type === FALSE) {
            fwrite(STDERR, "Given reference '{$ref}' is invalid.\n\n");
            exit(VERSIONCONTROL_GIT_ERROR_INVALID_REF);
        }
        try {
            _xgit_assert_type(array($new_obj => array('commit', 'tag', 'empty'), $old_obj => array('commit', 'tag', 'empty')));
        } catch (Exception $e) {
            fwrite(STDERR, $e->getMessage());
            exit(VERSIONCONTROL_GIT_ERROR_INVALID_OBJ);
        }
        // Debugging help
        if ($GLOBALS['xgit_debug']) {
            $commits = xgit_get_commits($old_obj, $new_obj);
            $commits = implode(' ', $commits);
            fwrite(STDERR, 'DEBUG COMMITS:' . $commits . "\n");
        }
        $label = xgit_label_for($ref, $old_obj, $new_obj);
        foreach (xgit_get_commits($old_obj, $new_obj) as $commit) {
            $access = xgit_check_commit_access($commit, $label);
            // Fail and print out error messages if commit access has been denied.
            if (!$access) {
                fwrite(STDERR, implode("\n\n", versioncontrol_get_access_errors()) . "\n\n");
                exit(VERSIONCONTROL_GIT_ERROR_NO_ACCESS);
            }
        }
    }
    // Everything succeeded. Allow operation to complete.
    exit(0);
}
Ejemplo n.º 2
0
function xgit_bootstrap()
{
    global $xgit;
    // Add $drupal_path to current value of the PHP include_path.
    set_include_path(get_include_path() . PATH_SEPARATOR . $xgit['drupal_path']);
    if (empty($_ENV['GIT_DRUPAL_UID'])) {
        fwrite(STDERR, "Error: No environment variable 'GIT_DRUPAL_UID' set or it is empty.\n");
        exit(VERSIONCONTROL_GIT_ERROR_NO_UID);
    }
    $xgit['uid'] = $_ENV['GIT_DRUPAL_UID'];
    chdir($xgit['drupal_path']);
    // Bootstrap Drupal so we can use drupal functions to access the databases, etc.
    if (!file_exists('./includes/bootstrap.inc')) {
        fwrite(STDERR, "Error: failed to load Drupal's bootstrap.inc file.\n");
        exit(VERSIONCONTROL_GIT_ERROR_FAILED_BOOTSTRAP);
    }
    // Set up the multisite directory if necessary.
    if ($xgit['multisite_directory']) {
        $_SERVER['HTTP_HOST'] = $xgit['multisite_directory'];
        // Set a dummy script name, so the multisite configuration
        // file search will always trigger.
        $_SERVER['SCRIPT_NAME'] = '/foo';
    }
    require_once './includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    // Overwrite db_prefix if this is a simpletest run.
    if (isset($GLOBALS['simpletest_db_prefix'])) {
        $GLOBALS['db_prefix'] = $GLOBALS['simpletest_db_prefix'];
    }
    require_once drupal_get_path('module', 'versioncontrol_git') . '/versioncontrol_git.module';
    require_once drupal_get_path('module', 'versioncontrol_git') . '/versioncontrol_git.log.inc';
    $xgit['repo'] = versioncontrol_get_repository($xgit['repo_id']);
    if (!isset($_ENV['GIT_DIR'])) {
        xgit_help($this_file, STDERR);
        exit(VERSIONCONTROL_GIT_ERROR_NO_GIT_DIR);
    }
    $xgit['git_dir'] = $_ENV['PWD'];
    if (empty($xgit['repo'])) {
        $message = "Error: git repository with id '%s' does not exist.\n";
        fwrite(STDERR, sprintf($message, $xgit['repo_id']));
        exit(VERSIONCONTROL_GIT_ERROR_NO_REPOSITORY);
    }
}