dotfiles

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

gitrepo (2421B)


      1 #!/bin/sh
      2 
      3 version=0.1.1
      4 
      5 die()
      6 {
      7 	printf '%s: error: %s\n' "${0##*/}" "$1" >&2
      8 	exit 1
      9 }
     10 
     11 show_help()
     12 {
     13 	echo "usage: ${0##*/} [OPTIONS] [PATH ...]"
     14 	echo "open inferred http url to repository in PATH."
     15 	echo "If PATH is not specified, the current directory is used."
     16 	echo
     17 	echo "OPTIONS are one or more of the following:"
     18 	echo "   -h, --help      show this message"
     19 	echo "   -v, --version   show version and license information"
     20 	echo "   -V, --verbose   show version and license information"
     21 	echo "   -o, --open      open remote http url with xdg-open"
     22 	echo "   --              do not consider any following args as options"
     23 }
     24 
     25 show_version()
     26 {
     27 	echo "${0##*/} version $version"
     28 	echo "Licensed under the ISC License"
     29 	echo "written by Anders Damsgaard, anders@adamsgaard.dk"
     30 	echo "https://src.adamsgaard.dk/dotfiles"
     31 }
     32 
     33 get_git_root()
     34 {
     35 	local _d="$1"
     36 	while :; do
     37 		if [ "$verbose" = 1 ]; then
     38 			printf 'git_get_root: %s\n' "$_d" >&2
     39 		fi
     40 		if [ -e "$_d/.git/config" ]; then
     41 			echo "$_d"
     42 			break
     43 		elif [ "$_d" = "" ]; then
     44 			die 'no git repository found'
     45 		else
     46 			_d="${_d%/*}"
     47 		fi
     48 	done
     49 }
     50 
     51 get_remote_url()
     52 {
     53 	local _conf="$1/.git/config"
     54 	if [ ! -r "$_conf" ]; then
     55 		die "git config file $_conf not found or not readable"
     56 	fi
     57 	awk '/\[ *remote +"origin" *\]/{f=1; next} f && /url *= */{print $0; f=0}' "$_conf" | \
     58 		sed 's/.*= *//'
     59 }
     60 
     61 url_to_http()
     62 {
     63 	case "$1" in
     64 		http://*|https://*)
     65 			echo "$1";;
     66 		git://*)
     67 			echo "$1" | sed 's/git:/http:/';;
     68 		*@*:*)
     69 			echo "$1" | sed 's/:/\//;s/.*@/http:\/\//';;
     70 		"")
     71 			die "remote url empty";;
     72 		*)
     73 			die "unknown remote url format: '$1'";;
     74 	esac
     75 }
     76 
     77 handle_path()
     78 {
     79 	local _root _url _httpurl
     80 	_root="$(get_git_root "$1")"
     81 	_url="$(get_remote_url "$_root")"
     82 	_httpurl="$(url_to_http "$_url")"
     83 	if [ "$verbose" = 1 ]; then
     84 		echo "url:\t$_url"
     85 		echo "root:\t$_root"
     86 		echo "http:\t$_httpurl"
     87 	fi
     88 	echo "$_httpurl"
     89 	if [ "$open" = 1 ]; then
     90 		xdg-open "$_httpurl"
     91 	fi
     92 }
     93 
     94 verbose=0
     95 open=0
     96 while :; do
     97 	case "$1" in
     98 		-h|-\?|--help)
     99 			show_help
    100 			exit 0
    101 			;;
    102 		-v|--version)
    103 			show_version
    104 			exit 0
    105 			;;
    106 		-V|--verbose)
    107 			verbose=1
    108 			;;
    109 		-o|--open)
    110 			open=1
    111 			;;
    112 		--) # end all options
    113 			shift
    114 			break
    115 			;;
    116 		-?*)
    117 			die 'unknown option specified'
    118 			;;
    119 		*)  # No more options
    120 			break
    121 	esac
    122 	shift
    123 done
    124 
    125 if [ $# -lt 1 ]; then
    126 	handle_path "$(pwd)"
    127 	exit 0
    128 fi
    129 
    130 for path in "$@"; do
    131 	cd "$path"
    132 	handle_path "$(pwd)"
    133 	cd - >/dev/null
    134 done