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 3e9e6cca 2022-07-04 thomas die()
8 3e9e6cca 2022-07-04 thomas {
9 3e9e6cca 2022-07-04 thomas echo "$@" >&2
10 3e9e6cca 2022-07-04 thomas exit 1
11 3e9e6cca 2022-07-04 thomas }
12 3e9e6cca 2022-07-04 thomas
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".
16 53f2da6f 2022-09-07 thomas nproc()
17 53f2da6f 2022-09-07 thomas {
18 53f2da6f 2022-09-07 thomas NPROCCMD="nproc"
19 53f2da6f 2022-09-07 thomas
20 5ec66d17 2022-09-18 thomas command "$NPROCCMD" >/dev/null 2>&1 || {
21 53f2da6f 2022-09-07 thomas NPROCCMD="sysctl -n hw.ncpu"
22 53f2da6f 2022-09-07 thomas }
23 53f2da6f 2022-09-07 thomas
24 53f2da6f 2022-09-07 thomas result="$(eval command $NPROCCMD)"
25 53f2da6f 2022-09-07 thomas [ -z "$result" -o "$result" -le 0 ] && result="1"
26 53f2da6f 2022-09-07 thomas
27 53f2da6f 2022-09-07 thomas echo "$result"
28 53f2da6f 2022-09-07 thomas }
29 53f2da6f 2022-09-07 thomas
30 3e9e6cca 2022-07-04 thomas [ -z "$(git status --porcelain)" ] || die "Working tree is not clean"
31 3e9e6cca 2022-07-04 thomas
32 3e9e6cca 2022-07-04 thomas echo "Updating main from origin..."
33 3e9e6cca 2022-07-04 thomas
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"
40 3e9e6cca 2022-07-04 thomas }
41 3e9e6cca 2022-07-04 thomas
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"
47 3e9e6cca 2022-07-04 thomas exit
48 3e9e6cca 2022-07-04 thomas }
49 3e9e6cca 2022-07-04 thomas
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.
53 3e9e6cca 2022-07-04 thomas
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"
58 3e9e6cca 2022-07-04 thomas }
59 3e9e6cca 2022-07-04 thomas
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
62 3e9e6cca 2022-07-04 thomas
63 3e9e6cca 2022-07-04 thomas read -p "Proceed? [Y/n]: " resp
64 3e9e6cca 2022-07-04 thomas
65 3e9e6cca 2022-07-04 thomas [ "$resp" = "N" -o "$resp" = "n" ] && exit
66 3e9e6cca 2022-07-04 thomas
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
70 3e9e6cca 2022-07-04 thomas
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'
74 c82e62cb 2022-07-04 thomas do
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
83 c82e62cb 2022-07-04 thomas do
84 c82e62cb 2022-07-04 thomas sed -i -e "/$h/d" "$file"
85 c82e62cb 2022-07-04 thomas done
86 c82e62cb 2022-07-04 thomas done
87 c82e62cb 2022-07-04 thomas
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"
101 3e9e6cca 2022-07-04 thomas }
102 3e9e6cca 2022-07-04 thomas } || die "Build failed"
103 3e9e6cca 2022-07-04 thomas }
104 3e9e6cca 2022-07-04 thomas
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"