guix-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

85/86: etc/committer: Record minimal context for hunks to avoid problems


From: guix-commits
Subject: 85/86: etc/committer: Record minimal context for hunks to avoid problems.
Date: Wed, 7 Apr 2021 21:22:19 -0400 (EDT)

rekado pushed a commit to branch master
in repository guix.

commit 43fb6b765d82ea5acfdc83f61472d99594ee1cbf
Author: Ricardo Wurmus <rekado@elephly.net>
AuthorDate: Thu Apr 8 03:00:57 2021 +0200

    etc/committer: Record minimal context for hunks to avoid problems.
    
    With zero context new definitions would be applied to the wrong location in
    the file.  More context lines lead to larger hunks, though, so we use just 
one
    line of context.
    
    * etc/committer.scm.in (diff-info): Invoke "git diff" with one line of
    context.
    [info]: Merge line break and first line.
    (lines-to-first-change): New procedure.
    (old-sexp, new-sexp): Use it.
---
 etc/committer.scm.in | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/etc/committer.scm.in b/etc/committer.scm.in
index 8744bae..376e1ac 100755
--- a/etc/committer.scm.in
+++ b/etc/committer.scm.in
@@ -91,10 +91,10 @@ LINE-NO in PORT."
   (let ((port (open-pipe* OPEN_READ
                           "git" "diff"
                           "--no-prefix"
-                          ;; Do not include any context lines.  This makes it
-                          ;; easier to find the S-expression surrounding the
-                          ;; change.
-                          "--unified=0"
+                          ;; Only include one context line to avoid lumping in
+                          ;; new definitions with changes to existing
+                          ;; definitions.
+                          "--unified=1"
                           "gnu")))
     (define (extract-line-number line-tag)
       (abs (string->number
@@ -132,13 +132,22 @@ LINE-NO in PORT."
                  (loop (cons (make-hunk file-name
                                         (extract-line-number old-start)
                                         (extract-line-number new-start)
-                                        (cons* line "\n" diff-lines)
+                                        (cons (string-append line "\n")
+                                              diff-lines)
                                         definition?) acc)
                        file-name)))))
            (else (loop acc file-name))))))
     (close-pipe port)
     info))
 
+(define (lines-to-first-change hunk)
+  "Return the number of diff lines until the first change."
+  (1- (count (lambda (line)
+               ((negate char-set-contains?)
+                (char-set #\+ #\-)
+                (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."
@@ -150,7 +159,9 @@ corresponding to the top-level definition containing the 
staged changes."
     (close-pipe port)
     (call-with-input-string contents
       (lambda (port)
-        (surrounding-sexp port (hunk-old-line-number hunk))))))
+        (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
@@ -158,7 +169,8 @@ corresponding to the top-level definition containing the 
staged changes."
   (call-with-input-file (hunk-file-name hunk)
     (lambda (port)
       (surrounding-sexp port
-                        (hunk-new-line-number hunk)))))
+                        (+ (lines-to-first-change hunk)
+                           (hunk-new-line-number hunk))))))
 
 (define* (change-commit-message file-name old new #:optional (port 
(current-output-port)))
   "Print ChangeLog commit message for changes between OLD and NEW."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]