Browse Source

fixcopyright: only change tracked files by default

fruit
Tony Garnock-Jones 4 months ago
parent
commit
452e29ff7f
  1. 50
      fixcopyright.rkt

50
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))
Loading…
Cancel
Save