fixcopyright: only change tracked files by default

This commit is contained in:
Tony Garnock-Jones 2021-06-04 16:39:54 +02:00
parent 2a360bff32
commit 452e29ff7f
1 changed files with 38 additions and 12 deletions

View File

@ -14,7 +14,14 @@
(define (get-git-config key) (define (get-git-config key)
(string-trim (with-output-to-string (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-name (get-git-config "user.name"))
(define user-email (get-git-config "user.email")) (define user-email (get-git-config "user.email"))
@ -29,9 +36,15 @@
(define total-file-count 0) (define total-file-count 0)
(define total-changed-files 0) (define total-changed-files 0)
(define dry-run? #f) (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 (fix-files #:file-type-name file-type-name
(define matched-files (glob file-pattern)) #: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 file-count (length matched-files))
(define changed-files 0) (define changed-files 0)
(for [(file-number (in-naturals)) (for [(file-number (in-naturals))
@ -72,17 +85,21 @@
(make-copyright user low this-year))] (make-copyright user low this-year))]
[_ l])) [_ l]))
head)) head))
(head (map (lambda (l) (string-append comment-prefix (head (map (lambda (l)
(match l (if (string=? (cadr l) "")
[(list #f v) v] (string-trim comment-prefix)
[(list k v) (format "~a: ~a" k v)]))) (string-append comment-prefix
(match l
[(list #f v) v]
[(list k v) (format "~a: ~a" k v)]))))
head)) head))
(new-lines `(,@front-matter (new-lines `(,@front-matter
,@head ,@head
"" ""
,@(dropf tail (lambda (l) (string=? (string-trim l) ""))))) ,@(dropf tail (lambda (l) (string=? (string-trim l) "")))))
(changed? (not (equal? all-lines new-lines)))) (would-change-if-written? (not (equal? all-lines new-lines)))
(when (and changed? (not dry-run?)) (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 (call-with-atomic-output-file
f f
(lambda (port _tmp-path) (lambda (port _tmp-path)
@ -90,7 +107,7 @@
(for [(l head)] (displayln l port)) (for [(l head)] (displayln l port))
(newline port) (newline port)
(for [(l (dropf tail (lambda (l) (string=? (string-trim l) ""))))] (displayln l 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)) (begin (set! changed-files (+ changed-files 1))
(printf "\e[41mchanged\e[0m\n")) (printf "\e[41mchanged\e[0m\n"))
(printf "\r\e[K")))) (printf "\r\e[K"))))
@ -101,9 +118,16 @@
(command-line #:once-each (command-line #:once-each
[("-n" "--dry-run") "Do not write back changes to files" [("-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" (printf "fixcopyright: ~a files examined, ~a ~a\n"
total-file-count total-file-count
total-changed-files total-changed-files
@ -114,5 +138,7 @@
(if (zero? total-changed-files) (if (zero? total-changed-files)
"changes were needed" "changes were needed"
"files were updated"))) "files were updated")))
(void (system "chmod a+x fixcopyright.rkt")) (void (system "chmod a+x fixcopyright.rkt"))
(exit (if (positive? total-changed-files) 1 0)) (exit (if (positive? total-changed-files) 1 0))