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