git.m455.casa

repo2html

clone url: git://git.m455.casa/repo2html


utils-git.scm

1 ;; functions that interact with the git executable
2
3 (module utils-git *
4 (import
5 (chicken base)
6 (chicken format) ;; format
7 (chicken io) ;; read-line
8 (chicken process) ;; call-with-input-pipe
9 (chicken string) ;; string-intersperse
10 (clojurian syntax)
11 scheme
12 )
13
14 (define lines-from-git-command (cute call-with-input-pipe <> read-lines))
15
16 (define (in-git-directory?)
17 (not (eof-object? (call-with-input-pipe "git rev-parse --git-dir" read-line))))
18
19 ;; a weird hack to detect whether git considers a blob to be a binary or a text
20 ;; file. works by requesting the numstat diff between it and the empty tree.
21 ;; binary files give not a line count but '-' placeholders.
22 (define (git-file-is-text? source-file)
23 (not (equal?
24 "-\t-\t"
25 (call-with-input-pipe
26 (string-append "git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 --numstat HEAD -- " (qs source-file))
27 (cute read-line <> 4)))))
28
29 (define (git-repository->paths-list)
30 (lines-from-git-command "git ls-tree -r --name-only HEAD"))
31
32 (define (git-file->lines source-file)
33 (->
34 source-file
35 (qs)
36 ((flip format) "git show HEAD:~a")
37 (lines-from-git-command)))
38
39 ;; the result of asking git for some configuration; #f if no result.
40 (define (git-config->string key)
41 (let [(result (call-with-input-pipe (string-append "git config " key) read-line))]
42 (if (eof-object? result) #f result)))
43
44 (define (git-commits)
45 ;; date ref title author, tab-separated.
46 (map (cute string-split <> "\t")
47 (lines-from-git-command "git log --format=format:%as%x09%h%x09%s%x09%aN HEAD")))
48
49 (define (git-contributors)
50 (lines-from-git-command "git shortlog -ns HEAD"))
51
52 ;; copy a file verbatim from the repo @HEAD to some path
53 (define (git-copy src dst)
54 (system (format "git show HEAD:~a > ~a" (qs src) (qs dst))))
55
56 )