From ae9dd17ef36db2afe8f897a5e29c0298cab5bc6e Mon Sep 17 00:00:00 2001 From: Midgard Date: Wed, 6 Nov 2019 14:08:47 +0100 Subject: [PATCH] Detect when no push is necessary Fixes #1 --- git-mirror.sh | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/git-mirror.sh b/git-mirror.sh index 6631ddd..506c0cd 100755 --- a/git-mirror.sh +++ b/git-mirror.sh @@ -57,6 +57,16 @@ error() { errors[${#errors[*]}]="$1" } +ref_exists() { + git show-ref --verify --quiet "$1" +} + +is_ref_at_sha() { + sha1="$(git show-ref --verify --hash "$1")" + sha2="$2" + [[ $sha1 == $sha2 ]] +} + for branchspec in "${branches[@]}"; do set $branchspec leader_sha="$1" @@ -66,19 +76,23 @@ for branchspec in "${branches[@]}"; do echo echo "Updating $remote/$branch" - # Prepare state to push follower_ref="refs/remotes/$remote/$branch" - if ! git show-ref --verify --quiet "$follower_ref"; then + + if ! ref_exists "$follower_ref"; then echo "Branch doesn't exist yet at remote, creating" git reset --hard "$leader_sha" -- + git push "$remote" "+HEAD:$branch" || { error "$remote $branch: failed to push"; continue; } + + elif is_ref_at_sha "$follower_ref" "$leader_sha"; then + echo "Branch up to date" + else echo "Branch existed at remote, updating" git reset --hard "$follower_ref" -- git merge --ff-only "$leader_sha" || { error "$remote $branch: FF not possible"; continue; } + git push "$remote" "+HEAD:$branch" || { error "$remote $branch: failed to push"; continue; } fi - git push "$remote" "+HEAD:$branch" || { error "$remote $branch: failed to push"; continue; } - done done