commit baabb97c464480745a7cba5b4ac0ec04469ef340
parent cfbf2130daa43633b317c572829b3527be305857
Author: Michael Weiss <dev.primeos@gmail.com>
Date: Wed, 7 Nov 2018 12:26:47 +0100
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/
Diffstat:
2 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/src/main.c b/src/main.c
@@ -1,3 +1,4 @@
+#define _XOPEN_SOURCE
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
@@ -67,10 +68,22 @@ static void parse_preamble(struct parser *p) {
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
@@ -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 <<EOF | grep '^\.TH "test" "8" "'"$(date +'%F')"'"' >/dev/null
test(8)
@@ -66,3 +69,11 @@ scdoc <<EOF | grep '^\.TH "test-manual" "8" "'"$(date +'%F')"'" "" "Header"' >/d
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 <<EOF | grep '^\.TH "reproducible-manual" "8" "2017-12-10"' >/dev/null
+reproducible-manual(8)
+EOF
+end 0