2019-11-06 13:47:23 +01:00
|
|
|
#!/bin/bash
|
2019-11-06 13:49:54 +01:00
|
|
|
|
|
|
|
# git-mirror: Mirror a git repo in leader-followers configuration
|
2022-05-23 14:35:29 +02:00
|
|
|
# Copyright © 2019-2022 Midgard
|
2019-11-06 13:49:54 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify it under the terms of the
|
|
|
|
# GNU Affero General Public License as published by the Free Software Foundation, either version 3
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
|
|
|
|
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along with this program.
|
|
|
|
# If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2019-11-06 13:47:23 +01:00
|
|
|
set -euo pipefail
|
|
|
|
IFS=$'\n'
|
|
|
|
|
2020-09-15 12:06:33 +02:00
|
|
|
tor=true
|
|
|
|
if [[ "$1" == "--notor" ]]; then
|
|
|
|
tor=false
|
|
|
|
shift 1
|
|
|
|
fi
|
|
|
|
|
2020-09-15 12:17:36 +02:00
|
|
|
if [[ $# -lt 3 ]]; then
|
|
|
|
echo "Usage: $0 [--notor] <workdir> <leader> <follower...>"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-11-06 13:47:23 +01:00
|
|
|
cd "$1"
|
|
|
|
|
|
|
|
leader="$2"
|
|
|
|
shift 2
|
|
|
|
followers=( "$@" )
|
|
|
|
|
|
|
|
echo "Working directory: $PWD"
|
|
|
|
echo "Leader remote: $leader"
|
|
|
|
echo "Follower remotes: ${followers[*]}" | tr '\n' ' '
|
|
|
|
echo; echo
|
|
|
|
|
2020-09-15 12:06:33 +02:00
|
|
|
if [[ $tor == true ]]; then
|
|
|
|
torify git fetch --prune --multiple "$leader"
|
|
|
|
else
|
|
|
|
git fetch --prune --multiple "$leader"
|
|
|
|
fi
|
2019-11-06 13:47:23 +01:00
|
|
|
git fetch --prune --multiple "${followers[@]}"
|
|
|
|
echo
|
|
|
|
|
|
|
|
echo "Leader remote information:"
|
|
|
|
git remote show -n "$leader"
|
|
|
|
echo
|
|
|
|
echo "Follower remotes information:"
|
|
|
|
git remote show -n "${followers[@]}"
|
|
|
|
echo
|
|
|
|
|
|
|
|
branches=( $(git show-ref | sed -n 's| refs/remotes/'"$leader"'/|\t|p') )
|
|
|
|
echo -e "${#branches[*]} branches at leader:"
|
|
|
|
echo "${branches[*]}"
|
|
|
|
|
|
|
|
IFS=$'\t'
|
|
|
|
|
2022-05-23 14:35:29 +02:00
|
|
|
up_to_date=( )
|
2019-11-06 13:47:23 +01:00
|
|
|
errors=( )
|
|
|
|
|
|
|
|
error() {
|
2022-05-23 14:35:29 +02:00
|
|
|
tput bold
|
|
|
|
tput setaf 1
|
|
|
|
echo -n "XXX"
|
|
|
|
tput sgr0
|
|
|
|
echo " $1" >&2
|
2019-11-06 13:47:23 +01:00
|
|
|
errors[${#errors[*]}]="$1"
|
|
|
|
}
|
|
|
|
|
2019-11-06 14:08:47 +01:00
|
|
|
ref_exists() {
|
|
|
|
git show-ref --verify --quiet "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
is_ref_at_sha() {
|
|
|
|
sha1="$(git show-ref --verify --hash "$1")"
|
|
|
|
sha2="$2"
|
|
|
|
[[ $sha1 == $sha2 ]]
|
|
|
|
}
|
|
|
|
|
2019-11-06 13:47:23 +01:00
|
|
|
for branchspec in "${branches[@]}"; do
|
|
|
|
set $branchspec
|
|
|
|
leader_sha="$1"
|
|
|
|
branch="$2"
|
2022-05-21 01:43:42 +02:00
|
|
|
if [ "$branch" = HEAD ]; then continue; fi
|
2019-11-06 13:47:23 +01:00
|
|
|
|
|
|
|
for remote in "${followers[@]}"; do
|
|
|
|
|
|
|
|
follower_ref="refs/remotes/$remote/$branch"
|
2019-11-06 14:08:47 +01:00
|
|
|
|
2022-05-23 14:35:29 +02:00
|
|
|
msg=""
|
2019-11-06 14:08:47 +01:00
|
|
|
if ! ref_exists "$follower_ref"; then
|
2022-05-23 14:35:29 +02:00
|
|
|
msg="Creating $remote/$branch"
|
|
|
|
elif ! is_ref_at_sha "$follower_ref" "$leader_sha"; then
|
|
|
|
msg="Updating $remote/$branch"
|
|
|
|
else
|
|
|
|
up_to_date[${#up_to_date[*]}]="$remote/$branch"
|
|
|
|
fi
|
2019-11-06 14:08:47 +01:00
|
|
|
|
2022-05-23 14:35:29 +02:00
|
|
|
if [[ -n $msg ]]; then
|
|
|
|
echo
|
|
|
|
tput bold
|
|
|
|
echo "$msg"
|
|
|
|
tput sgr0
|
2019-11-06 14:08:47 +01:00
|
|
|
|
2022-05-23 14:35:29 +02:00
|
|
|
git push "$remote" "+$leader_sha:refs/heads/$branch" || { error "$remote $branch: failed to push"; continue; }
|
2019-11-06 13:47:23 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
2022-05-23 14:35:29 +02:00
|
|
|
if [[ "${#up_to_date[*]}" -gt 0 ]]; then
|
|
|
|
echo
|
|
|
|
echo "Already up to date:"
|
|
|
|
echo "${up_to_date[@]}"
|
|
|
|
fi
|
|
|
|
|
2019-11-06 13:55:51 +01:00
|
|
|
echo
|
|
|
|
if [[ "${#errors[*]}" -gt 0 ]]; then
|
2022-05-23 14:35:29 +02:00
|
|
|
tput bold
|
|
|
|
tput setaf 1
|
2019-11-06 13:47:23 +01:00
|
|
|
echo "Errors:"
|
2022-05-23 14:35:29 +02:00
|
|
|
tput sgr0
|
|
|
|
tput bold
|
|
|
|
for (( i=0; i < ${#errors[*]}; i++ )); do
|
|
|
|
echo "${errors[$i]}"
|
|
|
|
done
|
|
|
|
tput sgr0
|
|
|
|
|
|
|
|
exit 1
|
2019-11-06 13:47:23 +01:00
|
|
|
else
|
|
|
|
echo "No errors."
|
|
|
|
fi
|