summaryrefslogtreecommitdiffstatshomepage
path: root/tekuti/git.scm
blob: b17c963c6fe95d22b36d7020035437aebe87e967 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
;; Tekuti
;; Copyright (C) 2008, 2010, 2011, 2012 Andy Wingo <wingo at pobox dot com>

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3 of
;; the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, contact:
;;
;; Free Software Foundation           Voice:  +1-617-542-5942
;; 59 Temple Place - Suite 330        Fax:    +1-617-542-2652
;; Boston, MA  02111-1307,  USA       gnu@gnu.org

;;; Commentary:
;;
;; Using git's object database as a persistent store.
;;
;;; Code:

(define-module (tekuti git)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 popen)
  #:use-module (tekuti util)
  #:use-module (tekuti config)
  #:use-module (tekuti match-bind)
  #:use-module (rnrs bytevectors)
  #:use-module (ice-9 binary-ports)
  #:use-module ((srfi srfi-1) #:select (filter-map partition
                                        delete-duplicates))
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-35)
  #:export (&git-condition git-condition? git-condition-argv
            git-condition-output git-condition-status false-if-git-error

            git git* ensure-git-repo git-ls-tree git-ls-subdirs
            git-mktree git-rev-parse git-hash-object git-update-ref
            git-commit-tree git-rev-list git-revert git-last-update
            git-latest-commits

            munge-tree munge-tree1 parse-commit commit-utc-timestamp

            with-output-to-blob with-input-from-blob))


;;;
;;; git conditions
;;;

(define-condition-type &git-condition &condition git-condition?
  (argv git-condition-argv)
  (output git-condition-output)
  (status git-condition-status))

