+#!/bin/bash
+
+err() { echo "error: $*" >&2; exit 1; }
+
+_cvs_url() {
+ [[ $# -gt 1 ]] && err "accepted args: <file>[:rev1[:rev2]]"
+
+ # spec has the form file:rev1[:rev2]
+ # rev2 defaults to rev1-1
+ export IFS=:
+ set -- $1
+ unset IFS
+
+ local file=$1
+ if [[ ! -e ${file} ]] ; then
+ echo "file '${file}' does not exist"
+ return 1
+ fi
+ local dir="."
+ [[ ${file} == */* ]] && dir=${file%/*}
+ file=${file##*/}
+
+ local rev2=$2
+ if [[ -z ${rev2} ]] ; then
+ rev2=$(
+ cd ${dir}
+ export IFS=/
+ set -- $(grep /${file}/ CVS/Entries)
+ unset IFS
+ echo $3
+ )
+ if [[ ${rev2} == "0" ]] ; then
+ # new file
+ rev2="1.1"
+ else
+ # existing file, bump rev automatically
+ rev2="1.$((${rev2#1.}+1))"
+ fi
+ fi
+ local rev2r=${rev2#1.}
+ local rev1=${3:-1.$((rev2r - 1))}
+
+ local cvsroot=$(<${dir}/CVS/Repository)
+ if [[ ${cvsroot} == gentoo-x86* ]] ; then
+ cvsroot=${cvsroot#gentoo-x86}
+ cvsroot=${cvsroot#/}
+ fi
+
+ local urirev
+ [[ ${rev2} == "1.1" || ${rev1} == "${rev2}" ]] \
+ && urirev="?rev=${rev2}" \
+ || urirev="?r1=${rev1}&r2=${rev2}"
+ echo "http://sources.gentoo.org/${cvsroot}/${file}${urirev}"
+}
+cvs_url() {
+ local f
+ for f in "$@" ; do
+ _cvs_url "${f}"
+ done
+}
+
+git_url() {
+ [[ $# -ne 0 ]] && err "no args supported"
+
+ local repo url
+ local remote=$(git config remote.origin.url)
+ case ${remote} in
+ *://uclibc.org/*|*://git.uclibc.org/*|\
+ *://busybox.net/*|*://git.busybox.net/*|\
+ *://buildroot.org/*|*://git.buildroot.org/*|\
+ *://buildroot.net/*|*://git.buildroot.net/*)
+ repo=$(echo "${remote}" | sed -e 's,^[^:]*://[^/]*/,,' -e 's:[.]git$::')
+ url="https://git.${repo,,}.org/${repo}/commit/?id="
+ ;;
+ *://git@git.gentoo.org/*|\
+ *://anongit.gentoo.org/*)
+ repo=$(echo "${remote}" | sed 's:.*git[.a-z]*.gentoo.org/::')
+ url="http://gitweb.gentoo.org/${repo}/commit/?id="
+ ;;
+ git@github.com/*|\
+ *://github.com/*)
+ repo=$(echo "${remote}" | sed -e 's,^git@github.com/,,' -e 's,^https://github.com/,,' -e 's:[.]git$::')
+ url="https://github.com/${repo}/commit/"
+ ;;
+ *)
+ echo "Unknown remote: ${remote}"
+ exit 1
+ ;;
+ esac
+
+ git log -n3 ${1:-HEAD} | sed "s,^commit ,${url},"
+}
+
+svn_url() {
+ if [[ $# -eq 0 ]] ; then
+ svn info | \
+ awk '{
+ if ($1 == "URL:") {
+ URL = "http://sources.gentoo.org/" gensub(/.*svnroot\/([^/]*).*/,"\\1","");
+ } else if ($1 == "Revision:") {
+ rev = $2
+ URL = URL "?rev=" (rev + 1) "&view=rev"
+ }
+ }
+ END { print URL }'
+ return 0
+ fi
+
+ local f
+ for f in "$@" ; do
+ svn info "${f}" | \
+ awk '{
+ if ($1 == "URL:") {
+ sub(/.*svnroot/,"")
+ URL = "http://sources.gentoo.org" $1 "?"
+ } else if ($1 == "Revision:") {
+ rev = $2
+ URL = URL "r1=" rev "&r2=" (rev + 1)
+ }
+ }
+ END { print URL }'
+ done
+}
+
+usage() {
+ exec cat <<-EOF
+ Usage: $0 [options] [args]
+
+ Options:
+ -c CVS URL
+ -g GIT URL (default)
+ -s SVN URL
+ -x Trace the script
+ -h Help
+ EOF
+}
+
+main() {
+ local vcs="git"
+ while [[ $# -gt 0 ]] ; do
+ case $1 in
+ -c) vcs="cvs";;
+ -s) vcs="svn";;
+ -g) vcs="git";;
+ -x) set -x;;
+ -h) usage;;
+ --) shift; break;;
+ -*) usage;;
+ *) break;;
+ esac
+ shift
+ done
+
+ case ${vcs} in
+ cvs) cvs_url "$@";;
+ git) git_url "$@";;
+ svn) svn_url "$@";;
+ esac
+}
+main "$@"