Changes between Version 26 and Version 27 of faq/gitification


Ignore:
Timestamp:
Jun 9, 2012, 11:14:20 AM (8 years ago)
Author:
Bart
Comment:

Added git_subtree_system_info_alter()

Legend:

Unmodified
Added
Removed
Modified
  • faq/gitification

    v26 v27  
    259259
    260260One way to solve the problem would be a Drupal module which uses the [http://api.drush.ws/api/drush/docs!drush.api.php/5.x Drush api] to clone the git repository of each Drupal module locally and then determines the version information by using the ''git-subtree-split'' line returned by [http://git-scm.com/docs/git-log git-log]. It could write this information to a ''mymodule.gitinfo'' file. A module could then retrieve this information from the ''mymodule.gitinfo'' file and a [http://api.drupal.org/api/drupal/developer!hooks!core.php/function/hook_system_info_alter/6 hook_system_info_alter] could induce it into the local Drupal ecosystem. By adding {{{*.gitinfo}}} to [http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository ''.gitignore''] we could be sure that these files do not cause any problems when syncing with upstream repositories.
     261
     262=== git_subtree_system_info_alter() ===
     263{{{
     264/**
     265 * Implement hook_system_info_alter() to provide metadata to drupal from git.
     266 *
     267 * We support populating $info from a gitinfo file.
     268 *
     269 * @param $info
     270 *   The module/theme info array we're altering.
     271 * @param $file
     272 *   An object describing the filesystem location of the module/theme.
     273 */
     274function git_subtree_system_info_alter(&$info, $file) {
     275  // Determine whether this is a theme or a module
     276  $type = isset($info['engine']) ? 'theme' : 'module';
     277  // We only need to look for a version if it is not yet set
     278  if (empty($info['version'])) {
     279    // Get the filename
     280    $filename = check_plain($file->filename);
     281    // Get the directory of the theme/module
     282    $directory = dirname($filename);
     283    // Check whether this belongs to core. Speed optimization.
     284    if (drupal_substr($directory, 0, drupal_strlen($type)) != $type) {
     285      // Guess the gitinfo filename
     286      $gitinfo_filename = preg_replace('/\.[^.]*/', '.gitinfo', $filename);
     287      // Check whether a gitinfo file exists
     288      if (file_exists($gitinfo_filename) && is_readable ($gitinfo_filename)) {
     289        // Parse the gitinfo file
     290        $gitinfo = parse_ini_file($gitinfo_filename);
     291        foreach (array_keys($gitinfo) as $key) {
     292          if (isset($gitinfo[$key])) {
     293            $info[$key] = $gitinfo[$key];
     294          }
     295        }
     296      }
     297    }
     298  }
     299}
     300}}}