Esempio 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);
}
Esempio n. 2
0
/**
 *  Return the operation type associated with the given reference.
 *
 *  @param $ref
 *    The reference of which to compute the operation type.
 *
 *  @return
 *    Either VERSIONCONTROL_OPERATION_BRANCH or VERSIONCONTROL_OPERATION_TAG.
 */
function xgit_operation_type($ref)
{
    switch (xgit_ref_type($ref)) {
        case 'heads':
        case 'remotes':
            return VERSIONCONTROL_OPERATION_BRANCH;
            break;
        case 'tags':
            return VERSIONCONTROL_OPERATION_TAG;
            break;
        default:
            throw new Exception("Unexpected operation type '{$ref}' received.");
            break;
    }
}