dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | README | LICENSE Back to index

commit b9f8d64dac5c66ae1d0ab3755df47d5bf0486501
parent 8d25d7294f2bbe12f23958139befdc867c745957
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Sun, 12 Jan 2020 08:22:09 +0100

Fix verbose flag and infinite loop on root

Diffstat:
M.local/bin/gitrepo | 62+++++++++++++++++++++++++++++++++++++-------------------------
1 file changed, 37 insertions(+), 25 deletions(-)

diff --git a/.local/bin/gitrepo b/.local/bin/gitrepo @@ -1,8 +1,15 @@ #!/bin/sh -version=0.1.0 +version=0.1.1 -show_help() { +die() +{ + printf '%s: error: %s\n' "${0##*/}" "$1" >&2 + exit 1 +} + +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." @@ -15,13 +22,25 @@ show_help() { echo " -- do not consider any following args as options" } -get_git_root() { +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" +} + +get_git_root() +{ local _d="$1" while :; do + if [ "$verbose" = 1 ]; then + printf 'git_get_root: %s\n' "$_d" >&2 + fi if [ -e "$_d/.git/config" ]; then echo "$_d" break - elif [ "$_d" = "/" ]; then + elif [ "$_d" = "" ]; then die 'no git repository found' else _d="${_d%/*}" @@ -29,16 +48,18 @@ get_git_root() { done } -get_remote_url() { +get_remote_url() +{ local _conf="$1/.git/config" if [ ! -r "$_conf" ]; then - die "$1: git config file $_conf not found or not readable" + die "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() { +url_to_http() +{ case "$1" in http://*|https://*) echo "$1";; @@ -46,16 +67,19 @@ url_to_http() { echo "$1" | sed 's/git:/http:/';; *@*:*) echo "$1" | sed 's/:/\//;s/.*@/http:\/\//';; + "") + die "remote url empty";; *) - die "unknown remote url format: $1";; + 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")" +handle_path() +{ + local _root _url _httpurl + _root="$(get_git_root "$1")" + _url="$(get_remote_url "$_root")" + _httpurl="$(url_to_http "$_url")" if [ "$verbose" = 1 ]; then echo "url:\t$_url" echo "root:\t$_root" @@ -67,18 +91,6 @@ handle_path() { 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