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