Blame


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