|
| 1 | +#!/bin/sh |
| 2 | +# Usage: sync.sh <path/to/filtered> |
| 3 | +# |
| 4 | +# A script to sync Mozilla's git mirror of WebRender [1] to the Servo project's |
| 5 | +# downstream fork [2]. This script runs as part of a GitHub Action in this |
| 6 | +# repository triggered on regular intervals. |
| 7 | +# |
| 8 | +# The procedure for the sync is: |
| 9 | +# |
| 10 | +# 1. Clone a copy of the GitHub gecko-dev repository to the "_cache" directory. |
| 11 | +# 2. Filter that repository using `git-filter-repo` and create a new local git |
| 12 | +# repository with the filtered contents into a directory specified by the |
| 13 | +# argument passed to this script. The filtered contents are determined |
| 14 | +# by the configuration in `.github/sync/webrender.paths`. |
| 15 | +# 3. Cherry-pick the new commits into the repository in the current working |
| 16 | +# directory. The commits applied from the filtered repository are determined |
| 17 | +# by choosing every commit after the hash found in the file |
| 18 | +# `.github/sync/UPSTREAM_COMMIT` |
| 19 | +# |
| 20 | +# Note that this script relies on the idea that filtering `gecko-dev` the same |
| 21 | +# way more than once will result in the same commit hashes. |
| 22 | +# |
| 23 | +# If at some point, `webrender.paths` is modified and the commit hashes change, |
| 24 | +# then a single manual filter will have to happen in order to translate the |
| 25 | +# hash in the original filtered repository to the new one. The procedure for this |
| 26 | +# is roughly: |
| 27 | +# |
| 28 | +# 1. Run `git-filter-repo` locally and note the new hash of the latest |
| 29 | +# commit included from upstream. |
| 30 | +# 2. Replace the contents `UPSTREAM_COMMIT` with that hash and commit |
| 31 | +# it together with your changes to `webrender.paths`. |
| 32 | +# |
| 33 | +# [1]: <https://github.com/mozilla/gecko-dev/> mirrored from |
| 34 | +# <https://hg.mozilla.org/mozilla-central> |
| 35 | +# [2]: <https://github.com/mozilla/gecko-dev/> |
| 36 | +set -eux |
| 37 | + |
| 38 | +root_dir=$(pwd) |
| 39 | +cache_dir=$root_dir/_cache |
| 40 | + |
| 41 | +# Configure git because we will be making commits. |
| 42 | +git_name="Webrender Upstream Sync" |
| 43 | + |
| 44 | + |
| 45 | +step() { |
| 46 | + if [ "${TERM-}" != '' ]; then |
| 47 | + tput setaf 12 |
| 48 | + fi |
| 49 | + >&2 printf '* %s\n' "$*" |
| 50 | + if [ "${TERM-}" != '' ]; then |
| 51 | + tput sgr0 |
| 52 | + fi |
| 53 | +} |
| 54 | + |
| 55 | +step "Creating directory for filtered upstream repo if needed" |
| 56 | +mkdir -p "$1" |
| 57 | +cd -- "$1" |
| 58 | +filtered=$(pwd) |
| 59 | + |
| 60 | +step "Creating cache directory if needed" |
| 61 | +mkdir -p "$cache_dir" |
| 62 | +cd "$cache_dir" |
| 63 | +export PATH="$PWD:$PATH" |
| 64 | + |
| 65 | +step "Downloading git-filter-repo if needed" |
| 66 | +if ! git filter-repo --version 2> /dev/null; then |
| 67 | + curl -O https://raw.githubusercontent.com/newren/git-filter-repo/v2.38.0/git-filter-repo |
| 68 | + chmod +x git-filter-repo |
| 69 | + |
| 70 | + git filter-repo --version |
| 71 | +fi |
| 72 | + |
| 73 | +step "Cloning upstream if needed" |
| 74 | +if ! [ -e upstream ]; then |
| 75 | + git clone --bare --single-branch --progress https://github.com/mozilla/gecko-dev.git upstream |
| 76 | +fi |
| 77 | + |
| 78 | +step "Updating upstream" |
| 79 | +branch=$(git -C upstream rev-parse --abbrev-ref HEAD) |
| 80 | +git -C upstream fetch origin $branch:$branch |
| 81 | + |
| 82 | +step "Filtering upstream" |
| 83 | +# Cloning and filtering is much faster than git filter-repo --source --target. |
| 84 | +git clone upstream -- "$filtered" |
| 85 | +git -C "$filtered" filter-repo --force --paths-from-file "$root_dir/.github/sync/webrender.paths" |
| 86 | + |
| 87 | +step "Adding filtered repository as a remote" |
| 88 | +cd "$root_dir" |
| 89 | +git remote add filtered-upstream "$filtered" |
| 90 | +git fetch filtered-upstream |
| 91 | + |
| 92 | +step "Resetting main branch to filtered repository HEAD" |
| 93 | +git switch -c __work |
| 94 | +git reset --hard filtered-upstream/master |
| 95 | +git show origin/test |
| 96 | +git cherry-pick origin/test |
| 97 | +git switch -c test |
| 98 | +git reset --hard __work |
0 commit comments