moved log_menu|content_action to log.c|h
[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
29 #include "llist.h"
30 #include <cwa.h>
31
32 /* todo configure this */
33 #define SHA1_HEADER <openssl/sha.h>
34 #include "git/cache.h"
35
36 #include <stdio.h>
37
38
39 /*
40   Main (repository list) page
41 */
42 static Html
43 webgit_main_menu_action (struct webgit_query* query)
44 {
45   (void) query;
46   return html ("TODO: main menu sidebar");
47 }
48
49 static Html
50 webgit_main_content_action (struct webgit_query* query)
51 {
52   Html table = html_list ();
53
54   html_list_append (table, html(
55                           html(html_tag ("tr"),
56                                html(html_tag ("th"),"Name"),
57                                html(html_tag ("th"),"Git URL"),
58                                html(html_tag ("th"),"Description"),
59                                html(html_tag ("th"),"Owner"),
60                                html(html_tag ("th"),"Last Change"),
61                                html(html_tag ("th"),"Author"),
62                                html(html_tag ("th"),"Committer"),
63                                html(html_tag ("th"),"Action")
64                                ),
65                           html_nl()
66                           )
67               );
68
69   LLIST_FOREACH (&query->repos, node)
70     {
71       struct webgit_repo_info* n = (struct webgit_repo_info*)node;
72
73       webgit_repoinfo_find_last (n);
74
75       html_list_append (table, html (
76                                      html_tag ("tr"),
77                                      html(html_tag ("td"), webgit_summary_link (query, n, html (n->name))),
78                                      html(html_tag ("td"), n->url ?
79                                           html (
80                                                 html_tag ("a", html_attr ("href", n->url)),
81                                                 n->url)
82                                           : html("N/A")
83                                           ),
84                                      html(html_tag ("td"), n->description),
85                                      html(html_tag ("td"), n->owner),
86                                      html(html_tag ("td"),
87                                           webgit_object_link (query,
88                                                               n->name, strlen(n->name),
89                                                               n->last_commit, 40,
90                                                               NULL,
91                                                               webgit_pretty_age (n->age))
92                                           ),
93                                      html(html_tag ("td"),
94                                           webgit_email_link (
95                                                              html (n->last_author_name),
96                                                              html (n->last_author_email)
97                                                              )
98                                           ),
99                                      html(html_tag ("td"),
100                                           webgit_email_link (
101                                                              html (n->last_committer_name),
102                                                              html (n->last_committer_email)
103                                                              )
104                                           ),
105                                      html(html_tag ("td"),
106                                           html(
107                                                webgit_log_link (query,
108                                                                 n->name,
109                                                                 n->last_head,
110                                                                 NULL, 0,
111                                                                 query->count,
112                                                                 html("Log")
113                                                                 ),
114                                                " ",
115
116                                                "Diff ",
117                                                //webgit_diff_link (query, n, n->last_head, html("Diff")),
118                                                //" ",
119                                                webgit_object_link (query,
120                                                                    n->name, strlen(n->name),
121                                                                    n->last_tree, 40,
122                                                                    NULL,
123                                                                    html("Tree"))
124                                                )
125                                           )
126                                      ),
127                         html_nl()
128                         );
129     }
130
131   return html (html_tag ("table", html_attr ("border", "1")), table);
132 }
133
134 static Html
135 webgit_main_action (struct webgit_query* query)
136 {
137   return html(
138               html(html_tag("div"), webgit_main_menu_action (query)), html_nl (),
139               html(html_tag("div"), webgit_main_content_action (query)), html_nl ()
140               );
141 }
142
143
144 /*
145   Summary of a single repository
146 */
147 static Html
148 webgit_summary_action (struct webgit_query* query)
149 {
150   (void) query;
151   return html("summary");
152 }
153
154
155 /*
156   Log page
157 */
158 static Html
159 webgit_log_action (struct webgit_query* query)
160 {
161   return html(
162               html(html_tag("div"), webgit_log_menu_action (query)), html_nl (),
163               html(html_tag("div"), webgit_log_content_action (query)), html_nl ()
164               );
165 }
166
167
168 /*
169   Diff page
170 */
171 static Html
172 webgit_diff_action (struct webgit_query* query)
173 {
174   (void) query;
175   return html("diff");
176 }
177
178
179 /*
180   pretty printed objects
181 */
182 static Html
183 webgit_object_action (struct webgit_query* query)
184 {
185   webgit_enter_repository (query);
186
187   unsigned char sha1[20];
188   if (get_sha1 (query->object, sha1))
189     return html("error: unknown object");
190
191   switch (sha1_object_info(sha1, NULL))
192     {
193     case OBJ_COMMIT:
194       if (!query->deref_to_tree)
195         return webgit_object_commit_action (query, sha1);
196       /* else fallthrough */
197     case OBJ_TREE:
198       return webgit_object_tree_action (query, sha1);
199     case OBJ_BLOB:
200       return webgit_object_blob_action (query, sha1);
201     case OBJ_TAG:
202       return html ("its a tag");
203
204       break;
205     default:
206       return html ("error: unknown object type");
207       break;
208     }
209 }
210
211
212 /*
213   treeish dereferenced to tree
214 */
215 static Html
216 webgit_tree_action (struct webgit_query* query)
217 {
218   query->deref_to_tree = 1;
219   return webgit_object_action (query);
220 }
221
222
223 /*
224   spew out raw data
225 */
226 static Html
227 webgit_raw_action (struct webgit_query* query)
228 {
229   webgit_enter_repository (query);
230
231   unsigned char sha1[20];
232   if (get_sha1 (query->object, sha1))
233     return html("error: unknown object");
234
235   void* buf;
236   unsigned long size;
237
238   buf = read_object_with_reference (sha1, "blob", &size, NULL);
239
240   free (query->content_type);
241   query->content_type = webgit_mimetype (query->path);
242
243   return html_binary (buf, size);
244 }
245
246
247 /*
248   show tag
249 */
250 static Html
251 webgit_tag_action (struct webgit_query* query)
252 {
253   (void) query;
254   return html("tag");
255 }
256
257
258 /*
259   edit an object
260 */
261 static Html
262 webgit_edit_action (struct webgit_query* query)
263 {
264   (void) query;
265   return html("edit");
266 }
267
268 /*
269   commit changes
270 */
271 static Html
272 webgit_commit_action (struct webgit_query* query)
273 {
274   (void) query;
275   return html("commit");
276 }
277
278
279 Html
280 webgit_action_dispatch (struct webgit_query* query)
281 {
282 #define WEBGIT_ACTION(name, _)                  \
283 {                                               \
284  #name,                                         \
285  webgit_##name##_action,                        \
286 },
287   struct action_table{
288     char* name;
289     Html (*cb)(struct webgit_query* query);
290   } cmds[] = {WEBGIT_ACTIONS {"", NULL}};
291 #undef WEBGIT_ACTION
292
293   for (struct action_table* j = cmds; j->cb; ++j)
294     {
295       if (!strcmp (j->name, query->action))
296         return j->cb (query);
297     }
298   return html("error, no such action");
299 }
300
301