Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created May 1, 2012 11:56
Show Gist options
  • Select an option

  • Save nfreear/2567565 to your computer and use it in GitHub Desktop.

Select an option

Save nfreear/2567565 to your computer and use it in GitHub Desktop.
Quickly checkout a bunch of Drupal contributed modules on *nix, or git-describe sub-directories.
#!/usr/bin/env php
<?php
/**
* Nick's Drupal Git CLI.
* Quickly checkout a bunch of Drupal contributed modules on Redhat 6, or git-describe sub-directories.
*
* > php ../../../../../ndrupalgit.php
*
* Issues: views?
*
* Copyright 2012-05-01 N.D.Freear.
*/
define('DRUPAL_GIT_CLONE_PATTERN', 'git clone http://git.drupal.org/project/__PROJECT__.git');
define('DRUPAL_VERSION', '6.x');
define('PROJECT_CONTRIB_ROOT', '__FILE__/pd_approval/sites/all/modules/contrib');
$do_checkout = FALSE;
/*
See:
http://drupal.org/node/1083580
http://www.open.ac.uk/wikis/drupal/Drupal_supported_modules_(6.21)
*/
$__contrib_modules = 'ctools|oembed|views|date|calendar|fckeditor|location|feeds|tagadelic'; # ckeditor, tinymce, views?
$__contrib_modules = 'global_redirect|nodewords|page_title|pathauto';
$__contrib_modules = 'token|job_scheduler|pathauto|fckeditor|location|feeds|tagadelic';
$contrib_modules = 'ldap_integration|ckeditor|cck|linkchecker';
$base = dirname(__FILE__);
$contrib_root = str_replace('__FILE__', $base, PROJECT_CONTRIB_ROOT);
$projects_list = explode('|', $contrib_modules);
if ($do_checkout) {
foreach ($projects_list as $module) {
chdir( $contrib_root );
if ($do_checkout) {
// Todo: check if the module is already present.
// Clone the module..
$clone_cmd = str_replace('__PROJECT__', $module, DRUPAL_GIT_CLONE_PATTERN);
$res = my_system($clone_cmd);
chdir($module);
$res = my_system('ls -al');
// Find a branch to checkout..
$remote_branches = my_system('git branch -a');
if (preg_match('#.*(remotes\/origin\/'. DRUPAL_VERSION .'-[\w\.]+)#', $remote_branches, $matches)) {
$rem_branch = $matches[1];
$branch = str_replace('remotes/origin/', '', $rem_branch);
$res = my_system("git checkout -b $branch $rem_branch");
$res = my_system('git log -1');
$res = my_system('git branch -a');
} else {
echo '> Error, no branch found. Aborting..'.PHP_EOL;
exit (1);
}
} else {
chdir($module);
}
$res = my_system('git describe --tags');
}
}
git_describe_all($contrib_root);
function git_describe_all($contrib_root) {
$dir_list = scandir($contrib_root);
$log_file = str_replace('.php', '', __FILE__) .'.log';
$describe_r = array();
foreach ($dir_list as $module) {
if ($module[0] == '.') continue;
chdir( $contrib_root .'/'. $module );
$describe_res = my_system('git describe --tags');
$describe_r[$module] = "$module,$describe_res";
}
$describe = implode('', array_values($describe_r));
echo '> describe all..'.PHP_EOL. $describe;
$bytes = file_put_contents($log_file, $describe); #FILE_APPEND.
}
function my_system($cmd ) {
echo '> '. $cmd .PHP_EOL;
$handle = popen(escapeshellcmd($cmd), 'r'); //2>&1
$result = fread($handle, 2096);
pclose($handle);
echo $result .PHP_EOL;
// Todo: need to check stderr!
if (FALSE !== strpos($result, 'fatal')) {
echo '> Fatal error. Aborting..'.PHP_EOL;
exit (1);
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment