-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhublab
executable file
·146 lines (133 loc) · 3.16 KB
/
hublab
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env bash
set -euo pipefail
SOFT=$(basename "$0")
usage() {
echo "$SOFT: usage"
echo
echo " $SOFT <path/to/fileA> "
echo
echo " opens fileA and folderB in the «origin»'s remote url, with current branch"
echo
echo ' -r | --remote <remote> use <remote> instead of origin'
echo ' -b | --branch <branch> forces '"$SOFT"' to open the file on <branch> instead'
echo ' --raw opens the file as raw'
echo
echo ' -e | --exec <command> executes command with output instead of echo. use {} to refer to the symbol'
}
err() {
>&2 echo -e "$SOFT: \e[31m$*\e[0m"
}
args=()
remote=origin
force_branch=
raw=false
if [ -n "${HUBLAB_EXECUTE-}" ]; then
execute="${HUBLAB_EXECUTE}"
else
execute="echo {}"
fi
while (( $# )); do
cmd=$1
shift
case "$cmd" in
-*=*)
set -- "${cmd/=*}" "${cmd#*=}" "$@"
;;
--help|-h)
usage
exit 0
;;
--remote|-r)
remote=$1
shift
;;
--branch|-b)
force_branch=$1
shift
;;
--raw)
raw=true
;;
--execute | -e)
execute=$1
shift
;;
--)
args+=("$@")
shift $#
;;
-*)
if [ "${#cmd}" -gt 2 ]; then
set -- "${cmd:0:2}" "-${cmd:2}" "$@"
continue
else
err "unknown flag $cmd"
exit 1
fi
;;
*)
args+=("$cmd")
;;
esac
done
set -- "${args[@]}"
resolveHubLab () {
case "$1" in
*gitlab*)
echo "$1/-"
;;
*)
echo "$1"
;;
esac
}
computeUrl() (
target=$(realpath "$1")
file=$target
type=tree
if [ -f "$file" ]; then
if $raw; then
type=raw
else
type=blob
fi
target=$(dirname "$file")
fi
project=$(git -C "$target" rev-parse --show-toplevel)
address=$(git -C "$project" remote get-url $remote)
address=${address/.git}
address=${address#*@}
hublab_domain=${address/:*}
project_path=${address#*:}
base="$hublab_domain/$project_path"
if [ "$project" == "$file" ]; then
echo "https://$base"
return
fi
semipath=${file:$((${#project} + 1))}
if [ -n "$force_branch" ]; then
branch=$force_branch
else
set +e
branch=$(git -C "$project" branch --show-current)
if [ -z "$branch" ]; then
err "no branches available when trying to open $1"
exit 1
fi
set -e
fi
semipath=${semipath// /%20}
echo "https://$(resolveHubLab "$base")/$type/$branch/$semipath"
)
if [ "${#args[@]}" -gt 0 ]; then
for page in "${args[@]}"; do
${execute//\{\}/$(computeUrl "$page")}
done
else
remote=$(git remote get-url origin)
remote=${remote/.git}
remote=${remote#*@}
hublab_domain=${remote/:*}
project_path=${remote#*:}
${execute/{}/"https://$hublab_domain/$project_path"}
fi