]> git.wh0rd.org Git - home.git/blob - .bin/vcs-url
cros-repo: run init with -c for speedup
[home.git] / .bin / vcs-url
1 #!/bin/bash
2
3 err() { echo "error: $*" >&2; exit 1; }
4
5 _cvs_url() {
6         [[ $# -gt 1 ]] && err "accepted args: <file>[:rev1[:rev2]]"
7
8         # spec has the form file:rev1[:rev2]
9         # rev2 defaults to rev1-1
10         export IFS=:
11         set -- $1
12         unset IFS
13
14         local file=$1
15         if [[ ! -e ${file} ]] ; then
16                 echo "file '${file}' does not exist"
17                 return 1
18         fi
19         local dir="."
20         [[ ${file} == */* ]] && dir=${file%/*}
21         file=${file##*/}
22
23         local rev2=$2
24         if [[ -z ${rev2} ]] ; then
25                 rev2=$(
26                         cd ${dir}
27                         export IFS=/
28                         set -- $(grep /${file}/ CVS/Entries)
29                         unset IFS
30                         echo $3
31                 )
32                 if [[ ${rev2} == "0" ]] ; then
33                         # new file
34                         rev2="1.1"
35                 else
36                         # existing file, bump rev automatically
37                         rev2="1.$((${rev2#1.}+1))"
38                 fi
39         fi
40         local rev2r=${rev2#1.}
41         local rev1=${3:-1.$((rev2r - 1))}
42
43         local cvsroot=$(<${dir}/CVS/Repository)
44         if [[ ${cvsroot} == gentoo-x86* ]] ; then
45                 cvsroot=${cvsroot#gentoo-x86}
46                 cvsroot=${cvsroot#/}
47         fi
48
49         local urirev
50         [[ ${rev2} == "1.1" || ${rev1} == "${rev2}" ]] \
51                 && urirev="?rev=${rev2}" \
52                 || urirev="?r1=${rev1}&r2=${rev2}"
53         echo "https://sources.gentoo.org/${cvsroot}/${file}${urirev}"
54 }
55 cvs_url() {
56         local f
57         for f in "$@" ; do
58                 _cvs_url "${f}"
59         done
60 }
61
62 git_url() {
63         [[ $# -gt 1 ]] && err "accepted args: <rev>"
64
65         local repo url
66         local remote=$(git config remote.origin.url)
67
68         if [[ -z ${remote} ]] ; then
69                 # Maybe the repo doesn't use "origin".  Try harder.
70                 local branch=$(git rev-parse --abbrev-ref HEAD)
71                 remote=$(git config "branch.${branch}.remote")
72                 if [[ -n ${remote} ]] ; then
73                         remote=$(git config "remote.${remote}.url")
74                 fi
75         fi
76
77         case ${remote} in
78         *://uclibc.org/*|*://git.uclibc.org/*|\
79         *://busybox.net/*|*://git.busybox.net/*|\
80         *://buildroot.org/*|*://git.buildroot.org/*|\
81         *://buildroot.net/*|*://git.buildroot.net/*)
82                 repo=$(echo "${remote}" | sed -e 's,^[^:]*://[^/]*/,,' -e 's:[.]git$::')
83                 url="https://git."
84                 case ${repo} in
85                 uclibc)    url+="uclibc.org" ;;
86                 buildroot) url+="buildroot.org" ;;
87                 *)         url+="busybox.net" ;;
88                 esac
89                 url+="/${repo}/commit/?id="
90                 ;;
91         *://git@git.gentoo.org/*|\
92         *://anongit.gentoo.org/*)
93                 repo=$(echo "${remote}" | sed 's:.*git[.a-z]*.gentoo.org/::')
94                 url="https://gitweb.gentoo.org/${repo}/commit/?id="
95                 ;;
96         git@github.com[:/]*|\
97         *://github.com/*)
98                 repo=$(echo "${remote}" | sed -e 's,^git@github.com[:/],,' -e 's,^https://github.com/,,' -e 's,^git://github.com/,,' -e 's:[.]git$::')
99                 url="https://github.com/${repo}/commit/"
100                 ;;
101         git@gitlab.com[:/]*|\
102         *://gitlab.com/*)
103                 repo=$(echo "${remote}" | sed -e 's,^git@gitlab.com[:/],,' -e 's,^https://gitlab.com/,,' -e 's,^git://gitlab.com/,,' -e 's:[.]git$::')
104                 url="https://gitlab.com/${repo}/commit/"
105                 ;;
106         git://git.sv.gnu.org/*|git://git.savannah.gnu.org/*)
107                 repo=$(echo "${remote}" | sed -r -e 's,^git://git.(sv|savannah).gnu.org/,,' -e 's:[.]git$::')
108                 url="http://git.savannah.gnu.org/cgit/${repo}.git/commit/?h="
109                 ;;
110         git://git.sv.nongnu.org/*|git://git.savannah.nongnu.org/*)
111                 repo=$(echo "${remote}" | sed -r -e 's,^git://git.(sv|savannah).nongnu.org/,,' -e 's:[.]git$::')
112                 url="http://git.savannah.nongnu.org/cgit/${repo}.git/commit/?h="
113                 ;;
114         git://git.code.sf.net/p/*|ssh://*@git.code.sf.net/p/*)
115                 repo=$(echo "${remote}" | sed -r -e 's,(git://|ssh://([^@]*@)?)git.code.sf.net/p/([^/]*)/.*,\3,')
116                 url="https://sourceforge.net/p/${repo}/code/ci/"
117                 ;;
118         *://sourceware.org/*)
119                 repo=$(echo "${remote}" | sed -e 's,.*/,,' -e 's,[.]git$,,')
120                 url="https://sourceware.org/git/?p=${repo}.git;a=commit;h="
121                 ;;
122         *.googlesource.com/*)
123                 url="${remote%.git}/+/"
124                 ;;
125         *://git.enlightenment.org/*)
126                 repo=$(echo "${remote}" | sed -e 's,^[^/]*//[^/]*/,,' -e 's,[.]git$,,')
127                 url="https://git.enlightenment.org/${repo}.git/commit/?id="
128                 ;;
129         *://git.qemu.org/*)
130                 repo=$(echo "${remote}" | sed -e 's,^[^/]*//[^/]*/,,' -e 's,[.]git$,,')
131                 url="https://git.qemu.org/?p=${repo}.git;a=commit;h="
132                 ;;
133         *://git.kernel.org/*)
134                 repo=$(echo "${remote}" | sed -e 's,^[^/]*//[^/]*/,,' -e 's,[.]git$,,')
135                 url="https://git.kernel.org/${repo}.git/commit/?id="
136                 ;;
137         *)
138                 echo "Unknown remote: ${remote}"
139                 exit 1
140                 ;;
141         esac
142
143         git log -n3 ${1:-HEAD} | sed "s,^commit ,${url},"
144 }
145
146 svn_url() {
147         if [[ $# -eq 0 ]] ; then
148                 svn info | \
149                 awk '{
150                         if ($1 == "URL:") {
151                                 URL = "http://sources.gentoo.org/" gensub(/.*svnroot\/([^/]*).*/, "\\1", 1);
152                         } else if ($1 == "Revision:") {
153                                 rev = $2
154                                 URL = URL "?rev=" (rev + 1) "&view=rev"
155                         }
156                 }
157                 END { print URL }'
158                 return 0
159         fi
160
161         local f
162         for f in "$@" ; do
163                 svn info "${f}" | \
164                 awk '{
165                         if ($1 == "URL:") {
166                                 sub(/.*svnroot/,"")
167                                 URL = "http://sources.gentoo.org" $1 "?"
168                         } else if ($1 == "Revision:") {
169                                 rev = $2
170                                 URL = URL "r1=" rev "&r2=" (rev + 1)
171                         }
172                 }
173                 END { print URL }'
174         done
175 }
176
177 usage() {
178         exec cat <<-EOF
179         Usage: $0 [options] [args]
180
181         Options:
182           -c       CVS URL
183           -g       GIT URL (default)
184           -s       SVN URL
185           -x       Trace the script
186           -h       Help
187         EOF
188 }
189
190 main() {
191         local vcs="auto"
192         while [[ $# -gt 0 ]] ; do
193                 case $1 in
194                 -c) vcs="cvs";;
195                 -s) vcs="svn";;
196                 -g) vcs="git";;
197                 -x) set -x;;
198                 -h) usage;;
199                 --) shift; break;;
200                 -*) usage;;
201                 *)  break;;
202                 esac
203                 shift
204         done
205
206         if [[ ${vcs} == "auto" ]] ; then
207                 if [[ -d CVS ]] ; then
208                         vcs="cvs"
209                 elif svn info >&/dev/null ; then
210                         vcs="svn"
211                 else
212                         vcs="git"
213                 fi
214         fi
215
216         case ${vcs} in
217         cvs) cvs_url "$@";;
218         git) git_url "$@";;
219         svn) svn_url "$@";;
220         esac
221 }
222 main "$@"