biome: 1.9.2 -> 1.9.3
[NixPkgs.git] / pkgs / build-support / setup-hooks / set-source-date-epoch-to-latest.sh
bloba9a0dc689086ca9bb182cbbf37fe9e2a386596c3
1 updateSourceDateEpoch() {
2 local path="$1"
3 # Avoid passing option-looking directory to find. The example is diffoscope-269:
4 # https://salsa.debian.org/reproducible-builds/diffoscope/-/issues/378
5 [[ $path == -* ]] && path="./$path"
7 # Get the last modification time of all regular files, sort them,
8 # and get the most recent. Maybe we should use
9 # https://github.com/0-wiz-0/findnewest here.
10 local -a res=($(find "$path" -type f -not -newer "$NIX_BUILD_TOP/.." -printf '%T@ %p\0' \
11 | sort -n --zero-terminated | tail -n1 --zero-terminated | head -c -1))
12 local time="${res[0]//\.[0-9]*/}" # remove the fraction part
13 local newestFile="${res[1]}"
15 # Update $SOURCE_DATE_EPOCH if the most recent file we found is newer.
16 if [ "${time:-0}" -gt "$SOURCE_DATE_EPOCH" ]; then
17 echo "setting SOURCE_DATE_EPOCH to timestamp $time of file $newestFile"
18 export SOURCE_DATE_EPOCH="$time"
20 # Warn if the new timestamp is too close to the present. This
21 # may indicate that we were being applied to a file generated
22 # during the build, or that an unpacker didn't restore
23 # timestamps properly.
24 local now="$(date +%s)"
25 if [ "$time" -gt $((now - 60)) ]; then
26 echo "warning: file $newestFile may be generated; SOURCE_DATE_EPOCH may be non-deterministic"
31 postUnpackHooks+=(_updateSourceDateEpochFromSourceRoot)
33 _updateSourceDateEpochFromSourceRoot() {
34 if [ -n "$sourceRoot" ]; then
35 updateSourceDateEpoch "$sourceRoot"