Blob


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