3d18ac18fd1b07cc2349fb38029f471cc456ba94
[webgit] / src / log.c
1 /*
2     cehtehs git web frontend
3
4   Copyright (C)
5     2007, 2008,         Christian Thaeter <ct@pipapo.org>
6
7   This program is free software: you can redistribute it and/or modify
8   it under the terms of the GNU Affero General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (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 Affero General Public License for more details.
16
17   You should have received a copy of the GNU Affero General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "log.h"
22 #include "object.h"
23
24 #define SHA1_HEADER <openssl/sha.h>
25 #include "git/cache.h"
26 #include "git/commit.h"
27 #include "git/diff.h"
28 #include "git/revision.h"
29
30 Html
31 webgit_log_link (struct webgit_query* query,
32                  const char* repo,
33                  const char* head,
34                  const char* object, int object_len,
35                  int count,
36                  Html text)
37 {
38   return html (
39                html_tag ("a",
40                          html_attr ("href", html(
41                                                  html_fmt ("%s?repo=%s&action=log",
42                                                            query->request->script_name,
43                                                            repo),
44                                                  head ? html_fmt("&head=%s", head)
45                                                  : html (),
46                                                  object ? html_fmt("&object=%.*s",
47                                                                    object_len, object)
48                                                  : html (),
49                                                  count > 0 ? html_fmt("&count=%d",
50                                                                       count)
51                                                  : html ()
52                                                  )
53                                     )
54                          ),
55                text
56                );
57 }
58
59
60 Html
61 webgit_log_menu_action (struct webgit_query* query)
62 {
63   (void) query;
64   return html (html_tag("div", html_attr("id", "sub-menu")), "TODO: log sidebar");
65 }
66
67
68 Html
69 webgit_log_table (struct webgit_query* query, const char* object, int count, int msg_flag)
70 {
71   struct rev_info rev;
72   const char* argv[] = {NULL, object, NULL};
73   int argc = 2;
74
75   init_revisions (&rev, NULL);
76   rev.verbose_header = 1;
77   rev.topo_order = 1;
78
79   setup_revisions (argc, argv, &rev, NULL);
80
81   prepare_revision_walk(&rev);
82
83   Html table = html_list ();
84   Html cont = html_list ();
85   int row = 0;
86
87   struct commit *commit;
88   while ((commit = get_revision (&rev)))
89     {
90       if (!count--)
91         {
92           /* displayed query->count lines, finish */
93           html_list_append (cont,
94                             webgit_log_link (
95                                              query,
96                                              query->repo,
97                                              query->head,
98                                              sha1_to_hex(commit->object.sha1), 40,
99                                              query->count_def,
100                                              html("More..."))
101                             );
102           free(commit->buffer);
103           commit->buffer = NULL;
104           free_commit_list(commit->parents);
105           commit->parents = NULL;
106           break;
107         }
108
109       /* TODO: check if commit is one of the heads and display that */
110
111       struct tm tm;
112       webgit_object_commit_committer_date_parse (commit, &tm);
113
114       char pretty_date[256];
115       strftime (pretty_date, 255, "%c", &tm);
116
117       ++row;
118
119       html_list_append (table,
120                         html (
121                               html (
122                                     html_tag ("tr",
123                                               html_attr ("class", (row & 1) ? "odd" : "even")
124                                               ),
125                                     html(html_tag ("td"),
126                                          html_strndup (pretty_date, 254)
127                                          ),
128                                     html(html_tag ("td"),
129                                          webgit_object_link (query,
130                                                              query->repo,
131                                                              strlen (query->repo),
132                                                              sha1_to_hex (commit->object.sha1),
133                                                              40,
134                                                              NULL,
135                                                              NULL,
136                                                              webgit_object_commit_header_parse (commit)
137                                                              )
138                                          )
139                                     ),
140                               html (
141                                     html_tag ("tr",
142                                               html_attr ("class", (row & 1) ? "odd" : "even")
143                                               ),
144                                     html(html_tag ("td"),
145                                          webgit_email_link (
146                                                             webgit_object_commit_author_name_parse (commit),
147                                                             webgit_object_commit_author_email_parse (commit)
148                                                             )
149                                          ),
150                                     html (html_tag ("td"),
151                                          html(
152                                               "Diff ",
153                                               " ",
154                                               webgit_object_link (query,
155                                                                   query->repo,
156                                                                   strlen (query->repo),
157                                                                   webgit_object_commit_tree_parse (commit),
158                                                                   40,
159                                                                   NULL,
160                                                                   NULL,
161                                                                   html ("Tree")
162                                                                   )
163                                               )
164                                           )
165                                     )
166                               )
167                         );
168
169       if (msg_flag)
170         html_list_append (table,
171                           html (
172                                 html_tag ("tr",
173                                           html_attr ("class", (row & 1) ? "odd" : "even")
174                                           ),
175                                 html(html_tag ("td", html_attr ("colspan", "2")),
176                                      html(html_tag ("pre"),
177                                           webgit_object_commit_message_parse (commit)
178                                           ),
179                                      html (html_tag ("hr"))
180                                      )
181                                 )
182                           );
183
184
185 #if 0 /* traverse parents */
186       struct commit *parent;
187       while ((parent = pop_commit (&commit->parents)))
188         {
189         }
190 #endif
191
192       free(commit->buffer);
193       commit->buffer = NULL;
194       free_commit_list(commit->parents);
195       commit->parents = NULL;
196     }
197
198   return html (html( html_tag ("table", html_attr ("id", "log")), table), cont);
199 }
200
201
202 Html
203 webgit_log_content_action (struct webgit_query* query)
204 {
205   const char* object;
206   if (query->object)
207     object = query->object;
208   else if (query->head)
209     object = query->head;
210   else
211     object = "HEAD";
212
213   return webgit_log_table (query, object, query->count, 1);
214 }
215
216 /*
217 //      Local Variables:
218 //      mode: C
219 //      c-file-style: "gnu"
220 //      indent-tabs-mode: nil
221 //      End:
222 */