blob: 31e75fe674e413161630ec1d35e841730df33c53 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#! /usr/bin/env guile
!#
(import (srfi srfi-26)
(srfi srfi-37)
(srfi srfi-171)
(ice-9 ftw)
(ice-9 match)
(ice-9 popen)
(ice-9 rdelim))
(define (invoke program . args)
(unless (zero? (apply system* program args))
(error "Invocation of program failed" (cons program args))))
(define (call-with-input-pipe proc program . args)
"Execute PROGRAM ARGS ... in a subprocess with a pipe to it. Call
PROC with an input port to that pipe. Close the pipe once PROC exits,
even if it exits non-locally. Return the value returned by PROC."
(let ((port #f))
(dynamic-wind (lambda () (set! port (apply open-pipe* OPEN_READ program args)))
(cut proc port)
(lambda ()
(let ((return-value (status:exit-val (close-pipe port))))
(unless (and return-value
(zero? return-value))
(error "Invocation of program failed" (cons program args))))))))
(define* (find-files directory #:optional (pred (const #t)))
"Recursively find all files under DIRECTORY that satisfy PRED."
(define (do-nothing name stat result)
result)
(file-system-fold (const #t)
(lambda (name stat result)
(if (pred name stat)
(cons name result)
result))
do-nothing
do-nothing
do-nothing
(lambda (name stat errno result)
(error (strerror errno) name))
'()
directory))
(define (assignees file)
"Return the list of assignees in gemini FILE."
(call-with-input-file file
(lambda (port)
(port-transduce (compose
(tfilter (cut string-prefix? "* assigned:" <>))
(tappend-map (lambda (line)
(map (cut string-trim-both <>)
(string-split
(substring line (string-length "* assigned:"))
#\,)))))
rcons
read-line
port))))
(define (git-updated-files transducer start-commit end-commit)
"Use TRANSDUCER to transduce over the list of files updated between
START-COMMIT and END-COMMIT."
(call-with-input-pipe
(lambda (port)
(port-transduce (compose (tmap (lambda (line)
(match (string-split line #\tab)
((status file)
(list (match status
("A" 'added)
("D" 'deleted)
("M" 'modified))
file)))))
transducer)
(const #t) read-line port))
"git" "diff" "--stat" "--name-status"
(string-append start-commit ".." end-commit)))
(define rlast
(case-lambda
(() #f)
((result) result)
((result input) input)))
(define (git-first-commit-since since)
"Return the hash of the first git commit since SINCE, where SINCE is
passed verbatim to the --since argument of `git log'. Return #f if
there is no such commit."
(call-with-input-pipe
(lambda (port)
(port-transduce (tmap identity)
rlast
read-line
port))
"git" "log" "--format=format:%H" "--since" since))
(define (color code str)
"Return STR within ANSI escape CODE, thus rendering it in color in a
terminal."
(format #f "~a[~am~a~a[0m" #\esc code str #\esc))
(define red (cut color 31 <>))
(define green (cut color 32 <>))
(define magenta (cut color 35 <>))
(define blue (cut color 34 <>))
(define cyan (cut color 36 <>))
(define (invalid-option name arg loads)
(error "Invalid option" name))
(define (invalid-operand arg loads)
(error "Invalid argument" arg))
(define main
(match-lambda*
((_ "news" args ...)
(let ((args (args-fold args
(list (option (list "since") #t #f
(lambda (opt name arg loads)
(acons 'since arg loads))))
invalid-option
invalid-operand
'())))
(unless (assq 'since args)
(error "--since argument required"))
(git-updated-files (tlog (match-lambda*
((_ (status file))
(format #t ((case status
((added) green)
((deleted) red)
((modified) magenta))
"~a (~a)~%")
file
(case status
((added) "new")
((deleted) "deleted")
((modified) "updated"))))))
(or (git-first-commit-since (assq-ref args 'since))
"HEAD")
"HEAD")))
((_ "list" args ...)
(let ((args (args-fold args
(list (option (list "assigned") #t #f
(lambda (opt name arg loads)
(acons 'assigned arg loads))))
invalid-option
invalid-operand
'())))
(format #t "~%total ~a~%"
(list-transduce (compose (tmap (lambda (file)
(list (substring file (string-length "./"))
(assignees file))))
(tfilter (match-lambda
((_ assignees)
(or (not (assq 'assigned args))
(member (assq-ref args 'assigned) assignees)))))
(tlog (match-lambda*
((_ (file ()))
(format #t "~a~%" file))
((_ (file assignees))
(format #t "~a ~a~%"
file
(magenta (string-append "(assigned: "
(string-join assignees ", ")
")")))))))
rcount
;; Get all gemini files except
;; README.gmi and hidden files. Text
;; editors tend to create hidden files
;; while editing, and we want to avoid
;; them.
(find-files "." (lambda (name _)
(and (string-suffix? ".gmi" name)
(not (string=? (basename name) "README.gmi"))
(not (string-prefix? "." (basename name))))))))))))
(apply main (command-line))
|