pgit.sh: Make some (!) variable names less messy

Some variable names are too short for global scope ($p, $pdir,
$pdirs), among others. For those mentioned: Make them longer and more
descriptive.

Also add a variable project_name, which denotes what a project is in
a remote repository, and which is currently but not necessarily
always the same as the project directory.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-02-16 15:30:36 +01:00
commit f28ee62209

View file

@ -1,5 +1,7 @@
#!/bin/sh #!/bin/sh
# ----------------- Helper functions
log() log()
{ {
echo $@ echo $@
@ -26,21 +28,27 @@ fat_marker()
log "# ==================================================== [$cur/$n_projects] $@" log "# ==================================================== [$cur/$n_projects] $@"
} }
config() #
# Guess and set the following global variables:
#
# - projects_dir: The directory all projects are contained in
# - project_dirs: A space separated list of all directories containing a project
# - n_projects: the number of projects
#
set_global_variables()
{ {
[ "$pdir" ] || { [ "$projects_dir" ] || {
# guess pdir projects_dir=`pwd`
pdir=`pwd`
while [ ! -r Makefile ] || ! grep -q some-random-string-to-id-this-makefile Makefile; do while [ ! -r Makefile ] || ! grep -q some-random-string-to-id-this-makefile Makefile; do
[ "$pdir" = / ] && fatal "didn't find \"proj\" in directory components" [ "$projects_dir" = / ] && fatal "Failed to find projects directory"
pdir=`dirname $pdir` projects_dir=`dirname $projects_dir`
done done
} }
[ "$pdirs" ] || { [ "$project_dirs" ] || {
pdirs=`(cd $pdir; ls -d */.git 2>/dev/null | sed 's%/.git%%')` project_dirs=`(cd $projects_dir; ls -d */.git 2>/dev/null | sed 's%/.git%%')`
} }
n_projects=`echo $pdirs | wc -w` n_projects=`echo $project_dirs | wc -w`
} }
run_git() run_git()
@ -49,25 +57,26 @@ run_git()
git "$@" git "$@"
} }
# ------------- commands # ----------------- Commands
run()
{(
local cmd=$1
local d
cmd_run()
(
local d
local cmd=$1
shift shift
config
cd $pdir set_global_variables
cd $projects_dir
if [ "$PGIT_KEEP_GOING" != y ]; then set -e; fi if [ "$PGIT_KEEP_GOING" != y ]; then set -e; fi
for d in $pdirs; do for d in $project_dirs; do
cur=`expr $cur + 1` cur=`expr $cur + 1`
run_git -C $d $cmd "$@" run_git -C $d $cmd "$@"
done done
)} )
commit() cmd_commit()
{( (
local d do_cvs local d do_cvs
if [ "$1" = --cvs ]; then if [ "$1" = --cvs ]; then
@ -75,11 +84,11 @@ commit()
shift shift
fi fi
config set_global_variables
cd $pdir cd $projects_dir
if [ "$PGIT_KEEP_GOING" != y ]; then set -e; fi if [ "$PGIT_KEEP_GOING" != y ]; then set -e; fi
for d in $pdirs; do for d in $project_dirs; do
cur=`expr $cur + 1` cur=`expr $cur + 1`
if run_git -C $d diff-index --quiet HEAD --; then if run_git -C $d diff-index --quiet HEAD --; then
log "Nothing to commit" log "Nothing to commit"
@ -87,10 +96,10 @@ commit()
fi fi
run_git -C $d commit "$@" run_git -C $d commit "$@"
done done
)} )
clone() cmd_clone()
{( (
run_clone() { run_clone() {
local url="$1" local url="$1"
local p="$2" local p="$2"
@ -102,10 +111,9 @@ clone()
local remote_base="$global_remote_base" local remote_base="$global_remote_base"
local remote_subpath="$global_remote_subpath" local remote_subpath="$global_remote_subpath"
local p
local whoami="$(id -un)" local whoami="$(id -un)"
config set_global_variables
cd $pdir cd $projects_dir
local projects="$PGIT_CLONE_PROJECTS" local projects="$PGIT_CLONE_PROJECTS"
local ignore="$PGIT_IGNORE" local ignore="$PGIT_IGNORE"
local thisdir="${0%/*}" local thisdir="${0%/*}"
@ -115,7 +123,7 @@ clone()
local refspec=() local refspec=()
long_opts="$long_opts,refspec:" long_opts="$long_opts,refspec:"
local login="$whoami" local login="$whoami"
[ "$cmd_login" ] && login="$cmd_login" [ "$remote_user" ] && login="$remote_user"
local opts local opts
opts=$(getopt -o C --long "$long_opts" -n clone -- "$@") || fatal "Failed to parse options $@" opts=$(getopt -o C --long "$long_opts" -n clone -- "$@") || fatal "Failed to parse options $@"
@ -156,64 +164,66 @@ clone()
n_projects=`echo $projects | wc -w` n_projects=`echo $projects | wc -w`
if [ "$PGIT_KEEP_GOING" != y ]; then set -e; fi if [ "$PGIT_KEEP_GOING" != y ]; then set -e; fi
for p in $projects; do local project_dir
if echo $ignore | grep -q "\b$p\b"; then for project_dir in $projects; do
local project_name=$(readlink -f $project_dir | xargs basename)
if echo $ignore | grep -q "\b$project_name\b"; then
continue continue
fi fi
cur=`expr $cur + 1` cur=`expr $cur + 1`
local pullurl=$remote_base/$fromuser$remote_subpath/$p local pullurl=$remote_base/$fromuser$remote_subpath/$project_name
local pushurl=$remote_base/$login$remote_subpath/$p local pushurl=$remote_base/$login$remote_subpath/$project_name
local curref="" local curref=""
fat_marker "Fetching project $p from user $fromuser" fat_marker "Fetching project $project_name from user $fromuser"
if [ "$fromuser" = "$login" ]; then if [ "$fromuser" = "$login" ]; then
if [ -d $p ]; then if [ -d $project_dir ]; then
run_git -C $p pull --recurse-submodules=on-demand run_git -C $project_dir pull --recurse-submodules=on-demand
run_git -C $p submodule foreach --recursive 'git fetch --tags -f origin' run_git -C $project_dir submodule foreach --recursive 'git fetch --tags -f origin'
else else
run_clone $remote_base/$fromuser$remote_subpath/$p $p run_clone $remote_base/$fromuser$remote_subpath/$project_name $project_dir
fi fi
else else
local remotename="jw-$fromuser" local remotename="jw-$fromuser"
if [ -d $p ]; then if [ -d $project_dir ]; then
run_git -C $p remote | grep -q "^$remotename$" || { run_git -C $project_dir remote | grep -q "^$remotename$" || {
run_git -C $p remote add $remotename $pullurl run_git -C $project_dir remote add $remotename $pullurl
run_git -C $p remote set-url --push $remotename no_push run_git -C $project_dir remote set-url --push $remotename no_push
} }
run_git -C $p fetch --prune --recurse-submodules=on-demand $remotename $fromref run_git -C $project_dir fetch --prune --recurse-submodules=on-demand $remotename $fromref
run_git -C $p submodule foreach --recursive 'git fetch --tags -f origin' run_git -C $project_dir submodule foreach --recursive 'git fetch --tags -f origin'
if [ "$toref" ]; then if [ "$toref" ]; then
run_git -C $p rebase --autostash $remotename/$fromref $toref run_git -C $project_dir rebase --autostash $remotename/$fromref $toref
run_git -C $p merge --ff-only $remotename/$fromref $toref run_git -C $project_dir merge --ff-only $remotename/$fromref $toref
fi fi
else else
# set -x # set -x
run_clone $remote_base/$fromuser$remote_subpath/$p $p run_clone $remote_base/$fromuser$remote_subpath/$project_name $project_dir
run_git -C $p remote rename origin $remotename || fatal failed to rename remote in $p run_git -C $project_dir remote rename origin $remotename || fatal "Failed to rename remote in $project_dir"
run_git -C $p remote set-url --push $remotename no_push run_git -C $project_dir remote set-url --push $remotename no_push
if [ $create_remote_user_repos = true ]; then if [ $create_remote_user_repos = true ]; then
$git_srv_admin -u $login -j create-personal-project $p $git_srv_admin -u $login -j create-personal-project $project_name
run_git -C $p remote add origin $pushurl run_git -C $project_dir remote add origin $pushurl
run_git -C $p push --recurse-submodules=on-demand origin master run_git -C $project_dir push --recurse-submodules=on-demand origin master
$git_srv_admin -u $login -j update-descriptions $p $git_srv_admin -u $login -j update-descriptions $project_name
run_git -C $p branch --set-upstream-to origin/master master run_git -C $project_dir branch --set-upstream-to origin/master master
fi fi
fi fi
fi fi
run_git -C $p submodule update --init --recursive || fatal git submodule update failed in $p run_git -C $project_dir submodule update --init --recursive || fatal "git submodule update failed in $project_dir"
done done
)} )
diff() cmd_diff()
{( (
local d local d
config set_global_variables
cd $pdir cd $projects_dir
for d in $pdirs; do for d in $project_dirs; do
cur=`expr $cur + 1` cur=`expr $cur + 1`
# marker $d # marker $d
run_git -C $d diff --src-prefix=$d/ --dst-prefix=$d/ "$@" run_git -C $d diff --src-prefix=$d/ --dst-prefix=$d/ "$@"
done done
)} )
echo "running $0 $@ GIT_SSH=$GIT_SSH" >&2 echo "running $0 $@ GIT_SSH=$GIT_SSH" >&2
@ -245,7 +255,7 @@ shift
while [ "${1:0:1}" = - ]; do while [ "${1:0:1}" = - ]; do
case $1 in case $1 in
'--login') '--login')
cmd_login="$2" remote_user="$2"
shift 2 shift 2
;; ;;
*) *)
@ -256,9 +266,9 @@ done
case $cmd in case $cmd in
clone|diff|commit) clone|diff|commit)
$cmd "$@" cmd_${cmd//-/_} "$@"
;; ;;
*) *)
run $cmd "$@" cmd_run $cmd "$@"
;; ;;
esac esac