Detect when no push is necessary

Fixes #1
This commit is contained in:
Midgard 2019-11-06 14:08:47 +01:00
parent 20a088e96d
commit ae9dd17ef3
Signed by: midgard
GPG Key ID: 511C112F1331BBB4
1 changed files with 18 additions and 4 deletions

View File

@ -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