gitrefresh

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

commit b50579b13457467b9cfa16bfdb4de06512491506
Author: lash <dev@holbrook.no>
Date:   Sun,  9 Jan 2022 14:52:37 +0000

Migrate from cic-tools repo

Diffstat:
Agitlist.sh | 129+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Agitrefresh.sh | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Agitstart.sh | 19+++++++++++++++++++
3 files changed, 265 insertions(+), 0 deletions(-)

diff --git a/gitlist.sh b/gitlist.sh @@ -0,0 +1,129 @@ +#!/bin/bash + +_level=3 +while test $# != 0; do + case "$1" in + -v) + _debug=1 + shift + _level=$1 + shift + ;; + -n) + _no_remotes=1 + shift + ;; + -u) + _update=1 + _no_remotes=1 + shift + ;; + -p) + _path=1 + shift + ;; + *) + break + ;; + esac +done + +dbg() { + if [ "$1" -lt "$_level" ]; then + return 0 + fi + + case "$1" in + 1) + lvl='debug' + clr='\e[0;36m' + ;; + 2) + lvl='info' + clr='\e[0;32m' + ;; + + 3) + lvl='warn' + clr='\e[0;33m' + ;; + 4) + lvl='error' + clr='\e[0;31m' + ;; + + esac + + if [ -z $_debug ]; then + return 0 + fi + >&2 echo -e "$clr$(printf %-9s [$lvl])$2\e[0m" +} + +git_at_to_https() { + url=$1 + if [ ${1:0:4} == 'git@' ]; then + url_right=`echo ${1:4} | sed -e 's/:/\//'` + url="https://${url_right}" + fi + echo $url +} + +wd=${1:-`pwd`} + +dbg 1 "root dir $wd" + +_IFS=$IFS +IFS=$(echo -en "\n\b") +used='' +pushd "$wd" > /dev/null +#for d in $(find ~- -type d -not -name ".git"); do +for d in $(find . -type d -not -name ".git"); do + if [ ! -z $used ]; then + if [[ "$d" =~ ^$used ]]; then + dbg 1 "still in repo, skipping $d" + continue + else + used='' + fi + fi + + pushd "$d" > /dev/null + if [ $(git rev-parse HEAD 2> /dev/null) ]; then + used=$d + dbg 2 "found repo $d" + if [ ! -z $_update ]; then + git remote update + continue + fi + if [ ! -z $_no_remotes ]; then + echo $d + continue + fi + + origin=`git remote -v | awk '$1 ~ /origin/ && $3 == "(fetch)" { print $2; }'` + if [[ $origin =~ ^ssh ]]; then + dbg 4 "skipping ssh url $origin" + popd > /dev/null + continue + elif [ -z $origin ]; then + dbg 4 "origin missing from repo $d" + dbg 4 "available remotes $(git remote -v)" + popd > /dev/null + continue + fi + t=$origin + origin=$(git_at_to_https $origin) + if [ "$t" != "$origin" ]; then + dbg 3 "changed $t -> $origin" + fi + if [ ! -z $_path ]; then + p=${d/$wd} + echo "$origin ${p:1} " + else + echo "$origin" + fi + fi + popd > /dev/null +done +popd > /dev/null diff --git a/gitrefresh.sh b/gitrefresh.sh @@ -0,0 +1,117 @@ +#!/bin/bash + +wd=${2:-.} +if [ ! -d $wd ]; then + >&2 echo $wd is not a directory + exit 1 +fi + + +t='' + +d='' +p='.' +cmd=$1 +if [ -z "$cmd" ]; then + exit 1 +fi + +case "$cmd" in + init) + t=`mktemp -d` + ;; + update) + pushd $2 + ;; + *) + >&2 echo invalid command: "$cmd" + exit 1 + break +esac +>&2 echo using repo dir $t + +repo_update() { + >&2 echo updating `pwd` + git remote update +# if [ "$?" == "0" ]; then +# if [[ $remote =~ ^git ]]; then +# ... +# fi +# sed -e "s/^.*:\(.*\)$/\1/g" +# fi + git fetch +} + +repo_init() { + gf=`echo "$1" | grep "(fetch)" | sed -e "s/^\(.*\) .*$/\1/g"` + mkdir -p $t/$2 + pushd $t/$2 + git init + _IFS=$IFS + IFS=$'\n' + for gr in ${gf[@]}; do + echo adding remote $gr + echo $gr | tr "\t" " " | xargs git remote add + done + IFS=$_IFS + popd +} + +scan() { + echo entering $d parent $p + pushd "$d" > /dev/null + if [ "$?" -ne "0" ]; then + p=`dirname $p` + return + fi + g=`git remote -v` + if [ "$?" -eq "0" ]; then + if [ "$cmd" == "init" ]; then + repo_init "$g" "$p" + elif [ "$cmd" == "update" ]; then + repo_update $p + fi + else + for d in `find . -maxdepth 1 -not -path "\." -type d -printf "%f\n"`; do + #echo scan $d + p="$p/$d" + scan + done + fi + popd > /dev/null + p=`dirname $p` + echo exiting, parent now $p +} + +pushd $wd +d='.' +scan + +if [ "$cmd" == "update" ]; then + popd +fi +#update() { +# pushd $wd +# for d in ${ds[@]}; do +# pushd $d +# for sd in `find . -maxdepth 1 -type d`; do +# pushd $sd +# g=`git remote -v show 2> /dev/null` +# go=`git remote -v show 2> /dev/null | grep -e "^origin"` +# if [ "$?" -gt "0" ]; then +# >&2 echo no git in $sd +# else +# u=`echo $g | grep "(fetch)" | awk '{print $2}'` +# uo=`echo $go | grep "(fetch)" | awk '{print $2}'` +# >&2 echo found git $u +# b='basename $sd' +# echo $u > $t/$sd +# git remote update +# git fetch +# fi +# popd +# done +# popd +# done +# popd +#} diff --git a/gitstart.sh b/gitstart.sh @@ -0,0 +1,19 @@ +if [ ! -z $1 ]; then + pushd $1 +fi + +while IFS= read -r repo; do + basename_raw=$(basename $repo) + basename_chomped=${basename_raw%.git} + >&2 echo "checkout repo $basename_chomped ($repo)" + basename_git=${basename_chomped}.git + if [ -e $basename_git ]; then + >&2 echo "folder $basename_git already exists, skipping" + continue + fi + git clone $repo $basename_git +done + +if [ ! -z $1 ]; then + popd +fi