gitrefresh.sh (3589B)
1 #!/bin/bash 2 3 _level=3 4 5 bdbg_check_env_level() { 6 if [ ! -z "$BDBG_LEVEL" ]; then 7 _level=$BDBG_LEVEL 8 fi 9 } 10 11 bdbg_check_env_toggle() { 12 if [ ! -z "$BDBG" ]; then 13 if [ "$BDBG" -gt "0" ]; then 14 _debug=1 15 fi 16 fi 17 } 18 19 20 dbg() { 21 if [ "$1" -lt "$_level" ]; then 22 return 0 23 fi 24 25 case "$1" in 26 1) 27 lvl='debug' 28 clr='\e[0;96m' 29 ;; 30 2) 31 lvl='info' 32 clr='\e[0;92m' 33 ;; 34 35 3) 36 lvl='warn' 37 clr='\e[0;93m' 38 ;; 39 4) 40 lvl='error' 41 clr='\e[0;91m' 42 ;; 43 44 esac 45 46 if [ -z $_debug ]; then 47 return 0 48 fi 49 >&2 echo -e "$clr$(printf %-9s [$lvl])$2\e[0m" 50 } 51 52 bdbg_check_env_level 53 bdbg_check_env_toggle 54 55 wd=${2:-.} 56 if [ ! -d $wd ]; then 57 >&2 echo $wd is not a directory 58 exit 1 59 fi 60 61 62 t='' 63 64 d='' 65 p='.' 66 cmd=$1 67 if [ -z "$cmd" ]; then 68 exit 1 69 fi 70 71 _pull= 72 case "$cmd" in 73 init) 74 t=`mktemp -d` 75 ;; 76 update) 77 pushd $2 78 ;; 79 pull) 80 pushd $2 81 _pull=1 82 ;; 83 *) 84 >&2 echo invalid command: "$cmd" 85 exit 1 86 break 87 esac 88 89 90 #repo_check_access() { 91 # statuscode=`curl ${repo_url}/info/refs?service=git-upload-pack -i -w '%{http_code}' -o /dev/null` 92 # if [ "$statuscode" -ne "200" ]; then 93 # # skip 94 # fi 95 #} 96 97 repo_fetch_checkout() { 98 git fetch > /dev/null 2>&1 99 if [ "$?" -gt "0" ]; then 100 dbg 4 "fetch heads failed in $(pwd)" 101 return 1 102 fi 103 if [ ! -z "$_pull" ]; then 104 #branch=`git branch --show-current` 105 branch=`git rev-parse --abbrev-ref HEAD` 106 git pull --ff-only origin $branch > /dev/null 2>&1 107 fi 108 } 109 110 repo_fetch_bare() { 111 git fetch origin 'refs/heads/*:refs/heads/*' > /dev/null 2>&1 112 if [ "$?" -gt "0" ]; then 113 >&2 echo "fetch heads failed in $(pwd)" 114 return 1 115 fi 116 git fetch origin 'refs/tags/*:refs/tags/*' > /dev/null 2>&1 117 if [ "$?" -gt "0" ]; then 118 >&2 echo "fetch heads failed in $(pwd)" 119 return 1 120 fi 121 } 122 123 repo_update() { 124 #>&2 echo updating `pwd` 125 git remote update > /dev/null 126 if [ "$?" -gt "0" ]; then 127 # if [[ $remote =~ ^git ]]; then 128 # ... 129 # fi 130 # sed -e "s/^.*:\(.*\)$/\1/g" 131 >&2 echo "remote update failed in $(pwd)" 132 return 1 133 fi 134 if [ -d ".git" ]; then 135 dbg 2 "updating work repo $remote -> $(pwd)" 136 repo_fetch_checkout 137 else 138 dbg 2 "updating bare repo $remote -> $(pwd)" 139 repo_fetch_bare 140 fi 141 } 142 143 repo_init() { 144 gf=`echo "$1" | grep "(fetch)" | sed -e "s/^\(.*\) .*$/\1/g"` 145 mkdir -p $t/$2 146 pushd $t/$2 147 git init 148 _IFS=$IFS 149 IFS=$'\n' 150 for gr in ${gf[@]}; do 151 dbg 2 adding remote $gr 152 echo $gr | tr "\t" " " | xargs git remote add 153 done 154 IFS=$_IFS 155 popd 156 } 157 158 p=$(realpath $p) 159 scan() { 160 dd=$(realpath $d) 161 dbg 1 "entering $dd, parent $p" 162 pushd "$d" > /dev/null 163 if [ "$?" -ne "0" ]; then 164 p=`dirname $p` 165 return 166 fi 167 g=`git remote -v 2> /dev/null` 168 if [ "$?" -eq "0" ]; then 169 if [ "$cmd" == "init" ]; then 170 repo_init "$g" "$p" 171 elif [ "$cmd" == "update" ]; then 172 repo_update $p 173 elif [ "$cmd" == "pull" ]; then 174 repo_update $p 175 fi 176 else 177 for d in `find . -maxdepth 1 -not -path "\." -type d -printf "%f\n"`; do 178 #echo scan $d 179 p="$p/$d" 180 scan 181 done 182 fi 183 popd > /dev/null 184 p=`dirname $p` 185 #echo exiting, parent now $p 186 } 187 188 pushd $wd 189 d='.' 190 scan 191 192 if [ "$cmd" == "update" ]; then 193 popd 194 fi 195 #update() { 196 # pushd $wd 197 # for d in ${ds[@]}; do 198 # pushd $d 199 # for sd in `find . -maxdepth 1 -type d`; do 200 # pushd $sd 201 # g=`git remote -v show 2> /dev/null` 202 # go=`git remote -v show 2> /dev/null | grep -e "^origin"` 203 # if [ "$?" -gt "0" ]; then 204 # >&2 echo no git in $sd 205 # else 206 # u=`echo $g | grep "(fetch)" | awk '{print $2}'` 207 # uo=`echo $go | grep "(fetch)" | awk '{print $2}'` 208 # >&2 echo found git $u 209 # b='basename $sd' 210 # echo $u > $t/$sd 211 # git remote update 212 # git fetch 213 # fi 214 # popd 215 # done 216 # popd 217 # done 218 # popd 219 #}