diff mbox series

[bug#50377,v2,1/2] git: 'resolve-reference' handles 'git describe'-style commit IDs.

Message ID 87pmtmprdz.fsf@gnu.org
State Accepted
Headers show
Series [bug#50377,v2,1/2] git: 'resolve-reference' handles 'git describe'-style commit IDs. | expand

Commit Message

Marius Bakke Sept. 5, 2021, 10:52 p.m. UTC
Marius Bakke <marius@gnu.org> skriver:

> * guix/git.scm (resolve-reference): Use REVPARSE-SINGLE for looking up
> commits.  Rewrite tag-or-commit case to recognize 'git describe' style
> identifiers and resolve them as commits.

I realize we can do this without resorting to 'revparse-single', which
does essentially the same as this procedure.  We just have to extract
the short commit from the describe string.  New patch attached:
diff mbox series

Patch

From 9af0f5fd7d8209ffe94d9e0007f58a1a475fe4f1 Mon Sep 17 00:00:00 2001
From: Marius Bakke <marius@gnu.org>
Date: Mon, 6 Sep 2021 00:21:51 +0200
Subject: [PATCH] git: 'resolve-reference' handles 'git describe'-style commit
 IDs.

* guix/git.scm (resolve-reference): Rewrite tag-or-commit case to recognize
'git describe' style identifiers and resolve them as commits.
---
 guix/git.scm | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/guix/git.scm b/guix/git.scm
index 9c6f326c36..09efab1182 100644
--- a/guix/git.scm
+++ b/guix/git.scm
@@ -2,6 +2,7 @@ 
 ;;; Copyright © 2017, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
 ;;; Copyright © 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2021 Kyle Meyer <kyle@kyleam.com>
+;;; Copyright © 2021 Marius Bakke <marius@gnu.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -223,15 +224,28 @@  corresponding Git object."
              (object-lookup-prefix repository (string->oid commit) len)
              (object-lookup repository (string->oid commit)))))
       (('tag-or-commit . str)
-       (if (or (> (string-length str) 40)
-               (not (string-every char-set:hex-digit str)))
-           (resolve `(tag . ,str))              ;definitely a tag
-           (catch 'git-error
-             (lambda ()
-               (resolve `(tag . ,str)))
-             (lambda _
-               ;; There's no such tag, so it must be a commit ID.
-               (resolve `(commit . ,str))))))
+       (cond ((and (string-contains str "-g")
+                   (match (string-split str #\-)
+                     ((version ... revision g+commit)
+                      (if (and (> (string-length g+commit) 7)
+                               (string-every char-set:digit revision)
+                               (string-every char-set:hex-digit
+                                             (string-drop g+commit 1)))
+                          ;; Looks like a 'git describe' style id, like
+                          ;; v1.3.0-7-gaa34d4d28d.
+                          (resolve `(commit . ,(string-drop g+commit 1)))
+                          #f))
+                     (_ #f))))
+             ((or (> (string-length str) 40)
+                  (not (string-every char-set:hex-digit str)))
+              (resolve `(tag . ,str)))      ;definitely a tag
+             (else
+              (catch 'git-error
+                (lambda ()
+                  (resolve `(tag . ,str)))
+                (lambda _
+                  ;; There's no such tag, so it must be a commit ID.
+                  (resolve `(commit . ,str)))))))
       (('tag    . tag)
        (let ((oid (reference-name->oid repository
                                        (string-append "refs/tags/" tag))))
-- 
2.31.1