]> gitweb.ps.run Git - ps-cgit/blob - ui-atom.c
git: update to v2.46.0
[ps-cgit] / 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 #define USE_THE_REPOSITORY_VARIABLE
10
11 #include "cgit.h"
12 #include "ui-atom.h"
13 #include "html.h"
14 #include "ui-shared.h"
15
16 static void add_entry(struct commit *commit, const char *host)
17 {
18         char delim = '&';
19         char *hex;
20         char *mail, *t, *t2;
21         struct commitinfo *info;
22
23         info = cgit_parse_commit(commit);
24         hex = oid_to_hex(&commit->object.oid);
25         html("<entry>\n");
26         html("<title>");
27         html_txt(info->subject);
28         html("</title>\n");
29         html("<updated>");
30         html_txt(show_date(info->committer_date, 0,
31                     date_mode_from_type(DATE_ISO8601_STRICT)));
32         html("</updated>\n");
33         html("<author>\n");
34         if (info->author) {
35                 html("<name>");
36                 html_txt(info->author);
37                 html("</name>\n");
38         }
39         if (info->author_email && !ctx.cfg.noplainemail) {
40                 mail = xstrdup(info->author_email);
41                 t = strchr(mail, '<');
42                 if (t)
43                         t++;
44                 else
45                         t = mail;
46                 t2 = strchr(t, '>');
47                 if (t2)
48                         *t2 = '\0';
49                 html("<email>");
50                 html_txt(t);
51                 html("</email>\n");
52                 free(mail);
53         }
54         html("</author>\n");
55         html("<published>");
56         html_txt(show_date(info->author_date, 0,
57                     date_mode_from_type(DATE_ISO8601_STRICT)));
58         html("</published>\n");
59         if (host) {
60                 char *pageurl;
61                 html("<link rel='alternate' type='text/html' href='");
62                 html(cgit_httpscheme());
63                 html_attr(host);
64                 pageurl = cgit_pageurl(ctx.repo->url, "commit", NULL);
65                 html_attr(pageurl);
66                 if (ctx.cfg.virtual_root)
67                         delim = '?';
68                 html_attrf("%cid=%s", delim, hex);
69                 html("'/>\n");
70                 free(pageurl);
71         }
72         html("<id>");
73         html_txtf("urn:%s:%s", the_hash_algo->name, hex);
74         html("</id>\n");
75         html("<content type='text'>\n");
76         html_txt(info->msg);
77         html("</content>\n");
78         html("</entry>\n");
79         cgit_free_commitinfo(info);
80 }
81
82
83 void cgit_print_atom(char *tip, const char *path, int max_count)
84 {
85         char *host;
86         const char *argv[] = {NULL, tip, NULL, NULL, NULL};
87         struct commit *commit;
88         struct rev_info rev;
89         int argc = 2;
90         bool first = true;
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         repo_init_revisions(the_repository, &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 *fullurl = cgit_currentfullurl();
132                 char *repourl = cgit_repourl(ctx.repo->url);
133                 html("<id>");
134                 html_txtf("%s%s%s", cgit_httpscheme(), host, fullurl);
135                 html("</id>\n");
136                 html("<link rel='self' href='");
137                 html_attrf("%s%s%s", cgit_httpscheme(), host, fullurl);
138                 html("'/>\n");
139                 html("<link rel='alternate' type='text/html' href='");
140                 html_attrf("%s%s%s", cgit_httpscheme(), host, repourl);
141                 html("'/>\n");
142                 free(fullurl);
143                 free(repourl);
144         }
145         while ((commit = get_revision(&rev)) != NULL) {
146                 if (first) {
147                         html("<updated>");
148                         html_txt(show_date(commit->date, 0,
149                                 date_mode_from_type(DATE_ISO8601_STRICT)));
150                         html("</updated>\n");
151                         first = false;
152                 }
153                 add_entry(commit, host);
154                 release_commit_memory(the_repository->parsed_objects, commit);
155                 commit->parents = NULL;
156         }
157         html("</feed>\n");
158         free(host);
159 }