From bbb07c04c6b69cdc6a001dcac7c129cf3b8dc86b Mon Sep 17 00:00:00 2001 From: Andrew Nesbitt Date: Sun, 29 Nov 2026 10:46:53 +0000 Subject: [PATCH] Use SOURCE_DATE_EPOCH for the man page date Makes the generated man pages reproducible. The date is always UTC so it does not vary with the build machine's timezone. --- scripts/generate-man/main.go | 22 +++++++++++++++-- scripts/generate-man/main_test.go | 40 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 scripts/generate-man/main_test.go diff --git a/scripts/generate-man/main.go b/scripts/generate-man/main.go index f1a7226..6d0ec8f 100644 --- a/scripts/generate-man/main.go +++ b/scripts/generate-man/main.go @@ -7,8 +7,10 @@ package main import ( + "fmt" "log" "os" + "strconv" "time" "github.com/spf13/cobra/doc" @@ -23,11 +25,14 @@ func main() { log.Fatal(err) } - now := time.Now() + date, err := buildDate() + if err != nil { + log.Fatal(err) + } header := &doc.GenManHeader{ Title: "PIN", Section: "1", - Date: &now, + Date: &date, Source: "pin", Manual: "Pin Manual", } @@ -36,3 +41,16 @@ func main() { log.Fatal(err) } } + +// UTC so the header date does not depend on the build machine's timezone. +func buildDate() (time.Time, error) { + epoch := os.Getenv("SOURCE_DATE_EPOCH") + if epoch == "" { + return time.Now().UTC(), nil + } + seconds, err := strconv.ParseInt(epoch, 10, 64) + if err != nil { + return time.Time{}, fmt.Errorf("invalid SOURCE_DATE_EPOCH: %w", err) + } + return time.Unix(seconds, 0).UTC(), nil +} diff --git a/scripts/generate-man/main_test.go b/scripts/generate-man/main_test.go new file mode 100644 index 0000000..9b19ed9 --- /dev/null +++ b/scripts/generate-man/main_test.go @@ -0,0 +1,40 @@ +package main + +import ( + "testing" + "time" +) + +func TestBuildDateUsesSourceDateEpoch(t *testing.T) { + t.Setenv("SOURCE_DATE_EPOCH", "1764547200") + t.Setenv("TZ", "America/Los_Angeles") + + got, err := buildDate() + if err != nil { + t.Fatal(err) + } + want := time.Date(2025, time.December, 1, 0, 0, 0, 0, time.UTC) + if !got.Equal(want) || got.Location() != time.UTC { + t.Errorf("buildDate() = %v, want %v", got, want) + } +} + +func TestBuildDateRejectsInvalidEpoch(t *testing.T) { + t.Setenv("SOURCE_DATE_EPOCH", "yesterday") + + if _, err := buildDate(); err == nil { + t.Error("buildDate() accepted a non-numeric SOURCE_DATE_EPOCH") + } +} + +func TestBuildDateDefaultsToNow(t *testing.T) { + t.Setenv("SOURCE_DATE_EPOCH", "") + + got, err := buildDate() + if err != nil { + t.Fatal(err) + } + if time.Since(got) > time.Minute || got.Location() != time.UTC { + t.Errorf("buildDate() = %v, want the current time in UTC", got) + } +}