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/
master
Michael Weiss 6 years ago committed by Drew DeVault
parent cfbf2130da
commit baabb97c46
  1. 13
      src/main.c
  2. 11
      test/preamble

@ -1,3 +1,4 @@
#define _XOPEN_SOURCE
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <stdbool.h> #include <stdbool.h>
@ -67,10 +68,22 @@ static void parse_preamble(struct parser *p) {
int section = -1; int section = -1;
uint32_t ch; uint32_t ch;
char date[256]; char date[256];
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_t now;
time(&now); time(&now);
struct tm *now_tm = localtime(&now); struct tm *now_tm = localtime(&now);
strftime(date, sizeof(date), "%F", now_tm); strftime(date, sizeof(date), "%F", now_tm);
}
while ((ch = parser_getch(p)) != UTF8_INVALID) { while ((ch = parser_getch(p)) != UTF8_INVALID) {
if ((ch < 0x80 && isalnum(ch)) || ch == '_' || ch == '-' || ch == '.') { if ((ch < 0x80 && isalnum(ch)) || ch == '_' || ch == '-' || ch == '.') {
int ret = str_append_ch(name, ch); int ret = str_append_ch(name, ch);

@ -37,6 +37,9 @@ test(8)
EOF EOF
end 0 end 0
# Make sure SOURCE_DATE_EPOCH is not set for the next tests
unset SOURCE_DATE_EPOCH
begin "Writes the appropriate header" begin "Writes the appropriate header"
scdoc <<EOF | grep '^\.TH "test" "8" "'"$(date +'%F')"'"' >/dev/null scdoc <<EOF | grep '^\.TH "test" "8" "'"$(date +'%F')"'"' >/dev/null
test(8) test(8)
@ -66,3 +69,11 @@ scdoc <<EOF | grep '^\.TH "test-manual" "8" "'"$(date +'%F')"'" "" "Header"' >/d
test-manual(8) "" "Header" test-manual(8) "" "Header"
EOF EOF
end 0 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

Loading…
Cancel
Save