2016-01-05 14:32:59 +00:00
|
|
|
updateSourceDateEpoch() {
|
|
|
|
local path="$1"
|
2024-06-03 06:28:26 +00:00
|
|
|
# Avoid passing option-looking directory to find. The example is diffoscope-269:
|
|
|
|
# https://salsa.debian.org/reproducible-builds/diffoscope/-/issues/378
|
|
|
|
[[ $path == -* ]] && path="./$path"
|
2016-01-05 14:32:59 +00:00
|
|
|
|
|
|
|
# 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.
|
2017-09-17 07:03:37 +00:00
|
|
|
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))
|
2017-08-13 09:28:05 +00:00
|
|
|
local time="${res[0]//\.[0-9]*/}" # remove the fraction part
|
2016-01-05 14:32:59 +00:00
|
|
|
local newestFile="${res[1]}"
|
|
|
|
|
|
|
|
# Update $SOURCE_DATE_EPOCH if the most recent file we found is newer.
|
2018-11-29 08:49:08 +00:00
|
|
|
if [ "${time:-0}" -gt "$SOURCE_DATE_EPOCH" ]; then
|
2016-01-05 14:32:59 +00:00
|
|
|
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() {
|
2016-01-24 22:51:30 +00:00
|
|
|
if [ -n "$sourceRoot" ]; then
|
|
|
|
updateSourceDateEpoch "$sourceRoot"
|
|
|
|
fi
|
2016-01-05 14:32:59 +00:00
|
|
|
}
|