|
| 1 | +#! /bin/sh |
| 2 | +# -*-scheme-*- |
| 3 | +export LANG=C LANGUAGE=C LC_TIME=C |
| 4 | +export TZ=UTC0 |
| 5 | +exec guile --no-auto-compile -L $srcdir -C $srcdir -e '(mdate-from-git)' -s "$0" "$@" |
| 6 | +!# |
| 7 | + |
| 8 | +;;; GNU Guix --- Functional package management for GNU |
| 9 | +;;; Copyright © 2024 Janneke Nieuwenhuizen <[email protected]> |
| 10 | +;;; |
| 11 | +;;; This file is part of GNU Guix. |
| 12 | +;;; |
| 13 | +;;; This program is free software; you can redistribute it and/or modify it |
| 14 | +;;; under the terms of the GNU General Public License as published by |
| 15 | +;;; the Free Software Foundation; either version 3 of the License, or (at |
| 16 | +;;; your option) any later version. |
| 17 | +;;; |
| 18 | +;;; This program is distributed in the hope that it will be useful, but |
| 19 | +;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | +;;; GNU General Public License for more details. |
| 22 | +;;; |
| 23 | +;;; You should have received a copy of the GNU General Public License |
| 24 | +;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 | + |
| 26 | +;;;; Commentary: |
| 27 | +;;; |
| 28 | +;;; Usage: mdate-from-git.scm FILE |
| 29 | +;;; |
| 30 | +;;; This script is compatible with Automake's `mdate-sh' but uses the timestamp |
| 31 | +;;; from Git instead of from the file system. Also, it can be appended to |
| 32 | +;;; mdate-sh. |
| 33 | + |
| 34 | +;;; As a special exception for Guix, it caters for doc/guix.LANG.texi files that |
| 35 | +;;; are not stored in Git, by using po/doc/guix-manual.LANG.po for the Git |
| 36 | +;;; timestamp. Test doing something like: |
| 37 | +;;; |
| 38 | +;;; build-aux/mdate-from-git.scm doc/guix.de.texi |
| 39 | +;;; |
| 40 | +;;;; Code: |
| 41 | + |
| 42 | +(define-module (mdate-from-git) |
| 43 | + #:use-module (ice-9 match) |
| 44 | + #:use-module (ice-9 popen) |
| 45 | + #:use-module (ice-9 rdelim) |
| 46 | + #:use-module (ice-9 regex) |
| 47 | + #:export (main)) |
| 48 | + |
| 49 | +(define (pipe-command command) |
| 50 | + (let* ((port (apply open-pipe* OPEN_READ command)) |
| 51 | + (output (read-string port))) |
| 52 | + (close-port port) |
| 53 | + output)) |
| 54 | + |
| 55 | +(define (guix.LANG.texi->guix-manual.LANG.po file-name) |
| 56 | + "Translated manuals doc/guix.LANG.texi are not tracked in Git and are |
| 57 | +generated from po/doc/guix-manual.LANG.po. For such an untraced .TEXI file, |
| 58 | +return its .PO counterpart." |
| 59 | + (let ((m (string-match "doc/guix.([^.]+).texi" file-name))) |
| 60 | + (if (not m) file-name |
| 61 | + (let ((lang (match:substring m 1))) |
| 62 | + (format #f "po/doc/guix-manual.~a.po" lang))))) |
| 63 | + |
| 64 | + |
| 65 | +;;; |
| 66 | +;;; Entry point. |
| 67 | +;;; |
| 68 | +(define (main args) |
| 69 | + (match args |
| 70 | + ((script file-name) |
| 71 | + (let* ((command `("git" "ls-files" "--error-unmatch" "--" ,file-name)) |
| 72 | + (tracked? (zero? (with-error-to-port (%make-void-port "w") |
| 73 | + (lambda _ |
| 74 | + (with-output-to-port (%make-void-port "w") |
| 75 | + (lambda _ (apply system* command))))))) |
| 76 | + (file-name (if tracked? file-name |
| 77 | + (guix.LANG.texi->guix-manual.LANG.po file-name))) |
| 78 | + (command `("git" "log" "--pretty=format:%ct" "-n1" "--" ,file-name)) |
| 79 | + (timestamp (with-error-to-port (%make-void-port "w") |
| 80 | + (lambda _ (pipe-command command)))) |
| 81 | + (source-date-epoch (or (getenv "SOURCE_DATE_EPOCH") "1")) |
| 82 | + (timestamp (if (string-null? timestamp) source-date-epoch |
| 83 | + timestamp)) |
| 84 | + (time (gmtime (string->number timestamp))) |
| 85 | + (d-m-y (strftime "%-d %B %Y" time))) |
| 86 | + (display d-m-y))) |
| 87 | + (_ |
| 88 | + (format (current-error-port) "Usage: mdate-from-git.scm FILE\n") |
| 89 | + (exit 2)))) |
0 commit comments