Datasets:
File size: 2,798 Bytes
c61ad0d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #!/usr/bin/env bash
set -euo pipefail
BRANCH="${BRANCH:-main}"
retry_cmd() {
local attempts="$1"
shift
local n=1
until "$@"; do
if (( n >= attempts )); then
echo "Command failed after ${attempts} attempts: $*"
return 1
fi
local wait_time=$((2 ** n))
echo "Attempt ${n}/${attempts} failed for: $*"
echo "Retrying in ${wait_time}s..."
sleep "${wait_time}"
n=$((n + 1))
done
}
echo "==> Fetching remotes..."
retry_cmd 5 git fetch --prune origin
retry_cmd 5 git fetch --prune hf
HAS_ORIGIN_BRANCH=0
if git show-ref --verify --quiet "refs/remotes/origin/${BRANCH}"; then
HAS_ORIGIN_BRANCH=1
fi
HAS_HF_BRANCH=0
if git show-ref --verify --quiet "refs/remotes/hf/${BRANCH}"; then
HAS_HF_BRANCH=1
fi
if (( HAS_ORIGIN_BRANCH == 0 && HAS_HF_BRANCH == 0 )); then
echo "Neither origin/${BRANCH} nor hf/${BRANCH} exists. Cannot bootstrap."
exit 1
fi
if (( HAS_ORIGIN_BRANCH == 0 && HAS_HF_BRANCH == 1 )); then
echo "origin/${BRANCH} missing. Bootstrapping GitHub from hf/${BRANCH}..."
retry_cmd 5 git push origin "refs/remotes/hf/${BRANCH}:refs/heads/${BRANCH}"
retry_cmd 5 git fetch --prune origin
fi
if (( HAS_HF_BRANCH == 0 && HAS_ORIGIN_BRANCH == 1 )); then
echo "hf/${BRANCH} missing. Bootstrapping Hugging Face from origin/${BRANCH}..."
retry_cmd 5 git push hf "refs/remotes/origin/${BRANCH}:refs/heads/${BRANCH}"
retry_cmd 5 git fetch --prune hf
fi
echo "==> Preparing working branch..."
git checkout -B "${BRANCH}" "origin/${BRANCH}"
ORIGIN_SHA="$(git rev-parse "origin/${BRANCH}")"
HF_SHA="$(git rev-parse "hf/${BRANCH}")"
echo "origin/${BRANCH}: ${ORIGIN_SHA}"
echo "hf/${BRANCH}: ${HF_SHA}"
if [[ "${ORIGIN_SHA}" != "${HF_SHA}" ]]; then
echo "==> Divergence detected. Attempting merge..."
set +e
git merge --no-ff --no-edit "hf/${BRANCH}"
MERGE_RC=$?
set -e
if (( MERGE_RC != 0 )); then
git merge --abort || true
echo "Merge conflict detected between origin/${BRANCH} and hf/${BRANCH}."
echo "Resolve manually and rerun workflow."
exit 2
fi
else
echo "==> Branches already aligned. Nothing to merge."
fi
LOCAL_SHA="$(git rev-parse HEAD)"
echo "local HEAD: ${LOCAL_SHA}"
if [[ "${LOCAL_SHA}" != "${ORIGIN_SHA}" ]]; then
echo "==> Pushing updates to GitHub..."
retry_cmd 5 git push origin "${BRANCH}:${BRANCH}"
else
echo "==> GitHub already up to date."
fi
# Refresh hf reference in case another process updated it in the meantime
retry_cmd 5 git fetch --prune hf "${BRANCH}"
HF_SHA_NOW="$(git rev-parse "hf/${BRANCH}")"
if [[ "${LOCAL_SHA}" != "${HF_SHA_NOW}" ]]; then
echo "==> Pushing updates to Hugging Face..."
retry_cmd 5 git push hf "${BRANCH}:${BRANCH}"
else
echo "==> Hugging Face already up to date."
fi
echo "==> Mirror sync completed successfully."
|