Skip to content

Commit e3f4ff4

Browse files
tniessentargos
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 7c552e6 commit e3f4ff4

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

.github/workflows/tools.yml

+9
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
@@ -235,6 +236,14 @@ jobs:
235236
cat temp-output
236237
tail -n1 temp-output | grep "NEW_VERSION=" >> "$GITHUB_ENV" || true
237238
rm temp-output
239+
- id: googletest
240+
subsystem: deps
241+
label: dependencies, test
242+
run: |
243+
./tools/dep_updaters/update-googletest.sh > temp-output
244+
cat temp-output
245+
tail -n1 temp-output | grep "NEW_VERSION=" >> "$GITHUB_ENV" || true
246+
rm temp-output
238247
steps:
239248
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
240249
if: github.event_name == 'schedule' || inputs.id == 'all' || inputs.id == matrix.id
+93
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)