Skip to content

Instantly share code, notes, and snippets.

@daniel-fanjul-alcuten
Created September 17, 2014 14:20
Show Gist options
  • Save daniel-fanjul-alcuten/94c3f165b0f7c2b1f1e9 to your computer and use it in GitHub Desktop.
Save daniel-fanjul-alcuten/94c3f165b0f7c2b1f1e9 to your computer and use it in GitHub Desktop.
update git mirror
#!/bin/bash
set -e
set -x
upstream_repo=${upstream_repo:-CENSORED}
upstream_ref=${upstream_ref:-refs/heads/master}
target_repo=${target_repo:-CENSORED}
target_master_branch=${target_master_branch:-refs/heads/master}
target_upstream_branch=${target_upstream_branch:-refs/heads/upstream}
git checkout --detach
if git ls-remote --exit-code "$target_repo" "$target_upstream_branch" >/dev/null 2>/dev/null; then
git fetch --no-tags "$target_repo" "$target_upstream_branch"
commit=$(git rev-parse -q --verify FETCH_HEAD)
else
commit=
fi
git fetch --no-tags "$upstream_repo" +"$upstream_ref:$target_upstream_branch"
if [ "$commit" ]; then
commit2=$(git rev-parse -q --verify FETCH_HEAD)
if [ "$commit2" = "$commit" ]; then
exit 0
fi
else
commit="$target_upstream_branch"
fi
if git ls-remote --exit-code "$target_repo" "$target_master_branch" >/dev/null 2>/dev/null; then
git fetch --no-tags "$target_repo" +"$target_master_branch:$target_master_branch"
else
git branch -f "$target_master_branch" "$target_upstream_branch"
fi
branch=$(git rev-parse -q --verify --symbolic-full-name --abbrev-ref "$target_master_branch")
git rebase --onto "$target_upstream_branch" "$commit" "$branch"
IFS= status=$(git status --porcelain)
if grep -q -e ^.U -e ^U. <<< "$status"; then
exit 1
fi
tag1=master-$(date +%Y%m%d-%H%M%S)
tag2=master-latest
git tag -f -a -m "upstream rebase" "$tag1" "$target_master_branch"
git tag -f "$tag2" "$tag1"
git push -v -f "$target_repo" "$target_upstream_branch" "$target_master_branch" tag "$tag1" tag "$tag2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment