grep those gits aka drupal contribs

http://images.icanhascheezburger.com/completestore/2009/1/21/128770736688691365.

Following along the git migration and project cannot be grepped anymore I brewed the following code.

It assumes the gits are already cloned and placed is one common directory.

After running the script all repositories with a $major == 7 branch are set into the highest version.

I'm puzzling how to 'empty' the non $major compatible repos.

The fun part is now doing analysis on the tree

find . -name "*.info" -exec grep -H "^dependencies" {} \; > dependencies-7.x.txt

which shows some new dependency types
./downloads/amfserver/amfserver.info:dependencies[] = services (>3.0-rc1)
./downloads/book_access/book_access.info:dependencies[] = vars (2.x)

for which I hope graphapi to show this version dependency too.

Infra people are working hard on project_dependency to make the test infra structure working on module testing. Even for versioned dependencies to work. Cool!

/**
 * @file
 *
 * Processes all child directories assuming they are drupal based git repos
 *
 * Each git repo is switched to the given major release
 *
 * TODO : does not work for drupal core
 */
// Directory with all git repo's to work with
$gits_dir = 'downloads';
// Drupal major version
$major = 7;

switch_tree($gits_dir, $major);

function switch_tree($gits_dir, $major) {
  $gits = scandir($gits_dir);
  // pop of ./ and ../
  array_shift($gits);
  array_shift($gits);

  foreach ( $gits as $git) {
    $dir = $gits_dir . "/$git/";
    // From stackoverflow.com/questions/1386291/git-git-dir-not-working-as-expected
    $git_exec ="git --git-dir=$dir/.git --work-tree=$dir ";
    $result = shell_exec("$git_exec branch -la");
    $branches = split("\n", $result);
    $result = shell_exec("$git_exec tag -l");
    $tags = split("\n", $result);

    list( $current, $target) = find_branch($major, $branches, $tags);
    if ($target) {
      echo "Checkout $git-$target\n";
      shell_exec("$git_exec checkout $target");
    }
    else if ($current) {
      echo "Already on $git-" . $current . "\n";
    }
    else {
      echo "No $major.x for $git\n";
    }
  }
}

/**
 * Find latest branch based on $major release
 *
 * This could use some more inteligence by using $tags
 * for filtering out a stable branch
 */
function find_branch($major, $branches, $tags) {
  $current = array_shift(preg_grep( "[^\* ]", $branches));
  $current = substr( $current, 2);
 
  $needle = "[remotes/origin/".$major."\.x-[0-9]*\.x$]";
  $targets = preg_grep($needle, $branches);
  if ($targets) {
    $m = max($targets);
    $m = trim($m);
    $m = str_replace('remotes/origin/', '', $m);
    if ($m != $current) {
      return array( $current, $m);
    }
    else {
      return array( $current, FALSE);
    }
  }
  else {
    return array( FALSE, FALSE);
  }
}

Refs:
- all projects can't be grepped : http://drupal.org/node/1057386
- clone contrib projects from git.drupal.org : http://lin-clark.com/blog/writing-scripts-clone-contrib-projects-gitdrup...
- http://drupal.org/project/issues/project_dependency