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