refactor ctgit_object_link to be more flexible
[webgit] / src / object.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 "object.h"
23
24 #define SHA1_HEADER <openssl/sha.h>
25 #include "git/git-compat-util.h"
26 #include "git/object.h"
27
28
29 Html
30 ctgit_object_link (struct ctgit_query* query,
31                    const char* repo,
32                    int repo_len,
33                    const char* object,
34                    int object_len,
35                    Html text)
36 {
37   return html (
38                html_tag ("a",
39                          html_attr ("href", html_fmt ("%s?repo=%.*s&object=%.*s",
40                                                       query->request->script_name,
41                                                       repo_len, repo,
42                                                       object_len, object)
43                                     )
44                          ),
45                text
46                );
47 }
48
49
50 /*
51   Display committs
52 */
53
54 static Html
55 ctgit_object_commit_menu_action (struct ctgit_query* query, unsigned char* sha1, void* buf, unsigned long size)
56 {
57   return html ("TODO: commit-object sidebar");
58 }
59
60 static Html
61 ctgit_object_commit_content_action (struct ctgit_query* query, unsigned char* sha1, void* buf, unsigned long size)
62 {
63   Html tree = html_list ();
64   Html parents = html_list ();
65   Html author_text = html_list ();
66   Html author = html_list ();
67   Html committer = html_list ();
68
69   const char* author_beg = NULL;
70   const char* author_end = NULL;
71
72   const char* i = buf;
73   while (i && i < buf+size)
74     {
75       if (*i == '\n')
76         {
77           while (*i == '\n')
78             ++i;
79           break; /* message */
80         }
81       if (!strncmp (i, "tree ", 5))
82         {
83
84           html_list_append (tree,
85                             "Tree: ",
86                             ctgit_object_link (query,
87                                                query->repo, strlen (query->repo),
88                                                i+5, 40,
89                                                html_fmt ("%.40s", i+5))
90                             );
91
92           (i = strchr (i, '\n')) && ++i;
93           continue;
94         }
95       else if (!strncmp (i, "parent ", 7))
96         {
97           html_list_append (parents, html (
98                                            "Parent: ",
99                                            ctgit_object_link (query,
100                                                               query->repo, strlen (query->repo),
101                                                               i+7, 40,
102                                                               html_fmt ("%.40s", i+7))
103                                            )
104                             );
105           (i = strchr (i, '\n')) && ++i;
106           continue;
107         }
108       else if (!strncmp (i, "author ", 7))
109         {
110           char* email_beg = strchr (i, '<');
111           char* email_end = strchr (email_beg, '>');
112
113           author_beg = i+7;
114           author_end = strchr (author_beg, '\n');
115
116           Html name = html_strndup (i+7, email_beg - i - 8);
117           Html email = html_strndup (email_beg + 1, email_end - email_beg - 1);
118
119           struct tm tm;
120           strptime (email_end + 2, "%s %Z", &tm);
121           char pretty_date[256];
122           strftime (pretty_date, 255, "%c", &tm);
123
124           html_list_append (author_text, "Author");
125
126           html_list_append (author, html (
127                                           html ( author_text ), /*BUG: libcwa bug, must be wraped in html()*/
128                                           html (
129                                                 html_tag("a",
130                                                          html_attr ("href",
131                                                                     html ("mailto:", email))
132                                                          ),
133                                                 name
134                                                 ),
135                                           html_strndup (pretty_date, 255)
136                                           )
137                             );
138           (i = strchr (i, '\n')) && ++i;
139           continue;
140         }
141       else if (!strncmp (i, "committer ", 10))
142         {
143           char* email_beg = strchr (i, '<');
144           char* email_end = strchr (email_beg, '>');
145
146           if (author_beg && author_end && strncmp (i + 10, author_beg, author_end - author_beg))
147             {
148               Html name = html_strndup (i+10, email_beg - i - 11);
149               Html email = html_strndup (email_beg + 1, email_end - email_beg - 1);
150
151               struct tm tm;
152               strptime (email_end + 2, "%s %Z", &tm);
153               char pretty_date[256];
154               strftime (pretty_date, 255, "%c", &tm);
155
156               html_list_append (committer, html ("Committer: ",
157                                                  html(
158                                                       html_tag("a",
159                                                                html_attr("href",
160                                                                          html ("mailto:", email))
161                                                                ),
162                                                       name
163                                                       ),
164                                                  html_strndup (pretty_date, 255)
165                                                  )
166                                 );
167             }
168           else
169             html_list_append (author_text, " and Committer");
170
171           (i = strchr (i, '\n')) && ++i;
172           continue;
173         }
174       ++i;
175     }
176
177   html_list_append (author_text, ": ");
178
179
180
181   Html headline = html_list ();
182   Html message = html_list ();
183
184   if (i)
185     {
186       const char* message_beg = strchr (i, '\n');
187
188       if (message_beg)
189         {
190           html_list_append (headline,
191                             html_strndup (i, message_beg - i)
192                             );
193
194           while (*message_beg == '\n')
195             ++message_beg;
196
197           html_list_append (message,
198                             html_strndup (message_beg, SIZE_MAX)
199                             );
200         }
201       else
202         {
203           html_list_append (headline,
204                             html_strndup (i, SIZE_MAX)
205                             );
206           html_list_append (message,
207                             html_strndup (i, SIZE_MAX)
208                             );
209         }
210     }
211
212   /* diffstat */
213
214
215   return html (
216                html (html_tag ("h3"), headline),
217                html (html_tag ("div"), tree), html_nl (),
218                html (html_tag ("div"), parents), html_nl (),
219                html (html_tag ("div"), author), html_nl (),
220                html (html_tag ("div"), committer), html_nl (),
221                html (html_tag ("pre"), message)
222                );
223 }
224
225 Html
226 ctgit_object_commit_action (struct ctgit_query* query, unsigned char* sha1)
227 {
228   void* buf;
229   unsigned long size;
230
231   buf = read_object_with_reference (sha1, "commit", &size, NULL);
232
233   return html(
234               html(html_tag("div"), ctgit_object_commit_menu_action (query, sha1, buf, size)), html_nl (),
235               html(html_tag("div"), ctgit_object_commit_content_action (query, sha1, buf, size)), html_nl ()
236               );
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250 static Html
251 ctgit_object_tree_menu_action (struct ctgit_query* query, unsigned char* sha1, void* buf, unsigned long size)
252 {
253   return html ("TODO: tree-object sidebar");
254 }
255
256 static Html
257 ctgit_object_tree_content_action (struct ctgit_query* query, unsigned char* sha1, void* buf, unsigned long size)
258 {
259
260   return html_fmt ("tree content %s", buf);
261 }
262
263 Html
264 ctgit_object_tree_action (struct ctgit_query* query, unsigned char* sha1)
265 {
266   unsigned long size;
267   void* buf;
268   buf = read_object_with_reference (sha1, "tree", &size, NULL);
269
270   return html(
271               html(html_tag("div"), ctgit_object_tree_menu_action (query, sha1, buf, size)), html_nl (),
272               html(html_tag("div"), ctgit_object_tree_content_action (query, sha1, buf, size)), html_nl ()
273               );
274 }
275
276
277
278
279 /*pretty printer for sourcecode*/
280 //Html
281 //ctgit_object_blob_dispatch (struct ctgit_query* query, const char *sha1);