• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2set -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
7BASE_DIR=$(cd "$(dirname "$0")/../.." && pwd)
8DEPS_DIR="$BASE_DIR/deps"
9
10NEW_UPSTREAM_SHA1=$(git ls-remote "https://github.com/google/googletest.git" HEAD | awk '{print $1}')
11NEW_VERSION=$(echo "$NEW_UPSTREAM_SHA1" | head -c 7)
12
13echo "Comparing $NEW_VERSION with current revision"
14
15git remote add googletest-upstream https://github.com/google/googletest.git
16git fetch googletest-upstream "$NEW_UPSTREAM_SHA1"
17git remote remove googletest-upstream
18
19DIFF_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
25if [ -z "$DIFF_TREE" ]; then
26  echo "Skipped because googletest is on the latest version."
27  exit 0
28fi
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.
38LAST_CHANGE_DATE=$(git log -1 --format=%ct "$NEW_UPSTREAM_SHA1" -- LICENSE googletest/include googletest/src)
39TWO_DAYS_AGO=$(date -d 'now - 2 days' '+%s')
40if [ "$LAST_CHANGE_DATE" -gt "$TWO_DAYS_AGO" ]; then
41  echo "Skipped because the latest version is too recent."
42  exit 0
43fi
44
45echo "Creating temporary work tree"
46
47WORKSPACE=$(mktemp -d 2> /dev/null || mktemp -d -t 'tmp')
48WORKTREE="$WORKSPACE/googletest"
49
50cleanup () {
51  EXIT_CODE=$?
52  [ -d "$WORKTREE" ] && git worktree remove -f "$WORKTREE"
53  [ -d "$WORKSPACE" ] && rm -rf "$WORKSPACE"
54  exit $EXIT_CODE
55}
56
57trap cleanup INT TERM EXIT
58
59git worktree add "$WORKTREE" "$NEW_UPSTREAM_SHA1"
60
61echo "Copying LICENSE, include and src to deps/googletest"
62for 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")"
65done
66
67echo "Updating googletest.gyp"
68
69NEW_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
81echo "$NEW_GYP" >"$DEPS_DIR/googletest/googletest.gyp"
82
83echo "All done!"
84echo ""
85echo "Please git stage googletest, commit the new version:"
86echo ""
87echo "$ git stage -A deps/googletest"
88echo "$ git commit -m \"deps: update googletest to $NEW_VERSION\""
89echo ""
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.
93echo "NEW_VERSION=$NEW_VERSION"
94