rtomayko (owner)

Revisions

gist: 219241 Download_button fork
public
Public Clone URL: git://gist.github.com/219241.git
Embed All Files: show embed
github-open #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/sh
#
## Usage: github-open FILE [LINE]
## Open GitHub file/blob page for FILE on LINE. FILE is the path to the
## file on disk; it must exist and be tracked with the current HEAD
## revision. LINE is the line number or line number range (e.g., 10-50).
##
## Open foo/bar.rb in browser:
## $ github-open foo/bar.rb
##
## Open foo/bar in browser w/ lines 50-57 highlighted:
## $ github-open foo/bar.rb 50-57
##
## Open current file in vim on line 20:
## :!github-open % 20
#
 
FILE="$1"
LINE="$2"
 
# usage and help
test -z "$FILE" -o "$FILE" = '--help' && {
  cat "$0" | grep '^##' | cut -c4- 1>&2
  exit 1
}
 
# figure out relative path to the file from the root
# of the work tree
path="$(basename $FILE)"
cd $(dirname $FILE)
while test ! -d .git ;
do
test "$(pwd)" = / && {
    echo "error: git repository not found" 1>&2
    exit 1
  }
  path="$(basename $(pwd))/$path"
  cd ..
done
 
# at this point we're in root of the work tree and $path is
# the relative path to file.
ref=$(git rev-parse origin)
remote=$(git config --get remote.origin.url)
repo=$(echo $remote | sed 's/^.*:\(.*\)\.git/\1/')
url="http://github.com/$repo/blob/$ref/$path"
 
# throw the line number on there if specified
test -n "$LINE" && url="$url#L$LINE"
 
open "$url"