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