]> git.wh0rd.org Git - home.git/blob - .bin/vcs-url
4090c7708c24a9f02af13960fa84f44d302940c7
[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 "http://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         case ${remote} in
68         *://uclibc.org/*|*://git.uclibc.org/*|\
69         *://busybox.net/*|*://git.busybox.net/*|\
70         *://buildroot.org/*|*://git.buildroot.org/*|\
71         *://buildroot.net/*|*://git.buildroot.net/*)
72                 repo=$(echo "${remote}" | sed -e 's,^[^:]*://[^/]*/,,' -e 's:[.]git$::')
73                 url="https://git.${repo,,}.org/${repo}/commit/?id="
74                 ;;
75         *://git@git.gentoo.org/*|\
76         *://anongit.gentoo.org/*)
77                 repo=$(echo "${remote}" | sed 's:.*git[.a-z]*.gentoo.org/::')
78                 url="http://gitweb.gentoo.org/${repo}/commit/?id="
79                 ;;
80         git@github.com/*|\
81         *://github.com/*)
82                 repo=$(echo "${remote}" | sed -e 's,^git@github.com/,,' -e 's,^https://github.com/,,' -e 's:[.]git$::')
83                 url="https://github.com/${repo}/commit/"
84                 ;;
85         git://git.sv.gnu.org/*)
86                 repo=$(echo "${remote}" | sed -e 's,^git://git.sv.gnu.org/,,' -e 's:[.]git$::')
87                 url="http://git.savannah.gnu.org/cgit/${repo}.git/commit/?h="
88                 ;;
89         *)
90                 echo "Unknown remote: ${remote}"
91                 exit 1
92                 ;;
93         esac
94
95         git log -n3 ${1:-HEAD} | sed "s,^commit ,${url},"
96 }
97
98 svn_url() {
99         if [[ $# -eq 0 ]] ; then
100                 svn info | \
101                 awk '{
102                         if ($1 == "URL:") {
103                                 URL = "http://sources.gentoo.org/" gensub(/.*svnroot\/([^/]*).*/,"\\1","");
104                         } else if ($1 == "Revision:") {
105                                 rev = $2
106                                 URL = URL "?rev=" (rev + 1) "&view=rev"
107                         }
108                 }
109                 END { print URL }'
110                 return 0
111         fi
112
113         local f
114         for f in "$@" ; do
115                 svn info "${f}" | \
116                 awk '{
117                         if ($1 == "URL:") {
118                                 sub(/.*svnroot/,"")
119                                 URL = "http://sources.gentoo.org" $1 "?"
120                         } else if ($1 == "Revision:") {
121                                 rev = $2
122                                 URL = URL "r1=" rev "&r2=" (rev + 1)
123                         }
124                 }
125                 END { print URL }'
126         done
127 }
128
129 usage() {
130         exec cat <<-EOF
131         Usage: $0 [options] [args]
132
133         Options:
134           -c       CVS URL
135           -g       GIT URL (default)
136           -s       SVN URL
137           -x       Trace the script
138           -h       Help
139         EOF
140 }
141
142 main() {
143         local vcs="git"
144         while [[ $# -gt 0 ]] ; do
145                 case $1 in
146                 -c) vcs="cvs";;
147                 -s) vcs="svn";;
148                 -g) vcs="git";;
149                 -x) set -x;;
150                 -h) usage;;
151                 --) shift; break;;
152                 -*) usage;;
153                 *)  break;;
154                 esac
155                 shift
156         done
157
158         case ${vcs} in
159         cvs) cvs_url "$@";;
160         git) git_url "$@";;
161         svn) svn_url "$@";;
162         esac
163 }
164 main "$@"