few more parameters for webgit_log_link
[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,
99                                                                 n->name,
100                                                                 n->last_head,
101                                                                 NULL, 0,
102                                                                 query->count,
103                                                                 html("Log")
104                                                                 ),
105                                                " ",
106
107                                                "Diff ",
108                                                //webgit_diff_link (query, n, n->last_head, html("Diff")),
109                                                //" ",
110                                                webgit_object_link (query,
111                                                                    n->name, strlen(n->name),
112                                                                    n->last_tree, 40,
113                                                                    NULL,
114                                                                    html("Tree"))
115                                                )
116                                           )
117                                      ),
118                         html_nl()
119                         );
120     }
121
122   return html (html_tag ("table", html_attr ("border", "1")), table);
123 }
124
125 static Html
126 webgit_main_action (struct webgit_query* query)
127 {
128   return html(
129               html(html_tag("div"), webgit_main_menu_action (query)), html_nl (),
130               html(html_tag("div"), webgit_main_content_action (query)), html_nl ()
131               );
132 }
133
134
135 /*
136   Summary of a single repository
137 */
138 static Html
139 webgit_summary_action (struct webgit_query* query)
140 {
141   return html("summary");
142 }
143
144
145 /*
146   Log page
147 */
148 static Html
149 webgit_log_action (struct webgit_query* query)
150 {
151   
152
153   return html("log");
154 }
155
156
157 /*
158   Diff page
159 */
160 static Html
161 webgit_diff_action (struct webgit_query* query)
162 {
163   return html("diff");
164 }
165
166
167 /*
168   pretty printed objects
169 */
170 static Html
171 webgit_object_action (struct webgit_query* query)
172 {
173   webgit_enter_repository (query);
174
175   unsigned char sha1[20];
176   if (get_sha1 (query->object, sha1))
177     return html("error: unknown object");
178
179   switch (sha1_object_info(sha1, NULL))
180     {
181     case OBJ_COMMIT:
182       if (!query->deref_to_tree)
183         return webgit_object_commit_action (query, sha1);
184       /* else fallthrough */
185     case OBJ_TREE:
186       return webgit_object_tree_action (query, sha1);
187     case OBJ_BLOB:
188       return webgit_object_blob_action (query, sha1);
189     case OBJ_TAG:
190       return html ("its a tag");
191
192       break;
193     default:
194       return html ("error: unknown object type");
195       break;
196     }
197 }
198
199
200 /*
201   treeish dereferenced to tree
202 */
203 static Html
204 webgit_tree_action (struct webgit_query* query)
205 {
206   query->deref_to_tree = 1;
207   return webgit_object_action (query);
208 }
209
210
211 /*
212   spew out raw data
213 */
214 static Html
215 webgit_raw_action (struct webgit_query* query)
216 {
217   webgit_enter_repository (query);
218
219   unsigned char sha1[20];
220   if (get_sha1 (query->object, sha1))
221     return html("error: unknown object");
222
223   void* buf;
224   unsigned long size;
225
226   buf = read_object_with_reference (sha1, "blob", &size, NULL);
227
228   free (query->content_type);
229   query->content_type = webgit_mimetype (query->path);
230
231   return html_binary (buf, size);
232 }
233
234
235 /*
236   show tag
237 */
238 static Html
239 webgit_tag_action (struct webgit_query* query)
240 {
241   return html("tag");
242 }
243
244
245 /*
246   edit an object
247 */
248 static Html
249 webgit_edit_action (struct webgit_query* query)
250 {
251   return html("edit");
252 }
253
254 /*
255   commit changes
256 */
257 static Html
258 webgit_commit_action (struct webgit_query* query)
259 {
260   return html("commit");
261 }
262
263
264 Html
265 webgit_action_dispatch (struct webgit_query* query)
266 {
267 #define WEBGIT_ACTION(name, _)                  \
268 {                                               \
269  #name,                                         \
270  webgit_##name##_action,                        \
271 },
272   struct action_table{
273     char* name;
274     Html (*cb)(struct webgit_query* query);
275   } cmds[] = {WEBGIT_ACTIONS {"", NULL}};
276 #undef WEBGIT_ACTION
277
278   for (struct action_table* j = cmds; j->cb; ++j)
279     {
280       if (!strcmp (j->name, query->action))
281         return j->cb (query);
282     }
283   return html("error, no such action");
284 }
285
286