Skip to content

Instantly share code, notes, and snippets.

@q0rban
Last active August 29, 2015 14:25

Revisions

  1. James Sansbury revised this gist Jul 22, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion INSTALL.md
    Original file line number Diff line number Diff line change
    @@ -12,5 +12,5 @@ $ mv git-drush.sh /usr/local/bin/git-drush
    $ chmod +x /your/bin/dir/git-drush
    # Run help to make sure it works and see how to use it!
    $ git drush --help
    $ git drush help
    ```
  2. James Sansbury revised this gist Jul 22, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions git-drush.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ set -e
    # Output an error to STDERR.
    echoerr() { echo "$@" 1>&2; }

    # Displays the help for this command. Type `git drush --help`
    # 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"
    @@ -16,7 +16,7 @@ help() {
    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'
    echo ' $ git drush help'
    }

    # Gets the parent's parent process id.
    @@ -30,7 +30,7 @@ ppid() {
    }

    # Display the help.
    if [[ $1 = "--help" ]]; then
    if [[ $1 = "help" ]]; then
    help
    exit
    fi
  3. James Sansbury revised this gist Jul 22, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions INSTALL.md
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,8 @@ 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 /usr/local/bin:
    $ mv git-drush.sh /your/bin/dir/git-drush
    # 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
  4. James Sansbury revised this gist Jul 22, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions INSTALL.md
    Original file line number Diff line number Diff line change
    @@ -4,10 +4,13 @@ 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 /usr/local/bin:
    $ mv git-drush.sh /your/bin/dir/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
    ```
  5. James Sansbury revised this gist Jul 22, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion INSTALL.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # INSTALLATION INSTRUCTIONS
    # Installation Instructions
    First, copy the link to the raw git-drush.sh file.

    ```
  6. James Sansbury revised this gist Jul 22, 2015. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions INSTALL.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    # 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 /usr/local/bin:
    $ mv git-drush.sh /your/bin/dir/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
    ```
  7. James Sansbury renamed this gist Jul 22, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. James Sansbury created this gist Jul 22, 2015.
    92 changes: 92 additions & 0 deletions -
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@
    #!/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