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