#!/bin/bash # # Transfer a repo to a different user (or organisation) # $1 = Module name e.g. e-smith-dnscache # $2 = Source Organisation (any valid organisation or user) # $3 = Target Organisation (any valid organisation or user - you need owner privileges) # $4-$7 can appear in any order and are optional # = "local" will use parameters set for local repository else it will use remote # = "force" will automagically delete an existing repo, otherwise it will prompt # = "debug" run in debug mode # = "silent" suppress success message # if [[ -z $1 ]] ; then echo "Transfer a repo to a different user (or organisation)" echo "git-transfer-repo.sh ,target organisation> [ ]" echo " = repository name (e.g. smeserver-clamav)" echo " (any valid organisation or user)" echo " (any valid organisation or user - you need owner privileges)" echo " can appear in any order and are optional" echo " - will use parameters set for local repository, else it will use remote" echo " - will automagically delete an existing target repo, otherwise it will prompt" echo " - run in debug mode" echo " - suppress success message" exit 0 fi # # Pull in parameters from a config file ~/.smegit/config # inifilename=$(echo ~)"/.smegit/config" if [ ! -e $inifilename ] ; then # Not here, look at system default if [ ! -e /etc/smegit.ini ] ; then echo "No ini file found $inifiename or /etc/smegit.ini" echo "git-cvs2git.sh []" exit 1 else initfilename="/etc/smegit.ini" fi fi while read -r line || [[ -n "$line" ]]; do if [[ $line =~ ^\[.*\]$ ]] then section=${line#*[} section=${section%]*} else if [[ $line =~ ^[^#]*= ]] then key=${line%=*} value=${line#*=} declare "${section}_${key}=$value" fi fi done < "$inifilename" DEBUG= if [ ${smegit_DEBUG} == "true" ] ; then DEBUG=true ; fi if [ $DEBUG ] ; then echo "************found ini file: $inifilename" ; fi if [[ $smegit_WORKDIR == ~* ]] ; then # relative to users home dir WORKDIR=$(echo ~)${smegit_WORKDIR:1} else # absolute path WORKDIR=${smegit_WORKDIR} fi # Command line parameters REPO=$1 SOURCEORG=$2 TARGETORG=$3 GITEAHOST=${remote_GITEAHOST} GITEAACCESSTOKEN=${remote_GITEAACCESSTOKEN} DELETEIT= NOOUTPUT= for param in $4 $5 $6 $7; do if [ $param ] ; then case $param in local ) GITEAHOST=${local_GITEAHOST} GITEAACCESSTOKEN=${local_GITEAACCESSTOKEN} ;; force ) DELETEIT=true ;; debug ) DEBUG=true ;; silent ) NOOUTPUT=true ;; * ) echo "Ignoring $param" ;; esac else break fi done # Debug settings # Make this null if you want lots of output. Still quite a bit anyway QUIET="--quiet" SILENT="-s" if [ $DEBUG ] ; then QUIET= SILENT="-v" fi # Make this null to get curl to check ssl cert checkSSL="-k" #Check that source package exists if [ $DEBUG ] ; then echo "Check if $SOURCEORG/$REPO is there!" ; fi RESPONSE=$(curl $SILENT -o /dev/null -w "%{http_code}" "$GITEAHOST/api/v1/repos/$SOURCEORG/$REPO") if [ "$RESPONSE" == "200" ]; then if [ $DEBUG ] ; then echo "Repository for $SOURCEORG/$REPO exists!" ; fi else echo "************Repository for $SOURCEORG/$REPO does not exist on $GITEAHOST ($RESPONSE)" exit 1 fi if [ $DEBUG ] ; then echo "Transferring $TARGETORG/$REPO to $SOURCEORG/$REPO" ; fi #Check that target repo does not exist if [ $DEBUG ] ; then echo "Check if $TARGETORG/$REPO is there!" ; fi RESPONSE=$(curl $SILENT -o /dev/null -w "%{http_code}" "$GITEAHOST/api/v1/repos/$TARGETORG/$REPO") EXISTS= if [ "$RESPONSE" == "200" ]; then EXISTS=true if [ $DEBUG ] ; then echo "Repository for $TARGETORG/$REPO exists!" ; fi if [ -z ${DELETEIT} ] ; then while true; do read -p "$TARGETORG/$REPO exists! Do you wish to delete it and continue?(y/n) " yn case $yn in [Yy]* ) DELETEIT=true break ;; [Nn]* ) DELETEIT= break ;; * ) echo "Please answer yes or no.";; esac done fi if [ $DELETEIT ] ; then if [ $DEBUG ] ; then echo "Deleting $TARGETORG/$REPO" ; fi RESPONSE=$(curl "$checkSSL" "$SILENT" -o /dev/null -w "%{http_code}" -X 'DELETE' \ "$GITEAHOST/api/v1/repos/$TARGETORG/$REPO" \ -H 'accept: application/json' \ -H "Authorization: token $GITEAACCESSTOKEN" ) if [[ $RESPONSE != "204" ]] ; then echo "Unable to delete $TARGETORG/$REPO ($RESPONSE)" else EXISTS= fi fi else if [ $DEBUG ] ; then echo "Repository for $SOURCEORG/$REPO does not exist on $GITEAHOST" ; fi fi # Transfer the repo if [[ -z $EXISTS ]] ; then if [ $DEBUG ] ; then echo "Actual Transfer" ; fi RESPONSE=$(curl "$SILENT" "$checkSSL" -o /dev/null -w "%{http_code}" -X 'POST' \ "$GITEAHOST/api/v1/repos/$SOURCEORG/$REPO/transfer" \ -H 'accept: application/json' \ -H "Authorization: token $GITEAACCESSTOKEN" \ -H 'Content-Type: application/json' \ -d '{ "new_owner": "'"$TARGETORG"'" }' ) fi if [[ $EXISTS || $RESPONSE != "202" ]] ; then echo "Unable to transfer $SOURCEORG/$REPO to $TARGETORG/$REPO ($RESPONSE) - Aborting!" exit 1 else if [[ -z $NOOUTPUT ]] ; then echo "$SOURCEORG/$REPO has been transferred to $TARGETORG/$REPO" ; fi fi exit 0