add odd/even class for table coloring
[webgit] / src / tag.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 "tag.h"
22 #include "object.h"
23 #include "age.h"
24 #include "log.h"
25
26 #include <cwa.h>
27
28 #define SHA1_HEADER <openssl/sha.h>
29 #include "git/cache.h"
30 #include "git/tag.h"
31 //#include "git/commit.h"
32 #include "git/refs.h"
33
34 #include <time.h>
35
36 struct tag_context
37 {
38   struct webgit_repo_info* repo;
39   Html table;
40   Html cont;
41   const char* start;
42   int count;
43   int row;
44 };
45
46
47 Html
48 webgit_tag_link (struct webgit_repo_info* repo, const char* tag, int count, Html text)
49 {
50   return html (
51                html_tag ("a",
52                          html_attr ("href", html(
53                                                  html_fmt ("%s?repo=%s&action=tag",
54                                                            repo->query->request->script_name,
55                                                            repo->name),
56                                                  tag ? html_fmt("&ref=%s", tag) : html (),
57                                                  count > 0 ? html_fmt("&count=%d",
58                                                                       count) : html ()
59                                                  )
60                                     )
61                          ),
62                text
63                );
64 }
65
66 static int
67 show_tag_ref (const char *refname, const unsigned char *sha1, int flags, void *data)
68 {
69   (void) flags;
70   struct tag_context* ctx = (struct tag_context*) data;
71   struct webgit_repo_info* repo = ctx->repo;
72
73   if (ctx->start)
74     {
75       if (!strcmp (ctx->start, refname))
76         ctx->start = NULL;
77       else
78         return 0;
79     }
80
81   if (!ctx->count--)
82     {
83       /* displayed query->count lines, finish */
84       html_list_append (ctx->cont,
85                         webgit_tag_link (
86                                          repo, refname,
87                                          repo->query->count,
88                                          html ("More..."))
89                         );
90       return 1;
91     }
92
93   struct tag* tag = lookup_tag (sha1);
94
95   enum object_type type;
96   void *buffer;
97   unsigned long size;
98
99   buffer = read_sha1_file (tag->object.sha1, &type, &size);
100
101   if (!tag->object.parsed)
102     parse_tag_buffer (tag, buffer, size);
103
104   unsigned long date =
105     webgit_object_tag_date_parse (buffer, size, NULL);
106
107   char tagref[256];
108   snprintf (tagref, 256, "refs/tags/%s", refname);
109
110   struct object* derefed = deref_tag ((struct object*)tag, NULL, 0);
111
112   html_list_append (ctx->table,
113                     html (
114                           html_tag ("tr",
115                                     html_attr ("class", (++ctx->row & 1) ? "odd" : "even")
116                                     ),
117                           html (
118                                 html_tag ("td"),
119                                 webgit_object_link (repo->query,
120                                                     repo->name, strlen(repo->name),
121                                                     sha1_to_hex (sha1), 40,
122                                                     NULL,
123                                                     NULL,
124                                                     html_strndup (refname, SIZE_MAX))
125                                 ),
126                           html (
127                                 html_tag ("td"),
128                                 webgit_object_link (repo->query,
129                                                     repo->name, strlen(repo->name),
130                                                     sha1_to_hex (derefed->sha1), 40,
131                                                     NULL,
132                                                     NULL,
133                                                     webgit_pretty_age (repo->query->now - date))
134                                 ),
135                           html (
136                                 html_tag ("td"),
137                                 webgit_email_link (
138                                                    webgit_object_tag_name_parse (buffer, size),
139                                                    webgit_object_tag_email_parse (buffer, size)
140                                                    )
141                                 ),
142                           html (
143                                 html_tag ("td"),
144                                 webgit_object_tag_header_parse (buffer, size)
145                                 ),
146                           html (
147                                 html_tag ("td"),
148                                 webgit_log_link (repo->query,
149                                                  repo->name,
150                                                  tagref,
151                                                  NULL, 0,
152                                                  repo->query->count,
153                                                  html ("Log")),
154                                 webgit_object_link (repo->query,
155                                                     repo->name, strlen(repo->name),
156                                                     sha1_to_hex (derefed->sha1), 40,
157                                                     NULL,
158                                                     "tree",
159                                                     html ("Tree")),
160                                 " Snapshot"
161                                 )
162                           )
163                     );
164   return 0;
165 }
166
167
168 Html
169 webgit_tag_table (struct webgit_repo_info* repo, const char* head, int count)
170 {
171   struct tag_context ctx;
172   ctx.repo = repo;
173   ctx.table = html_list ();
174   ctx.cont = html_list ();
175   ctx.count = count;
176   ctx.start = head;
177   ctx.row = 0;
178
179   for_each_tag_ref (show_tag_ref, &ctx);
180
181   return html (html (html_tag ("table", html_attr ("id", "tag")), ctx.table), ctx.cont);
182 }
183
184 /*
185 //      Local Variables:
186 //      mode: C
187 //      c-file-style: "gnu"
188 //      indent-tabs-mode: nil
189 //      End:
190 */