e2b4c6490018074c96fe30dfce86588f8fc6d205
[webgit] / src / log.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 "log.h"
23 #include "object.h"
24
25 #define SHA1_HEADER <openssl/sha.h>
26 #include "git/cache.h"
27 #include "git/commit.h"
28 #include "git/diff.h"
29 #include "git/revision.h"
30
31 Html
32 webgit_log_link (struct webgit_query* query,
33                  const char* repo,
34                  const char* head,
35                  const char* object, int object_len,
36                  int count,
37                  Html text)
38 {
39   return html (
40                html_tag ("a",
41                          html_attr ("href", html(
42                                                  html_fmt ("%s?repo=%s&action=log&head=%s",
43                                                            query->request->script_name,
44                                                            repo,
45                                                            head?head:"master"),
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 ("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
86   struct commit *commit;
87   while ((commit = get_revision (&rev)))
88     {
89       if (!count--)
90         {
91           /* displayed query->count lines, finish */
92           html_list_append (cont,
93                             webgit_log_link (
94                                              query,
95                                              query->repo,
96                                              query->head,
97                                              sha1_to_hex(commit->object.sha1), 40,
98                                              query->count_def,
99                                              html("More..."))
100                             );
101           free(commit->buffer);
102           commit->buffer = NULL;
103           free_commit_list(commit->parents);
104           commit->parents = NULL;
105           break;
106         }
107
108       /* TODO: check if commit is one of the heads and display that */
109
110       struct tm tm;
111       webgit_commit_committer_date_parse (commit, &tm);
112
113       char pretty_date[256];
114       strftime (pretty_date, 255, "%c", &tm);
115
116       html_list_append (table,
117                         html (
118                               html (
119                                     html_tag ("tr"),
120                                     html(html_tag ("td"),
121                                          html_strndup (pretty_date, 254)
122                                          ),
123                                     html(html_tag ("td"),
124                                          webgit_object_link (query,
125                                                              query->repo,
126                                                              strlen (query->repo),
127                                                              sha1_to_hex (commit->object.sha1),
128                                                              40,
129                                                              NULL,
130                                                              NULL,
131                                                              webgit_commit_header_parse (commit)
132                                                              )
133                                          )
134                                     ),
135                               html (
136                                     html_tag ("tr"),
137                                     html(html_tag ("td"),
138                                          webgit_email_link (
139                                                             webgit_commit_author_name_parse (commit),
140                                                             webgit_commit_author_email_parse (commit)
141                                                             )
142                                          ),
143                                     html (html_tag ("td"),
144                                          html(
145                                               "Diff ",
146                                               " ",
147                                               webgit_object_link (query,
148                                                                   query->repo,
149                                                                   strlen (query->repo),
150                                                                   webgit_commit_tree_parse (commit),
151                                                                   40,
152                                                                   NULL,
153                                                                   NULL,
154                                                                   html ("Tree")
155                                                                   )
156                                               )
157                                           )
158                                     )
159                               )
160                         );
161
162       if (msg_flag)
163         html_list_append (table,
164                           html (
165                                 html_tag ("tr"),
166                                 html(html_tag ("td", html_attr ("colspan", "2")),
167                                      html(html_tag ("pre"),
168                                           webgit_commit_message_parse (commit)
169                                           ),
170                                      html (html_tag ("hr"))
171                                      )
172                                 )
173                           );
174
175
176 #if 0 /* traverse parents */
177       struct commit *parent;
178       while ((parent = pop_commit (&commit->parents)))
179         {
180         }
181 #endif
182
183       free(commit->buffer);
184       commit->buffer = NULL;
185       free_commit_list(commit->parents);
186       commit->parents = NULL;
187     }
188
189   return html (html ( html_tag ("table"), table), cont);
190 }
191
192
193 Html
194 webgit_log_content_action (struct webgit_query* query)
195 {
196   const char* object;
197   if (query->object)
198     object = query->object;
199   else if (query->head)
200     object = query->head;
201   else
202     object = "HEAD";
203
204   return webgit_log_table (query, object, query->count, 1);
205 }