refactor ctgit_object_link to be more flexible
[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 ctgit_main_menu_action (struct ctgit_query* query)
39 {
40   return html ("TODO: main menu sidebar");
41 }
42
43 static Html
44 ctgit_main_content_action (struct ctgit_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 ctgit_repo_info* n = (struct ctgit_repo_info*)node;
63       html_list_append (table, html (
64                                      html_tag ("tr"),
65                                      html(html_tag ("td"), ctgit_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                                           ctgit_object_link (query,
70                                                              n->name, strlen(n->name),
71                                                              n->last_commit, 40,
72                                                              ctgit_pretty_age (n->age))
73                                           ),
74                                      html(html_tag ("td"),
75                                           html(
76                                                ctgit_log_link (query, n, n->last_head, html("Log")),
77                                                " ",
78                                                //ctgit_diff_link (query, n, n->last_head, html("Diff")),
79                                                //" ",
80                                                ctgit_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 ctgit_main_action (struct ctgit_query* query)
96 {
97   return html(
98               html(html_tag("div"), ctgit_main_menu_action (query)), html_nl (),
99               html(html_tag("div"), ctgit_main_content_action (query)), html_nl ()
100               );
101 }
102
103 static Html
104 ctgit_summary_action (struct ctgit_query* query)
105 {
106   return html("summary");
107 }
108
109 static Html
110 ctgit_log_action (struct ctgit_query* query)
111 {
112   return html("log");
113 }
114
115 static Html
116 ctgit_diff_action (struct ctgit_query* query)
117 {
118   return html("diff");
119 }
120
121 static Html
122 ctgit_object_action (struct ctgit_query* query)
123 {
124   LLIST_FOREACH (&query->repos, node)
125     {
126       struct ctgit_repo_info* ri = (struct ctgit_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 ctgit_object_commit_action (query, sha1);
144     case OBJ_TREE:
145       return ctgit_object_tree_action (query, sha1);
146     case OBJ_BLOB:
147       return html ("its a blob");
148
149       break;
150     case OBJ_TAG:
151       return html ("its a tag");
152
153       break;
154     default:
155       return html ("error: unknown object type");
156       break;
157     }
158 }
159
160 static Html
161 ctgit_raw_action (struct ctgit_query* query)
162 {
163   return html("raw");
164 }
165
166 static Html
167 ctgit_tag_action (struct ctgit_query* query)
168 {
169   return html("tag");
170 }
171
172 static Html
173 ctgit_edit_action (struct ctgit_query* query)
174 {
175   return html("edit");
176 }
177
178 static Html
179 ctgit_commit_action (struct ctgit_query* query)
180 {
181   return html("commit");
182 }
183
184
185 Html
186 ctgit_action_dispatch (struct ctgit_query* query)
187 {
188 #define CTGIT_ACTION(name, _)                   \
189 {                                               \
190  #name,                                         \
191  ctgit_##name##_action,                         \
192 },
193   struct action_table{
194     char* name;
195     Html (*cb)(struct ctgit_query* query);
196   } cmds[] = {CTGIT_ACTIONS {"", NULL}};
197 #undef CTGIT_ACTION
198
199   for (struct action_table* j = cmds; j->cb; ++j)
200     {
201       if (!strcmp (j->name, query->action))
202         return j->cb (query);
203     }
204   return html("error, no such action");
205 }
206
207