first object ui for commits
[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, n, n->last_commit, ctgit_pretty_age (n->age))
70                                           ),
71                                      html(html_tag ("td"),
72                                           html(
73                                                ctgit_log_link (query, n, n->last_head, html("Log")),
74                                                " ",
75                                                //ctgit_diff_link (query, n, n->last_head, html("Diff")),
76                                                //" ",
77                                                ctgit_object_link (query, n, n->last_tree, html("Tree"))
78                                                )
79                                           )
80                                      ),
81                         html_nl()
82                         );
83     }
84
85   return html (html_tag ("table", html_attr ("border", "1")), table);
86 }
87
88 static Html
89 ctgit_main_action (struct ctgit_query* query)
90 {
91   return html(
92               html(html_tag("div"), ctgit_main_menu_action (query)), html_nl (),
93               html(html_tag("div"), ctgit_main_content_action (query)), html_nl ()
94               );
95 }
96
97 static Html
98 ctgit_summary_action (struct ctgit_query* query)
99 {
100   return html("summary");
101 }
102
103 static Html
104 ctgit_log_action (struct ctgit_query* query)
105 {
106   return html("log");
107 }
108
109 static Html
110 ctgit_diff_action (struct ctgit_query* query)
111 {
112   return html("diff");
113 }
114
115 static Html
116 ctgit_object_action (struct ctgit_query* query)
117 {
118   LLIST_FOREACH (&query->repos, node)
119     {
120       struct ctgit_repo_info* ri = (struct ctgit_repo_info*) node;
121       if (!strcmp (ri->name, query->repo))
122         {
123           chdir (ri->path);
124           break;
125         }
126     }
127
128   setup_git_directory ();
129
130   unsigned char sha1[20];
131   if (get_sha1(query->object, sha1))
132     return html("error: unknown object");
133
134   switch (sha1_object_info(sha1, NULL))
135     {
136     case OBJ_COMMIT:
137       return ctgit_object_commit_action (query, sha1);
138     case OBJ_TREE:
139       return ctgit_object_tree_action (query, sha1);
140     case OBJ_BLOB:
141       return html ("its a blob");
142
143       break;
144     case OBJ_TAG:
145       return html ("its a tag");
146
147       break;
148     default:
149       return html ("error: unknown object type");
150       break;
151     }
152 }
153
154 static Html
155 ctgit_raw_action (struct ctgit_query* query)
156 {
157   return html("raw");
158 }
159
160 static Html
161 ctgit_tag_action (struct ctgit_query* query)
162 {
163   return html("tag");
164 }
165
166 static Html
167 ctgit_edit_action (struct ctgit_query* query)
168 {
169   return html("edit");
170 }
171
172 static Html
173 ctgit_commit_action (struct ctgit_query* query)
174 {
175   return html("commit");
176 }
177
178
179 Html
180 ctgit_action_dispatch (struct ctgit_query* query)
181 {
182 #define CTGIT_ACTION(name, _)                   \
183 {                                               \
184  #name,                                         \
185  ctgit_##name##_action,                         \
186 },
187   struct action_table{
188     char* name;
189     Html (*cb)(struct ctgit_query* query);
190   } cmds[] = {CTGIT_ACTIONS {"", NULL}};
191 #undef CTGIT_ACTION
192
193   for (struct action_table* j = cmds; j->cb; ++j)
194     {
195       if (!strcmp (j->name, query->action))
196         return j->cb (query);
197     }
198   return html("error, no such action");
199 }
200
201