Big rename! ctgit is dead, long live webgit
[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 static Html
38 webgit_main_menu_action (struct webgit_query* query)
39 {
40   return html ("TODO: main menu sidebar");
41 }
42
43 static Html
44 webgit_main_content_action (struct webgit_query* query)
45 {
46   Html table = html_list ();
47
48   html_list_append (table, html(
49                           html(html_tag ("tr"),
50                                html(html_tag ("th"),"Name"),
51                                html(html_tag ("th"),"Description"),
52                                html(html_tag ("th"),"Owner"),
53                                html(html_tag ("th"),"Last Change"),
54                                html(html_tag ("th"),"Action")
55                                ),
56                           html_nl()
57                           )
58               );
59
60   LLIST_FOREACH (&query->repos, node)
61     {
62       struct webgit_repo_info* n = (struct webgit_repo_info*)node;
63       html_list_append (table, html (
64                                      html_tag ("tr"),
65                                      html(html_tag ("td"), webgit_summary_link (query, n, html (n->name))),
66                                      html(html_tag ("td"), n->description),
67                                      html(html_tag ("td"), n->owner),
68                                      html(html_tag ("td"),
69                                           webgit_object_link (query,
70                                                              n->name, strlen(n->name),
71                                                              n->last_commit, 40,
72                                                              webgit_pretty_age (n->age))
73                                           ),
74                                      html(html_tag ("td"),
75                                           html(
76                                                webgit_log_link (query, n, n->last_head, html("Log")),
77                                                " ",
78                                                //webgit_diff_link (query, n, n->last_head, html("Diff")),
79                                                //" ",
80                                                webgit_object_link (query,
81                                                                   n->name, strlen(n->name),
82                                                                   n->last_tree, 40,
83                                                                   html("Tree"))
84                                                )
85                                           )
86                                      ),
87                         html_nl()
88                         );
89     }
90
91   return html (html_tag ("table", html_attr ("border", "1")), table);
92 }
93
94 static Html
95 webgit_main_action (struct webgit_query* query)
96 {
97   return html(
98               html(html_tag("div"), webgit_main_menu_action (query)), html_nl (),
99               html(html_tag("div"), webgit_main_content_action (query)), html_nl ()
100               );
101 }
102
103 static Html
104 webgit_summary_action (struct webgit_query* query)
105 {
106   return html("summary");
107 }
108
109 static Html
110 webgit_log_action (struct webgit_query* query)
111 {
112   return html("log");
113 }
114
115 static Html
116 webgit_diff_action (struct webgit_query* query)
117 {
118   return html("diff");
119 }
120
121 static Html
122 webgit_object_action (struct webgit_query* query)
123 {
124   LLIST_FOREACH (&query->repos, node)
125     {
126       struct webgit_repo_info* ri = (struct webgit_repo_info*) node;
127       if (!strcmp (ri->name, query->repo))
128         {
129           chdir (ri->path);
130           break;
131         }
132     }
133
134   setup_git_directory ();
135
136   unsigned char sha1[20];
137   if (get_sha1(query->object, sha1))
138     return html("error: unknown object");
139
140   switch (sha1_object_info(sha1, NULL))
141     {
142     case OBJ_COMMIT:
143       return webgit_object_commit_action (query, sha1);
144     case OBJ_TREE:
145       return webgit_object_tree_action (query, sha1);
146     case OBJ_BLOB:
147       return webgit_object_blob_action (query, sha1);
148     case OBJ_TAG:
149       return html ("its a tag");
150
151       break;
152     default:
153       return html ("error: unknown object type");
154       break;
155     }
156 }
157
158 static Html
159 webgit_raw_action (struct webgit_query* query)
160 {
161   return html("raw");
162 }
163
164 static Html
165 webgit_tag_action (struct webgit_query* query)
166 {
167   return html("tag");
168 }
169
170 static Html
171 webgit_edit_action (struct webgit_query* query)
172 {
173   return html("edit");
174 }
175
176 static Html
177 webgit_commit_action (struct webgit_query* query)
178 {
179   return html("commit");
180 }
181
182
183 Html
184 webgit_action_dispatch (struct webgit_query* query)
185 {
186 #define WEBGIT_ACTION(name, _)                   \
187 {                                               \
188  #name,                                         \
189  webgit_##name##_action,                         \
190 },
191   struct action_table{
192     char* name;
193     Html (*cb)(struct webgit_query* query);
194   } cmds[] = {WEBGIT_ACTIONS {"", NULL}};
195 #undef WEBGIT_ACTION
196
197   for (struct action_table* j = cmds; j->cb; ++j)
198     {
199       if (!strcmp (j->name, query->action))
200         return j->cb (query);
201     }
202   return html("error, no such action");
203 }
204
205