Skip to content

Instantly share code, notes, and snippets.

@q0rban
Last active August 29, 2015 14:25
Show Gist options
  • Save q0rban/505412db465e448b5cb0 to your computer and use it in GitHub Desktop.
Save q0rban/505412db465e448b5cb0 to your computer and use it in GitHub Desktop.
git-drush
#!/usr/bin/env bash
set -e
# Output an error to STDERR.
echoerr() { echo "$@" 1>&2; }
# Displays the help for this command. Type `git drush help`
help() {
echo -e "\nThis command saves a Drush alias to the local config, so that you don't have to remember what alias is associated with what repository. This is helpful if you switch between lots of different sites.\n"
echo -e "To save a Drush alias to the local config on this repository:\n"
echo ' $ git drush @foo'
echo -e "\nTo use the stored Drush alias from this repository:\n"
echo ' $ git drush'
echo -e "\nTo delete the stored Drush alias from the local config:\n"
echo ' $ git drush --unset'
echo -e "\nTo clear the alias that is being used by drush:\n"
echo ' $ git drush --clear'
echo -e "\n To show this help:\n"
echo ' $ git drush help'
}
# Gets the parent's parent process id.
ppid() {
pid=`ps -p ${1:-$$} -o ppid=;`
if [[ -z $1 ]]; then
ppid $pid
else
echo $pid
fi
}
# Display the help.
if [[ $1 = "help" ]]; then
help
exit
fi
# Remove the alias from local config.
if [[ $1 = "--unset" || $1 = "--unset-all" ]]; then
git config --unset-all --local drush.alias &&
echo "Drush alias has been removed from local config." ||
echoerr "No Drush alias found in local config."
exit
fi
# Clear the current site alias.
if [[ $1 = "--clear" ]]; then
drush site-set
exit
fi
alias=$1
if [[ -z $alias ]]; then
# If no alias is saved, fail gracefully by using echo, log an error, and
# then fail explicitly.
alias=`git config --get --local drush.alias || echo ""`
if [[ -z $alias ]]; then
echoerr "Drush alias not detected on this project. To set one, pass it as an argument to this script."
help
exit 23
fi
fi
# If the alias doesn't start with an @ symbol, exit.
if [[ `echo "$alias" | grep -v '^@'` ]]; then
echoerr "Not a valid drush alias. Drush aliases should start with an @ symbol."
help
exit 23
fi
# Attempt to use the alias. If this fails, the script will stop here, preventing
# a broken alias from getting saved in the local config.
drush site-set $alias
# Since this script is running in a subshell, Drush will save the wrong ppid.
# We need to load up the ppid two levels up, and then rename the file that Drush
# creates to use that ppid, so that this will be active in the shell that
# executed this command.
this_pid=$$
real_pid=`ppid`
drush_filename=`drush ev "drush_print(drush_sitealias_get_envar_filename());"`
new_filename=`echo "$drush_filename" | sed -e "s/$this_pid\$/$real_pid/"`
# Attempt to move the file. If this fails, log an error with instructions.
mv -f $drush_filename $new_filename ||
echoerr "Unable to move $drush_filename to $new_filename. You will need to call drush site-set $alias manually."
# If the alias is valid (the above command was successful), save it to local
# config.
if [[ -n $1 ]]; then
git config --replace-all --local drush.alias "$alias"
echo "Saved drush alias $alias to local git config."
fi

Installation Instructions

First, copy the link to the raw git-drush.sh file.

# Download the file
$ wget [link-to-raw-git-drush.sh-file]

# Move it to a directory where you store binaries, such as ~/bin or /usr/local/bin:
$ mv git-drush.sh /usr/local/bin/git-drush

# Make sure it is executable.
$ chmod +x /your/bin/dir/git-drush

# Run help to make sure it works and see how to use it!
$ git drush help
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment