mirror of
https://github.com/oSoWoSo/DistroHopper.git
synced 2026-12-06 04:43:46 +00:00
278 lines
No EOL
11 KiB
Bash
Executable file
278 lines
No EOL
11 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Apply upstream quickemu changes to the vendored copies in this repository,
|
|
# preserving DistroHopper's local patches. Called by
|
|
# .github/workflows/sync-upstream.yml (runs locally for testing too).
|
|
#
|
|
# Model:
|
|
# upstream/baseline/<file> snapshot of the upstream file that the current
|
|
# local file is derived from.
|
|
# upstream/stamp.json upstream repo/branch/sha of the last applied sync.
|
|
#
|
|
# For every vendored file:
|
|
# - local == upstream nothing to do
|
|
# - local == baseline clean fast-forward to upstream
|
|
# - otherwise three-way merge (ours, base, upstream HEAD);
|
|
# conflicts are left with markers for a PR.
|
|
#
|
|
# On the first run there is no baseline. If the working copy is plain upstream
|
|
# or new, the current upstream files are recorded. A customized working copy is
|
|
# re-based onto the upstream revision it actually forks from (so it can advance
|
|
# to current upstream on the same run); a baseline recorded from the wrong
|
|
# revision is detected and corrected later when merges yield no effective
|
|
# change.
|
|
#
|
|
# Writes a shell-sourceable state file (default $RUNNER_TEMP/upstream-sync-state)
|
|
# used by the workflow to decide between commit / push and open-a-PR.
|
|
set -euo pipefail
|
|
|
|
UPSTREAM_REPO="${UPSTREAM_REPO:-quickemu-project/quickemu}"
|
|
UPSTREAM_BRANCH="${UPSTREAM_BRANCH:-master}"
|
|
VENDORED_FILES="${UPSTREAM_VENDORED_FILES:-quickemu quickget chunkcheck quickreport}"
|
|
BASELINE_DIR="${UPSTREAM_BASELINE_DIR:-upstream/baseline}"
|
|
STAMP_FILE="${UPSTREAM_STAMP_FILE:-upstream/stamp.json}"
|
|
STATE_FILE="${UPSTREAM_STATE_FILE:-/tmp/upstream-sync-state}"
|
|
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
cd "${REPO_ROOT}"
|
|
|
|
TMP="$(mktemp -d)"
|
|
trap 'rm -rf "${TMP}"' EXIT
|
|
|
|
CHANGED=()
|
|
CONFLICTED=()
|
|
BASELINE_CREATED=0
|
|
|
|
echo "== Fetching ${UPSTREAM_REPO} (${UPSTREAM_BRANCH}) =="
|
|
git remote remove upstream 2>/dev/null || true
|
|
git remote add upstream "https://github.com/${UPSTREAM_REPO}.git"
|
|
# History is needed to locate the upstream base the working copy actually forks
|
|
# from (see find_base_commit). If the server rejects --shallow-since, fetch the
|
|
# full branch history instead of a bare tip -- find_base_commit is useless with
|
|
# a single snapshotted commit and would silently keep a customized file wedged.
|
|
if ! git fetch --shallow-since="${UPSTREAM_SINCE:-4 years ago}" upstream "${UPSTREAM_BRANCH}" >/dev/null 2>&1; then
|
|
echo "WARN: shallow fetch unsupported for ${UPSTREAM_REPO}; fetching full history" >&2
|
|
git fetch upstream "${UPSTREAM_BRANCH}" >/dev/null 2>&1 || {
|
|
echo "ERROR: cannot fetch ${UPSTREAM_REPO} ${UPSTREAM_BRANCH}" >&2
|
|
exit 1
|
|
}
|
|
fi
|
|
UPSTREAM_SHA="$(git rev-parse "upstream/${UPSTREAM_BRANCH}")"
|
|
echo "Upstream HEAD: ${UPSTREAM_SHA}"
|
|
|
|
mkdir -p "${BASELINE_DIR}"
|
|
|
|
# Locate the upstream commit the working copy of $file actually forks from so
|
|
# the three-way merge uses a meaningful base. The local patches make the file
|
|
# unrecognizable to git as any single upstream blob, so pick the revision whose
|
|
# content is closest to the working copy; a zero-line diff breaks out early.
|
|
find_base_commit() {
|
|
local file="$1" rev="upstream/${UPSTREAM_BRANCH}" c d best bestdiff
|
|
best=""
|
|
bestdiff=999999999
|
|
while IFS= read -r c; do
|
|
# lint: content distance is measured by actual added/deleted lines only
|
|
# (-U0 drops context; headers are filtered out), not raw -u output.
|
|
# grep -c exits 1 on zero matches; under pipefail that would abort the
|
|
# substitution, so swallow the status but keep the count.
|
|
d="$(git show "${c}:${file}" 2>/dev/null | diff -U0 - "${file}" 2>/dev/null | grep -cE '^[+-][^+-]')" || true
|
|
[ -n "${d}" ] || continue
|
|
if [ "${d}" -lt "${bestdiff}" ]; then
|
|
best="${c}"
|
|
bestdiff="${d}"
|
|
[ "${d}" -eq 0 ] && break
|
|
fi
|
|
done < <(git log "${rev}" --format='%H' -- "${file}" 2>/dev/null)
|
|
printf '%s\n' "${best}"
|
|
}
|
|
|
|
# The fork base of a customized file is stable, so remember it and only run the
|
|
# expensive history scan when the cache is empty or its commit is unavailable.
|
|
cached_base() {
|
|
local file="$1" cache="${BASELINE_DIR}/${file}.base" base=""
|
|
if [ -f "${cache}" ]; then
|
|
base="$(cat "${cache}")"
|
|
# Validate the cached commit is still part of the fetched branch; a
|
|
# retained object (e.g. from a rewritten upstream) would rebase wrongly.
|
|
if ! git cat-file -e "${base}:${file}" 2>/dev/null ||
|
|
! git merge-base --is-ancestor "${base}" "upstream/${UPSTREAM_BRANCH}" 2>/dev/null; then
|
|
base=""
|
|
fi
|
|
fi
|
|
if [ -z "${base}" ]; then
|
|
base="$(find_base_commit "${file}")"
|
|
[ -n "${base}" ] && printf '%s\n' "${base}" > "${cache}"
|
|
fi
|
|
printf '%s\n' "${base}"
|
|
}
|
|
|
|
# Restore the executable bit of a materialized vendored file. Existing working
|
|
# copies keep their current mode; brand-new files take the upstream blob mode.
|
|
apply_vendored_mode() {
|
|
local file="$1" mode="${VENDORED_MODE}"
|
|
if [ -z "${mode}" ]; then
|
|
mode="$(git ls-tree "upstream/${UPSTREAM_BRANCH}" -- "${file}" | awk '{print $1}')"
|
|
[ "${mode}" = "100755" ] && mode=755 || mode=""
|
|
fi
|
|
if [ -n "${mode}" ]; then
|
|
chmod "${mode}" "${file}"
|
|
fi
|
|
}
|
|
|
|
for file in ${VENDORED_FILES}; do
|
|
if ! git cat-file -e "upstream/${UPSTREAM_BRANCH}:${file}" 2>/dev/null; then
|
|
echo "WARN: upstream no longer ships '${file}'"
|
|
if [ -f "${file}" ]; then
|
|
CONFLICTED+=("${file} (removed upstream)")
|
|
fi
|
|
continue
|
|
fi
|
|
git show "upstream/${UPSTREAM_BRANCH}:${file}" > "${TMP}/${file}"
|
|
# git show spools the blob with default permissions, dropping the upstream
|
|
# executable bit; remember the working copy's mode (or, for brand-new files,
|
|
# fall back to the upstream blob mode) so scripts keep +x after materializing.
|
|
VENDORED_MODE=""
|
|
if [ -e "${file}" ]; then
|
|
# GNU stat -c %a and BSD stat -f %Lp both print the full octal permissions.
|
|
VENDORED_MODE="$(stat -c %a "${file}" 2>/dev/null || stat -f %Lp "${file}" 2>/dev/null || true)"
|
|
fi
|
|
|
|
baseline="${BASELINE_DIR}/${file}"
|
|
if [ ! -f "${baseline}" ]; then
|
|
if [ ! -f "${file}" ]; then
|
|
# Brand-new vendored file: vendor it and record a baseline, and mark
|
|
# it changed so the workflow stages it (a missing root file would
|
|
# otherwise break the next run when it tries to merge it).
|
|
cp "${TMP}/${file}" "${file}"
|
|
cp "${TMP}/${file}" "${baseline}"
|
|
apply_vendored_mode "${file}"
|
|
BASELINE_CREATED=1
|
|
CHANGED+=("${file}: added")
|
|
echo "added new vendored file ${file} (${UPSTREAM_SHA:0:12})"
|
|
continue
|
|
fi
|
|
if cmp -s "${file}" "${TMP}/${file}"; then
|
|
cp "${TMP}/${file}" "${baseline}"
|
|
BASELINE_CREATED=1
|
|
echo "baseline recorded for ${file} (${UPSTREAM_SHA:0:12})"
|
|
continue
|
|
fi
|
|
# The working copy forks a previous upstream revision, not HEAD: record
|
|
# the real base so the merge below can advance local to current upstream.
|
|
base="$(find_base_commit "${file}")"
|
|
if [ -z "${base}" ]; then
|
|
echo "ERROR: cannot determine the upstream base of ${file}; set upstream/baseline/${file} from the upstream revision the local file is derived from" >&2
|
|
exit 1
|
|
fi
|
|
git show "${base}:${file}" > "${baseline}"
|
|
BASELINE_CREATED=1
|
|
printf '%s\n' "${base}" > "${BASELINE_DIR}/${file}.base"
|
|
echo "baseline for ${file} recorded from upstream ${base:0:12} (local fork base)"
|
|
fi
|
|
|
|
if cmp -s "${file}" "${TMP}/${file}"; then
|
|
cp "${TMP}/${file}" "${baseline}" # keep baseline pinned to upstream HEAD
|
|
echo "already in sync: ${file}"
|
|
continue
|
|
fi
|
|
|
|
if cmp -s "${file}" "${baseline}"; then
|
|
cp "${TMP}/${file}" "${file}"
|
|
cp "${TMP}/${file}" "${baseline}"
|
|
apply_vendored_mode "${file}"
|
|
CHANGED+=("${file}: updated")
|
|
echo "updated ${file}"
|
|
continue
|
|
fi
|
|
|
|
# DistroHopper patches live on top of the baseline -> three-way merge.
|
|
# If no effective change falls out, the baseline may have been recorded from
|
|
# current upstream instead of the revision the local file actually forks
|
|
# from (which wedges the file "in sync" forever); re-base and retry once.
|
|
attempts=0
|
|
while :; do
|
|
attempts=$((attempts + 1))
|
|
set +e
|
|
git merge-file -p "${file}" "${baseline}" "${TMP}/${file}" > "${TMP}/${file}.merged"
|
|
rc=$?
|
|
set -e
|
|
if [ "${rc}" -eq 0 ]; then
|
|
if cmp -s "${TMP}/${file}.merged" "${file}"; then
|
|
if [ "${attempts}" -eq 1 ] && cmp -s "${baseline}" "${TMP}/${file}" && ! cmp -s "${file}" "${baseline}"; then
|
|
base="$(cached_base "${file}")"
|
|
if [ -n "${base}" ] && git show "${base}:${file}" > "${TMP}/${file}.rebase" 2>/dev/null; then
|
|
if ! cmp -s "${TMP}/${file}.rebase" "${baseline}"; then
|
|
cp "${TMP}/${file}.rebase" "${baseline}"
|
|
echo "re-based ${file} onto upstream ${base:0:12}"
|
|
continue
|
|
fi
|
|
fi
|
|
fi
|
|
# No effective change (e.g. baseline and upstream are identical).
|
|
echo "no effective change for ${file}"
|
|
cp "${TMP}/${file}" "${baseline}" # keep baseline pinned to upstream HEAD
|
|
break
|
|
else
|
|
mv "${TMP}/${file}.merged" "${file}"
|
|
apply_vendored_mode "${file}"
|
|
cp "${TMP}/${file}" "${baseline}"
|
|
CHANGED+=("${file}: merged")
|
|
echo "3-way merged ${file} (clean)"
|
|
break
|
|
fi
|
|
elif [ "${rc}" -eq 1 ]; then
|
|
mv "${TMP}/${file}.merged" "${file}"
|
|
apply_vendored_mode "${file}"
|
|
CONFLICTED+=("${file}")
|
|
echo "CONFLICT in ${file}: conflict markers written, manual resolution needed"
|
|
break
|
|
else
|
|
echo "ERROR: git merge-file failed for ${file} (rc=${rc})"
|
|
exit 1
|
|
fi
|
|
done
|
|
done
|
|
|
|
mkdir -p "$(dirname "${STAMP_FILE}")"
|
|
cat > "${STAMP_FILE}" <<JSON
|
|
{
|
|
"repo": "${UPSTREAM_REPO}",
|
|
"branch": "${UPSTREAM_BRANCH}",
|
|
"sha": "${UPSTREAM_SHA}",
|
|
"sync_at": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
}
|
|
JSON
|
|
|
|
if [ "${#CHANGED[@]}" -gt 0 ]; then
|
|
CHANGED_FILES="$(printf '%s; ' "${CHANGED[@]}")"
|
|
else
|
|
CHANGED_FILES=""
|
|
fi
|
|
if [ "${#CONFLICTED[@]}" -gt 0 ]; then
|
|
CONFLICT_FILES="$(printf '%s; ' "${CONFLICTED[@]}")"
|
|
else
|
|
CONFLICT_FILES=""
|
|
fi
|
|
|
|
cat > "${STATE_FILE}" <<EOF
|
|
UPSTREAM_REPO=${UPSTREAM_REPO}
|
|
UPSTREAM_BRANCH=${UPSTREAM_BRANCH}
|
|
UPSTREAM_SHA=${UPSTREAM_SHA}
|
|
UPSTREAM_SHORT=${UPSTREAM_SHA:0:12}
|
|
FIRST_RUN=${BASELINE_CREATED}
|
|
CHANGED=${#CHANGED[@]}
|
|
CONFLICTS=${#CONFLICTED[@]}
|
|
CONFLICT_FILES='${CONFLICT_FILES}'
|
|
CHANGED_FILES='${CHANGED_FILES}'
|
|
EOF
|
|
|
|
echo
|
|
echo "== Summary =="
|
|
echo "Changed: ${#CHANGED[@]}"
|
|
echo "Conflicts: ${#CONFLICTED[@]}"
|
|
echo "First run: ${BASELINE_CREATED}"
|
|
echo "State: ${STATE_FILE}"
|
|
for entry in "${CHANGED[@]}"; do
|
|
echo " + ${entry}"
|
|
done
|
|
for entry in "${CONFLICTED[@]}"; do
|
|
echo " ! ${entry} (conflict)"
|
|
done |