jw-pkg/scripts/pgit.sh
Jan Lindemann 7116403214 pgit.sh: Prepare submodules, too
Signed-off-by: Jan Lindemann <jan@janware.com>
2017-04-27 13:34:50 +00:00

139 lines
2.7 KiB
Bash

#!/bin/sh
log()
{
echo $@
}
err()
{
log $@
}
fatal()
{
err $@
exit 1
}
marker()
{
log "# ------------- $@"
}
fat_marker()
{
log "# ========================== $@"
}
config()
{
# guess pdir
pdir=`pwd`
while [ `cat $pdir/CVS/Repository 2>/dev/null` != proj ]; do
[ "$pdir" = / ] && fatal "didn't find \"proj\" in directory components"
pdir=`dirname $pdir`
done
pdirs=`(cd $pdir; ls -d */.git 2>/dev/null | sed 's%/.git%%')`
}
run_git()
{
marker git "$@"
eval git "$@"
}
# ------------- commands
run()
{(
local cmd=$1
local d
shift
config
cd $pdir
set -e
for d in $pdirs; do
run_git -C $d $cmd "$@"
done
)}
clone()
{(
local p
config
cd $pdir
local fromuser=$PGIT_CLONE_FROM_USER
local login=$JANWARE_USER
local projects="$PGIT_CLONE_PROJECTS"
[ "$login" ] || login=`whoami`
[ "$fromuser" ] || fromuser=`whoami`
local git_srv_admin="$SSH $login@git.jannet.de /opt/jw-build/bin/git-srv-admin.sh"
if [ -z "$projects" ]; then
projects=`$git_srv_admin -u $fromuser -j list-personal-projects`
[ "$?" != 0 ] && exit 1
fi
set -e
for p in $projects; do
local pullurl=ssh://$login@git.jannet.de/srv/git/$fromuser/proj/$p
local pushurl=ssh://$login@git.jannet.de/srv/git/$login/proj/$p
fat_marker "Fetching project $p from user $fromuser"
if [ "$fromuser" = "$login" ]; then
if [ -d $p ]; then
run_git -C $p pull
else
run_git clone ssh://$login@git.jannet.de/srv/git/$fromuser/proj/$p
fi
run_git -C $p submodule init || fatal git submodule init failed in $p
run_git -C $p submodule update || fatal git submodule update failed in $p
else
local remotename="user-$fromuser"
if [ -d $p ]; then
run_git -C $p remote | grep -q "^$remotename$" || {
run_git -C $p remote add $remotename $pullurl
run_git -C $p remote set-url --push $remotename no_push
}
run_git -C $p pull $remotename master
else
set -x
run_git clone ssh://$login@git.jannet.de/srv/git/$fromuser/proj/$p
run_git -C $p remote rename origin $remotename || fatal failed to rename remote in $p
run_git -C $p remote set-url --push $remotename no_push
$git_srv_admin -u $login -j create-personal-project $p
run_git -C $p remote add origin $pushurl
run_git -C $p push origin master
run_git -C $p branch --set-upstream-to origin/master master
set +x
fi
run_git -C $p submodule init || fatal git submodule init failed in $p
run_git -C $p submodule update || fatal git submodule update failed in $p
fi
done
)}
diff()
{(
local d
config
cd $pdir
for d in $pdirs; do
# marker $d
run_git -C $d diff --src-prefix=a/$d/ --dst-prefix=b/$d/ "$@"
done
)}
SSH=ssh
[ "$GIT_SSH" ] && SSH=$GIT_SSH
cmd=$1
shift
case $cmd in
clone|diff)
$cmd "$@"
;;
*)
run $cmd "$@"
;;
esac