(define-syntax false-if-git-error
  (syntax-rules ()
    ((_ body0 body ...)
     (guard (c ((git-condition? c) #f))
        body0 body ...))))

;;;
;;; running git
;;;

(define *debug* #f)
(define (trc . args)
  (if *debug*
      (apply pk args)
      (car (last-pair args))))

(define (run env input-file args)
  (define (prepend-env args)
    (if (null? env)
        args
        (cons "/usr/bin/env" (append env args))))
  (define (redirect-input args)
    (if input-file
        (list "/bin/sh" "-c"
              (string-append (string-join (map shell:quote args) " ")
                             "<" input-file))
        args))
  (let* ((real-args (trc (redirect-input (prepend-env args))))
         (pipe (apply open-pipe* OPEN_READ real-args))
         (output (begin
                   (let ((bv (get-bytevector-all pipe)))
                     (if (eof-object? bv)
                         ""
                         (utf8->string bv)))))
         (ret (close-pipe pipe)))
    (case (status:exit-val ret)
      ((0) output)
      (else (trc 'git-error output ret real-args)
            (raise (condition (&git-condition
                               (argv real-args)
                               (output output)
                               (status ret))))))))

(define* (git* args #:key (input #f) (env '()))
  (if input
      (call-with-temp-file
       input
       (lambda (tempname)
         (trc input)
         (run env tempname (cons* *git* "--bare" args))))
      (run env #f (cons* *git* "--bare" args))))

(define (git . args)
  (git* args))

;;;
;;; git commands
;;;

(define (is-dir? path)
  (catch 'system-error
         (lambda () (eq? (stat:type (stat path)) 'directory))
         (lambda args #f)))

(define (ensure-git-repo)
  (let ((d (expanduser *git-dir*)))
    (if (not (is-dir? d))
        (begin
          (mkdir d)
          (chdir d)
          (git "init")
          (git "update-ref" "refs/heads/master"
               (git-commit-tree (string-trim-both (git* '("mktree") #:input ""))
                                #f "initial commit" #f)))
        (chdir d))))

(define (git-ls-tree treeish path)
  (or (and treeish
           (false-if-git-error
            (match-lines (git "ls-tree" treeish (or path "."))
                         "^(.+) (.+) (.+)\t(.+)$" (_ mode type object name)
                         ;; reversed for assoc
                         (list name object (string->symbol type)))))
      '()))

(define (git-ls-subdirs treeish path)
  (or (and treeish
           (false-if-git-error
            (match-lines (git "ls-tree" treeish (or path "."))
                         "^(.+) tree (.+)\t(.+)$" (_ mode object name)
                         (cons name object))))
      '()))

(define (git-mktree alist)
  (if (null? alist)
      #f
      (string-trim-both
       (git* '("mktree")
             #:input (string-join
                      (map (lambda (l)
                             (format #f
                                     (if (or (null? (cddr l))
                                             (equal? (caddr l) 'blob))
                                         "100644 blob ~a\t~a"
                                         "040000 tree ~a\t~a")
                                     (cadr l) (car l)))
                           alist)
                      "\n" 'suffix)))))

(define (git-rev-parse rev)
  (or (false-if-exception
       (let ((s (string-trim-both (call-with-input-file rev read-line))))
         (and (= (string-length s) 40)
              s)))
      (git "rev-parse" rev)))

(define (git-rev-list rev n)
  (let lp ((lines (string-split
                   (git "rev-list" "--pretty=format:%ct %s"
                        "-n" (number->string n) rev) #\newline))
           (ret '()))
    (if (or (null? lines)
            (and (null? (cdr lines)) (string-null? (car lines))))
        (reverse ret)
        (lp (cddr lines)
            (let ((line1 (car lines)) (line2 (cadr lines)))
              (match-bind
               "^commit (.*)$" line1 (_ sha1)
               (match-bind
                "^([0-9]+) (.*)$" line2 (_ ts subject)
                (cons `(,sha1 ,(string->number ts) ,subject) ret)
                (error "bad line2" line2))
               (error "bad line1" line1)))))))

(define* (git-latest-commits rev n #:optional (git-dir *git-dir*))
  (let lp ((lines (string-split
                   (git "--git-dir" (expanduser git-dir)
                        "rev-list" "--pretty=format:%ar\t%s\t%an"
                        "-n" (number->string n) rev) #\newline))
           (ret '()))
    (if (or (null? lines)
            (and (null? (cdr lines)) (string-null? (car lines))))
        (reverse ret)
        (lp (cddr lines)
            (let ((line (cadr lines)))
              (match-bind
                "^([^\t]+)\t([^\t]+)\t(.*)$" line (_ sha1 subject author)
                (cons `(,sha1 ,subject ,author) ret)
                (error "bad line" line)))))))


(define* (git-last-update #:optional (git-dir *git-dir*))
  (let lp ((lines (string-split
                   (git "--git-dir" (expanduser git-dir)
                        "rev-list" "--pretty=format:%ar" "-n" "1" "HEAD")
                   #\newline))
           (ret '()))
    (if (or (null? lines)
            (and (null? (cdr lines)) (string-null? (car lines))))
        ret
        (lp (cddr lines)
            (cadr lines)))))

(define (git-hash-object contents)
  (string-trim-both
   (git* '("hash-object" "-w" "--stdin") #:input contents)))

(define (git-update-ref refname proc count)
  (let* ((ref (git-rev-parse refname))
         (commit (proc ref)))
    (cond
     ((zero? count)
      (error "my god, we looped 5 times" commit))
     ((false-if-git-error
       (git "update-ref" refname commit ref))
      commit)
     (else
      (pk "failed to update the ref, trying again..." refname)
      (git-update-ref refname proc (1- count))))))

(define (git-commit-tree tree parent message timestamp)
  (string-trim-both
   (git* (cons* "commit-tree" tree
                (if parent (list "-p" parent) '()))
         #:input message
         #:env (if timestamp
                   (list "GIT_COMMMITTER=tekuti"
                         (format #f "GIT_COMMITTER_DATE=~a +0100" timestamp)
                         (format #f "GIT_AUTHOR_DATE=~a +0100" timestamp))
                   (list "GIT_COMMMITTER=tekuti")))))

;;;
;;; utilities
;;;

;; unused.
(define (patch-blob sha1 patch)
  (call-with-temp-file
   (git "cat-file" "blob" sha1)
   (lambda (orig)
     (run '() patch (list "patch" "-N" "-s" "-u" "-r" "/dev/null" orig))
     (with-output-to-blob
       (display
        (call-with-input-file orig
          (lambda (port)
            (read-delimited "" port))))))))

;; could leave stray comments if the post directory changes. but this is
;; probably the best that we can do, given that git does not track
;; directory renames.
(define (git-commit-reverse-operations sha1)
  (with-input-from-string* (git "diff-tree" "-R" "-r" sha1)
    (lambda ()
      (read-line) ;; throw away the header
      (let lp ((ops '()))
        (let ((line (read-line)))
          (if (eof-object? line)
              ops
              (match-bind
               "^:([0-9]+) ([0-9]+) ([0-9a-f]+) ([0-9a-f]+) (.)\t(.*)$"
               line (_ mode1 mode2 ob1 ob2 op path)
               (let ((head (let ((d (dirname path)))
                                  (if (string=? d ".") '()
                                      (string-split d #\/))))
                     (tail (basename path)))
                 (lp
                  (case (string-ref op 0)
                    ((#\D) (cons `(delete ,head (,tail))
                                 ops))
                    ((#\A) (cons `(create ,head (,tail ,ob2 blob))
                                 ops))
                    ((#\M) (cons* `(delete ,head (,tail))
                                  `(create ,head (,tail ,ob2 blob))
                                  ops)))))
               (error "crack line" line))))))))

(define (git-revert ref sha1)
  (let ((ops (git-commit-reverse-operations sha1)))
    (git-update-ref ref
                    (lambda (master)
                      (git-commit-tree (munge-tree master ops)
                                       master "revert change" #f))
                    5)))

(define (munge-tree1-local dents command arg)
  (define (command-error why)
    (error "munge-tree1-local error" why command arg))
  (let ((dent (assoc (car arg) dents)))
    (git-mktree
     (case command
       ((create) (if dent
                     (command-error 'file-present)
                     (cons arg dents)))
       ((delete) (if dent
                     (delq dent dents)
                     (command-error 'file-not-present)))
       ((rename) (if dent
                     (acons (cadr arg) (cdr dent) (delq dent dents))
                     (command-error 'file-not-present)))
       (else (command-error 'unrecognized))))))

(define (munge-tree1-recursive dents command ldir rdir arg)
  (define (command-error why)
    (error "munge-tree1-recursive error" why command ldir rdir arg))
  (let ((dent (assoc ldir dents)))
    (if (and dent (not (eq? (caddr dent) 'tree)))
        (command-error 'not-a-tree))
    (let ((subtree (and=> dent cadr))
          (other-dents (if dent (delq dent dents) dents)))
      (let ((new (case command
                   ((create)
                    (munge-tree1 subtree command rdir arg))
                   ((delete rename)
                    (if subtree
                        (munge-tree1 subtree command rdir arg)
                        (command-error 'file-not-present)))
                   (else (command-error 'unrecognized)))))
        (git-mktree (if new
                        (cons (list ldir new 'tree) other-dents)
                        other-dents))))))

(define (munge-tree1 treeish command dir arg)
  (let ((dents (git-ls-tree treeish #f)))
    (if (null? dir)
        (munge-tree1-local dents command arg)
        (munge-tree1-recursive dents command (car dir) (cdr dir) arg))))

;; (munge-tree sha1 ((create (key comments) (name sha1 blob))
;;                   (delete (foo bar) (name))
;;                   (rename (baz borky) (from to))))
(define (munge-tree treeish operations)
  (if (null? operations)
      treeish
      (let ((op (car operations)))
        (munge-tree (munge-tree1 treeish (car op) (cadr op) (caddr op))
                    (cdr operations)))))

(define (parse-commit commit)
  (let ((text (git "cat-file" "commit" commit)))
    (match-bind
     "\n\n(.*)$" text (_ message)
     (acons
      'message message
      (match-lines (substring text 0 (- (string-length text) (string-length _)))
                   "^([^ ]+) (.*)$" (_ k v)
                   (cons (string->symbol k) v))))))

(define (commit-utc-timestamp commit)
  (match-bind
   "^(.*) ([0-9]+) ([+-][0-9]+)" (assq-ref (parse-commit commit) 'committer)
   (_ who ts tz)
   (let ((ts (string->number ts)) (tz (string->number tz)))
     (- ts (* (+ (* (quotient tz 100) 60) (remainder tz 100)) 60)))))

(define (with-output-to-blob* thunk)
  (git-hash-object (with-output-to-string* thunk)))

(define-syntax with-output-to-blob
  (syntax-rules ()
    ((_ f f* ...)
     (with-output-to-blob* (lambda () f f* ...)))))

(define (with-input-from-blob* sha1 thunk)
  (with-input-from-string* (git "show" sha1) thunk))

(define-syntax with-input-from-blob
  (syntax-rules ()
    ((_ sha1 f f* ...)
     (with-input-from-blob* sha1 (lambda () f f* ...)))))