the summary_action
[webgit] / src / actions.c
1 /*
2     cehtehs git web frontend
3
4   Copyright (C)
5     2007,               Christian Thaeter <ct@pipapo.org>
6
7   This program is free software; you can redistribute it and/or
8   modify it under the terms of the GNU General Public License as
9   published by the Free Software Foundation; either version 2 of the
10   License, or (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "actions.h"
23 #include "summary.h"
24 #include "object.h"
25 #include "log.h"
26 #include "repo.h"
27 #include "age.h"
28 #include "branch.h"
29
30 #include "llist.h"
31 #include <cwa.h>
32
33 /* todo configure this */
34 #define SHA1_HEADER <openssl/sha.h>
35 #include "git/cache.h"
36 #include "git/refs.h"
37
38 #include <stdio.h>
39
40
41 /*
42   Main (repository list) page
43 */
44 static Html
45 webgit_main_menu_action (struct webgit_query* query)
46 {
47   (void) query;
48   return html ("TODO: main menu sidebar");
49 }
50
51 static Html
52 webgit_main_content_action (struct webgit_query* query)
53 {
54   Html table = html_list ();
55
56   html_list_append (table, html(
57                           html(html_tag ("tr"),
58                                html(html_tag ("th"),"Name"),
59                                html(html_tag ("th"),"Git URL"),
60                                html(html_tag ("th"),"Description"),
61                                html(html_tag ("th"),"Owner"),
62                                html(html_tag ("th"),"Last Change"),
63                                html(html_tag ("th"),"Author"),
64                                html(html_tag ("th"),"Committer"),
65                                html(html_tag ("th"),"Action")
66                                ),
67                           html_nl()
68                           )
69               );
70
71   LLIST_FOREACH (&query->repos, node)
72     {
73       struct webgit_repo_info* n = (struct webgit_repo_info*)node;
74
75       webgit_repoinfo_find_last (n);
76
77       html_list_append (table, html (
78                                      html_tag ("tr"),
79                                      html(html_tag ("td"), webgit_summary_link (query, n, html (n->name))),
80                                      html(html_tag ("td"), n->url ?
81                                           html (
82                                                 html_tag ("a", html_attr ("href", n->url)),
83                                                 n->url)
84                                           : html("N/A")
85                                           ),
86                                      html(html_tag ("td"), n->description),
87                                      html(html_tag ("td"), n->owner),
88                                      html(html_tag ("td"),
89                                           webgit_object_link (query,
90                                                               n->name, strlen(n->name),
91                                                               n->last_commit, 40,
92                                                               NULL,
93                                                               webgit_pretty_age (n->age))
94                                           ),
95                                      html(html_tag ("td"),
96                                           webgit_email_link (
97                                                              html (n->last_author_name),
98                                                              html (n->last_author_email)
99                                                              )
100                                           ),
101                                      html(html_tag ("td"),
102                                           webgit_email_link (
103                                                              html (n->last_committer_name),
104                                                              html (n->last_committer_email)
105                                                              )
106                                           ),
107                                      html(html_tag ("td"),
108                                           html(
109                                                webgit_log_link (query,
110                                                                 n->name,
111                                                                 n->last_head,
112                                                                 NULL, 0,
113                                                                 query->count,
114                                                                 html("Log")
115                                                                 ),
116                                                " ",
117
118                                                "Diff ",
119                                                //webgit_diff_link (query, n, n->last_head, html("Diff")),
120                                                //" ",
121                                                webgit_object_link (query,
122                                                                    n->name, strlen(n->name),
123                                                                    n->last_tree, 40,
124                                                                    NULL,
125                                                                    html("Tree"))
126                                                )
127                                           )
128                                      ),
129                         html_nl()
130                         );
131     }
132
133   return html (html_tag ("table", html_attr ("border", "1")), table);
134 }
135
136 static Html
137 webgit_main_action (struct webgit_query* query)
138 {
139   return html(
140               html(html_tag("div"), webgit_main_menu_action (query)), html_nl (),
141               html(html_tag("div"), webgit_main_content_action (query)), html_nl ()
142               );
143 }
144
145
146 /*
147   Summary of a single repository
148 */
149 static Html
150 webgit_summary_menu_action (struct webgit_repo_info* repo)
151 {
152   (void) repo;
153   return html ("TODO: summary sidebar");
154 }
155
156
157 static Html
158 webgit_summary_content_action (struct webgit_repo_info* repo)
159 {
160   Html content = html_list ();
161
162   /* Title */
163   html_list_append (content, html (html_tag ("h1"), repo->description));
164
165   /* Readme */
166   if (repo->readme)
167     html_list_append (content, html_include (repo->readme));
168
169   /* Info */
170   html_list_append (content,
171                     html (
172                           html (html_tag ("h2"), "Repository information"),
173                           repo->url ?
174                           html (
175                                 html_tag ("div"),
176                                 "Public URL: ",
177                                 html (
178                                       html_tag ("a", html_attr ("href", repo->url)),
179                                       repo->url
180                                       )
181                                 ) : html(),
182                           html (
183                                 html_tag ("div"),
184                                 "Owner: ", repo->owner
185                                 ),
186                           html(
187                                html_tag ("div"),
188                                "Last change: ",
189                                webgit_object_link (repo->query,
190                                                    repo->name, strlen(repo->name),
191                                                    repo->last_commit, 40,
192                                                    NULL,
193                                                    webgit_pretty_age (repo->age)),
194                                " in branch ",
195                                 webgit_log_link (repo->query,
196                                                  repo->name,
197                                                  repo->last_head,
198                                                  NULL, 0,
199                                                  repo->query->count,
200                                                  html ("'", repo->last_head, "'")
201                                                  )
202                                )
203                           )
204                     );
205
206   /* log */
207   html_list_append (content,
208                     html (
209                           html (html_tag ("h2"), "Log of branch '", repo->last_head, "'"),
210                           webgit_log_table (repo->query, repo->last_head, 5 /*TODO: config this*/, 0))
211                     );
212
213
214   /* branches */
215   html_list_append (content,
216                     html (
217                           html (html_tag ("h2"), "Branches"),
218                           webgit_branch_table (repo, NULL, 5 /*TODO: config this*/)
219                           )
220                     );
221
222   /* tags */
223
224
225   return content;
226 }
227
228
229 static Html
230 webgit_summary_action (struct webgit_query* query)
231 {
232   struct webgit_repo_info* repo = webgit_repo_enter (query);
233   webgit_repoinfo_find_last (repo);
234
235   return html(
236               html(html_tag("div"), webgit_summary_menu_action (repo)), html_nl (),
237               html(html_tag("div"), webgit_summary_content_action (repo)), html_nl ()
238               );
239 }
240
241
242 /*
243   Log page
244 */
245 static Html
246 webgit_log_action (struct webgit_query* query)
247 {
248   webgit_repo_enter (query);
249
250   return html(
251               html(html_tag("div"), webgit_log_menu_action (query)), html_nl (),
252               html(html_tag("div"), webgit_log_content_action (query)), html_nl ()
253               );
254 }
255
256
257 /*
258   Diff page
259 */
260 static Html
261 webgit_diff_action (struct webgit_query* query)
262 {
263   (void) query;
264   return html("diff");
265 }
266
267
268 /*
269   pretty printed objects
270 */
271 static Html
272 webgit_object_action (struct webgit_query* query)
273 {
274   webgit_repo_enter (query);
275
276   unsigned char sha1[20];
277   if (get_sha1 (query->object, sha1))
278     return html("error: unknown object");
279
280   switch (sha1_object_info(sha1, NULL))
281     {
282     case OBJ_COMMIT:
283       if (!query->deref_to_tree)
284         return webgit_object_commit_action (query, sha1);
285       /* else fallthrough */
286     case OBJ_TREE:
287       return webgit_object_tree_action (query, sha1);
288     case OBJ_BLOB:
289       return webgit_object_blob_action (query, sha1);
290     case OBJ_TAG:
291       return html ("its a tag");
292
293       break;
294     default:
295       return html ("error: unknown object type");
296       break;
297     }
298 }
299
300
301 /*
302   treeish dereferenced to tree
303 */
304 static Html
305 webgit_tree_action (struct webgit_query* query)
306 {
307   query->deref_to_tree = 1;
308   return webgit_object_action (query);
309 }
310
311
312 /*
313   spew out raw data
314 */
315 static Html
316 webgit_raw_action (struct webgit_query* query)
317 {
318   webgit_repo_enter (query);
319
320   unsigned char sha1[20];
321   if (get_sha1 (query->object, sha1))
322     return html("error: unknown object");
323
324   void* buf;
325   unsigned long size;
326
327   buf = read_object_with_reference (sha1, "blob", &size, NULL);
328
329   free ((char*)query->content_type);
330   query->content_type = webgit_mimetype (query->path);
331
332   return html_binary (buf, size);
333 }
334
335
336 /*
337   show branch
338 */
339 static Html
340 webgit_branch_menu_action (struct webgit_repo_info* repo)
341 {
342   (void) repo;
343   return html ("TODO: branch sidebar");
344 }
345
346
347 static Html
348 webgit_branch_action (struct webgit_query* query)
349 {
350   struct webgit_repo_info* repo = webgit_repo_enter (query);
351   webgit_repoinfo_find_last (repo);
352
353   return html(
354               html(html_tag("div"), webgit_branch_menu_action (repo)), html_nl (),
355               html(html_tag("div"), webgit_branch_table (repo, query->head, query->count), html_nl ())
356               );
357 }
358
359
360 /*
361   show tag
362 */
363 static Html
364 webgit_tag_action (struct webgit_query* query)
365 {
366   (void) query;
367   return html("tag");
368 }
369
370
371 /*
372   edit an object
373 */
374 static Html
375 webgit_edit_action (struct webgit_query* query)
376 {
377   (void) query;
378   return html("edit");
379 }
380
381 /*
382   commit changes
383 */
384 static Html
385 webgit_commit_action (struct webgit_query* query)
386 {
387   (void) query;
388   return html("commit");
389 }
390
391
392 Html
393 webgit_action_dispatch (struct webgit_query* query)
394 {
395 #define WEBGIT_ACTION(name, _)                  \
396 {                                               \
397  #name,                                         \
398  webgit_##name##_action,                        \
399 },
400   struct action_table{
401     char* name;
402     Html (*cb)(struct webgit_query* query);
403   } cmds[] = {WEBGIT_ACTIONS {"", NULL}};
404 #undef WEBGIT_ACTION
405
406   for (struct action_table* j = cmds; j->cb; ++j)
407     {
408       if (!strcmp (j->name, query->action))
409         return j->cb (query);
410     }
411   return html("error, no such action");
412 }
413
414