commit 8b182cf2ec96b0b1cc001194e308509922ba051d
parent cf112496c44942c93d6fc0ad4fbd40974c66a84b
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Thu, 9 Jan 2020 15:56:09 +0100
Add script to get and open remote http url in browser
Diffstat:
A | .local/bin/gitrepo | | | 122 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 122 insertions(+), 0 deletions(-)
diff --git a/.local/bin/gitrepo b/.local/bin/gitrepo
@@ -0,0 +1,122 @@
+#!/bin/sh
+
+version=0.1.0
+
+show_help() {
+ echo "usage: ${0##*/} [OPTIONS] [PATH ...]"
+ echo "open inferred http url to repository in PATH."
+ echo "If PATH is not specified, the current directory is used."
+ echo
+ echo "OPTIONS are one or more of the following:"
+ echo " -h, --help show this message"
+ echo " -v, --version show version and license information"
+ echo " -V, --verbose show version and license information"
+ echo " -o, --open open remote http url with xdg-open"
+ echo " -- do not consider any following args as options"
+}
+
+get_git_root() {
+ local _d="$1"
+ while :; do
+ if [ -e "$_d/.git/config" ]; then
+ echo "$_d"
+ break
+ elif [ "$_d" = "/" ]; then
+ die 'no git repository found'
+ else
+ _d="${_d%/*}"
+ fi
+ done
+}
+
+get_remote_url() {
+ local _conf="$1/.git/config"
+ if [ ! -r "$_conf" ]; then
+ die "$1: git config file $_conf not found or not readable"
+ fi
+ awk '/\[ *remote +"origin" *\]/{f=1; next} f && /url *= */{print $0; f=0}' "$_conf" | \
+ sed 's/.*= *//'
+}
+
+url_to_http() {
+ case "$1" in
+ http://*|https://*)
+ echo "$1";;
+ git://*)
+ echo "$1" | sed 's/git:/http:/';;
+ *@*:*)
+ echo "$1" | sed 's/:/\//;s/.*@/http:\/\//';;
+ *)
+ die "unknown remote url format: $1";;
+ esac
+}
+
+handle_path() {
+ #echo "$(url_to_http "$(get_remote_url "$(get_git_root "$1")")")"
+ local _root="$(get_git_root "$1")"
+ local _url="$(get_remote_url "$_root")"
+ local _httpurl="$(url_to_http "$_url")"
+ if [ "$verbose" = 1 ]; then
+ echo "url:\t$_url"
+ echo "root:\t$_root"
+ echo "http:\t$_httpurl"
+ fi
+ echo "$_httpurl"
+ if [ "$open" = 1 ]; then
+ xdg-open "$_httpurl"
+ fi
+}
+
+show_version() {
+ echo "${0##*/} version $version"
+ echo "Licensed under the ISC License"
+ echo "written by Anders Damsgaard, anders@adamsgaard.dk"
+ echo "https://src.adamsgaard.dk/dotfiles"
+}
+
+die() {
+ printf '%s: error: %s\n' "${0##*/}" "$1" >&2
+ exit 1
+}
+
+verbose=0
+open=0
+while :; do
+ case "$1" in
+ -h|-\?|--help)
+ show_help
+ exit 0
+ ;;
+ -v|--version)
+ show_version
+ exit 0
+ ;;
+ -V|--verbose)
+ verbose=1
+ ;;
+ -o|--open)
+ open=1
+ ;;
+ --) # end all options
+ shift
+ break
+ ;;
+ -?*)
+ die 'unknown option specified'
+ ;;
+ *) # No more options
+ break
+ esac
+ shift
+done
+
+if [ $# -lt 1 ]; then
+ handle_path "$(pwd)"
+ exit 0
+fi
+
+for path in "$@"; do
+ cd "$path"
+ handle_path "$(pwd)"
+ cd - >/dev/null
+done