├── octobiwan.jpg ├── package.json ├── README.md └── bin └── ghwd /octobiwan.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/github-modules/ghwd/HEAD/octobiwan.jpg -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ghwd", 3 | "version": "1.1.4", 4 | "description": "Open the GitHub URL that matches your shell's current branch and working directory. Works for BitBucket and GitLab too.", 5 | "keywords": [ 6 | "github", 7 | "repo", 8 | "productivity", 9 | "shortcut", 10 | "bash", 11 | "shell", 12 | "bitbucket", 13 | "gitlab" 14 | ], 15 | "repository": "https://github.com/zeke/ghwd", 16 | "author": "Zeke Sikelianos (http://zeke.sikelianos.com/)", 17 | "license": "ISC", 18 | "bin": { 19 | "ghwd": "./bin/ghwd", 20 | "g": "./bin/ghwd" 21 | }, 22 | "preferGlobal": true, 23 | "scripts": { 24 | "test": "echo yolo" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ghwd 2 | 3 | A shell command to open a git URL in your browser that matches your 4 | shell's current branch and working directory. Works for GitHub, 5 | BitBucket, and GitLab repositories. 6 | 7 | Runs on Mac, Linux, Cygwin, and Windows. 8 | 9 | ## Install it 10 | 11 | ```sh 12 | npm install ghwd --global 13 | ``` 14 | 15 | ## Use it 16 | 17 | ```sh 18 | cd myapp 19 | git checkout mybranch 20 | cd some/dir 21 | 22 | ghwd 23 | # https://github.com/bozo/myapp/tree/mybranch/some/dir 24 | 25 | ghwd foo.js 26 | # https://github.com/bozo/myapp/tree/mybranch/some/dir/foo.js 27 | ``` 28 | 29 | ## Use it faster 30 | 31 | A `g` alias is created automatically to save you three keystrokes: 32 | 33 | ```sh 34 | g foo.js 35 | ``` 36 | 37 | ## System Requirements 38 | 39 | Requires the `bash` or `zsh` shell to run. Supports Mac, Linux, Cygwin and Windows by 40 | searching for one of the following openers: `xdg-open`, `open`,`cygstart`,`start`. 41 | 42 | ## Providers 43 | 44 | `ghwd` supports git repos from the following providers: 45 | 46 | - [github.com](https://github.com) ssh, https and `git:` remotes 47 | - [bitbucket.org](https://bitbucket.org) ssh and https remotes 48 | - [gitlab.com](https://gitlab.com) ssh and https remotes 49 | 50 | ## See Also 51 | 52 | If you are interested in further integration between your shell and Github, 53 | check out the [hub](https://github.com/github/hub/) project. It adds a [git 54 | browse](https://github.com/github/hub/#git-browse) command which works much 55 | like this one, in addition to a number of other features. 56 | 57 | ## License 58 | 59 | [ISC](http://opensource.org/licenses/ISC) 60 | 61 | ![octobiwan](octobiwan.jpg) 62 | -------------------------------------------------------------------------------- /bin/ghwd: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Figure out github repo base URL 4 | base_url=$(git config --get remote.origin.url) 5 | base_url=${base_url%\.git} # remove .git from end of string 6 | 7 | # Fix git@github.com: URLs 8 | base_url=${base_url//git@github\.com:/https:\/\/github\.com\/} 9 | 10 | # Fix git://github.com URLS 11 | base_url=${base_url//git:\/\/github\.com/https:\/\/github\.com\/} 12 | 13 | # Fix git@bitbucket.org: URLs 14 | base_url=${base_url//git@bitbucket.org:/https:\/\/bitbucket\.org\/} 15 | 16 | # Fix git@gitlab.com: URLs 17 | base_url=${base_url//git@gitlab\.com:/https:\/\/gitlab\.com\/} 18 | 19 | # Validate that this folder is a git folder 20 | git branch 2>/dev/null 1>&2 21 | if [ $? -ne 0 ]; then 22 | echo Not a git repo! 23 | exit $? 24 | fi 25 | 26 | # Find current directory relative to .git parent 27 | full_path=$(pwd) 28 | git_base_path=$(cd ./$(git rev-parse --show-cdup); pwd) 29 | relative_path=${full_path#$git_base_path} # remove leading git_base_path from working directory 30 | 31 | # If filename argument is present, append it 32 | if [ "$1" ]; then 33 | relative_path="$relative_path/$1" 34 | fi 35 | 36 | # Figure out current git branch 37 | # git_where=$(command git symbolic-ref -q HEAD || command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null 38 | git_where=$(command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null 39 | 40 | # Remove cruft from branchname 41 | branch="${git_where#refs\/heads\/}" 42 | branch="${git_where#remotes\/origin\/}" 43 | branch="${branch#tags\/}" 44 | branch="${branch%^0}" 45 | 46 | [[ $base_url == *bitbucket* ]] && tree="src" || tree="tree" 47 | url="$base_url/$tree/$branch$relative_path" 48 | 49 | echo "$url" 50 | 51 | # Check for various OS openers. Quit as soon as we find one that works. 52 | # Don't assume this will work, provide a helpful diagnostic if it fails. 53 | for opener in xdg-open open cygstart "start"; { 54 | if command -v $opener; then 55 | open=$opener; 56 | break; 57 | fi 58 | } 59 | 60 | $open "$url" || (echo "Unrecognized OS: Expected to find one of the following launch commands: xdg-open, open, cygstart, start" && exit 1); 61 | --------------------------------------------------------------------------------