#!/bin/bash err() { echo "error: $*" >&2; exit 1; } _cvs_url() { [[ $# -gt 1 ]] && err "accepted args: [: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 "https://sources.gentoo.org/${cvsroot}/${file}${urirev}" } cvs_url() { local f for f in "$@" ; do _cvs_url "${f}" done } git_url() { [[ $# -gt 1 ]] && err "accepted args: " local repo url local remote=$(git config remote.origin.url) if [[ -z ${remote} ]] ; then # Maybe the repo doesn't use "origin". Try harder. local branch=$(git rev-parse --abbrev-ref HEAD) remote=$(git config "branch.${branch}.remote") if [[ -n ${remote} ]] ; then remote=$(git config "remote.${remote}.url") else # Still try harder. local remotes=$(git config --get-regexp 'remote\..*\.url') if [[ -n ${remotes} ]]; then remote=$(echo "${remotes}" | awk '{print $2; exit}') fi fi fi 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." case ${repo} in uclibc) url+="uclibc.org" ;; buildroot) url+="buildroot.org" ;; *) url+="busybox.net" ;; esac url+="/${repo}/commit/?id=" ;; *://git@git.gentoo.org/*|\ *://anongit.gentoo.org/*) repo=$(echo "${remote}" | sed 's:.*git[.a-z]*.gentoo.org/::') url="https://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://github.com/,,' -e 's:[.]git$::') url="https://github.com/${repo}/commit/" ;; git@gitlab.com[:/]*|\ *://gitlab.com/*) repo=$(echo "${remote}" | sed -e 's,^git@gitlab.com[:/],,' -e 's,^https://gitlab.com/,,' -e 's,^git://gitlab.com/,,' -e 's:[.]git$::') url="https://gitlab.com/${repo}/commit/" ;; git://git.sv.gnu.org/*|git://git.savannah.gnu.org/*|https://git.savannah.gnu.org/*) repo=$(echo "${remote}" | sed -E -e 's,^(git|https)://git.(sv|savannah).gnu.org/(git/)?,,' -e 's:[.]git$::') url="https://git.savannah.gnu.org/cgit/${repo}.git/commit/?h=" ;; git://git.sv.nongnu.org/*|git://git.savannah.nongnu.org/*) repo=$(echo "${remote}" | sed -r -e 's,^git://git.(sv|savannah).nongnu.org/,,' -e 's:[.]git$::') url="https://git.savannah.nongnu.org/cgit/${repo}.git/commit/?h=" ;; git://git.code.sf.net/p/*|ssh://*@git.code.sf.net/p/*) repo=$(echo "${remote}" | sed -r -e 's,(git://|ssh://([^@]*@)?)git.code.sf.net/p/([^/]*)/.*,\3,') url="https://sourceforge.net/p/${repo}/code/ci/" ;; *://sourceware.org/*) repo=$(echo "${remote}" | sed -e 's,.*/,,' -e 's,[.]git$,,') url="https://sourceware.org/git/?p=${repo}.git;a=commit;h=" ;; *://gcc.gnu.org/*) repo=$(echo "${remote}" | sed -e 's,.*/,,' -e 's,[.]git$,,') url="https://gcc.gnu.org/git/?p=${repo}.git;a=commit;h=" ;; rpc://*) url="${remote#rpc://}" url="https://${url/\//.googlesource.com/}/+/" ;; *.googlesource.com/*) url="${remote%.git}/+/" ;; *://git.enlightenment.org/*) repo=$(echo "${remote}" | sed -e 's,^[^/]*//[^/]*/,,' -e 's,[.]git$,,') url="https://git.enlightenment.org/${repo}.git/commit/?id=" ;; *://git.qemu.org/*) repo=$(echo "${remote}" | sed -e 's,^[^/]*//[^/]*/,,' -e 's,[.]git$,,') url="https://git.qemu.org/?p=${repo}.git;a=commit;h=" ;; *://git.kernel.org/*) repo=$(echo "${remote}" | sed -e 's,^[^/]*//[^/]*/,,' -e 's,[.]git$,,') url="https://git.kernel.org/${repo}.git/commit/?id=" ;; *) echo "Unknown remote: '${remote}'" exit 1 ;; esac git log -n${num} ${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", 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: -# Number of commits to show (default: 3) -n# Number of commits to show -c CVS URL -g GIT URL (default) -s SVN URL -x Trace the script -h Help EOF } main() { local vcs="auto" num="3" args=() while [[ $# -gt 0 ]] ; do case $1 in -n[0-9]*) num="${1:2}";; -[0-9]*) num="${1:1}";; -c) vcs="cvs";; -s) vcs="svn";; -g) vcs="git";; -x) set -x;; -h) usage;; --) shift; break;; -*) usage;; *) args+=( "$1" );; esac shift done set -- "${args}" if [[ ${vcs} == "auto" ]] ; then if [[ -d CVS ]] ; then vcs="cvs" elif svn info >&/dev/null ; then vcs="svn" else vcs="git" fi fi case ${vcs} in cvs) cvs_url "$@";; git) git_url "$@";; svn) svn_url "$@";; esac } main "$@"