gitrefresh

Easily export, reinitialize and update all git repositories in a file hierarchy
git clone git://git.defalsify.org/gitrefresh.git
Info | Log | Files | Refs | README | LICENSE

gitlist.sh (2062B)


      1 #!/bin/bash
      2 
      3 _level=3
      4 while test $# != 0; do
      5 	case "$1" in
      6 		-v)
      7 			_debug=1
      8 			shift
      9 			_level=$1
     10 			shift
     11 			;;
     12 		-n)
     13 			_no_remotes=1
     14 			shift
     15 			;;
     16 		-u)
     17 			_update=1
     18 			_no_remotes=1
     19 			shift
     20 			;;
     21 		-p)
     22 			_path=1
     23 			shift
     24 			;;
     25 		-pp)
     26 			_fullpath=1
     27 			_path=1
     28 			shift
     29 			;;
     30 
     31 		*)
     32 			break
     33 			;;
     34 	esac
     35 done
     36 
     37 dbg() {
     38 	if [ "$1" -lt "$_level" ]; then
     39 		return 0
     40 	fi
     41 
     42 	case "$1" in
     43 		1)
     44 			lvl='debug'
     45 			clr='\e[0;36m'
     46 			;;
     47 		2)
     48 			lvl='info'
     49 			clr='\e[0;32m'
     50 			;;
     51 
     52 		3)
     53 			lvl='warn'
     54 			clr='\e[0;33m'
     55 			;;
     56 		4)
     57 			lvl='error'
     58 			clr='\e[0;31m'
     59 			;;
     60 
     61 	esac
     62 
     63 	if [ -z $_debug ]; then
     64 		return 0
     65 	fi
     66 	>&2 echo -e "$clr$(printf %-9s [$lvl])$2\e[0m"
     67 }
     68 
     69 git_at_to_https() {
     70 	url=$1
     71 	if [ ${1:0:4} == 'git@' ]; then
     72 		url_right=`echo ${1:4} | sed -e 's/:/\//'`
     73 		url="https://${url_right}"
     74 	fi
     75 	echo $url
     76 }
     77 
     78 wd=${1:-`pwd`}
     79 wd=$(realpath $wd)
     80 
     81 dbg 1 "root dir $wd"
     82 
     83 _IFS=$IFS
     84 IFS=$(echo -en "\n\b")
     85 used=''
     86 pushd "$wd" > /dev/null
     87 #for d in $(find ~- -type d -not -name ".git"); do
     88 for d in $(find . -type d -not -name ".git"); do
     89 	if [ ! -z $used ]; then
     90 		if [[ "$d" =~ ^$used ]]; then
     91 			dbg 1 "still in repo, skipping $d"
     92 			continue
     93 		else
     94 			used=''
     95 		fi
     96 	fi
     97 	pushd "$d" > /dev/null
     98 	if [ $(git rev-parse HEAD 2> /dev/null) ]; then
     99 		used=$d
    100 		dbg 2 "found repo $d"
    101 		if [ ! -z $_update ]; then
    102 			git remote update
    103 			continue
    104 		fi
    105 		if [ ! -z $_no_remotes ]; then
    106 			echo $d
    107 			continue
    108 		fi
    109 
    110 		origin=`git remote -v | awk '$1 ~ /origin/ && $3 == "(fetch)" { print $2; }'`
    111 		if [[ $origin =~ ^ssh ]]; then
    112 			dbg 4 "skipping ssh url $origin"
    113 			popd > /dev/null
    114 			continue
    115 		elif [ -z "$origin" ]; then
    116 			dbg 4 "origin missing from repo $d"
    117 			dbg 4 "available remotes $(git remote -v)"
    118 			popd > /dev/null
    119 			continue
    120 		fi
    121 		t=$origin
    122 		origin=$(git_at_to_https $origin)
    123 		if [ "$t" != "$origin" ]; then
    124 			dbg 3 "changed $t -> $origin"
    125 		fi
    126 		if [ ! -z $_path ]; then
    127 			if [ -z $_fullpath ]; then
    128 				p=${d/$wd}
    129 			else
    130 				p=$wd/${d:2}
    131 			fi
    132 
    133 			echo -e "$origin\t$p"
    134 		else
    135 			echo "$origin"
    136 		fi
    137 	fi	
    138 	popd > /dev/null
    139 done
    140 popd > /dev/null