Functions for displayin and handling branches
[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_action (struct webgit_query* query)
151 {
152   (void) query;
153   return html("summary");
154 }
155
156
157 /*
158   Log page
159 */
160 static Html
161 webgit_log_action (struct webgit_query* query)
162 {
163   webgit_repo_enter (query);
164
165   return html(
166               html(html_tag("div"), webgit_log_menu_action (query)), html_nl (),
167               html(html_tag("div"), webgit_log_content_action (query)), html_nl ()
168               );
169 }
170
171
172 /*
173   Diff page
174 */
175 static Html
176 webgit_diff_action (struct webgit_query* query)
177 {
178   (void) query;
179   return html("diff");
180 }
181
182
183 /*
184   pretty printed objects
185 */
186 static Html
187 webgit_object_action (struct webgit_query* query)
188 {
189   webgit_repo_enter (query);
190
191   unsigned char sha1[20];
192   if (get_sha1 (query->object, sha1))
193     return html("error: unknown object");
194
195   switch (sha1_object_info(sha1, NULL))
196     {
197     case OBJ_COMMIT:
198       if (!query->deref_to_tree)
199         return webgit_object_commit_action (query, sha1);
200       /* else fallthrough */
201     case OBJ_TREE:
202       return webgit_object_tree_action (query, sha1);
203     case OBJ_BLOB:
204       return webgit_object_blob_action (query, sha1);
205     case OBJ_TAG:
206       return html ("its a tag");
207
208       break;
209     default:
210       return html ("error: unknown object type");
211       break;
212     }
213 }
214
215
216 /*
217   treeish dereferenced to tree
218 */
219 static Html
220 webgit_tree_action (struct webgit_query* query)
221 {
222   query->deref_to_tree = 1;
223   return webgit_object_action (query);
224 }
225
226
227 /*
228   spew out raw data
229 */
230 static Html
231 webgit_raw_action (struct webgit_query* query)
232 {
233   webgit_repo_enter (query);
234
235   unsigned char sha1[20];
236   if (get_sha1 (query->object, sha1))
237     return html("error: unknown object");
238
239   void* buf;
240   unsigned long size;
241
242   buf = read_object_with_reference (sha1, "blob", &size, NULL);
243
244   free (query->content_type);
245   query->content_type = webgit_mimetype (query->path);
246
247   return html_binary (buf, size);
248 }
249
250
251 /*
252   show branch
253 */
254 static Html
255 webgit_branch_menu_action (struct webgit_repo_info* repo)
256 {
257   (void) repo;
258   return html ("TODO: branch sidebar");
259 }
260
261
262 static Html
263 webgit_branch_action (struct webgit_query* query)
264 {
265   struct webgit_repo_info* repo = webgit_repo_enter (query);
266   webgit_repoinfo_find_last (repo);
267
268   return html(
269               html(html_tag("div"), webgit_branch_menu_action (repo)), html_nl (),
270               html(html_tag("div"), webgit_branch_table (repo, query->head, query->count), html_nl ())
271               );
272 }
273
274
275 /*
276   show tag
277 */
278 static Html
279 webgit_tag_action (struct webgit_query* query)
280 {
281   (void) query;
282   return html("tag");
283 }
284
285
286 /*
287   edit an object
288 */
289 static Html
290 webgit_edit_action (struct webgit_query* query)
291 {
292   (void) query;
293   return html("edit");
294 }
295
296 /*
297   commit changes
298 */
299 static Html
300 webgit_commit_action (struct webgit_query* query)
301 {
302   (void) query;
303   return html("commit");
304 }
305
306
307 Html
308 webgit_action_dispatch (struct webgit_query* query)
309 {
310 #define WEBGIT_ACTION(name, _)                  \
311 {                                               \
312  #name,                                         \
313  webgit_##name##_action,                        \
314 },
315   struct action_table{
316     char* name;
317     Html (*cb)(struct webgit_query* query);
318   } cmds[] = {WEBGIT_ACTIONS {"", NULL}};
319 #undef WEBGIT_ACTION
320
321   for (struct action_table* j = cmds; j->cb; ++j)
322     {
323       if (!strcmp (j->name, query->action))
324         return j->cb (query);
325     }
326   return html("error, no such action");
327 }
328
329