Skip to content

Instantly share code, notes, and snippets.

@perifer
Created March 1, 2012 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perifer/1949699 to your computer and use it in GitHub Desktop.
Save perifer/1949699 to your computer and use it in GitHub Desktop.
Automatic drush aliases from git remotes.
<?php
/**
* Place this file in e.g ~/.drush.
* See `drush topic docs-aliases` for more info.
*/
// Use require_once since this file is invoked multiple times.
require_once('git_remote_alias.php');
git_remote_alias_set('live', $aliases);
git_remote_alias_set('staging', $aliases);
<?php
function git_remote_alias_set($remote_name, &$aliases) {
static $cache = array();
if (!isset($cache[$remote_name])) {
drush_shell_exec("git config --get remote.$remote_name.url");
$uri = array_shift(drush_shell_exec_output());
if (empty($uri)) {
$cache[$remote_name] = FALSE;
}
else {
// Handle ssh://user@host/path as well as user@host:path
if (strpos($uri, 'ssh://') === false) {
$uri = 'ssh://' . str_replace(':', '', $uri);
}
$ssh_settings = parse_url($uri);
$cache[$remote_name] = array(
'remote-host' => $ssh_settings['host'],
'remote-user' => $ssh_settings['user'],
'root' => $ssh_settings['path'],
'uri' => $ssh_settings['host'],
);
}
};
// Only add the alias if there is a remote on this repo with the given name.
if ($cache[$remote_name]) {
$aliases['self.' . $remote_name] = $cache[$remote_name];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment