11cb0ef41Sopenharmony_ci#!/bin/sh
21cb0ef41Sopenharmony_ciset -e
31cb0ef41Sopenharmony_ci# Shell script to update zlib in the source tree to the most recent version.
41cb0ef41Sopenharmony_ci# Zlib rarely creates tags or releases, so we use the latest commit on the main branch.
51cb0ef41Sopenharmony_ci# See: https://github.com/nodejs/node/pull/47417
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciBASE_DIR=$(cd "$(dirname "$0")/../.." && pwd)
81cb0ef41Sopenharmony_ciDEPS_DIR="$BASE_DIR/deps"
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci# shellcheck disable=SC1091
111cb0ef41Sopenharmony_ci. "$BASE_DIR/tools/dep_updaters/utils.sh"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciecho "Comparing latest upstream with current revision"
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cigit fetch https://chromium.googlesource.com/chromium/src/third_party/zlib.git HEAD
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci# Revert zconf.h changes before checking diff 
181cb0ef41Sopenharmony_ciperl -i -pe 's|^//#include "chromeconf.h"|#include "chromeconf.h"|' "$DEPS_DIR/zlib/zconf.h"
191cb0ef41Sopenharmony_cigit stash -- "$DEPS_DIR/zlib/zconf.h"
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciDIFF_TREE=$(git diff --diff-filter=d 'stash@{0}:deps/zlib' FETCH_HEAD)
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_cigit stash drop
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciif [ -z "$DIFF_TREE" ]; then
261cb0ef41Sopenharmony_ci  echo "Skipped because zlib is on the latest version."
271cb0ef41Sopenharmony_ci  exit 0
281cb0ef41Sopenharmony_cifi
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci# This is a rather arbitrary restriction. This script is assumed to run on
311cb0ef41Sopenharmony_ci# Sunday, shortly after midnight UTC. This check thus prevents pulling in the
321cb0ef41Sopenharmony_ci# most recent commits if any changes were made on Friday or Saturday (UTC).
331cb0ef41Sopenharmony_ci# We don't want to pull in a commit that was just pushed, and instead rather
341cb0ef41Sopenharmony_ci# wait for the next week's update. If no commits have been pushed in the last
351cb0ef41Sopenharmony_ci# two days, we assume that the most recent commit is stable enough to be
361cb0ef41Sopenharmony_ci# pulled in.
371cb0ef41Sopenharmony_ciLAST_CHANGE_DATE=$(git log -1 --format=%ct FETCH_HEAD)
381cb0ef41Sopenharmony_ciTWO_DAYS_AGO=$(date -d 'now - 2 days' '+%s')
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciif [ "$LAST_CHANGE_DATE" -gt "$TWO_DAYS_AGO" ]; then
411cb0ef41Sopenharmony_ci  echo "Skipped because the latest version is too recent."
421cb0ef41Sopenharmony_ci  exit 0
431cb0ef41Sopenharmony_cifi
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciNEW_VERSION=$(git rev-parse --short=7 FETCH_HEAD)
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciecho "Making temporary workspace..."
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciWORKSPACE=$(mktemp -d 2> /dev/null || mktemp -d -t 'tmp')
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_cicd "$WORKSPACE"
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_cimkdir zlib
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ciZLIB_TARBALL="zlib-v$NEW_VERSION.tar.gz"
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ciecho "Fetching zlib source archive"
581cb0ef41Sopenharmony_cicurl -sL -o "$ZLIB_TARBALL" https://chromium.googlesource.com/chromium/src/+archive/refs/heads/main/third_party/zlib.tar.gz
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_cilog_and_verify_sha256sum "zlib" "$ZLIB_TARBALL"
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_cigzip -dc "$ZLIB_TARBALL" | tar xf - -C zlib/
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_cirm "$ZLIB_TARBALL"
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_cicp "$DEPS_DIR/zlib/zlib.gyp" "$DEPS_DIR/zlib/GN-scraper.py" "$DEPS_DIR/zlib/win32/zlib.def" "$DEPS_DIR"
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_cirm -rf "$DEPS_DIR/zlib" zlib/.git
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_cimv zlib "$DEPS_DIR/"
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_cimv "$DEPS_DIR/zlib.gyp" "$DEPS_DIR/GN-scraper.py" "$DEPS_DIR/zlib/"
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_cimkdir "$DEPS_DIR/zlib/win32"
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_cimv "$DEPS_DIR/zlib.def" "$DEPS_DIR/zlib/win32"
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ciperl -i -pe 's|^#include "chromeconf.h"|//#include "chromeconf.h"|' "$DEPS_DIR/zlib/zconf.h"
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ciecho "All done!"
811cb0ef41Sopenharmony_ciecho ""
821cb0ef41Sopenharmony_ciecho "Make sure to update the deps/zlib/zlib.gyp if any significant changes have occurred upstream"
831cb0ef41Sopenharmony_ciecho ""
841cb0ef41Sopenharmony_ciecho "Please git add zlib, commit the new version:"
851cb0ef41Sopenharmony_ciecho ""
861cb0ef41Sopenharmony_ciecho "$ git add -A deps/zlib"
871cb0ef41Sopenharmony_ciecho "$ git commit -m \"deps: update zlib to $NEW_VERSION\""
881cb0ef41Sopenharmony_ciecho ""
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci# The last line of the script should always print the new version,
911cb0ef41Sopenharmony_ci# as we need to add it to $GITHUB_ENV variable.
921cb0ef41Sopenharmony_ciecho "NEW_VERSION=$NEW_VERSION"
93