unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: Xinglu Chen <public@yoctocell.xyz>
To: 47670@debbugs.gnu.org
Cc: "Léo Le Bouter" <lle-bout@zaclys.net>
Subject: [bug#47670] [PATCH 2/2] gnu-maintenance: Add 'sourcehut-git' updater.
Date: Fri, 09 Apr 2021 11:05:05 +0200	[thread overview]
Message-ID: <8ea188fa0521e9ea5f07dcc9973e1fa916dc4494.1617958554.git.public@yoctocell.xyz> (raw)
In-Reply-To: <cover.1617958554.git.public@yoctocell.xyz>

* 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 --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)
-- 
2.31.1






  parent reply	other threads:[~2021-04-09  9:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-09  9:02 [bug#47670] [PATCH 0/2] Add updater for packages hosted as SourceHut Git repositories Xinglu Chen
2021-04-09  9:05 ` [bug#47670] [PATCH 1/2] upstream: Add predicate for Git URLs Xinglu Chen
2021-04-09  9:05 ` Xinglu Chen [this message]
2021-04-09 10:54 ` [bug#47670] [PATCH 0/2] Add updater for packages hosted as SourceHut Git repositories Léo Le Bouter via Guix-patches via
2021-04-09 11:58   ` Xinglu Chen
2021-04-09 12:04     ` Léo Le Bouter via Guix-patches via
2021-04-09 12:41       ` Xinglu Chen
2021-04-09 12:48         ` Xinglu Chen
2021-06-06 13:23           ` Ludovic Courtès
2021-06-11  9:25             ` Xinglu Chen
2021-07-27 10:19               ` Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8ea188fa0521e9ea5f07dcc9973e1fa916dc4494.1617958554.git.public@yoctocell.xyz \
    --to=public@yoctocell.xyz \
    --cc=47670@debbugs.gnu.org \
    --cc=lle-bout@zaclys.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).