From patchwork Thu Sep 21 14:46:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Wurmus X-Patchwork-Id: 54140 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id E060327BBE9; Thu, 21 Sep 2023 15:47:27 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id F263327BBE2 for ; Thu, 21 Sep 2023 15:47:25 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qjKxC-0001xd-Or; Thu, 21 Sep 2023 10:46:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qjKxA-0001wl-9N for guix-patches@gnu.org; Thu, 21 Sep 2023 10:46:52 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qjKx9-0000iD-Vw for guix-patches@gnu.org; Thu, 21 Sep 2023 10:46:52 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qjKxJ-0007Jn-Vc for guix-patches@gnu.org; Thu, 21 Sep 2023 10:47:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#66138] [PATCH 3/4] etc/committer: Avoid reading original files more than once. Resent-From: Ricardo Wurmus Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 21 Sep 2023 14:47:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 66138 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 66138@debbugs.gnu.org Cc: Ricardo Wurmus Received: via spool by 66138-submit@debbugs.gnu.org id=B66138.169530761228096 (code B ref 66138); Thu, 21 Sep 2023 14:47:01 +0000 Received: (at 66138) by debbugs.gnu.org; 21 Sep 2023 14:46:52 +0000 Received: from localhost ([127.0.0.1]:34606 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qjKx8-0007Iz-CH for submit@debbugs.gnu.org; Thu, 21 Sep 2023 10:46:52 -0400 Received: from sender3-of-o57.zoho.com ([136.143.184.57]:21797) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qjKx2-0007Ij-8Z for 66138@debbugs.gnu.org; Thu, 21 Sep 2023 10:46:48 -0400 Delivered-To: rekado@elephly.net ARC-Seal: i=1; a=rsa-sha256; t=1695307587; cv=none; d=zohomail.com; s=zohoarc; b=Bzyfw6oTh7gWkRZri0QaAqCuB4PXo0sDu81rlHxfCYqy+A/yk6aw4EiIJ2szwA2X55zKOFZmN4/PknJ34hcm5LBtg1tTpBG+GHd9mb8lWu8MpdZ54nXhsp4L7Y2/1tbk+gz3fenlv5xgDivxlG2Q187u0fnq9B9Yy/sQbMWiWyU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695307587; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=2sCtuGczjdVKVRe0xueDENm6iOweokxzh4DtmIal2s0=; b=T3zLWmmQJLbZy9OS9aFLKCObu0p8yYyURPGQEAQ7lW2qQWuVBSbs4/YEdiWxbO52Dij2zaCboDkkTV0++C3dZfk3kHfiyfnH4tN1hypg+lpylFYssez+BPAmtKCakuGid0eiTMLgxd6syAu0056ODjlzim9EmUptuQQtRHYSHac= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=elephly.net; spf=pass smtp.mailfrom=rekado@elephly.net; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1695307587; s=zoho; d=elephly.net; i=rekado@elephly.net; h=From:From:To:To:Cc:Cc:Message-ID:Subject:Subject:Date:Date:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Reply-To; bh=2sCtuGczjdVKVRe0xueDENm6iOweokxzh4DtmIal2s0=; b=G4DwDjjPK7mEmRWnfBYpKdU/tOJ1D1DgUBmBzONyKr+UqNvMqvXKIm6C96ecGxTy eU05B7Qiv/wZoe4PAkH73zAgvZSfOw9tA5wPWRWLrZWHm0WKEvlUB0dS+/UTNVN3jjH 563Nqp3Sr/jYrNGX/Cu1wENya4YlKGoZ7guMi3PI= Received: from localhost (141.80.245.246 [141.80.245.246]) by mx.zohomail.com with SMTPS id 1695307585901946.7685561359534; Thu, 21 Sep 2023 07:46:25 -0700 (PDT) From: Ricardo Wurmus Message-ID: Date: Thu, 21 Sep 2023 16:46:16 +0200 X-Mailer: git-send-email 2.41.0 In-Reply-To: <0ed6325c8784aacd4b46003bbde8542dd95e08aa.1695305549.git.rekado@elephly.net> References: <0ed6325c8784aacd4b46003bbde8542dd95e08aa.1695305549.git.rekado@elephly.net> MIME-Version: 1.0 X-ZohoMailClient: External 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+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * etc/committer.scm.in (%original-file-cache): New variable. (read-original-file): New procedure. (read-original-file*): New procedure. (old-sexp): Use it. --- etc/committer.scm.in | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/etc/committer.scm.in b/etc/committer.scm.in index 45efb68be2..eb8865513e 100755 --- a/etc/committer.scm.in +++ b/etc/committer.scm.in @@ -196,21 +196,34 @@ (define (lines-to-first-change hunk) (string-ref line 0))) (hunk-diff-lines hunk)))) -(define (old-sexp hunk) - "Using the diff information in HUNK return the unmodified S-expression -corresponding to the top-level definition containing the staged changes." - ;; TODO: We can't seek with a pipe port... +(define %original-file-cache + (make-hash-table)) + +(define (read-original-file file-name) + "Return the contents of FILE-NAME prior to any changes." (let* ((port (open-pipe* OPEN_READ "git" "cat-file" "-p" (string-append - "HEAD:" - (hunk-file-name hunk)))) + "HEAD:" file-name))) (contents (get-string-all port))) (close-pipe port) - (call-with-input-string contents - (lambda (port) - (surrounding-sexp port - (+ (lines-to-first-change hunk) - (hunk-old-line-number hunk))))))) + contents)) + +(define (read-original-file* file-name) + "Caching variant of READ-ORIGINAL-FILE." + (or (hashv-ref %original-file-cache file-name) + (let ((value (read-original-file file-name))) + (hashv-set! %original-file-cache file-name value) + value))) + +(define (old-sexp hunk) + "Using the diff information in HUNK return the unmodified S-expression +corresponding to the top-level definition containing the staged changes." + ;; TODO: We can't seek with a pipe port... + (call-with-input-string (read-original-file* (hunk-file-name hunk)) + (lambda (port) + (surrounding-sexp port + (+ (lines-to-first-change hunk) + (hunk-old-line-number hunk)))))) (define (new-sexp hunk) "Using the diff information in HUNK return the modified S-expression