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