diff --git a/scripts/pgit.sh b/scripts/pgit.sh index 37c6c2e4..ad527224 100644 --- a/scripts/pgit.sh +++ b/scripts/pgit.sh @@ -130,14 +130,12 @@ cmd_clone() cd $projects_dir local projects="$PGIT_CLONE_PROJECTS" local ignore="$PGIT_IGNORE" - local thisdir="${0%/*}" - local jw_projects="/usr/bin/python3 $thisdir/jw-pkg.py" + local this_dir="${0%/*}" + local jw_projects="/usr/bin/python3 $this_dir/jw-pkg.py" local create_remote_user_repos=false local long_opts="create-remote-user-repos" local refspec_arg refspec=() long_opts="$long_opts,refspec:" - local login="$whoami" - [ "$remote_user" ] && login="$remote_user" local opts opts=$(getopt -o C --long "$long_opts" -n clone -- "$@") || fatal "Failed to parse options $@" @@ -155,26 +153,22 @@ cmd_clone() *) fatal "Unknown option $1" ;; - esac shift done - local fromuser="${refspec[0]}" - local fromref="${refspec[1]}" - local toref="${refspec[2]}" - [ "$fromuser" ] || fromuser=$whoami - [ "$fromref" ] || fromref=master - local git_srv_admin="$SSH $login@git.janware.com /opt/jw-pkg/bin/git-srv-admin.sh" + local git_srv_admin="$SSH $remote_user@git.janware.com /opt/jw-pkg/bin/git-srv-admin.sh" + local from_user="${refspec[0]}" + [ "$from_user" ] || from_user=$whoami if [ -z "$projects" ]; then - projects=`$jw_projects projects list-repos --from-user $fromuser $remote_base` + projects=`$jw_projects projects list-repos --from-user $from_user $remote_base` [ "$?" != 0 ] && exit 1 fi - if [ "$login" ]; then - [ "${remote_base/@/}" = "${remote_base}" ] || fatal "Specified both --login $login and user in URL $remote_base" - remote_base=$(echo $remote_base | sed "s|://|://$login@|") + if [ "$remote_user" ]; then + [ "${remote_base/@/}" = "${remote_base}" ] || fatal "Specified both --login $remote_user and user in URL $remote_base" + remote_base=$(echo $remote_base | sed "s|://|://$remote_user@|") fi n_projects=`echo $projects | wc -w` @@ -182,42 +176,41 @@ cmd_clone() 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 + echo $ignore | grep -q "\b$project_name\b" && continue cur=`expr $cur + 1` - local pullurl=$remote_base/$fromuser$remote_subpath/$project_name - local pushurl=$remote_base/$login$remote_subpath/$project_name - fat_marker "Fetching project $project_name from user $fromuser" - local remotename="jw-$fromuser" - if [ "$fromuser" = "$login" ]; then - remotename="origin" - fi - local curref=$(git -C $project_dir rev-parse --abbrev-ref HEAD) - [ "$toref" = "current-branch" ] && toref=$curref - [ "$fromref" = "current-branch" ] && fromref=$curref - [ "$refspec_arg" != "$fromuser:$fromref:$toref" ] && log "Fetching $fromuser:$fromref:$toref ($refspec_arg)" + local pull_url=$remote_base/$from_user$remote_subpath/$project_name + local push_url=$remote_base/$remote_user$remote_subpath/$project_name + local cur_ref=$(git -C $project_dir rev-parse --abbrev-ref HEAD) + local remote_name="jw-$from_user" + local from_ref="${refspec[1]}" + local to_ref="${refspec[2]}" + [ "$from_ref" ] || from_ref=master + [ "$from_user" = "$remote_user" ] && remote_name="origin" + [ "$to_ref" = "current-branch" ] && to_ref=$cur_ref + [ "$from_ref" = "current-branch" ] && from_ref=$cur_ref + [ "$refspec_arg" != "$from_user:$from_ref:$to_ref" ] && log "Fetching $from_user:$from_ref:$to_ref ($refspec_arg)" + fat_marker "Fetching project $project_name from user $from_user" if [ -d $project_dir ]; then - run_git -C $project_dir remote | grep -q "^$remotename$" || { - run_git -C $project_dir remote add $remotename $pullurl - [ "$fromuser" = "$login" ] || run_git -C $project_dir remote set-url --push $remotename no_push + run_git -C $project_dir remote | grep -q "^$remote_name$" || { + run_git -C $project_dir remote add $remote_name $pull_url + [ "$from_user" = "$remote_user" ] || run_git -C $project_dir remote set-url --push $remote_name no_push } - run_git -C $project_dir fetch --prune --recurse-submodules=on-demand $remotename $fromref + run_git -C $project_dir fetch --prune --recurse-submodules=on-demand $remote_name $from_ref run_git -C $project_dir submodule foreach --recursive 'git fetch --tags -f origin' - if [ "$toref" ]; then - run_git -C $project_dir rebase --autostash $remotename/$fromref $toref - run_git -C $project_dir merge --ff-only $remotename/$fromref $toref + if [ "$to_ref" ]; then + run_git -C $project_dir rebase --autostash $remote_name/$from_ref $to_ref + run_git -C $project_dir merge --ff-only $remote_name/$from_ref $to_ref fi else - run_clone $remote_base/$fromuser$remote_subpath/$project_name $project_dir - if [ "$fromuser" != "$login" ]; then - 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 + run_clone $remote_base/$from_user$remote_subpath/$project_name $project_dir + if [ "$from_user" != "$remote_user" ]; then + run_git -C $project_dir remote rename origin $remote_name || fatal "Failed to rename remote in $project_dir" + run_git -C $project_dir remote set-url --push $remote_name no_push if [ $create_remote_user_repos = true ]; then - $git_srv_admin -u $login -j create-personal-project $project_name - run_git -C $project_dir remote add origin $pushurl + $git_srv_admin -u $remote_user -j create-personal-project $project_name + run_git -C $project_dir remote add origin $push_url run_git -C $project_dir push --recurse-submodules=on-demand origin master - $git_srv_admin -u $login -j update-descriptions $project_name + $git_srv_admin -u $remote_user -j update-descriptions $project_name run_git -C $project_dir branch --set-upstream-to origin/master master fi fi @@ -265,6 +258,7 @@ fi cmd=$1 shift +remote_user=$(id -un) while [ "${1:0:1}" = - ]; do case $1 in '--login')