From 452e29ff7f6502cef3f5f4d1c49a2cb92face209 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Fri, 4 Jun 2021 16:39:54 +0200 Subject: [PATCH] fixcopyright: only change tracked files by default --- fixcopyright.rkt | 50 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/fixcopyright.rkt b/fixcopyright.rkt index 8bb565e..d0b71db 100755 --- a/fixcopyright.rkt +++ b/fixcopyright.rkt @@ -14,7 +14,14 @@ (define (get-git-config key) (string-trim (with-output-to-string - (lambda () (system (format "git config --get ~a" key)))))) + (lambda () (system* "/usr/bin/env" "git" "config" "--get" key))))) + +(define (is-tracked? f) + (call-with-output-file "/dev/null" #:exists 'append + (lambda (sink) + (parameterize ((current-error-port sink) + (current-output-port sink)) + (system* "/usr/bin/env" "git" "ls-files" "--error-unmatch" f))))) (define user-name (get-git-config "user.name")) (define user-email (get-git-config "user.email")) @@ -29,9 +36,15 @@ (define total-file-count 0) (define total-changed-files 0) (define dry-run? #f) +(define modify-untracked? #f) -(define (fix-files file-type-name file-pattern front-matter-re leading-comment-re comment-prefix) - (define matched-files (glob file-pattern)) +(define (fix-files #:file-type-name file-type-name + #:file-pattern file-pattern + #:front-matter-re [front-matter-re #f] + #:leading-comment-re leading-comment-re + #:comment-prefix comment-prefix + #:file-filter [file-filter (lambda (x) #t)]) + (define matched-files (filter file-filter (glob file-pattern))) (define file-count (length matched-files)) (define changed-files 0) (for [(file-number (in-naturals)) @@ -72,17 +85,21 @@ (make-copyright user low this-year))] [_ l])) head)) - (head (map (lambda (l) (string-append comment-prefix - (match l - [(list #f v) v] - [(list k v) (format "~a: ~a" k v)]))) + (head (map (lambda (l) + (if (string=? (cadr l) "") + (string-trim comment-prefix) + (string-append comment-prefix + (match l + [(list #f v) v] + [(list k v) (format "~a: ~a" k v)])))) head)) (new-lines `(,@front-matter ,@head "" ,@(dropf tail (lambda (l) (string=? (string-trim l) ""))))) - (changed? (not (equal? all-lines new-lines)))) - (when (and changed? (not dry-run?)) + (would-change-if-written? (not (equal? all-lines new-lines))) + (write-needed? (and would-change-if-written? (or modify-untracked? (is-tracked? f))))) + (when (and write-needed? (not dry-run?)) (call-with-atomic-output-file f (lambda (port _tmp-path) @@ -90,7 +107,7 @@ (for [(l head)] (displayln l port)) (newline port) (for [(l (dropf tail (lambda (l) (string=? (string-trim l) ""))))] (displayln l port))))) - (if changed? + (if write-needed? (begin (set! changed-files (+ changed-files 1)) (printf "\e[41mchanged\e[0m\n")) (printf "\r\e[K")))) @@ -101,9 +118,16 @@ (command-line #:once-each [("-n" "--dry-run") "Do not write back changes to files" - (set! dry-run? #t)]) + (set! dry-run? #t)] + [("--modify-untracked") "Modify files not tracked by git as well as those that are" + (set! modify-untracked? #t)]) + +(void (fix-files #:file-type-name "Racket" + #:file-pattern "**.rkt" + #:front-matter-re #px"^#" + #:leading-comment-re #px"^;+ *" + #:comment-prefix ";;; ")) -(void (fix-files "Racket" "**.rkt" #px"^#" #px"^;+ *" ";;; ")) (printf "fixcopyright: ~a files examined, ~a ~a\n" total-file-count total-changed-files @@ -114,5 +138,7 @@ (if (zero? total-changed-files) "changes were needed" "files were updated"))) + (void (system "chmod a+x fixcopyright.rkt")) + (exit (if (positive? total-changed-files) 1 0))