factored the single object types out into their own sources
[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
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 "tag.h"
23 #include "object.h"
24 #include "age.h"
25 #include "log.h"
26
27 #include <cwa.h>
28
29 #define SHA1_HEADER <openssl/sha.h>
30 #include "git/cache.h"
31 #include "git/tag.h"
32 //#include "git/commit.h"
33 #include "git/refs.h"
34
35 #include <time.h>
36
37 struct tag_context
38 {
39   struct webgit_repo_info* repo;
40   Html table;
41   Html cont;
42   const char* start;
43   int count;
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 (
116                                 html_tag ("td"),
117                                 webgit_object_link (repo->query,
118                                                     repo->name, strlen(repo->name),
119                                                     sha1_to_hex (sha1), 40,
120                                                     NULL,
121                                                     NULL,
122                                                     html_strndup (refname, SIZE_MAX))
123                                 ),
124                           html (
125                                 html_tag ("td"),
126                                 webgit_object_link (repo->query,
127                                                     repo->name, strlen(repo->name),
128                                                     sha1_to_hex (derefed->sha1), 40,
129                                                     NULL,
130                                                     NULL,
131                                                     webgit_pretty_age (repo->query->now - date))
132                                 ),
133                           html (
134                                 html_tag ("td"),
135                                 webgit_email_link (
136                                                    webgit_object_tag_name_parse (buffer, size),
137                                                    webgit_object_tag_email_parse (buffer, size)
138                                                    )
139                                 ),
140                           html (
141                                 html_tag ("td"),
142                                 webgit_object_tag_header_parse (buffer, size)
143                                 ),
144                           html (
145                                 html_tag ("td"),
146                                 webgit_log_link (repo->query,
147                                                  repo->name,
148                                                  tagref,
149                                                  NULL, 0,
150                                                  repo->query->count,
151                                                  html ("Log")),
152                                 webgit_object_link (repo->query,
153                                                     repo->name, strlen(repo->name),
154                                                     sha1_to_hex (derefed->sha1), 40,
155                                                     NULL,
156                                                     "tree",
157                                                     html ("Tree")),
158                                 " Snapshot"
159                                 )
160                           )
161                     );
162   return 0;
163 }
164
165
166 Html
167 webgit_tag_table (struct webgit_repo_info* repo, const char* head, int count)
168 {
169   struct tag_context ctx;
170   ctx.repo = repo;
171   ctx.table = html_list ();
172   ctx.cont = html_list ();
173   ctx.count = count;
174   ctx.start = head;
175
176   for_each_tag_ref (show_tag_ref, &ctx);
177
178   return html (html (html_tag ("table"), ctx.table), ctx.cont);
179 }