Skip to content

Instantly share code, notes, and snippets.

@O5ten
Last active April 23, 2020 13:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save O5ten/dbcb662d8090a1f52eb1e47b679525a7 to your computer and use it in GitHub Desktop.
#!/bin/bash
URL=$(git config remote.origin.url)
if [ $(echo $URL | grep "http[s]*://" | grep --invert-match "@") ]
then
echo "Anonymous HTTP remotes is not supported"
exit 1
fi
USERNAME_HOSTNAME_AND_PORT=$(echo "$URL" | awk -F"/" '{print $3}')
HOSTNAME_AND_PORT=$(echo "$USERNAME_HOSTNAME_AND_PORT" | awk -F"@" '{print $2}')
HOSTNAME=$(echo "$HOSTNAME_AND_PORT" | awk -F":" '{print $1}')
PORT=$(echo "$HOSTNAME_AND_PORT" | awk -F":" '{print $2}')
USERNAME=$(echo "$USERNAME_HOSTNAME_AND_PORT" | awk -F"@" '{print $1}')
CONFIGURED_REMOTES=$(git remote | grep "/" |awk -F"/" '{print $2}')
ACTUAL_REMOTES=$(git branch -r | awk -F"/" '{print $2}')
echo "Disable direct push against origin"
git remote set-url --push origin DISABLED
git remote | grep ^review$ | grep --invert-match "/" > /dev/null
if [ $? -ne 0 ]
then
echo "Creating remote review"
git config remote.review.url "$URL"
git config remote.review.push HEAD:refs/for/master%ready
echo "Creating remote wip"
git config remote.wip.url "$URL"
git config remote.wip.push HEAD:refs/for/master%wip
fi
if [ -z "$CONFIGURED_REMOTES" ]
then
# If there are no configured remotes, set a dummy otherwise the grep below will fail
CONFIGURED_REMOTES="FoObAr"
fi
for branch in $(git branch -r | awk -F"/" '{print $2}' | grep --invert-match "HEAD\|master" | grep --invert-match "$CONFIGURED_REMOTES")
do
# Create remotes for all remote branches, except master and the ones already configured
REMOTENAME=review/"$branch"
echo "Creating remote $REMOTENAME"
git config remote."$REMOTENAME".url "$URL"
git config remote."$REMOTENAME".push HEAD:refs/for/"$branch"%ready
DRAFTNAME=wip/"$branch"
echo "Creating remote $DRAFTNAME"
git config remote."$DRAFTNAME".url "$URL"
git config remote."$DRAFTNAME".push HEAD:refs/for/"$branch"%wip
done
if [ ! -f ".git/hooks/commit-msg" ]
then
echo "Fetching commit-msg hook"
GITDIR=$(git rev-parse --git-dir)
if [ $(echo $URL | grep "http[s]*://") ]
then
curl -kLo "$GITDIR/hooks/commit-msg" "http://$HOSTNAME_AND_PORT/tools/hooks/commit-msg"
chmod +x "$GITDIR/hooks/commit-msg"
else
scp -p -P "$PORT" "$USERNAME"@"$HOSTNAME":hooks/commit-msg "$GITDIR/hooks/"
fi
fi
for branch in $CONFIGURED_REMOTES
do
if [ $(echo -n "$branch" | grep --invert-match "$ACTUAL_REMOTES") ]
then
echo "Remove non-existing remote $branch"
git remote rm "review/$branch"
git remote rm "wip/$branch"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment