1 769c6a98 2022-09-07 thomas #!/usr/bin/env bash
3 3e9e6cca 2022-07-04 thomas # Script to sync changes from upstream to -portable
5 3e9e6cca 2022-07-04 thomas # This script is under the same licence as gameoftrees itself.
9 3e9e6cca 2022-07-04 thomas echo "$@" >&2
13 53f2da6f 2022-09-07 thomas # Wrap the nproc command found on Linux, to return the number of CPU cores.
14 53f2da6f 2022-09-07 thomas # On non-Linux systems, the same value can be found via sysctl. For
15 53f2da6f 2022-09-07 thomas # everything else, just return "1".
18 53f2da6f 2022-09-07 thomas NPROCCMD="nproc"
20 53f2da6f 2022-09-07 thomas command -v "$NPROCCMD" >/dev/null 2>&1 || {
21 53f2da6f 2022-09-07 thomas NPROCCMD="sysctl -n hw.ncpu"
24 53f2da6f 2022-09-07 thomas result="$(eval command $NPROCCMD)"
25 53f2da6f 2022-09-07 thomas [ -z "$result" -o "$result" -le 0 ] && result="1"
27 53f2da6f 2022-09-07 thomas echo "$result"
30 3e9e6cca 2022-07-04 thomas [ -z "$(git status --porcelain)" ] || die "Working tree is not clean"
32 3e9e6cca 2022-07-04 thomas echo "Updating main from origin..."
34 3e9e6cca 2022-07-04 thomas # Update our copy of main
35 3e9e6cca 2022-07-04 thomas git checkout -q main && \
36 0dd633fe 2022-07-21 thomas git fetch -q -n gh >/dev/null 2>&1
37 3e9e6cca 2022-07-04 thomas git fetch -q -n upstream >/dev/null 2>&1 && \
38 3e9e6cca 2022-07-04 thomas git reset -q --hard upstream/main || {
39 3e9e6cca 2022-07-04 thomas die "Couldn't fetch from main and reset to that branch"
42 3e9e6cca 2022-07-04 thomas # Gather a list of commits to cherry-pick.
43 3e9e6cca 2022-07-04 thomas # Don't proceed with no commits.
44 3e9e6cca 2022-07-04 thomas commitc="$(git rev-list --count main...origin/main)"
45 3e9e6cca 2022-07-04 thomas [ -z "$commitc" -o "$commitc" -eq 0 ] && {
46 3e9e6cca 2022-07-04 thomas echo "All commits uptodate. Nothing to cherry-pick"
50 3e9e6cca 2022-07-04 thomas # Create a branch from linux (which is where the result of the cherry-picks
51 3e9e6cca 2022-07-04 thomas # will ultimately end up, but we do this work on a topic branch so that we can
52 3e9e6cca 2022-07-04 thomas # perform CI on it, and not break the 'linux' branch.
54 3e9e6cca 2022-07-04 thomas echo "Creating sync branch..."
55 3e9e6cca 2022-07-04 thomas git branch -q -D syncup >/dev/null 2>&1
56 3e9e6cca 2022-07-04 thomas git checkout -q linux && git checkout -q -b syncup || {
57 3e9e6cca 2022-07-04 thomas die "Can't checkout syncup branch"
60 3e9e6cca 2022-07-04 thomas echo "The following ($commitc) commits will be cherry-picked..."
61 3e9e6cca 2022-07-04 thomas git log --oneline main...origin/main
63 3e9e6cca 2022-07-04 thomas read -p "Proceed? [Y/n]: " resp
65 3e9e6cca 2022-07-04 thomas [ "$resp" = "N" -o "$resp" = "n" ] && exit
67 a8fa2ba8 2022-07-05 thomas # Pick the commits in reverse order.
68 a8fa2ba8 2022-07-05 thomas git rev-list --reverse --first-parent main...origin/main | \
69 a8fa2ba8 2022-07-05 thomas git cherry-pick --stdin --no-rerere-autoupdate -Xtheirs
71 3e9e6cca 2022-07-04 thomas [ $? -eq 0 ] && {
72 c82e62cb 2022-07-04 thomas # Sanity-check header files which are found portably and remove them.
73 c82e62cb 2022-07-04 thomas for h in 'sys\/queue.h' 'ssl\.h' 'endian\.h'
75 c82e62cb 2022-07-04 thomas # Use git's pathspec notation to exclude matching on files
76 c82e62cb 2022-07-04 thomas # where we *want* to keep those headers.
77 c82e62cb 2022-07-04 thomas git grep -Li "$h" -- \
78 c82e62cb 2022-07-04 thomas ':!maintscripts/**' \
79 c82e62cb 2022-07-04 thomas ':!configure.ac' \
80 c82e62cb 2022-07-04 thomas ':!gotweb/parse.y' \
81 c82e62cb 2022-07-04 thomas ':!include/got_compat.h' | \
82 c82e62cb 2022-07-04 thomas while read file
84 c82e62cb 2022-07-04 thomas sed -i -e "/$h/d" "$file"
88 3e9e6cca 2022-07-04 thomas echo "Performing sanity build..."
89 3e9e6cca 2022-07-04 thomas ./autogen.sh >/dev/null 2>&1 && \
90 3e9e6cca 2022-07-04 thomas ./configure >/dev/null 2>&1 && \
91 3e9e6cca 2022-07-04 thomas make -j $(nproc) >/dev/null 2>&1 && {
92 3e9e6cca 2022-07-04 thomas echo " Passed!"
93 c82e62cb 2022-07-04 thomas echo "Creating commit for portable changes..."
94 c82e62cb 2022-07-04 thomas git commit -am "portable: remove include files found portably"
95 3e9e6cca 2022-07-04 thomas echo "...Merging branch to linux"
96 3e9e6cca 2022-07-04 thomas git checkout linux && git merge --ff-only - && {
97 3e9e6cca 2022-07-04 thomas echo "Pushing to GH..."
98 3e9e6cca 2022-07-04 thomas git push gh || die "Couldn't push linux to GH"
99 3e9e6cca 2022-07-04 thomas git checkout main && \
100 3e9e6cca 2022-07-04 thomas git push gh || die "Couldn't push main to GH"
102 3e9e6cca 2022-07-04 thomas } || die "Build failed"
105 3e9e6cca 2022-07-04 thomas echo "Wait for Cirrus-CI..."
106 3e9e6cca 2022-07-04 thomas echo "Then push main and linux to origin"