]> git.cameronkatri.com Git - cgit.git/blob - ui-atom.c
ui-atom: fix resource leak: free allocation from cgit_pageurl
[cgit.git] / ui-atom.c
1 /* ui-atom.c: functions for atom feeds
2 *
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9 #include "cgit.h"
10 #include "ui-atom.h"
11 #include "html.h"
12 #include "ui-shared.h"
13
14 static void add_entry(struct commit *commit, const char *host)
15 {
16 char delim = '&';
17 char *hex;
18 char *mail, *t, *t2;
19 struct commitinfo *info;
20
21 info = cgit_parse_commit(commit);
22 hex = sha1_to_hex(commit->object.sha1);
23 html("<entry>\n");
24 html("<title>");
25 html_txt(info->subject);
26 html("</title>\n");
27 html("<updated>");
28 cgit_print_date(info->committer_date, FMT_ATOMDATE, 0);
29 html("</updated>\n");
30 html("<author>\n");
31 if (info->author) {
32 html("<name>");
33 html_txt(info->author);
34 html("</name>\n");
35 }
36 if (info->author_email && !ctx.cfg.noplainemail) {
37 mail = xstrdup(info->author_email);
38 t = strchr(mail, '<');
39 if (t)
40 t++;
41 else
42 t = mail;
43 t2 = strchr(t, '>');
44 if (t2)
45 *t2 = '\0';
46 html("<email>");
47 html_txt(t);
48 html("</email>\n");
49 free(mail);
50 }
51 html("</author>\n");
52 html("<published>");
53 cgit_print_date(info->author_date, FMT_ATOMDATE, 0);
54 html("</published>\n");
55 if (host) {
56 char *pageurl;
57 html("<link rel='alternate' type='text/html' href='");
58 html(cgit_httpscheme());
59 html_attr(host);
60 pageurl = cgit_pageurl(ctx.repo->url, "commit", NULL);
61 html_attr(pageurl);
62 if (ctx.cfg.virtual_root)
63 delim = '?';
64 htmlf("%cid=%s", delim, hex);
65 html("'/>\n");
66 free(pageurl);
67 }
68 htmlf("<id>%s</id>\n", hex);
69 html("<content type='text'>\n");
70 html_txt(info->msg);
71 html("</content>\n");
72 html("<content type='xhtml'>\n");
73 html("<div xmlns='http://www.w3.org/1999/xhtml'>\n");
74 html("<pre>\n");
75 html_txt(info->msg);
76 html("</pre>\n");
77 html("</div>\n");
78 html("</content>\n");
79 html("</entry>\n");
80 cgit_free_commitinfo(info);
81 }
82
83
84 void cgit_print_atom(char *tip, char *path, int max_count)
85 {
86 char *host;
87 const char *argv[] = {NULL, tip, NULL, NULL, NULL};
88 struct commit *commit;
89 struct rev_info rev;
90 int argc = 2;
91
92 if (ctx.qry.show_all)
93 argv[1] = "--all";
94 else if (!tip)
95 argv[1] = ctx.qry.head;
96
97 if (path) {
98 argv[argc++] = "--";
99 argv[argc++] = path;
100 }
101
102 init_revisions(&rev, NULL);
103 rev.abbrev = DEFAULT_ABBREV;
104 rev.commit_format = CMIT_FMT_DEFAULT;
105 rev.verbose_header = 1;
106 rev.show_root_diff = 0;
107 rev.max_count = max_count;
108 setup_revisions(argc, argv, &rev, NULL);
109 prepare_revision_walk(&rev);
110
111 host = cgit_hosturl();
112 ctx.page.mimetype = "text/xml";
113 ctx.page.charset = "utf-8";
114 cgit_print_http_headers();
115 html("<feed xmlns='http://www.w3.org/2005/Atom'>\n");
116 html("<title>");
117 html_txt(ctx.repo->name);
118 if (path) {
119 html("/");
120 html_txt(path);
121 }
122 if (tip && !ctx.qry.show_all) {
123 html(", branch ");
124 html_txt(tip);
125 }
126 html("</title>\n");
127 html("<subtitle>");
128 html_txt(ctx.repo->desc);
129 html("</subtitle>\n");
130 if (host) {
131 char *repourl = cgit_repourl(ctx.repo->url);
132 html("<link rel='alternate' type='text/html' href='");
133 html(cgit_httpscheme());
134 html_attr(host);
135 html_attr(repourl);
136 html("'/>\n");
137 free(repourl);
138 }
139 while ((commit = get_revision(&rev)) != NULL) {
140 add_entry(commit, host);
141 free_commit_buffer(commit);
142 free_commit_list(commit->parents);
143 commit->parents = NULL;
144 }
145 html("</feed>\n");
146 free(host);
147 }