Created
March 1, 2012 13:03
-
-
Save perifer/1949699 to your computer and use it in GitHub Desktop.
Automatic drush aliases from git remotes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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