You are here

function git_deploy_system_info_alter in Git Deploy 7

Same name and namespace in other branches
  1. 8.2 git_deploy.module \git_deploy_system_info_alter()
  2. 6.2 git_deploy.module \git_deploy_system_info_alter()
  3. 6 git_deploy.module \git_deploy_system_info_alter()
  4. 7.2 git_deploy.module \git_deploy_system_info_alter()

Implements hook_system_info_alter().

File

./git_deploy.module, line 39
Adds project, version and date information to projects checked out with Git.

Code

function git_deploy_system_info_alter(&$info, $file, $type) {

  // Use drupal_static() so we can free up memory from static variables later.
  $projects =& drupal_static(__FUNCTION__ . ':projects', array());
  $available =& drupal_static(__FUNCTION__ . ':available');
  $update =& drupal_static(__FUNCTION__ . ':update', array());

  // Include the glip library.
  if (module_exists('libraries')) {
    require_once libraries_get_path('glip') . '/lib/glip.php';
  }
  else {
    require_once variable_get('git_deploy_glip_path', 'sites/all/libraries/glip') . '/lib/glip.php';
  }

  // Core has hard-coded version numbers, so we need to verify them. Otherwise,
  // a valid version number indicates that this project was not installed with
  // Git.
  $is_core = isset($info['package']) && strpos($info['package'], 'Core') === 0;
  if (empty($info['hidden']) && (empty($info['version']) || ($is_core ? strstr($info['version'], '-dev') == '-dev' : $info['version'] == VERSION || !preg_match('/^7\\.x-\\d+\\..+/', $info['version'])))) {

    // Verify that we are in a Git repository.
    $path_components = explode('/', preg_replace('/^' . preg_quote(DRUPAL_ROOT . '/', '/') . '/', '', realpath($file->uri), 1));
    $path = array_pop($path_components);
    while (!empty($path_components) && !file_exists(implode('/', $path_components) . '/.git')) {
      $path = array_pop($path_components) . "/{$path}";
    }
    $path_components[] = '.git';
    $directory = implode('/', $path_components);

    // Handle submodules.
    if (is_file($directory)) {
      $gitdir = str_replace('gitdir: ', '', trim(current(file($directory))));
      $directory = preg_replace('/^' . preg_quote(DRUPAL_ROOT . '/', '/') . '/', '', realpath(dirname($directory) . "/{$gitdir}"), 1);
    }
    if (file_exists($directory)) {

      // Only check Git once per repository.
      if (!isset($projects[$directory])) {
        $projects[$directory] = array();

        // Ensure file is in repository.
        $git = new Git($directory);
        if (@$git
          ->getObject($git
          ->revParse())
          ->getTree()
          ->find($path)) {

          // Get Git configuration.
          $git_config = parse_ini_file($directory . '/config', TRUE);

          // Get upstream info.
          $upstream = _git_deploy_get_upstream($git, $is_core ? '7' : '7.x-*', $git_config);
          if ($is_core) {
            $project_name = 'drupal';
          }
          elseif (isset($upstream['remote']) && $git_config) {

            // Find the project name based on fetch URL.
            $remote = isset($git_config["remote {$upstream['remote']}"]) ? "remote {$upstream['remote']}" : current(preg_grep('/^remote\\s.+/', array_keys($git_config)));
            if (!empty($remote)) {
              $project_name = basename($git_config[$remote]['url'], '.git');
              $projects[$directory]['project'] = $project_name;
            }
          }

          // Set project datestamp.
          if (isset($upstream['datestamp'])) {
            $projects[$directory]['datestamp'] = $upstream['datestamp'];

            // The '_info_file_ctime' should always get the latest value.
            if (empty($info['_info_file_ctime'])) {
              $projects[$directory]['_info_file_ctime'] = $upstream['datestamp'];
            }
            else {
              $projects[$directory]['_info_file_ctime'] = max($info['_info_file_ctime'], $upstream['datestamp']);
            }
          }

          // Set version from tag.
          if (isset($upstream['tag'])) {
            $projects[$directory]['version'] = $upstream['tag'];
          }
          elseif (isset($upstream['branch'])) {
            if ($upstream['branch'] != 'master') {
              $projects[$directory]['version'] = "{$upstream['branch']}-dev";
            }
            if (module_exists('update') && ($upstream['synced'] || $upstream['branch'] == 'master')) {
              if (!isset($available)) {

                // We only get this from the cache because an update status
                // query is supposed to done only after processing all enabled
                // modules and themes.
                $available = _update_get_cached_available_releases();
              }
              if (!empty($available[$project_name]['releases'])) {
                if ($upstream['branch'] == 'master') {

                  // If there's available update_status data, we can use the
                  // version string the release node pointing to HEAD really
                  // has.
                  foreach ($available[$project_name]['releases'] as $release) {
                    if (isset($release['tag']) && $release['tag'] == 'HEAD') {
                      $projects[$directory]['version'] = $release['version'];
                      break;
                    }
                  }
                }
                if ($upstream['synced']) {

                  // This project's datestamp needs to be synced with upstream
                  // release.
                  $version = $projects[$directory]['version'];
                  if (!isset($available[$project_name]['releases'][$version]) || $projects[$directory]['_info_file_ctime'] > $available[$project_name]['last_fetch']) {

                    // We need to update available release data for this
                    // project.
                    $update[] = $project_name;
                  }
                  else {
                    _git_deploy_datestamp_sync($projects[$directory], $available[$project_name]['releases'][$version]);
                  }
                }
              }
            }
          }
        }
      }
      $info = $projects[$directory] + $info;
    }
  }
}