summaryrefslogtreecommitdiffstatshomepage
path: root/tekuti/projects.scm
blob: bca78e2115a94dcc5b50c89982bdff914f6196cc (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
(define-module (tekuti projects)
  #:use-module ((tekuti config) #:select (*projects-dir* *git-dir*))
  #:use-module ((tekuti page) #:select (page-not-found))
  #:use-module (ice-9 ftw)
  #:use-module (ice-9 match)
  #:use-module (ice-9 popen)
  #:use-module (ice-9 rdelim)
  #:use-module (tekuti git)
  #:use-module (tekuti match-bind)
  #:use-module (tekuti page-helpers)
  #:use-module (tekuti util)
  #:use-module (web request)
  #:use-module (web uri)
  #:export (page-projects page-project))

(define remove-stat
  (match-lambda
   ((name stat)
    name)
   ((name stat children ...)
    (list name (map remove-stat children)))))

(define (list-projects)
  (remove-stat
   (file-system-tree
    *projects-dir*
    (lambda (name stat)
      (not (file-exists? (string-append name "/config")))))))

(define (project-description project)
  (let ((dfile-name
         (string-append project "/description")))
    (if (file-exists? dfile-name)
        (with-input-from-file dfile-name
          (lambda ()
            (read-line)))
        "Unknown")))

(define (remove-suffix str suffix)
  (let ((pos (string-contains str suffix)))
    (if pos
        (substring str 0 pos)
        str)))

(define (project-url prefix name)
  (uri-encode
   (string-append prefix (if (string-null? prefix) "" "/") name)))

(define* (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 (project->row dir project prefix)
  (let ((project-dir (string-append dir "/" project))
        (project-name (remove-suffix project ".git")))
    `(tr
      (td (@ (class "span3"))
          (a (@ (href ,(project-url prefix project-name)))
             ,project-name))
      (td (@ (class "span6")) ,(project-description project-dir))
      (td (@ (class "span2")) ,(last-update project-dir))
      (td (@ (class "span1")) ""))))

(define (project-list->table top-dir list lvl prefix)
  (let ((head (car list))
        (body (cadr list)))
    `((,(string->symbol (string-append "h" (number->string lvl))) ,head)
      (table
       (@ (class "table table-condensed table-striped"))
       (thead
        (tr
         (th "Name")
         (th "Description")
         (th "Last update")
         (th "")))
       (tbody
        ,@(map (lambda (elt)
                 (project->row top-dir elt prefix))
               (filter (lambda (elt)
                         (and (string? elt)
                              (access? (string-append top-dir "/" elt) R_OK)))
                       body))))
      ,@(map (lambda (elt) (project-list->table
                            (string-append top-dir (car elt)) elt (1+ lvl)
                            (string-append prefix (car elt))))
             (filter
              (lambda (elt)
                (and (list? elt)
                     (access? (string-append top-dir (car elt)) X_OK)))
              body)))))

(define (page-projects request body index)
  (respond (project-list->table *projects-dir* (list-projects) 1 "")
           #:extra-headers '((pragma . (no-cache (broccoli . "tastyy"))))))

(define (project-pages-menu request project page)
  (let ((page-list '("summary" "refs" "log" "tree" "commit" "diff"
                     "stats" "about")))
    `((div (@ (class "navbar"))
           (div (@ (class "navbar-inner"))
                (a (@ (class "brand") (href ".")) ,project)
                (ul (@ (class "nav"))
                    ,@(map
                       (lambda (elm)
                         `(li (@ (class ,(if (string= page elm)
                                             "active"
                                             "")))
                              ,(rellink `("projects" ,project ,elm)
                                        (string-capitalize elm))))
                       page-list)))))))

(define* (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 (page-project request body index project page)
  (let ((project-dir (string-append *projects-dir* project ".git"))
        (page (or page "summary")))
    (if (file-exists? project-dir)
        (respond
         `(,(project-pages-menu request project page)
           (p "Goto: "
              (a (@ (href "http://code.ryuslash.org/cgit.cgi/"
                          ,project "/" ,page))
                 "cgit " ,page))
           ,@(case (string->symbol page)
               ((summary)
                `((h2 "Last 10 commits")
                  (table
                   (@ (class "table table-condensed table-striped"))
                   (tr
                    (th "Time")
                    (th "Subject")
                    (th "Author"))
                   ,@(map (lambda (elm)
                            `(tr
                              (td ,(car elm))
                              (td ,(cadr elm))
                              (td ,(caddr elm))))
                          (latest-commits "HEAD" 10 project-dir)))))
               (else '()))))
        (page-not-found request body index))))