Skip to content

Commit f6955a6

Browse files
tniessenMoLow
authored andcommitted
tools: add update script for googletest
GoogleTest follows the Abseil Live at Head philosophy, and rarely creates tags or GitHub releases, so instead, follow Google's recommendation and update to the upstream HEAD every once in a while. The tricky bit is properly updating googletest.gyp, and this script might fail doing so in the future. Refs: nodejs/security-wg#828 PR-URL: #47482 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
1 parent 4e1d87e commit f6955a6

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

.github/workflows/tools.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ on:
2323
- corepack
2424
- doc
2525
- eslint
26+
- googletest
2627
- libuv
2728
- lint-md-dependencies
2829
- llhttp
@@ -237,6 +238,14 @@ jobs:
237238
cat temp-output
238239
tail -n1 temp-output | grep "NEW_VERSION=" >> "$GITHUB_ENV" || true
239240
rm temp-output
241+
- id: googletest
242+
subsystem: deps
243+
label: dependencies, test
244+
run: |
245+
./tools/dep_updaters/update-googletest.sh > temp-output
246+
cat temp-output
247+
tail -n1 temp-output | grep "NEW_VERSION=" >> "$GITHUB_ENV" || true
248+
rm temp-output
240249
steps:
241250
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
242251
if: github.event_name == 'schedule' || inputs.id == 'all' || inputs.id == matrix.id
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/bin/sh
2+
set -e
3+
# Shell script to update GoogleTest in the source tree to the most recent version.
4+
# GoogleTest follows the Abseil Live at Head philosophy and rarely creates tags
5+
# or GitHub releases, so instead, we use the latest commit on the main branch.
6+
7+
BASE_DIR=$(cd "$(dirname "$0")/../.." && pwd)
8+
DEPS_DIR="$BASE_DIR/deps"
9+
10+
NEW_UPSTREAM_SHA1=$(git ls-remote "https://github.com/google/googletest.git" HEAD | awk '{print $1}')
11+
NEW_VERSION=$(echo "$NEW_UPSTREAM_SHA1" | head -c 7)
12+
13+
echo "Comparing $NEW_VERSION with current revision"
14+
15+
git remote add googletest-upstream https://github.com/google/googletest.git
16+
git fetch googletest-upstream "$NEW_UPSTREAM_SHA1"
17+
git remote remove googletest-upstream
18+
19+
DIFF_TREE=$(
20+
git diff HEAD:deps/googletest/LICENSE "$NEW_UPSTREAM_SHA1:LICENSE"
21+
git diff-tree HEAD:deps/googletest/include "$NEW_UPSTREAM_SHA1:googletest/include"
22+
git diff-tree HEAD:deps/googletest/src "$NEW_UPSTREAM_SHA1:googletest/src"
23+
)
24+
25+
if [ -z "$DIFF_TREE" ]; then
26+
echo "Skipped because googletest is on the latest version."
27+
exit 0
28+
fi
29+
30+
# This is a rather arbitrary restriction. This script is assumed to run on
31+
# Sunday, shortly after midnight UTC. This check thus prevents pulling in the
32+
# most recent commits if any changes were made on Friday or Saturday (UTC).
33+
# Because of Google's own "Live at Head" philosophy, new bugs that are likely to
34+
# affect Node.js tend to be fixed quickly, so we don't want to pull in a commit
35+
# that was just pushed, and instead rather wait for the next week's update. If
36+
# no commits have been pushed in the last two days, we assume that the most
37+
# recent commit is stable enough to be pulled in.
38+
LAST_CHANGE_DATE=$(git log -1 --format=%ct "$NEW_UPSTREAM_SHA1" -- LICENSE googletest/include googletest/src)
39+
TWO_DAYS_AGO=$(date -d 'now - 2 days' '+%s')
40+
if [ "$LAST_CHANGE_DATE" -gt "$TWO_DAYS_AGO" ]; then
41+
echo "Skipped because the latest version is too recent."
42+
exit 0
43+
fi
44+
45+
echo "Creating temporary work tree"
46+
47+
WORKSPACE=$(mktemp -d 2> /dev/null || mktemp -d -t 'tmp')
48+
WORKTREE="$WORKSPACE/googletest"
49+
50+
cleanup () {
51+
EXIT_CODE=$?
52+
[ -d "$WORKTREE" ] && git worktree remove -f "$WORKTREE"
53+
[ -d "$WORKSPACE" ] && rm -rf "$WORKSPACE"
54+
exit $EXIT_CODE
55+
}
56+
57+
trap cleanup INT TERM EXIT
58+
59+
git worktree add "$WORKTREE" "$NEW_UPSTREAM_SHA1"
60+
61+
echo "Copying LICENSE, include and src to deps/googletest"
62+
for p in LICENSE googletest/include googletest/src ; do
63+
rm -rf "$DEPS_DIR/googletest/$(basename "$p")"
64+
cp -R "$WORKTREE/$p" "$DEPS_DIR/googletest/$(basename "$p")"
65+
done
66+
67+
echo "Updating googletest.gyp"
68+
69+
NEW_GYP=$(
70+
sed "/'googletest_sources': \[/q" "$DEPS_DIR/googletest/googletest.gyp"
71+
for f in $( (cd deps/googletest/ && find include src -type f \( -iname '*.h' -o -iname '*.cc' \) ) | LANG=C LC_ALL=C sort --stable ); do
72+
if [ "$(basename "$f")" != "gtest_main.cc" ] &&
73+
[ "$(basename "$f")" != "gtest-all.cc" ] &&
74+
[ "$(basename "$f")" != "gtest_prod.h" ] ; then
75+
echo " '$f',"
76+
fi
77+
done
78+
sed -ne '/\]/,$ p' "$DEPS_DIR/googletest/googletest.gyp"
79+
)
80+
81+
echo "$NEW_GYP" >"$DEPS_DIR/googletest/googletest.gyp"
82+
83+
echo "All done!"
84+
echo ""
85+
echo "Please git stage googletest, commit the new version:"
86+
echo ""
87+
echo "$ git stage -A deps/googletest"
88+
echo "$ git commit -m \"deps: update googletest to $NEW_VERSION\""
89+
echo ""
90+
91+
# The last line of the script should always print the new version,
92+
# as we need to add it to $GITHUB_ENV variable.
93+
echo "NEW_VERSION=$NEW_VERSION"

0 commit comments

Comments
 (0)