From: Ricardo Wurmus <rekado@elephly.net>
To: 66138@debbugs.gnu.org
Cc: Ricardo Wurmus <rekado@elephly.net>
Subject: [bug#66138] [PATCH 3/4] etc/committer: Avoid reading original files more than once.
Date: Thu, 21 Sep 2023 16:46:16 +0200 [thread overview]
Message-ID: <b1ed99518dd4ce828c38775c74fb8fc8a82a5dea.1695305549.git.rekado@elephly.net> (raw)
In-Reply-To: <0ed6325c8784aacd4b46003bbde8542dd95e08aa.1695305549.git.rekado@elephly.net>
* 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
--
2.41.0
next prev parent reply other threads:[~2023-09-21 14:47 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-21 14:12 [bug#66138] [PATCH 1/4] etc/committer: Do not recompute changes when there are no definitions Ricardo Wurmus
2023-09-21 14:46 ` [bug#66138] [PATCH 2/4] etc/committer: Do not record positions when reading from git files Ricardo Wurmus
2023-09-21 14:46 ` Ricardo Wurmus [this message]
2023-09-21 14:46 ` [bug#66138] [PATCH 4/4] etc/committer: Speed up surrounding-sexp Ricardo Wurmus
2023-09-24 12:12 ` bug#66138: [PATCH 1/4] etc/committer: Do not recompute changes when there are no definitions Ricardo Wurmus
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b1ed99518dd4ce828c38775c74fb8fc8a82a5dea.1695305549.git.rekado@elephly.net \
--to=rekado@elephly.net \
--cc=66138@debbugs.gnu.org \
/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 external index
https://git.savannah.gnu.org/cgit/guix.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.