From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id cLqGNaAz22EpSAEAgWs5BA (envelope-from ) for ; Sun, 09 Jan 2022 20:12:32 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id gEY4LqAz22GEvAAAG6o9tA (envelope-from ) for ; Sun, 09 Jan 2022 20:12:32 +0100 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 40FBC3DFE8 for ; Sun, 9 Jan 2022 20:12:32 +0100 (CET) Received: from localhost ([::1]:41328 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n6dcE-00030H-CE for larch@yhetil.org; Sun, 09 Jan 2022 14:12:30 -0500 Received: from eggs.gnu.org ([209.51.188.92]:54338) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n6das-0001b6-1X for guix-patches@gnu.org; Sun, 09 Jan 2022 14:11:06 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:58439) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n6dar-00069D-PM for guix-patches@gnu.org; Sun, 09 Jan 2022 14:11:05 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1n6dar-0007n5-Kx for guix-patches@gnu.org; Sun, 09 Jan 2022 14:11:05 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#53144] [PATCH 11/13] git: Support resolving references without cloning. Resent-From: Maxime Devos Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sun, 09 Jan 2022 19:11:05 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 53144 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 53144@debbugs.gnu.org Cc: Maxime Devos Received: via spool by 53144-submit@debbugs.gnu.org id=B53144.164175543429828 (code B ref 53144); Sun, 09 Jan 2022 19:11:05 +0000 Received: (at 53144) by debbugs.gnu.org; 9 Jan 2022 19:10:34 +0000 Received: from localhost ([127.0.0.1]:51328 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n6daL-0007ku-CD for submit@debbugs.gnu.org; Sun, 09 Jan 2022 14:10:33 -0500 Received: from xavier.telenet-ops.be ([195.130.132.52]:49144) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n6daA-0007iQ-Np for 53144@debbugs.gnu.org; Sun, 09 Jan 2022 14:10:25 -0500 Received: from localhost.localdomain ([188.188.180.65]) by xavier.telenet-ops.be with bizsmtp id gjAG2600J1R3YAc01jANsT; Sun, 09 Jan 2022 20:10:22 +0100 From: Maxime Devos Date: Sun, 9 Jan 2022 19:10:13 +0000 Message-Id: <20220109191015.33058-11-maximedevos@telenet.be> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20220109191015.33058-1-maximedevos@telenet.be> References: <20220109191015.33058-1-maximedevos@telenet.be> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telenet.be; s=r22; t=1641755422; bh=upQS00ODOcvEJm2qGuFwgCEsZwJgFPknkrfDPqc8QiQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Xm4bycv9tbecohAyHZ1gSCYSsuOzaP3BHnCF9y+43shFHCfpOno5Wq7XzOkD1Bd3C gcIXrANTHLJtoee3rlxbL7+pNdWwrfjN5L2laaKEo0V5jMKEKyFWOjPUGJQX2kZeb+ ymrKik4XHHMoBhZsWn0mvXDTwihp1w+S4LWWwbv1SxQNMXoJebO66DQH52JqbzTHlO /ocoXjvvwcKbvV543GlY1KCRrH12BD/Fr3EN03dUqL0nB9CbjXz+pxP7r5rPvMYrPr zhk4lPE8jhA2w6SNkIJjm7QjKrfC08ZgtKBIqYDlHtmj7D+4XSHh5XADY5pfhWtpF0 ER9pgQ6eVvOhg== X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1641755552; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=upQS00ODOcvEJm2qGuFwgCEsZwJgFPknkrfDPqc8QiQ=; b=KjuIshwob10MlHacYdpuC95f2H0ezmk01/dFkzWX63yLN6W7nVLeUVitJikJ2Bomj+YF7C RHTcmjd6E+urmgl5S9v4Elo7C0lzs5bGOoH25PEAWGkdkfEDcHHNJXvMyNQ0jooWbvDj5Y Fsu1TW/ue23sVO0B7wdfylhidjHXs8XoJOgJpNSBwJkKoKEKxLVnfIPwersuQRmipcczne 3DqUQn7PtcEDI0HsJj1mN1Hkj7s+f67ughJ2xzS7eEZjPMF6f8FF/WiN3YqQ0KKVxXzUfO z95cAWyiRxZxw68Pi6ep9rMx8qSB3apU8ws9k7E/mOYuS4xVI2HLFFk7KVpQBw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1641755552; a=rsa-sha256; cv=none; b=gIXdMaJzRJJu/ZKk1fE/+4o4owiTenzh3JPTOC8ZWHRdraRs8fpPWeMO/WDdBOGKkuFYxn V2Nb7UWfc3bnE5nStd7ykJhLjeHJU++PJAAPA8YGCflSljUMj580J4BNNJ4xU3D0rtNR6m 0ec+dgrDW5ceDBcfviudSy4t8LJuNhFEqDiZYT1P9E6UDIipvG32ntyXPW1loK65nZ3k68 1oqA7UXu021BZTiKzyAiZcgEXnLPfjs9KUSvWkWF2rsAqYheGDi+eiarAdWWI9v3+4AheT OWiy08ZKGIm9uziw+ha3rVqpTcnkRR4cB2ygm3maYWBGqvfyxOJZUDFuLdG3yA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=telenet.be header.s=r22 header.b=Xm4bycv9; dmarc=fail reason="SPF not aligned (relaxed)" header.from=telenet.be (policy=none); spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -1.11 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=telenet.be header.s=r22 header.b=Xm4bycv9; dmarc=fail reason="SPF not aligned (relaxed)" header.from=telenet.be (policy=none); spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 40FBC3DFE8 X-Spam-Score: -1.11 X-Migadu-Scanner: scn1.migadu.com X-TUID: IrPoLEvzTez3 * guix/git.scm (remote-refs): Split off some logic to ... (call-with-detached-remote): ... this new procedure. (lookup-reference): New procedure. * tests/git.scm ("lookup-reference: branch and HEAD"): New test. --- guix/git.scm | 57 +++++++++++++++++++++++++++++++++++++++------------ tests/git.scm | 22 +++++++++++++++++++- 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/guix/git.scm b/guix/git.scm index 43e85a5026..1c07eba584 100644 --- a/guix/git.scm +++ b/guix/git.scm @@ -62,6 +62,7 @@ (define-module (guix git) commit-relation remote-refs + lookup-reference git-checkout git-checkout? @@ -628,6 +629,22 @@ (define (commit-relation old new) ;;; Remote operations. ;;; +;; TODO: it would be nice to use 'remote-create-detached' here, +;; but that procedure isn't in any released version of guile-git yet. +(define (call-with-detached-remote url proc) + "Call PROC with a remote for URL. The remote is closed after PROC returns." + (call-with-temporary-directory + (lambda (cache-directory) + (let* ((repository (repository-init cache-directory)) + ;; Create an in-memory remote so we don't touch disk. + (remote (remote-create-anonymous repository url))) + (remote-connect remote) + (let-values ((result (proc remote))) + ;; Wait until we're finished with the repository before closing it. + (remote-disconnect remote) + (repository-close! repository) + (apply values result)))))) + (define* (remote-refs url #:key tags?) "Return the list of references advertised at Git repository URL. If TAGS? is true, limit to only refs/tags." @@ -649,19 +666,33 @@ (define (remote-head->ref remote) name))) (with-libgit2 - (call-with-temporary-directory - (lambda (cache-directory) - (let* ((repository (repository-init cache-directory)) - ;; Create an in-memory remote so we don't touch disk. - (remote (remote-create-anonymous repository url))) - (remote-connect remote) - - (let* ((remote-heads (remote-ls remote)) - (refs (filter-map remote-head->ref remote-heads))) - ;; Wait until we're finished with the repository before closing it. - (remote-disconnect remote) - (repository-close! repository) - refs)))))) + (call-with-detached-remote + url + (lambda (remote) + (define remote-heads (remote-ls remote)) + (filter-map remote-head->ref remote-heads))))) + +(define* (lookup-reference url reference-name) + "Lookup the reference named REFERENCE-NAME advertised at the Git repository +at URL and return the commit string. If the reference was not found, return +#false instead." + (define (oid->commit oid) + (define str (oid->string oid)) + ;; FIXME: why is the result of oid->string prefixed by 8 zeroes + ;; when remote-ls is used? To make hash collisions harder, it would + ;; be nice if the commit was not abbreviated. + (if (string-prefix? "00000000" str) + (string-drop str 8) + str)) + (define (match? remote-head) + (string=? reference-name (remote-head-name remote-head))) + (with-libgit2 + (call-with-detached-remote + url + (lambda (remote) + (define remote-heads (remote-ls remote)) + (define head (find match? remote-heads)) + (and=> head (compose oid->commit remote-head-oid)))))) ;;; diff --git a/tests/git.scm b/tests/git.scm index d0646bbc85..f8eaf9e93b 100644 --- a/tests/git.scm +++ b/tests/git.scm @@ -1,6 +1,7 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2019, 2020 Ludovic Courtès -;;; Copyright © 2021 Xinglu Chen +;;; Copyright © 2022 Maxime Devos ;;; ;;; This file is part of GNU Guix. ;;; @@ -189,4 +190,23 @@ (define-module (test-git) (tag "v1.1" "Release 1.1")) (remote-refs directory #:tags? #t))) +(test-equal "lookup-reference: branch and HEAD" + '(#true #true) + (with-temporary-git-repository directory + '((add "a.txt" "A") + (commit "First commit") + (branch "a-branch") + (add "b.txt" "B") + (commit "Second commit")) + (with-repository directory repository + ;; See 'oid->commit' in (guix git) for why not string=?. + (list (string-prefix? + (lookup-reference directory "refs/heads/a-branch") + (oid->string + (commit-id (find-commit repository "First commit")))) + (string-prefix? + (lookup-reference directory "HEAD") + (oid->string + (commit-id (find-commit repository "Second commit")))))))) + (test-end "git") -- 2.34.0