#!/bin/bash # # Keep git remotes in sync among media archives. # # Parameters BASENAME="$0" DEST="$1" MEDIA="/var/cache/media" # Syntax if [ -z "$DEST" ]; then echo "usage: $BASENAME " exit 1 elif [ ! -d "$DEST/$MEDIA" ]; then echo "folder $DEST/$MEDIA does not exist." exit 1 fi # Run for folder in `ls $MEDIA`; do if [ -d "$DEST/$MEDIA/$folder/.git" ]; then # Add new remotes git -C $MEDIA/$folder remote -v | while read remote; do cd $DEST/$MEDIA/$folder name="`echo $remote | cut -d ' ' -f 1`" addr="`echo $remote | cut -d ' ' -f 2`" type="`echo $remote | cut -d ' ' -f 3`" if [ "$type" == "(push)" ]; then command="set-url --add --push" else command="add" fi if ! git remote -v | sed -e 's/\t/ /g' | grep -q "^$name $addr $type$"; then # This might not sync everything in the first run as some weird behavior # happens according to this example: # # mkdir test && cd test # git init # git remote add origin git.example.org:test # git remote set-url --add --push origin test.example.org:test # git remote -v # # Expected output: # # origin git.example.org:test (fetch) # origin git.example.org:test (push) # origin test.example.org:test (push) # # Actual output: # # origin git.example.org:test (fetch) # origin test.example.org:test (push) # # You can get the expected output by doing this additional step: # # git remote set-url --add --push origin git.example.org:test git remote $command $name $addr fi done # Delete old remotes git -C $DEST/$MEDIA/$folder remote -v | while read remote; do cd $MEDIA/$folder name="`echo $remote | cut -d ' ' -f 1`" addr="`echo $remote | cut -d ' ' -f 2`" type="`echo $remote | cut -d ' ' -f 3`" if [ "$type" == "(push)" ]; then command="set-url --delete" arg="$addr" else command="remove" arg="" fi if ! git remote -v | sed -e 's/\t/ /g' | grep -q "^$name $addr $type$"; then # Check if was not already removed by a previous command if git -C $DEST/$MEDIA/$folder remote | grep -q "^$name$"; then git -C $DEST/$MEDIA/$folder remote $command $name $arg fi fi done fi done