Subject: [PATCH 1/2] git: 'update-cached-checkout' returns the commit ID when given a tag.

From: Ludovic Courtès <[email-masked]>
Date: Tue, 11 Oct 2022 11:52:28 +0200

Previously, starting with commit
efa578ecaece67366b4b0e2266de7c2faaa4ae54, 'update-cached-checkout' would
return the OID of the annotated tag the tag points to.  With this change
it returns the OID of the commit object in all cases.

* guix/git.scm (resolve-reference): In the 'tag' case, call
'tag-target-id' and 'tag-lookup' when OID designates an annotated tag.
* tests/git.scm ("update-cached-checkout, tag"): New test.
---
 guix/git.scm  | 15 +++++++++------
 tests/git.scm | 32 +++++++++++++++++++++++++++++++-
 2 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/guix/git.scm b/guix/git.scm
index d7fd320f50..0220fe1068 100644
--- a/guix/git.scm
+++ b/guix/git.scm
@@ -271,12 +271,15 @@ (define (resolve-reference repository ref)
                   ;; 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))))
-         ;; OID may point to a "tag" object, but it can also point directly
-         ;; to a "commit" object, as surprising as it may seem.  Return that
-         ;; object, whatever that is.
-         (object-lookup repository oid))))))
+       (let* ((oid (reference-name->oid repository
+                                        (string-append "refs/tags/" tag)))
+              (obj (object-lookup repository oid)))
+         ;; OID may designate an "annotated tag" object or a "commit" object.
+         ;; Return the commit object in both cases.
+         (if (= OBJ-TAG (object-type obj))
+             (object-lookup repository
+                            (tag-target-id (tag-lookup repository oid)))
+             obj))))))
 
 (define (switch-to-ref repository ref)
   "Switch to REPOSITORY's branch, commit or tag specified by REF.  Return the
diff --git a/tests/git.scm b/tests/git.scm
index ca59d2a33e..9c944d65b1 100644
--- a/tests/git.scm
+++ b/tests/git.scm
@@ -22,8 +22,12 @@ (define-module (test-git)
   #:use-module (guix git)
   #:use-module (guix tests git)
   #:use-module (guix build utils)
+  #:use-module ((guix utils) #:select (call-with-temporary-directory))
   #:use-module (srfi srfi-1)
-  #:use-module (srfi srfi-64))
+  #:use-module (srfi srfi-64)
+  #:use-module (srfi srfi-71)
+  #:use-module (ice-9 popen)
+  #:use-module (ice-9 textual-ports))
 
 ;; Test the (guix git) tools.
 
@@ -239,4 +243,30 @@ (define-module (test-git)
         (tag "v1.1" "Release 1.1"))
     (remote-refs directory #:tags? #t)))
 
+(unless (which (git-command)) (test-skip 1))
+(test-assert "update-cached-checkout, tag"
+  (call-with-temporary-directory
+   (lambda (cache)
+     (with-temporary-git-repository directory
+         '((add "a.txt" "A")
+           (commit "First commit")
+           (tag "v1.0" "release-1.0")
+           (branch "develop")
+           (checkout "develop")
+           (add "b.txt" "B")
+           (commit "Second commit")
+           (tag "v1.1" "release-1.1"))
+       (let ((directory commit relation
+                        (update-cached-checkout directory
+                                                #:ref '(tag . "v1.1")
+                                                #:cache-directory cache))
+             (head   (let* ((pipe (open-pipe* OPEN_READ (git-command)
+                                              "-C" directory
+                                              "rev-parse" "HEAD"))
+                            (str  (get-string-all pipe)))
+                       (close-pipe pipe)
+                       (string-trim-right str))))
+         ;; COMMIT should be the ID of the commit object, not that of the tag.
+         (string=? commit head))))))
+
 (test-end "git")
-- 
2.38.0