renamed 'giturl' option to 'url'
[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       html_list_append (table, html (
70                                      html_tag ("tr"),
71                                      html(html_tag ("td"), webgit_summary_link (query, n, html (n->name))),
72                                      html(html_tag ("td"), n->url ?
73                                           html (
74                                                 html_tag ("a", html_attr ("href", n->url)),
75                                                 n->url)
76                                           : "N/A"
77                                           ),
78                                      html(html_tag ("td"), n->description),
79                                      html(html_tag ("td"), n->owner),
80                                      html(html_tag ("td"),
81                                           webgit_object_link (query,
82                                                               n->name, strlen(n->name),
83                                                               n->last_commit, 40,
84                                                               webgit_pretty_age (n->age))
85                                           ),
86                                      html(html_tag ("td"),
87                                           webgit_email_link (n->last_author_name, n->last_author_email)
88                                           ),
89                                      html(html_tag ("td"),
90                                           webgit_email_link (n->last_committer_name, n->last_committer_email)
91                                           ),
92                                      html(html_tag ("td"),
93                                           html(
94                                                webgit_log_link (query, n, n->last_head, html("Log")),
95                                                " ",
96
97                                                "Diff ",
98                                                //webgit_diff_link (query, n, n->last_head, html("Diff")),
99                                                //" ",
100                                                webgit_object_link (query,
101                                                                   n->name, strlen(n->name),
102                                                                   n->last_tree, 40,
103                                                                   html("Tree"))
104                                                )
105                                           )
106                                      ),
107                         html_nl()
108                         );
109     }
110
111   return html (html_tag ("table", html_attr ("border", "1")), table);
112 }
113
114 static Html
115 webgit_main_action (struct webgit_query* query)
116 {
117   return html(
118               html(html_tag("div"), webgit_main_menu_action (query)), html_nl (),
119               html(html_tag("div"), webgit_main_content_action (query)), html_nl ()
120               );
121 }
122
123
124 /*
125   Summary of a single repository
126 */
127 static Html
128 webgit_summary_action (struct webgit_query* query)
129 {
130   return html("summary");
131 }
132
133
134 /*
135   Log page
136 */
137 static Html
138 webgit_log_action (struct webgit_query* query)
139 {
140   
141
142   return html("log");
143 }
144
145
146 /*
147   Diff page
148 */
149 static Html
150 webgit_diff_action (struct webgit_query* query)
151 {
152   return html("diff");
153 }
154
155
156 /*
157   pretty printed objects
158 */
159 static Html
160 webgit_object_action (struct webgit_query* query)
161 {
162   LLIST_FOREACH (&query->repos, node)
163     {
164       struct webgit_repo_info* ri = (struct webgit_repo_info*) node;
165       if (!strcmp (ri->name, query->repo))
166         {
167           chdir (ri->path);
168           break;
169         }
170     }
171
172   setup_git_directory ();
173
174   unsigned char sha1[20];
175   if (get_sha1(query->object, sha1))
176     return html("error: unknown object");
177
178   switch (sha1_object_info(sha1, NULL))
179     {
180     case OBJ_COMMIT:
181       if (!query->deref_to_tree)
182         return webgit_object_commit_action (query, sha1);
183       /* else fallthrough */
184     case OBJ_TREE:
185       return webgit_object_tree_action (query, sha1);
186     case OBJ_BLOB:
187       return webgit_object_blob_action (query, sha1);
188     case OBJ_TAG:
189       return html ("its a tag");
190
191       break;
192     default:
193       return html ("error: unknown object type");
194       break;
195     }
196 }
197
198
199 /*
200   treeish dereferenced to tree
201 */
202 static Html
203 webgit_tree_action (struct webgit_query* query)
204 {
205   query->deref_to_tree = 1;
206   return webgit_object_action (query);
207 }
208
209
210 /*
211   spew out raw data only
212 */
213 static Html
214 webgit_raw_action (struct webgit_query* query)
215 {
216   return html("raw");
217 }
218
219
220 /*
221   show tag
222 */
223 static Html
224 webgit_tag_action (struct webgit_query* query)
225 {
226   return html("tag");
227 }
228
229
230 /*
231   edit an object
232 */
233 static Html
234 webgit_edit_action (struct webgit_query* query)
235 {
236   return html("edit");
237 }
238
239 /*
240   commit changes
241 */
242 static Html
243 webgit_commit_action (struct webgit_query* query)
244 {
245   return html("commit");
246 }
247
248
249 Html
250 webgit_action_dispatch (struct webgit_query* query)
251 {
252 #define WEBGIT_ACTION(name, _)                  \
253 {                                               \
254  #name,                                         \
255  webgit_##name##_action,                        \
256 },
257   struct action_table{
258     char* name;
259     Html (*cb)(struct webgit_query* query);
260   } cmds[] = {WEBGIT_ACTIONS {"", NULL}};
261 #undef WEBGIT_ACTION
262
263   for (struct action_table* j = cmds; j->cb; ++j)
264     {
265       if (!strcmp (j->name, query->action))
266         return j->cb (query);
267     }
268   return html("error, no such action");
269 }
270
271