webgit_enter_directory() to pick and setup a git repository
[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   webgit_enter_repository (query);
163
164   unsigned char sha1[20];
165   if (get_sha1 (query->object, sha1))
166     return html("error: unknown object");
167
168   switch (sha1_object_info(sha1, NULL))
169     {
170     case OBJ_COMMIT:
171       if (!query->deref_to_tree)
172         return webgit_object_commit_action (query, sha1);
173       /* else fallthrough */
174     case OBJ_TREE:
175       return webgit_object_tree_action (query, sha1);
176     case OBJ_BLOB:
177       return webgit_object_blob_action (query, sha1);
178     case OBJ_TAG:
179       return html ("its a tag");
180
181       break;
182     default:
183       return html ("error: unknown object type");
184       break;
185     }
186 }
187
188
189 /*
190   treeish dereferenced to tree
191 */
192 static Html
193 webgit_tree_action (struct webgit_query* query)
194 {
195   query->deref_to_tree = 1;
196   return webgit_object_action (query);
197 }
198
199
200 /*
201   spew out raw data only
202 */
203 static Html
204 webgit_raw_action (struct webgit_query* query)
205 {
206   return html("raw");
207 }
208
209
210 /*
211   show tag
212 */
213 static Html
214 webgit_tag_action (struct webgit_query* query)
215 {
216   return html("tag");
217 }
218
219
220 /*
221   edit an object
222 */
223 static Html
224 webgit_edit_action (struct webgit_query* query)
225 {
226   return html("edit");
227 }
228
229 /*
230   commit changes
231 */
232 static Html
233 webgit_commit_action (struct webgit_query* query)
234 {
235   return html("commit");
236 }
237
238
239 Html
240 webgit_action_dispatch (struct webgit_query* query)
241 {
242 #define WEBGIT_ACTION(name, _)                  \
243 {                                               \
244  #name,                                         \
245  webgit_##name##_action,                        \
246 },
247   struct action_table{
248     char* name;
249     Html (*cb)(struct webgit_query* query);
250   } cmds[] = {WEBGIT_ACTIONS {"", NULL}};
251 #undef WEBGIT_ACTION
252
253   for (struct action_table* j = cmds; j->cb; ++j)
254     {
255       if (!strcmp (j->name, query->action))
256         return j->cb (query);
257     }
258   return html("error, no such action");
259 }
260
261