You are here

function git_deploy_system_info_alter in Git Deploy 7.2

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 git_deploy.module \git_deploy_system_info_alter()

Implements hook_system_info_alter().

File

./git_deploy.module, line 36
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());

  // 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'])))) {

    // Work around bug that causes Git to fail for users without home directory.
    $home = getenv('HOME') === FALSE ? 'HOME=' . DRUPAL_ROOT . ' ' : '';

    // Verify that we are in a Git repository. For core, also verify that this
    // is really a Drupal repository.
    $directory = exec($home . 'git -C ' . escapeshellarg(dirname($file->uri)) . ' rev-parse --show-toplevel 2> ' . GIT_DEPLOY_ERROR_DUMP);
    if (!empty($directory) && (!$is_core || $directory == DRUPAL_ROOT)) {

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

        // Make sure Git operates in the right directory.
        $git = $home . 'git -C ' . escapeshellarg($directory);

        // Ensure file is in repository.
        if (exec("{$git} ls-files " . escapeshellarg(str_replace("{$directory}/", '', realpath($file->uri))) . ' 2> ' . GIT_DEPLOY_ERROR_DUMP)) {

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

            // Find the project name based on fetch URL.
            $fetch_url = exec("{$git} config --get " . escapeshellarg("remote.{$upstream['remote']}.url") . ' 2> ' . GIT_DEPLOY_ERROR_DUMP);
            if (!empty($fetch_url)) {
              $project_name = basename($fetch_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;
    }
  }
}