1#!/bin/sh 2set -e 3# Shell script to update zlib in the source tree to the most recent version. 4# Zlib rarely creates tags or releases, so we use the latest commit on the main branch. 5# See: https://github.com/nodejs/node/pull/47417 6 7BASE_DIR=$(cd "$(dirname "$0")/../.." && pwd) 8DEPS_DIR="$BASE_DIR/deps" 9 10# shellcheck disable=SC1091 11. "$BASE_DIR/tools/dep_updaters/utils.sh" 12 13echo "Comparing latest upstream with current revision" 14 15git fetch https://chromium.googlesource.com/chromium/src/third_party/zlib.git HEAD 16 17# Revert zconf.h changes before checking diff 18perl -i -pe 's|^//#include "chromeconf.h"|#include "chromeconf.h"|' "$DEPS_DIR/zlib/zconf.h" 19git stash -- "$DEPS_DIR/zlib/zconf.h" 20 21DIFF_TREE=$(git diff --diff-filter=d 'stash@{0}:deps/zlib' FETCH_HEAD) 22 23git stash drop 24 25if [ -z "$DIFF_TREE" ]; then 26 echo "Skipped because zlib 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# We don't want to pull in a commit that was just pushed, and instead rather 34# wait for the next week's update. If no commits have been pushed in the last 35# two days, we assume that the most recent commit is stable enough to be 36# pulled in. 37LAST_CHANGE_DATE=$(git log -1 --format=%ct FETCH_HEAD) 38TWO_DAYS_AGO=$(date -d 'now - 2 days' '+%s') 39 40if [ "$LAST_CHANGE_DATE" -gt "$TWO_DAYS_AGO" ]; then 41 echo "Skipped because the latest version is too recent." 42 exit 0 43fi 44 45NEW_VERSION=$(git rev-parse --short=7 FETCH_HEAD) 46 47echo "Making temporary workspace..." 48 49WORKSPACE=$(mktemp -d 2> /dev/null || mktemp -d -t 'tmp') 50 51cd "$WORKSPACE" 52 53mkdir zlib 54 55ZLIB_TARBALL="zlib-v$NEW_VERSION.tar.gz" 56 57echo "Fetching zlib source archive" 58curl -sL -o "$ZLIB_TARBALL" https://chromium.googlesource.com/chromium/src/+archive/refs/heads/main/third_party/zlib.tar.gz 59 60log_and_verify_sha256sum "zlib" "$ZLIB_TARBALL" 61 62gzip -dc "$ZLIB_TARBALL" | tar xf - -C zlib/ 63 64rm "$ZLIB_TARBALL" 65 66cp "$DEPS_DIR/zlib/zlib.gyp" "$DEPS_DIR/zlib/GN-scraper.py" "$DEPS_DIR/zlib/win32/zlib.def" "$DEPS_DIR" 67 68rm -rf "$DEPS_DIR/zlib" zlib/.git 69 70mv zlib "$DEPS_DIR/" 71 72mv "$DEPS_DIR/zlib.gyp" "$DEPS_DIR/GN-scraper.py" "$DEPS_DIR/zlib/" 73 74mkdir "$DEPS_DIR/zlib/win32" 75 76mv "$DEPS_DIR/zlib.def" "$DEPS_DIR/zlib/win32" 77 78perl -i -pe 's|^#include "chromeconf.h"|//#include "chromeconf.h"|' "$DEPS_DIR/zlib/zconf.h" 79 80echo "All done!" 81echo "" 82echo "Make sure to update the deps/zlib/zlib.gyp if any significant changes have occurred upstream" 83echo "" 84echo "Please git add zlib, commit the new version:" 85echo "" 86echo "$ git add -A deps/zlib" 87echo "$ git commit -m \"deps: update zlib to $NEW_VERSION\"" 88echo "" 89 90# The last line of the script should always print the new version, 91# as we need to add it to $GITHUB_ENV variable. 92echo "NEW_VERSION=$NEW_VERSION" 93