ianwinter
1/31/2014 - 9:22 AM

Github_terminal.md

## Open the current branch in browser
## ====================================
## Open the current repo & branch from the command line
## Use flags to navigate to other areas of the repo such as
## commits, branches, pull requests or issues.
## ====================================
## This is an adaptation of @jasonneylon's script.
## Source: http://jasonneylon.wordpress.com/2011/04/22/opening-github-in-your-browser-from-the-terminal/
## ====================================
## @usage: gh
## @usage: gh c
## @usage: gh pr branch-name
function gh() {
  giturl=$(git config --get remote.origin.url)
  if [ "$giturl" == "" ]
    then
     echo "Not a git repository or no remote.origin.url set"
     exit 1;
  fi
 
  giturl=${giturl/git\@github\.com\:/https://github.com/}
  branch="$(git symbolic-ref HEAD 2>/dev/null)" ||
  branch="(unnamed branch)"     # detached HEAD
  branch=${branch##refs/heads/}
 
  if [ "$1" = "" ]; then ## default => code
    giturl=${giturl/\.git/\/tree/}
    giturl=$giturl$branch
    open $giturl
  elif [ "$1" = "h" ]; then ## h => help
    echo ""
    echo "========================================"
    echo "Did you know that 'gh' can be used as is?"
    echo "There are also some other options, listed below:"
    echo "    [h]                      => View help"
    echo "    [c]                      => View commits"
    echo "    [c {SHA}]                => View specific commit from commit SHA"
    echo "    [b]                      => View branches"
    echo "    [pr]                     => View current branch compared to master"
    echo "    [pr branch]              => View current branch compared to specified branch"
    echo "    [i]                      => View issues"
    echo "    [a]                      => View assigned issues (in dev)"
    echo "    [w]                      => View wiki"
    echo "    [s]                      => View settings"
    echo "    [p]                      => View pulse"
    echo "    [g]                      => View graphs"
    echo "    [n]                      => View network"
    echo "    [<filename.ext>]         => Open a file in it's current state in the current branch"
    echo "========================================"
    echo ""
  elif [ "$1" = "c" ]; then ## c => commits
    if [ "$2" ]; then
      giturl=${giturl/\.git/\/commit/$2}
    else
      giturl=${giturl/\.git/\/commits/}
      giturl=$giturl$branch
    fi
    open $giturl
  elif [ "$1" = "b" ]; then ## b => branches
    giturl=${giturl/\.git/\/branches}
    open $giturl
  elif [ "$1" = "pr" ]; then ## pr => pull requests
    if [[ -z "$2" ]]; then
      # if a branch has not been specified, compare with master
      giturl=${giturl/\.git/\/compare/$branch?expand=1}
    else
      # if a branch has been specified, compare with current branch
      giturl=${giturl/\.git/\/compare/$2...$branch?expand=1}
    fi
    open $giturl
  elif [ "$1" = "i" ]; then ## i => issues
    giturl=${giturl/\.git/\/issues}
    open $giturl
  # This would be incredibly useful when I have the time
  # elif [ "$1" = "a" ]; then ## a => assigned issues
  #   giturl=${giturl/\.git/\/issues/assigned/$github_user}
  #   open $giturl
  elif [ "$1" = "w" ]; then ## w => wiki
    giturl=${giturl/\.git/\/wiki}
    open $giturl
  elif [ "$1" = "s" ]; then ## w => settings
    giturl=${giturl/\.git/\/settings}
    open $giturl
  elif [ "$1" = "p" ]; then ## w => pulse
    giturl=${giturl/\.git/\/pulse}
    open $giturl
  elif [ "$1" = "g" ]; then ## w => graphs
    giturl=${giturl/\.git/\/graphs}
    open $giturl
  elif [ "$1" = "n" ]; then ## w => network
    giturl=${giturl/\.git/\/network}
    open $giturl
  elif [ -f "$1" ]; then ## w => <filename.ext>
    giturl=${giturl/\.git/\/blob/$branch/$1}
    open $giturl
  # elif [ "$1" = "files" ]; then ## w => files
  #   giturl=${giturl/\.git/\/network}
  #   open $giturl
  else
    echo ""
    echo "========================================"
    echo "That option is invalid."
    echo "Did you mean:"
    echo "    [h]                      => View help"
    echo "    [c]                      => View commits"
    echo "    [c {SHA}]                => View specific commit from commit SHA"
    echo "    [b]                      => View branches"
    echo "    [pr]                     => View current branch compared to master"
    echo "    [pr branch]              => View current branch compared to specified branch"
    echo "    [i]                      => View issues"
    echo "    [a]                      => View assigned issues (in dev)"
    echo "    [w]                      => View wiki"
    echo "    [s]                      => View settings"
    echo "    [p]                      => View pulse"
    echo "    [g]                      => View graphs"
    echo "    [n]                      => View network"
    echo "    [<filename.ext>]         => Open a file in it's current state in the current branch"
    echo "========================================"
    echo ""
  fi
}

This is an adaptation of https://twitter.com/jasonneylon script.

In the terminal window, you can open your current repo (at the current branch) in your default browser.

My adaptation was adding options to view the commits, branches, pull requests or issues for the repo using one of the additional options (added support for wiki, settings, pulse, graphs, network):

[h]               => View help
[c]               => View commits
[c {SHA}]         => View specific commit from commit SHA
[b]               => View branches
[pr]              => View current branch compared to master
[pr branch]       => View current branch compared to specified branch
[i]               => View issues
[a]               => View assigned issues (in dev)
[w]               => View wiki
[s]               => View settings
[p]               => View pulse
[g]               => View graphs
[n]               => View network
[<filename.ext>]  => Open a file in it's current state in the current branch

Installation

This is a bash script, so it would be recommended that you include it in your ~/.bashrc (or ~/.bash_profile) file.

Using the command line (as it's generally easier than searching for potentially hidden files):

  • $ vi ~/.bash_rc - or use your preferred text editor e.g. mate, subl etc.
  • Paste function gh() { ... } into ~/.bashrc file
    • If you opted to use $ vi, you may need to press a to allow you to edit the file
  • Save .bashrc (you may also close the file)
    • If you opted to use $ vi, save and close the file: ESC and :wq
  • $ source ~/.bashrc - updates the Terminal/Shell with your changes, activating them ready for use

Usage

  • $ gh Open your repo from the command line
  • $ gh i View/list helper options
  • $ gh c Open your branches Commits from the command line
  • [c {SHA}] View specific commit from commit SHA
  • $ gh b Open your repo's Branches from the command line
  • $ gh pr Compare the current branch to master from the command line, ready for a pull request
  • $ gh pr branch Compare the current branch to the branch specified from the command line, ready for a pull request
  • $ gh i Open your repo's Issues from the command line
  • $ gh w Open your repo's Wiki from the command line
  • $ gh s Open your repo's Settings from the command line
  • $ gh p Open your repo's Pulse Activity from the command line
  • $ gh g Open your repo's Graphs/Analytics from the command line
  • $ gh n Open your repo's Network Graph from the command line
  • $ gh <filename.ext> Open a file in it's current state in the current branch

Improvements

I plan on making some improvements to this script (when I get the time to study bash/github a little more):

  • Add elif [ "$1" = "a" ]; to only show Issues assigned to a particular user, either the current user, or a specified user

Thanks for using

Any thoughts, suggestions or improvements? Let me know https://twitter.com/carmat71

Source: http://jasonneylon.wordpress.com/2011/04/22/opening-github-in-your-browser-from-the-terminal/

N.B. This script works for most* operating systems, although it does involve editing slightly. The only parts that should need editing are wher you see the command open. This is specific to Mac OS. Other OS's will need to be customised like so:

  • Mac open
  • Windows start or cmd /c start
  • Linux/Unix xdg-open
  • Cygwin cygstart

*Mac OS and MS Windows are the only operating sytems I have been able to test.