-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote
More file actions
executable file
·81 lines (74 loc) · 2.23 KB
/
remote
File metadata and controls
executable file
·81 lines (74 loc) · 2.23 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/sh
#/ remote: Opens remote git repository in your broswer
#/
#/ Description:
#/ If no file is specified, then the repository's main page is opened. Note that if
#/ '-s|--start-line' or '-e|--end-line' are supplied, then only one file can be supllied.
#/
#/ Usage:
#/ remote [-h|--help] [-b|--branch branch_name] [-s|--start-line line_num] [-e|--end-line line_num] [file...]
set -e
open=$(command -v open || command -v xdg-open)
branch="$(git branch --show-current)"
start_line=
end_line=
for i in "$@"; do
case $i in
-h|--help)
grep ^\#\/ < "$0" | cut -c4-
exit 1
;;
-b|--branch)
branch="$2"
shift; shift
;;
-s|--start-line)
start_line="$2"
shift; shift
;;
-e|--end-line)
end_line="$2"
shift; shift
;;
esac
done
[ -n "$end_line" ] && [ -z "$start_line" ] && {
echo "Endline specified without a startline!"
exit 1
}
git ls-remote --heads --exit-code origin "$branch" >/dev/null || {
default_branch=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
echo "WARN: Branch '$branch' was not found on the origin repo, defaulting to $default_branch"
branch=$default_branch
}
remote=$(git remote -v | grep fetch | awk '{ print $2 }')
case $remote in
https://*)
;;
git@*)
remote=$(echo "$remote" |\
awk 'BEGIN { FS = "[@:.]" } ; { print "https://"$2"."$3"/"$4 }')
;;
*)
echo "Unknown remote format" >&2
exit 1
;;
esac
[ "$#" -lt 1 ] && {
$open "$remote/tree/$branch"
exit
}
break_early=false
[ -n "$start_line" ] && [ "$#" -gt 1 ] && {
echo "WARN: Multiple files specified when using '-s|--start-line' flag; defaulting to only opening the first file."
break_early=true
}
for file in "$@"; do
repository_root=$(basename $(git rev-parse --show-toplevel))
path=$(pwd | sed "s/.*\/$repository_root\/\{0,1\}\(.*\)/\1/")
line_range=""
[ -n "$start_line" ] && line_range="$line_range#L$start_line"
[ -n "$end_line" ] && line_range="$line_range-L$end_line"
$open "$remote/blob/$branch/$path/$file$line_range"
$break_early && exit
done