From baabb97c464480745a7cba5b4ac0ec04469ef340 Mon Sep 17 00:00:00 2001 From: Michael Weiss Date: Wed, 07 Nov 2018 12:26:47 +0100 Subject: [PATCH] Support $SOURCE_DATE_EPOCH for reproducible man pages The environment variable SOURCE_DATE_EPOCH [0] is standardized and can be used to produce reproducible output. Distributions like Debian will set this variable before the build and scdoc should use it (instead of the current date) for any timestamps within the man pages. [0]: https://reproducible-builds.org/docs/source-date-epoch/ --- src/main.c | 21 +++++++++++++++++---- test/preamble | 11 +++++++++++ diff --git a/src/main.c b/src/main.c index 08957bfa4f94304d0e5019495153c2d84bdcd57a..72173c6cc7a84b3788433e614ab82a15bdb1f755 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#define _XOPEN_SOURCE #include #include #include @@ -67,10 +68,22 @@ str_t *extras[2] = { NULL }; int section = -1; uint32_t ch; char date[256]; - time_t now; - time(&now); - struct tm *now_tm = localtime(&now); - strftime(date, sizeof(date), "%F", now_tm); + char *source_date_epoch = getenv("SOURCE_DATE_EPOCH"); + if (source_date_epoch != NULL) { + struct tm source_date_epoch_tm; + char *ret = strptime(source_date_epoch, "%s", &source_date_epoch_tm); + if (ret == NULL || *ret != '\0') { + fprintf(stderr, + "Error: $SOURCE_DATE_EPOCH is set but malformed.\n"); + exit(1); + } + strftime(date, sizeof(date), "%F", &source_date_epoch_tm); + } else { + time_t now; + time(&now); + struct tm *now_tm = localtime(&now); + strftime(date, sizeof(date), "%F", now_tm); + } while ((ch = parser_getch(p)) != UTF8_INVALID) { if ((ch < 0x80 && isalnum(ch)) || ch == '_' || ch == '-' || ch == '.') { int ret = str_append_ch(name, ch); diff --git a/test/preamble b/test/preamble index 03e2d0c528d3ff1b9a590aab6f822f25d0d55913..e46dc32f78eb2c3257f4b5c88925dc4ebe10b1a9 100755 --- a/test/preamble +++ b/test/preamble @@ -37,6 +37,9 @@ test(8) EOF end 0 +# Make sure SOURCE_DATE_EPOCH is not set for the next tests +unset SOURCE_DATE_EPOCH + begin "Writes the appropriate header" scdoc </dev/null test(8) @@ -66,3 +69,11 @@ scdoc </dev/null test-manual(8) "" "Header" EOF end 0 + +export SOURCE_DATE_EPOCH=$(date --date="2017-12-09 23:18:57 -0500" +'%s') + +begin "Supports \$SOURCE_DATE_EPOCH" +scdoc </dev/null +reproducible-manual(8) +EOF +end 0 -- 2.48.1