]> git.wh0rd.org - home.git/commitdiff
vcs-url: generalize gentoo helpers
authorMike Frysinger <vapier@gentoo.org>
Wed, 16 Dec 2015 18:34:40 +0000 (13:34 -0500)
committerMike Frysinger <vapier@gentoo.org>
Wed, 16 Dec 2015 18:34:40 +0000 (13:34 -0500)
.bin/vcs-url [new file with mode: 0755]
.profile.d/gentoo.sh

diff --git a/.bin/vcs-url b/.bin/vcs-url
new file mode 100755 (executable)
index 0000000..ccda748
--- /dev/null
@@ -0,0 +1,160 @@
+#!/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 "$@"
index 1627c0d2df4bd00cf3729fa3ee2798c612d4dc1b..b4d355808946f53158712b9194f46128d9b1d36f 100644 (file)
@@ -2,102 +2,6 @@ alias ebuild-sync='rsync -av ./ /usr/portage/${PWD#/usr/local/src/gentoo/repo/ge
 alias ebuild-emerge='sudo emerge -1av $(echo ${PWD} | awk -F/ "{printf \"%s/%s\", \$(NF-1), \$(NF)}")'
 alias ekeyword='ekeyword -v -q'
 
-cvs_gentoo_url() {
-_cvs_gentoo_url() {
-       if [[ -n $2 ]] ; then
-               echo "Usage: cvs_gentoo_url <file>[:rev1[:rev2]]"
-               return 1
-       fi
-
-       # 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}"
-}
-       local f
-       for f in "$@" ; do
-               _cvs_gentoo_url "${f}"
-       done
-       unset _cvs_gentoo_url
-}
-git_gentoo_url() {
-       local repo=$(git config remote.origin.url | sed 's:.*git[.a-z]*.gentoo.org/::')
-       git log -n3 ${1:-HEAD} | \
-               sed "s,^commit ,http://gitweb.gentoo.org/${repo}/commit/?id=,"
-}
-svn_gentoo_url() {
-       if [[ -z $* ]] ; 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
-}
-
 _echangelog() {
        [[ ! -e ChangeLog ]] \
                && echo "No ChangeLog" \