diff mbox series

[bug#47670,2/2] gnu-maintenance: Add 'sourcehut-git' updater.

Message ID 8ea188fa0521e9ea5f07dcc9973e1fa916dc4494.1617958554.git.public@yoctocell.xyz
State New
Headers show
Series Add updater for packages hosted as SourceHut Git repositories | expand

Checks

Context Check Description
cbaines/submitting builds success
cbaines/comparison success View comparision
cbaines/git branch success View Git branch
cbaines/applying patch success View Laminar job
cbaines/issue success View issue
cbaines/comparison success View comparision
cbaines/git branch success View Git branch
cbaines/applying patch success View Laminar job
cbaines/issue success View issue

Commit Message

Xinglu Chen April 9, 2021, 9:05 a.m. UTC
* guix/gnu-maintenance.scm (latest-git-tag-version, sourcehut-git-package?,
latest-sourcehut-git-release): New procedures.
(%sourcehut-git-updater): New variable.
* doc/guix.texi (Invoking guix refresh): Document it.
---
 doc/guix.texi            |  3 ++
 guix/gnu-maintenance.scm | 90 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)
diff mbox series

Patch

diff --git a/doc/guix.texi b/doc/guix.texi
index d1a15cb28b..6b6e3401f0 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -11745,6 +11745,9 @@  the updater for @uref{https://www.stackage.org, Stackage} packages.
 the updater for @uref{https://crates.io, Crates} packages.
 @item launchpad
 the updater for @uref{https://launchpad.net, Launchpad} packages.
+@item sourcehut-git
+the updater for packages hosted as @uref{https://sourcehut.org,
+SourceHut} Git repositories.
 @item generic-html
 a generic updater that crawls the HTML page where the source tarball of
 the package is hosted, when applicable.
diff --git a/guix/gnu-maintenance.scm b/guix/gnu-maintenance.scm
index fece84b341..6a2a4ccf34 100644
--- a/guix/gnu-maintenance.scm
+++ b/guix/gnu-maintenance.scm
@@ -2,6 +2,7 @@ 
 ;;; Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2012, 2013 Nikita Karetnikov <nikita@karetnikov.org>
 ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
+;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -25,6 +26,9 @@ 
   #:use-module (sxml simple)
   #:use-module (ice-9 regex)
   #:use-module (ice-9 match)
+  #:use-module (ice-9 popen)
+  #:use-module (ice-9 rdelim)
+  #:use-module (ice-9 receive)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-11)
   #:use-module (srfi srfi-26)
@@ -38,6 +42,7 @@ 
   #:use-module (guix records)
   #:use-module (guix upstream)
   #:use-module (guix packages)
+  #:use-module (guix git-download)
   #:autoload   (zlib) (call-with-gzip-input-port)
   #:autoload   (htmlprag) (html->sxml)            ;from Guile-Lib
   #:export (gnu-package-name
@@ -69,6 +74,7 @@ 
             %sourceforge-updater
             %xorg-updater
             %kernel.org-updater
+            %sourcehut-git-updater
             %generic-html-updater))
 
 ;;; Commentary:
@@ -802,6 +808,83 @@  the directory containing its source tarball."
           (apply throw key args))
         #f))))
 
+(define (latest-git-tag-version package)
+  "Return the latest version of PACKAGE based on Git tags.  This relies on the
+Git tag having the version of the package in the name."
+  (let* ((url (git-reference-url (origin-uri (package-source package))))
+         (port (open-pipe* OPEN_READ
+                           "git"
+                           "ls-remote"
+                           "--tags"
+                           url)))
+
+    (define read-tags
+      (let loop ((lines '()))
+        (let ((line (read-line port)))
+          (cond
+           ((eof-object? line) lines)
+           ;; The hash on the lines without "^{}" dont't correspond to a
+           ;; commit.
+           ;;
+           ;; 0545ff5df25ea019fcb6fc1dcb40da06b35320e9	refs/tags/0.8.1
+           ;; 13042ec03837b72f8d14c04e9abe3ddae88449fa	refs/tags/0.8.1^{}
+           ((not (string-suffix? "^{}" line)) (loop lines))
+           ;; Drop the "^{}"
+           (else (loop (cons (string-drop-right line 3) lines)))))))
+
+    (close-pipe port)
+
+    (define (tag->version tag)
+      (if (string-prefix? "v"tag)
+          (substring tag 1)
+          tag))
+
+    (define (valid-version? tag)
+      (if (string-match "^[0-9._-]*$" (tag->version tag)) #t #f))
+
+    ;; Some projects will publish release candidates which we usually don't
+    ;; want to package.
+    (if (not (null? read-tags))
+        (receive (valid invalid)
+            (partition valid-version?
+                       (map (lambda (line)
+                              (last (string-split line #\/)))
+                            read-tags))
+          (tag->version (first valid)))
+        (package-version package))))
+
+;; Not guaranteed to always work correctly since you can self-host it.
+(define sourcehut-git-package?
+  (let ((hosting-site "git.sr.ht"))
+    (git-url-predicate (lambda (url)
+                         (match (string->uri url)
+                           (#f #f)
+                           (uri
+                            (let ((scheme (uri-scheme uri))
+                                  (host   (uri-host uri)))
+                              (and (memq scheme '(http https))
+                                   (if (string-match hosting-site host)
+                                       #t #f)))))))))
+
+(define (latest-sourcehut-git-release package)
+  "Return the latest release of PACKAGE."
+  (let ((name (package-name package))
+        (old-version (package-version package))
+        (new-version (latest-git-tag-version package))
+        (url (git-reference-url (origin-uri (package-source package)))))
+    (define (ensure-trailing-slash str)
+      (if (string-suffix? "/" str) str (string-append str "/")))
+
+    (if (not (string= old-version new-version))
+        (upstream-source
+         (package name)
+         (version new-version)
+         (urls (list (string-append (ensure-trailing-slash url)
+                                    "archive/"
+                                    new-version
+                                    ".tar.gz"))))
+        #f)))                           ; no tags
+
 (define %gnu-updater
   ;; This is for everything at ftp.gnu.org.
   (upstream-updater
@@ -849,6 +932,13 @@  the directory containing its source tarball."
    (pred (url-prefix-predicate "mirror://kernel.org/"))
    (latest latest-kernel.org-release)))
 
+(define %sourcehut-git-updater
+  (upstream-updater
+   (name 'sourcehut-git)
+   (description "Updater for packages hosted as SourceHut Git repositories")
+   (pred sourcehut-git-package?)
+   (latest latest-sourcehut-git-release)))
+
 (define %generic-html-updater
   (upstream-updater
    (name 'generic-html)