nixpkgs/pkgs/build-support/setup-hooks/set-source-date-epoch-to-latest.sh
Sergei Trofimovich a3f1b476c7 setup-hooks/set-source-date-epoch-to-latest.sh: handle $path that starts with dash
Without the change `updateSourceDateEpoch` fails on tarballs like
`diffoscope-269` that contain single top-level `-269` root as:

    diffoscope> source root is -269
    diffoscope> find: unknown predicate `-269'
    diffoscope> chmod: invalid mode: ‘-269’
    diffoscope> Try 'chmod --help' for more information.

Currently `diffoscope-269` has a `sourceRoot = "./-269";` workaround to
bypass the failure.

Co-authored-by: Philip Taron <philip.taron@gmail.com>
2024-06-10 23:56:46 +01:00

38 lines
1.5 KiB
Bash

updateSourceDateEpoch() {
local path="$1"
# Avoid passing option-looking directory to find. The example is diffoscope-269:
# https://salsa.debian.org/reproducible-builds/diffoscope/-/issues/378
[[ $path == -* ]] && path="./$path"
# Get the last modification time of all regular files, sort them,
# and get the most recent. Maybe we should use
# https://github.com/0-wiz-0/findnewest here.
local -a res=($(find "$path" -type f -not -newer "$NIX_BUILD_TOP/.." -printf '%T@ %p\0' \
| sort -n --zero-terminated | tail -n1 --zero-terminated | head -c -1))
local time="${res[0]//\.[0-9]*/}" # remove the fraction part
local newestFile="${res[1]}"
# Update $SOURCE_DATE_EPOCH if the most recent file we found is newer.
if [ "${time:-0}" -gt "$SOURCE_DATE_EPOCH" ]; then
echo "setting SOURCE_DATE_EPOCH to timestamp $time of file $newestFile"
export SOURCE_DATE_EPOCH="$time"
# Warn if the new timestamp is too close to the present. This
# may indicate that we were being applied to a file generated
# during the build, or that an unpacker didn't restore
# timestamps properly.
local now="$(date +%s)"
if [ "$time" -gt $((now - 60)) ]; then
echo "warning: file $newestFile may be generated; SOURCE_DATE_EPOCH may be non-deterministic"
fi
fi
}
postUnpackHooks+=(_updateSourceDateEpochFromSourceRoot)
_updateSourceDateEpochFromSourceRoot() {
if [ -n "$sourceRoot" ]; then
updateSourceDateEpoch "$sourceRoot"
fi
}