Blame


1 2c251c14 2020-01-15 tracey /*
2 9460dac0 2020-01-15 tracey * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 2c251c14 2020-01-15 tracey * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 2c251c14 2020-01-15 tracey *
5 2c251c14 2020-01-15 tracey * Permission to use, copy, modify, and distribute this software for any
6 2c251c14 2020-01-15 tracey * purpose with or without fee is hereby granted, provided that the above
7 2c251c14 2020-01-15 tracey * copyright notice and this permission notice appear in all copies.
8 2c251c14 2020-01-15 tracey *
9 2c251c14 2020-01-15 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 2c251c14 2020-01-15 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 2c251c14 2020-01-15 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 2c251c14 2020-01-15 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 2c251c14 2020-01-15 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 2c251c14 2020-01-15 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 2c251c14 2020-01-15 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 2c251c14 2020-01-15 tracey */
17 2c251c14 2020-01-15 tracey
18 2c251c14 2020-01-15 tracey #include <sys/queue.h>
19 2c251c14 2020-01-15 tracey #include <sys/stat.h>
20 2c251c14 2020-01-15 tracey #include <sys/types.h>
21 2c251c14 2020-01-15 tracey
22 017d6da3 2020-01-29 tracey #include <ctype.h>
23 2c251c14 2020-01-15 tracey #include <dirent.h>
24 2c251c14 2020-01-15 tracey #include <err.h>
25 c25c2314 2020-01-28 stsp #include <errno.h>
26 474370cb 2020-01-15 tracey #include <regex.h>
27 2c251c14 2020-01-15 tracey #include <stdarg.h>
28 2c251c14 2020-01-15 tracey #include <stdint.h>
29 2c251c14 2020-01-15 tracey #include <stdio.h>
30 2c251c14 2020-01-15 tracey #include <stdlib.h>
31 2c251c14 2020-01-15 tracey #include <string.h>
32 2c251c14 2020-01-15 tracey #include <unistd.h>
33 2c251c14 2020-01-15 tracey
34 2c251c14 2020-01-15 tracey #include <got_object.h>
35 2c251c14 2020-01-15 tracey #include <got_reference.h>
36 2c251c14 2020-01-15 tracey #include <got_repository.h>
37 2c251c14 2020-01-15 tracey #include <got_path.h>
38 2c251c14 2020-01-15 tracey #include <got_cancel.h>
39 2c251c14 2020-01-15 tracey #include <got_worktree.h>
40 2c251c14 2020-01-15 tracey #include <got_diff.h>
41 2c251c14 2020-01-15 tracey #include <got_commit_graph.h>
42 2c251c14 2020-01-15 tracey #include <got_blame.h>
43 2c251c14 2020-01-15 tracey #include <got_privsep.h>
44 2c251c14 2020-01-15 tracey #include <got_opentemp.h>
45 2c251c14 2020-01-15 tracey
46 2c251c14 2020-01-15 tracey #include <kcgi.h>
47 2c251c14 2020-01-15 tracey #include <kcgihtml.h>
48 2c251c14 2020-01-15 tracey
49 2c251c14 2020-01-15 tracey #include "gotweb.h"
50 2c251c14 2020-01-15 tracey
51 2c251c14 2020-01-15 tracey #ifndef nitems
52 2c251c14 2020-01-15 tracey #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 2c251c14 2020-01-15 tracey #endif
54 2c251c14 2020-01-15 tracey
55 54415d85 2020-01-15 tracey struct gw_trans {
56 f2f46662 2020-01-23 tracey TAILQ_HEAD(headers, gw_header) gw_headers;
57 f2f46662 2020-01-23 tracey TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 deac617c 2020-02-14 tracey struct got_repository *repo;
59 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir;
60 2c251c14 2020-01-15 tracey struct gotweb_conf *gw_conf;
61 2c251c14 2020-01-15 tracey struct ktemplate *gw_tmpl;
62 2c251c14 2020-01-15 tracey struct khtmlreq *gw_html_req;
63 2c251c14 2020-01-15 tracey struct kreq *gw_req;
64 f1200fe3 2020-02-13 tracey const struct got_error *error;
65 f652ec0c 2020-02-13 stsp const char *repo_name;
66 2c251c14 2020-01-15 tracey char *repo_path;
67 56997cd3 2020-02-14 tracey char *commit_id;
68 4d6abe2b 2020-02-13 stsp const char *repo_file;
69 ec46ccd7 2020-01-15 tracey char *repo_folder;
70 742ba378 2020-02-14 stsp const char *headref;
71 2c251c14 2020-01-15 tracey unsigned int action;
72 2c251c14 2020-01-15 tracey unsigned int page;
73 2c251c14 2020-01-15 tracey unsigned int repos_total;
74 387a29ba 2020-01-15 tracey enum kmime mime;
75 2c251c14 2020-01-15 tracey };
76 2c251c14 2020-01-15 tracey
77 f2f46662 2020-01-23 tracey struct gw_header {
78 f2f46662 2020-01-23 tracey TAILQ_ENTRY(gw_header) entry;
79 f2f46662 2020-01-23 tracey struct got_reflist_head refs;
80 f2f46662 2020-01-23 tracey char *path;
81 f2f46662 2020-01-23 tracey
82 f2f46662 2020-01-23 tracey char *refs_str;
83 f2f46662 2020-01-23 tracey char *commit_id; /* id_str1 */
84 f2f46662 2020-01-23 tracey char *parent_id; /* id_str2 */
85 f2f46662 2020-01-23 tracey char *tree_id;
86 4e8d6e3e 2020-02-14 tracey char *author;
87 4e8d6e3e 2020-02-14 tracey char *committer;
88 f2f46662 2020-01-23 tracey char *commit_msg;
89 f2f46662 2020-01-23 tracey time_t committer_time;
90 2c251c14 2020-01-15 tracey };
91 2c251c14 2020-01-15 tracey
92 2c251c14 2020-01-15 tracey struct gw_dir {
93 2c251c14 2020-01-15 tracey TAILQ_ENTRY(gw_dir) entry;
94 2c251c14 2020-01-15 tracey char *name;
95 2c251c14 2020-01-15 tracey char *owner;
96 2c251c14 2020-01-15 tracey char *description;
97 2c251c14 2020-01-15 tracey char *url;
98 2c251c14 2020-01-15 tracey char *age;
99 2c251c14 2020-01-15 tracey char *path;
100 2c251c14 2020-01-15 tracey };
101 2c251c14 2020-01-15 tracey
102 f2f46662 2020-01-23 tracey enum gw_key {
103 f2f46662 2020-01-23 tracey KEY_ACTION,
104 f2f46662 2020-01-23 tracey KEY_COMMIT_ID,
105 f2f46662 2020-01-23 tracey KEY_FILE,
106 f2f46662 2020-01-23 tracey KEY_FOLDER,
107 f2f46662 2020-01-23 tracey KEY_HEADREF,
108 f2f46662 2020-01-23 tracey KEY_PAGE,
109 f2f46662 2020-01-23 tracey KEY_PATH,
110 f2f46662 2020-01-23 tracey KEY__ZMAX
111 f2f46662 2020-01-23 tracey };
112 f2f46662 2020-01-23 tracey
113 54415d85 2020-01-15 tracey enum gw_tmpl {
114 f2f46662 2020-01-23 tracey TEMPL_CONTENT,
115 2c251c14 2020-01-15 tracey TEMPL_HEAD,
116 2c251c14 2020-01-15 tracey TEMPL_HEADER,
117 f2f46662 2020-01-23 tracey TEMPL_SEARCH,
118 2c251c14 2020-01-15 tracey TEMPL_SITEPATH,
119 2c251c14 2020-01-15 tracey TEMPL_SITEOWNER,
120 2c251c14 2020-01-15 tracey TEMPL_TITLE,
121 2c251c14 2020-01-15 tracey TEMPL__MAX
122 2c251c14 2020-01-15 tracey };
123 2c251c14 2020-01-15 tracey
124 54415d85 2020-01-15 tracey enum gw_ref_tm {
125 387a29ba 2020-01-15 tracey TM_DIFF,
126 387a29ba 2020-01-15 tracey TM_LONG,
127 387a29ba 2020-01-15 tracey };
128 387a29ba 2020-01-15 tracey
129 54415d85 2020-01-15 tracey enum gw_tags {
130 87f9ebf5 2020-01-15 tracey TAGBRIEF,
131 87f9ebf5 2020-01-15 tracey TAGFULL,
132 87f9ebf5 2020-01-15 tracey };
133 87f9ebf5 2020-01-15 tracey
134 54415d85 2020-01-15 tracey static const char *const gw_templs[TEMPL__MAX] = {
135 f2f46662 2020-01-23 tracey "content",
136 2c251c14 2020-01-15 tracey "head",
137 2c251c14 2020-01-15 tracey "header",
138 f2f46662 2020-01-23 tracey "search",
139 2c251c14 2020-01-15 tracey "sitepath",
140 2c251c14 2020-01-15 tracey "siteowner",
141 2c251c14 2020-01-15 tracey "title",
142 2c251c14 2020-01-15 tracey };
143 2c251c14 2020-01-15 tracey
144 ec46ccd7 2020-01-15 tracey static const struct kvalid gw_keys[KEY__ZMAX] = {
145 2c251c14 2020-01-15 tracey { kvalid_stringne, "action" },
146 2c251c14 2020-01-15 tracey { kvalid_stringne, "commit" },
147 2c251c14 2020-01-15 tracey { kvalid_stringne, "file" },
148 ec46ccd7 2020-01-15 tracey { kvalid_stringne, "folder" },
149 8087c3c5 2020-01-15 tracey { kvalid_stringne, "headref" },
150 ec46ccd7 2020-01-15 tracey { kvalid_int, "page" },
151 ec46ccd7 2020-01-15 tracey { kvalid_stringne, "path" },
152 2c251c14 2020-01-15 tracey };
153 2c251c14 2020-01-15 tracey
154 f2f46662 2020-01-23 tracey static struct gw_header *gw_init_header(void);
155 2c251c14 2020-01-15 tracey
156 b0a1bc86 2020-02-14 stsp static void gw_free_header(struct gw_header *);
157 00f13350 2020-02-10 tracey
158 00f13350 2020-02-10 tracey static int gw_template(size_t, void *);
159 00f13350 2020-02-10 tracey
160 f1200fe3 2020-02-13 tracey static const struct got_error *gw_error(struct gw_trans *);
161 f1200fe3 2020-02-13 tracey static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
162 185ba3ba 2020-02-04 tracey static const struct got_error *gw_get_repo_description(char **,
163 185ba3ba 2020-02-04 tracey struct gw_trans *, char *);
164 9a1cc63f 2020-02-03 stsp static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
165 2c251c14 2020-01-15 tracey char *);
166 55ccf528 2020-02-03 stsp static const struct got_error *gw_get_time_str(char **, time_t, int);
167 d126c1b7 2020-01-29 stsp static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
168 2011c142 2020-02-14 stsp char *, const char *, int);
169 9a9d12ca 2020-02-06 tracey static const struct got_error *gw_output_file_blame(struct gw_trans *);
170 e0857cfe 2020-02-06 tracey static const struct got_error *gw_output_blob_buf(struct gw_trans *);
171 626b25b6 2020-02-06 tracey static const struct got_error *gw_output_repo_tree(struct gw_trans *);
172 38b240fb 2020-02-06 tracey static const struct got_error *gw_output_diff(struct gw_trans *,
173 f2f46662 2020-01-23 tracey struct gw_header *);
174 38b240fb 2020-02-06 tracey static const struct got_error *gw_output_repo_tags(struct gw_trans *,
175 4ff7391f 2020-01-28 tracey struct gw_header *, int, int);
176 9eed6f10 2020-02-08 tracey static const struct got_error *gw_output_repo_heads(struct gw_trans *);
177 4ae179a2 2020-02-08 tracey static const struct got_error *gw_output_site_link(struct gw_trans *);
178 185ba3ba 2020-02-04 tracey static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
179 185ba3ba 2020-02-04 tracey char *);
180 f7cc9480 2020-02-05 tracey static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
181 2c251c14 2020-01-15 tracey
182 21a55cc7 2020-02-04 tracey static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
183 21a55cc7 2020-02-04 tracey char*);
184 f7cc9480 2020-02-05 tracey static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
185 f7cc9480 2020-02-05 tracey char*);
186 21a55cc7 2020-02-04 tracey static const struct got_error *gw_gen_author_header(struct gw_trans *,
187 21a55cc7 2020-02-04 tracey const char *);
188 21a55cc7 2020-02-04 tracey static const struct got_error *gw_gen_age_header(struct gw_trans *,
189 21a55cc7 2020-02-04 tracey const char *);
190 21a55cc7 2020-02-04 tracey static const struct got_error *gw_gen_committer_header(struct gw_trans *,
191 21a55cc7 2020-02-04 tracey const char *);
192 f7cc9480 2020-02-05 tracey static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
193 f7cc9480 2020-02-05 tracey char *);
194 f7cc9480 2020-02-05 tracey static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
195 00f13350 2020-02-10 tracey static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
196 2c251c14 2020-01-15 tracey enum kmime);
197 00f13350 2020-02-10 tracey static const struct got_error *gw_display_index(struct gw_trans *);
198 00f13350 2020-02-10 tracey static const struct got_error *gw_get_header(struct gw_trans *,
199 f2f46662 2020-01-23 tracey struct gw_header *, int);
200 00f13350 2020-02-10 tracey static const struct got_error *gw_get_commits(struct gw_trans *,
201 3c245a0e 2020-02-14 tracey struct gw_header *, int,
202 3c245a0e 2020-02-14 tracey struct got_object_id *);
203 00f13350 2020-02-10 tracey static const struct got_error *gw_get_commit(struct gw_trans *,
204 4e8d6e3e 2020-02-14 tracey struct gw_header *,
205 4e8d6e3e 2020-02-14 tracey struct got_commit_object *,
206 3c245a0e 2020-02-14 tracey struct got_object_id *);
207 d4159696 2020-02-13 stsp static const struct got_error *gw_apply_unveil(const char *);
208 00f13350 2020-02-10 tracey static const struct got_error *gw_blame_cb(void *, int, int,
209 ec46ccd7 2020-01-15 tracey struct got_object_id *);
210 00f13350 2020-02-10 tracey static const struct got_error *gw_load_got_paths(struct gw_trans *);
211 00f13350 2020-02-10 tracey static const struct got_error *gw_load_got_path(struct gw_trans *,
212 2c251c14 2020-01-15 tracey struct gw_dir *);
213 00f13350 2020-02-10 tracey static const struct got_error *gw_parse_querystring(struct gw_trans *);
214 00f13350 2020-02-10 tracey static const struct got_error *gw_blame(struct gw_trans *);
215 00f13350 2020-02-10 tracey static const struct got_error *gw_blob(struct gw_trans *);
216 00f13350 2020-02-10 tracey static const struct got_error *gw_diff(struct gw_trans *);
217 00f13350 2020-02-10 tracey static const struct got_error *gw_index(struct gw_trans *);
218 00f13350 2020-02-10 tracey static const struct got_error *gw_commits(struct gw_trans *);
219 00f13350 2020-02-10 tracey static const struct got_error *gw_briefs(struct gw_trans *);
220 00f13350 2020-02-10 tracey static const struct got_error *gw_summary(struct gw_trans *);
221 00f13350 2020-02-10 tracey static const struct got_error *gw_tree(struct gw_trans *);
222 00f13350 2020-02-10 tracey static const struct got_error *gw_tag(struct gw_trans *);
223 2c251c14 2020-01-15 tracey
224 2c251c14 2020-01-15 tracey struct gw_query_action {
225 2c251c14 2020-01-15 tracey unsigned int func_id;
226 2c251c14 2020-01-15 tracey const char *func_name;
227 54415d85 2020-01-15 tracey const struct got_error *(*func_main)(struct gw_trans *);
228 2c251c14 2020-01-15 tracey char *template;
229 2c251c14 2020-01-15 tracey };
230 2c251c14 2020-01-15 tracey
231 2c251c14 2020-01-15 tracey enum gw_query_actions {
232 2c251c14 2020-01-15 tracey GW_BLAME,
233 e46f587c 2020-01-29 tracey GW_BLOB,
234 f2f46662 2020-01-23 tracey GW_BRIEFS,
235 f2f46662 2020-01-23 tracey GW_COMMITS,
236 f2f46662 2020-01-23 tracey GW_DIFF,
237 2c251c14 2020-01-15 tracey GW_ERR,
238 2c251c14 2020-01-15 tracey GW_INDEX,
239 2c251c14 2020-01-15 tracey GW_SUMMARY,
240 4ff7391f 2020-01-28 tracey GW_TAG,
241 b772de24 2020-01-15 tracey GW_TREE,
242 2c251c14 2020-01-15 tracey };
243 2c251c14 2020-01-15 tracey
244 2c251c14 2020-01-15 tracey static struct gw_query_action gw_query_funcs[] = {
245 f2f46662 2020-01-23 tracey { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
246 017d6da3 2020-01-29 tracey { GW_BLOB, "blob", NULL, NULL },
247 f2f46662 2020-01-23 tracey { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
248 f2f46662 2020-01-23 tracey { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
249 f2f46662 2020-01-23 tracey { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
250 f1200fe3 2020-02-13 tracey { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
251 f2f46662 2020-01-23 tracey { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
252 f2f46662 2020-01-23 tracey { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
253 4ff7391f 2020-01-28 tracey { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
254 f2f46662 2020-01-23 tracey { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
255 2c251c14 2020-01-15 tracey };
256 5d1296de 2020-02-13 stsp
257 5d1296de 2020-02-13 stsp static const char *
258 5d1296de 2020-02-13 stsp gw_get_action_name(struct gw_trans *gw_trans)
259 5d1296de 2020-02-13 stsp {
260 5d1296de 2020-02-13 stsp return gw_query_funcs[gw_trans->action].func_name;
261 5d1296de 2020-02-13 stsp }
262 c25c2314 2020-01-28 stsp
263 c25c2314 2020-01-28 stsp static const struct got_error *
264 c25c2314 2020-01-28 stsp gw_kcgi_error(enum kcgi_err kerr)
265 c25c2314 2020-01-28 stsp {
266 c25c2314 2020-01-28 stsp if (kerr == KCGI_OK)
267 c25c2314 2020-01-28 stsp return NULL;
268 c25c2314 2020-01-28 stsp
269 c25c2314 2020-01-28 stsp if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
270 c25c2314 2020-01-28 stsp return got_error(GOT_ERR_CANCELLED);
271 c25c2314 2020-01-28 stsp
272 c25c2314 2020-01-28 stsp if (kerr == KCGI_ENOMEM)
273 f7cc9480 2020-02-05 tracey return got_error_set_errno(ENOMEM,
274 7afec891 2020-02-06 stsp kcgi_strerror(kerr));
275 2c251c14 2020-01-15 tracey
276 c25c2314 2020-01-28 stsp if (kerr == KCGI_ENFILE)
277 f7cc9480 2020-02-05 tracey return got_error_set_errno(ENFILE,
278 7afec891 2020-02-06 stsp kcgi_strerror(kerr));
279 c25c2314 2020-01-28 stsp
280 c25c2314 2020-01-28 stsp if (kerr == KCGI_EAGAIN)
281 f7cc9480 2020-02-05 tracey return got_error_set_errno(EAGAIN,
282 7afec891 2020-02-06 stsp kcgi_strerror(kerr));
283 c25c2314 2020-01-28 stsp
284 c25c2314 2020-01-28 stsp if (kerr == KCGI_FORM)
285 f7cc9480 2020-02-05 tracey return got_error_msg(GOT_ERR_IO,
286 7afec891 2020-02-06 stsp kcgi_strerror(kerr));
287 c25c2314 2020-01-28 stsp
288 7afec891 2020-02-06 stsp return got_error_from_errno(kcgi_strerror(kerr));
289 c25c2314 2020-01-28 stsp }
290 c25c2314 2020-01-28 stsp
291 2c251c14 2020-01-15 tracey static const struct got_error *
292 d4159696 2020-02-13 stsp gw_apply_unveil(const char *repo_path)
293 2c251c14 2020-01-15 tracey {
294 2c251c14 2020-01-15 tracey const struct got_error *err;
295 2c251c14 2020-01-15 tracey
296 2c251c14 2020-01-15 tracey if (repo_path && unveil(repo_path, "r") != 0)
297 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", repo_path);
298 2c251c14 2020-01-15 tracey
299 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
300 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
301 2c251c14 2020-01-15 tracey
302 2c251c14 2020-01-15 tracey err = got_privsep_unveil_exec_helpers();
303 2c251c14 2020-01-15 tracey if (err != NULL)
304 2c251c14 2020-01-15 tracey return err;
305 2c251c14 2020-01-15 tracey
306 2c251c14 2020-01-15 tracey if (unveil(NULL, NULL) != 0)
307 2c251c14 2020-01-15 tracey return got_error_from_errno("unveil");
308 65b95fb2 2020-01-15 tracey
309 32cd4d18 2020-02-03 stsp return NULL;
310 5894d523 2020-02-03 stsp }
311 5894d523 2020-02-03 stsp
312 5894d523 2020-02-03 stsp static int
313 e0857cfe 2020-02-06 tracey isbinary(const uint8_t *buf, size_t n)
314 5894d523 2020-02-03 stsp {
315 e0857cfe 2020-02-06 tracey size_t i;
316 e0857cfe 2020-02-06 tracey
317 e0857cfe 2020-02-06 tracey for (i = 0; i < n; i++)
318 e0857cfe 2020-02-06 tracey if (buf[i] == 0)
319 e0857cfe 2020-02-06 tracey return 1;
320 e0857cfe 2020-02-06 tracey return 0;
321 32cd4d18 2020-02-03 stsp }
322 5894d523 2020-02-03 stsp
323 32cd4d18 2020-02-03 stsp static const struct got_error *
324 54415d85 2020-01-15 tracey gw_blame(struct gw_trans *gw_trans)
325 2c251c14 2020-01-15 tracey {
326 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
327 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
328 78a9dab5 2020-02-07 tracey char *age = NULL;
329 1b33afc0 2020-02-14 tracey enum kcgi_err kerr = KCGI_OK;
330 2c251c14 2020-01-15 tracey
331 6bee13de 2020-01-16 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
332 f2f46662 2020-01-23 tracey NULL) == -1)
333 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
334 6bee13de 2020-01-16 tracey
335 f2f46662 2020-01-23 tracey if ((header = gw_init_header()) == NULL)
336 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
337 f2f46662 2020-01-23 tracey
338 d4159696 2020-02-13 stsp error = gw_apply_unveil(gw_trans->gw_dir->path);
339 ec46ccd7 2020-01-15 tracey if (error)
340 5ddf0079 2020-01-29 stsp goto done;
341 ec46ccd7 2020-01-15 tracey
342 1b33afc0 2020-02-14 tracey /* check querystring */
343 1b33afc0 2020-02-14 tracey if (gw_trans->repo_file == NULL) {
344 1b33afc0 2020-02-14 tracey error = got_error_msg(GOT_ERR_QUERYSTRING,
345 1b33afc0 2020-02-14 tracey "file required in querystring");
346 1b33afc0 2020-02-14 tracey goto done;
347 1b33afc0 2020-02-14 tracey }
348 1b33afc0 2020-02-14 tracey if (gw_trans->commit_id == NULL) {
349 1b33afc0 2020-02-14 tracey error = got_error_msg(GOT_ERR_QUERYSTRING,
350 1b33afc0 2020-02-14 tracey "commit required in querystring");
351 1b33afc0 2020-02-14 tracey goto done;
352 1b33afc0 2020-02-14 tracey }
353 1b33afc0 2020-02-14 tracey
354 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
355 f2f46662 2020-01-23 tracey if (error)
356 5ddf0079 2020-01-29 stsp goto done;
357 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
358 9a9d12ca 2020-02-06 tracey "blame_header_wrapper", KATTR__MAX);
359 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
360 9a9d12ca 2020-02-06 tracey goto done;
361 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
362 9a9d12ca 2020-02-06 tracey "blame_header", KATTR__MAX);
363 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
364 9a9d12ca 2020-02-06 tracey goto done;
365 9a9d12ca 2020-02-06 tracey error = gw_get_time_str(&age, header->committer_time,
366 9a9d12ca 2020-02-06 tracey TM_LONG);
367 a81f3554 2020-02-03 stsp if (error)
368 a81f3554 2020-02-03 stsp goto done;
369 9a9d12ca 2020-02-06 tracey error = gw_gen_age_header(gw_trans, age ?age : "");
370 55ccf528 2020-02-03 stsp if (error)
371 55ccf528 2020-02-03 stsp goto done;
372 9a9d12ca 2020-02-06 tracey error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
373 9a9d12ca 2020-02-06 tracey if (error)
374 6d9fc692 2020-01-28 stsp goto done;
375 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
376 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
377 6d9fc692 2020-01-28 stsp goto done;
378 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
379 9a9d12ca 2020-02-06 tracey "dotted_line", KATTR__MAX);
380 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
381 9a9d12ca 2020-02-06 tracey goto done;
382 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
383 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
384 9a9d12ca 2020-02-06 tracey goto done;
385 9a9d12ca 2020-02-06 tracey
386 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
387 9a9d12ca 2020-02-06 tracey "blame", KATTR__MAX);
388 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
389 9a9d12ca 2020-02-06 tracey goto done;
390 9a9d12ca 2020-02-06 tracey error = gw_output_file_blame(gw_trans);
391 9a9d12ca 2020-02-06 tracey if (error)
392 9a9d12ca 2020-02-06 tracey goto done;
393 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
394 6d9fc692 2020-01-28 stsp done:
395 b0a1bc86 2020-02-14 stsp gw_free_header(header);
396 9a9d12ca 2020-02-06 tracey if (error == NULL && kerr != KCGI_OK)
397 9a9d12ca 2020-02-06 tracey error = gw_kcgi_error(kerr);
398 2c251c14 2020-01-15 tracey return error;
399 2c251c14 2020-01-15 tracey }
400 2c251c14 2020-01-15 tracey
401 2c251c14 2020-01-15 tracey static const struct got_error *
402 e46f587c 2020-01-29 tracey gw_blob(struct gw_trans *gw_trans)
403 e46f587c 2020-01-29 tracey {
404 3b7fdcf4 2020-02-14 tracey const struct got_error *error = NULL, *err = NULL;
405 e46f587c 2020-01-29 tracey struct gw_header *header = NULL;
406 3b7fdcf4 2020-02-14 tracey enum kcgi_err kerr = KCGI_OK;
407 e46f587c 2020-01-29 tracey
408 e46f587c 2020-01-29 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
409 e46f587c 2020-01-29 tracey NULL) == -1)
410 e46f587c 2020-01-29 tracey return got_error_from_errno("pledge");
411 e46f587c 2020-01-29 tracey
412 e46f587c 2020-01-29 tracey if ((header = gw_init_header()) == NULL)
413 e46f587c 2020-01-29 tracey return got_error_from_errno("malloc");
414 e46f587c 2020-01-29 tracey
415 d4159696 2020-02-13 stsp error = gw_apply_unveil(gw_trans->gw_dir->path);
416 e46f587c 2020-01-29 tracey if (error)
417 e46f587c 2020-01-29 tracey goto done;
418 e46f587c 2020-01-29 tracey
419 3b7fdcf4 2020-02-14 tracey /* check querystring */
420 3b7fdcf4 2020-02-14 tracey if (gw_trans->repo_file == NULL) {
421 3b7fdcf4 2020-02-14 tracey error = got_error_msg(GOT_ERR_QUERYSTRING,
422 3b7fdcf4 2020-02-14 tracey "file required in querystring");
423 3b7fdcf4 2020-02-14 tracey goto done;
424 3b7fdcf4 2020-02-14 tracey }
425 3b7fdcf4 2020-02-14 tracey if (gw_trans->commit_id == NULL) {
426 3b7fdcf4 2020-02-14 tracey error = got_error_msg(GOT_ERR_QUERYSTRING,
427 3b7fdcf4 2020-02-14 tracey "commit required in querystring");
428 3b7fdcf4 2020-02-14 tracey goto done;
429 3b7fdcf4 2020-02-14 tracey }
430 e46f587c 2020-01-29 tracey error = gw_get_header(gw_trans, header, 1);
431 e46f587c 2020-01-29 tracey if (error)
432 e46f587c 2020-01-29 tracey goto done;
433 e46f587c 2020-01-29 tracey
434 e0857cfe 2020-02-06 tracey error = gw_output_blob_buf(gw_trans);
435 e46f587c 2020-01-29 tracey done:
436 3b7fdcf4 2020-02-14 tracey
437 3b7fdcf4 2020-02-14 tracey if (error) {
438 3b7fdcf4 2020-02-14 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
439 3b7fdcf4 2020-02-14 tracey err = gw_display_index(gw_trans);
440 3b7fdcf4 2020-02-14 tracey if (err) {
441 3b7fdcf4 2020-02-14 tracey error = err;
442 3b7fdcf4 2020-02-14 tracey goto errored;
443 3b7fdcf4 2020-02-14 tracey }
444 3b7fdcf4 2020-02-14 tracey kerr = khttp_puts(gw_trans->gw_req, error->msg);
445 3b7fdcf4 2020-02-14 tracey }
446 3b7fdcf4 2020-02-14 tracey errored:
447 b0a1bc86 2020-02-14 stsp gw_free_header(header);
448 3b7fdcf4 2020-02-14 tracey if (error == NULL && kerr != KCGI_OK)
449 3b7fdcf4 2020-02-14 tracey error = gw_kcgi_error(kerr);
450 e46f587c 2020-01-29 tracey return error;
451 e46f587c 2020-01-29 tracey }
452 e46f587c 2020-01-29 tracey
453 e46f587c 2020-01-29 tracey static const struct got_error *
454 f2f46662 2020-01-23 tracey gw_diff(struct gw_trans *gw_trans)
455 2c251c14 2020-01-15 tracey {
456 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
457 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
458 78a9dab5 2020-02-07 tracey char *age = NULL;
459 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
460 2c251c14 2020-01-15 tracey
461 f2f46662 2020-01-23 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
462 f2f46662 2020-01-23 tracey NULL) == -1)
463 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
464 6bee13de 2020-01-16 tracey
465 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
466 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
467 f2f46662 2020-01-23 tracey
468 d4159696 2020-02-13 stsp error = gw_apply_unveil(gw_trans->gw_dir->path);
469 8087c3c5 2020-01-15 tracey if (error)
470 390d412c 2020-01-28 stsp goto done;
471 8087c3c5 2020-01-15 tracey
472 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
473 f2f46662 2020-01-23 tracey if (error)
474 55ccf528 2020-02-03 stsp goto done;
475 f7cc9480 2020-02-05 tracey
476 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
477 f7cc9480 2020-02-05 tracey "diff_header_wrapper", KATTR__MAX);
478 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
479 f7cc9480 2020-02-05 tracey goto done;
480 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
481 f7cc9480 2020-02-05 tracey "diff_header", KATTR__MAX);
482 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
483 f7cc9480 2020-02-05 tracey goto done;
484 f7cc9480 2020-02-05 tracey error = gw_gen_diff_header(gw_trans, header->parent_id,
485 f7cc9480 2020-02-05 tracey header->commit_id);
486 f7cc9480 2020-02-05 tracey if (error)
487 f7cc9480 2020-02-05 tracey goto done;
488 f7cc9480 2020-02-05 tracey error = gw_gen_commit_header(gw_trans, header->commit_id,
489 f7cc9480 2020-02-05 tracey header->refs_str);
490 f7cc9480 2020-02-05 tracey if (error)
491 f7cc9480 2020-02-05 tracey goto done;
492 f7cc9480 2020-02-05 tracey error = gw_gen_tree_header(gw_trans, header->tree_id);
493 f7cc9480 2020-02-05 tracey if (error)
494 f7cc9480 2020-02-05 tracey goto done;
495 f7cc9480 2020-02-05 tracey error = gw_gen_author_header(gw_trans, header->author);
496 f7cc9480 2020-02-05 tracey if (error)
497 f7cc9480 2020-02-05 tracey goto done;
498 f7cc9480 2020-02-05 tracey error = gw_gen_committer_header(gw_trans, header->author);
499 f7cc9480 2020-02-05 tracey if (error)
500 f7cc9480 2020-02-05 tracey goto done;
501 f7cc9480 2020-02-05 tracey error = gw_get_time_str(&age, header->committer_time,
502 f7cc9480 2020-02-05 tracey TM_LONG);
503 f7cc9480 2020-02-05 tracey if (error)
504 f7cc9480 2020-02-05 tracey goto done;
505 f7cc9480 2020-02-05 tracey error = gw_gen_age_header(gw_trans, age ?age : "");
506 070fee27 2020-02-03 stsp if (error)
507 070fee27 2020-02-03 stsp goto done;
508 f7cc9480 2020-02-05 tracey error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
509 f7cc9480 2020-02-05 tracey if (error)
510 390d412c 2020-01-28 stsp goto done;
511 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
512 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
513 390d412c 2020-01-28 stsp goto done;
514 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
515 f7cc9480 2020-02-05 tracey "dotted_line", KATTR__MAX);
516 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
517 f7cc9480 2020-02-05 tracey goto done;
518 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
519 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
520 f7cc9480 2020-02-05 tracey goto done;
521 87f9ebf5 2020-01-15 tracey
522 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
523 f7cc9480 2020-02-05 tracey "diff", KATTR__MAX);
524 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
525 f7cc9480 2020-02-05 tracey goto done;
526 38b240fb 2020-02-06 tracey error = gw_output_diff(gw_trans, header);
527 f7cc9480 2020-02-05 tracey if (error)
528 f7cc9480 2020-02-05 tracey goto done;
529 f7cc9480 2020-02-05 tracey
530 b4fba448 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
531 390d412c 2020-01-28 stsp done:
532 b0a1bc86 2020-02-14 stsp gw_free_header(header);
533 55ccf528 2020-02-03 stsp free(age);
534 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
535 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
536 2204c934 2020-01-15 tracey return error;
537 2204c934 2020-01-15 tracey }
538 2204c934 2020-01-15 tracey
539 2204c934 2020-01-15 tracey static const struct got_error *
540 54415d85 2020-01-15 tracey gw_index(struct gw_trans *gw_trans)
541 2c251c14 2020-01-15 tracey {
542 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
543 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir = NULL;
544 b3f1f953 2020-02-09 tracey char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
545 b3f1f953 2020-02-09 tracey char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
546 387a29ba 2020-01-15 tracey unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
547 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
548 2c251c14 2020-01-15 tracey
549 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
550 6bee13de 2020-01-16 tracey NULL) == -1) {
551 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
552 6bee13de 2020-01-16 tracey return error;
553 6bee13de 2020-01-16 tracey }
554 6bee13de 2020-01-16 tracey
555 d4159696 2020-02-13 stsp error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
556 46b9c89b 2020-01-15 tracey if (error)
557 46b9c89b 2020-01-15 tracey return error;
558 46b9c89b 2020-01-15 tracey
559 2c251c14 2020-01-15 tracey error = gw_load_got_paths(gw_trans);
560 46b9c89b 2020-01-15 tracey if (error)
561 2c251c14 2020-01-15 tracey return error;
562 2c251c14 2020-01-15 tracey
563 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
564 b3f1f953 2020-02-09 tracey "index_header", KATTR__MAX);
565 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
566 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
567 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
568 b3f1f953 2020-02-09 tracey "index_header_project", KATTR__MAX);
569 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
570 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
571 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Project");
572 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
573 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
574 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
575 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
576 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
577 2c251c14 2020-01-15 tracey
578 b3f1f953 2020-02-09 tracey if (gw_trans->gw_conf->got_show_repo_description) {
579 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
580 b3f1f953 2020-02-09 tracey "index_header_description", KATTR__MAX);
581 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
582 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
583 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Description");
584 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
585 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
586 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
587 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
588 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
589 b3f1f953 2020-02-09 tracey }
590 b3f1f953 2020-02-09 tracey
591 b3f1f953 2020-02-09 tracey if (gw_trans->gw_conf->got_show_repo_owner) {
592 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
593 b3f1f953 2020-02-09 tracey "index_header_owner", KATTR__MAX);
594 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
595 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
596 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
597 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
598 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
599 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
600 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
601 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
602 b3f1f953 2020-02-09 tracey }
603 b3f1f953 2020-02-09 tracey
604 b3f1f953 2020-02-09 tracey if (gw_trans->gw_conf->got_show_repo_age) {
605 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
606 b3f1f953 2020-02-09 tracey "index_header_age", KATTR__MAX);
607 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
608 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
609 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
610 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
611 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
612 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
613 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
614 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
615 b3f1f953 2020-02-09 tracey }
616 b3f1f953 2020-02-09 tracey
617 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
618 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
619 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
620 b3f1f953 2020-02-09 tracey
621 b3f1f953 2020-02-09 tracey if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
622 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
623 b3f1f953 2020-02-09 tracey "index_wrapper", KATTR__MAX);
624 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
625 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
626 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req,
627 b3f1f953 2020-02-09 tracey "No repositories found in ");
628 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
629 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
630 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req,
631 b3f1f953 2020-02-09 tracey gw_trans->gw_conf->got_repos_path);
632 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
633 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
634 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
635 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
636 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
637 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
638 b3f1f953 2020-02-09 tracey "dotted_line", KATTR__MAX);
639 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
640 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
641 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
642 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
643 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
644 c25c2314 2020-01-28 stsp return error;
645 bce5dac1 2020-01-28 stsp }
646 bce5dac1 2020-01-28 stsp
647 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
648 2c251c14 2020-01-15 tracey dir_c++;
649 2c251c14 2020-01-15 tracey
650 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
651 2c251c14 2020-01-15 tracey if (gw_trans->page > 0 && (gw_trans->page *
652 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
653 2c251c14 2020-01-15 tracey prev_disp++;
654 2c251c14 2020-01-15 tracey continue;
655 2c251c14 2020-01-15 tracey }
656 2c251c14 2020-01-15 tracey
657 2c251c14 2020-01-15 tracey prev_disp++;
658 f2f46662 2020-01-23 tracey
659 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
660 b3f1f953 2020-02-09 tracey "index_wrapper", KATTR__MAX);
661 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
662 a02d5f81 2020-02-13 stsp goto done;
663 b3f1f953 2020-02-09 tracey
664 b3f1f953 2020-02-09 tracey if (asprintf(&href_summary, "?path=%s&action=summary",
665 a02d5f81 2020-02-13 stsp gw_dir->name) == -1) {
666 a02d5f81 2020-02-13 stsp error = got_error_from_errno("asprintf");
667 a02d5f81 2020-02-13 stsp goto done;
668 a02d5f81 2020-02-13 stsp }
669 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
670 b3f1f953 2020-02-09 tracey "index_project", KATTR__MAX);
671 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
672 b3f1f953 2020-02-09 tracey goto done;
673 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
674 b3f1f953 2020-02-09 tracey href_summary, KATTR__MAX);
675 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
676 b3f1f953 2020-02-09 tracey goto done;
677 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
678 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
679 b3f1f953 2020-02-09 tracey goto done;
680 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
681 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
682 b3f1f953 2020-02-09 tracey goto done;
683 b3f1f953 2020-02-09 tracey if (gw_trans->gw_conf->got_show_repo_description) {
684 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
685 b3f1f953 2020-02-09 tracey KATTR_ID, "index_project_description", KATTR__MAX);
686 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
687 b3f1f953 2020-02-09 tracey goto done;
688 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req,
689 b3f1f953 2020-02-09 tracey gw_dir->description ? gw_dir->description : "");
690 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
691 b3f1f953 2020-02-09 tracey goto done;
692 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
693 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
694 b3f1f953 2020-02-09 tracey goto done;
695 b3f1f953 2020-02-09 tracey }
696 b3f1f953 2020-02-09 tracey if (gw_trans->gw_conf->got_show_repo_owner) {
697 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
698 b3f1f953 2020-02-09 tracey KATTR_ID, "index_project_owner", KATTR__MAX);
699 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
700 b3f1f953 2020-02-09 tracey goto done;
701 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req,
702 b3f1f953 2020-02-09 tracey gw_dir->owner ? gw_dir->owner : "");
703 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
704 b3f1f953 2020-02-09 tracey goto done;
705 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
706 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
707 b3f1f953 2020-02-09 tracey goto done;
708 b3f1f953 2020-02-09 tracey }
709 b3f1f953 2020-02-09 tracey if (gw_trans->gw_conf->got_show_repo_age) {
710 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
711 b3f1f953 2020-02-09 tracey KATTR_ID, "index_project_age", KATTR__MAX);
712 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
713 b3f1f953 2020-02-09 tracey goto done;
714 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req,
715 b3f1f953 2020-02-09 tracey gw_dir->age ? gw_dir->age : "");
716 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
717 b3f1f953 2020-02-09 tracey goto done;
718 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
719 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
720 b3f1f953 2020-02-09 tracey goto done;
721 b3f1f953 2020-02-09 tracey }
722 b3f1f953 2020-02-09 tracey
723 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
724 b3f1f953 2020-02-09 tracey "navs_wrapper", KATTR__MAX);
725 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
726 b3f1f953 2020-02-09 tracey goto done;
727 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
728 b3f1f953 2020-02-09 tracey "navs", KATTR__MAX);
729 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
730 b3f1f953 2020-02-09 tracey goto done;
731 b3f1f953 2020-02-09 tracey
732 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
733 b3f1f953 2020-02-09 tracey href_summary, KATTR__MAX);
734 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
735 b3f1f953 2020-02-09 tracey goto done;
736 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "summary");
737 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
738 b3f1f953 2020-02-09 tracey goto done;
739 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
740 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
741 b3f1f953 2020-02-09 tracey goto done;
742 2c251c14 2020-01-15 tracey
743 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
744 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
745 b3f1f953 2020-02-09 tracey goto done;
746 b3f1f953 2020-02-09 tracey
747 b3f1f953 2020-02-09 tracey if (asprintf(&href_briefs, "?path=%s&action=briefs",
748 b3f1f953 2020-02-09 tracey gw_dir->name) == -1) {
749 b3f1f953 2020-02-09 tracey error = got_error_from_errno("asprintf");
750 b3f1f953 2020-02-09 tracey goto done;
751 b3f1f953 2020-02-09 tracey }
752 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
753 b3f1f953 2020-02-09 tracey href_briefs, KATTR__MAX);
754 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
755 b3f1f953 2020-02-09 tracey goto done;
756 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
757 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
758 b3f1f953 2020-02-09 tracey goto done;
759 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
760 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
761 b3f1f953 2020-02-09 tracey error = gw_kcgi_error(kerr);
762 b3f1f953 2020-02-09 tracey
763 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
764 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
765 b3f1f953 2020-02-09 tracey goto done;
766 b3f1f953 2020-02-09 tracey
767 b3f1f953 2020-02-09 tracey if (asprintf(&href_commits, "?path=%s&action=commits",
768 b3f1f953 2020-02-09 tracey gw_dir->name) == -1) {
769 b3f1f953 2020-02-09 tracey error = got_error_from_errno("asprintf");
770 b3f1f953 2020-02-09 tracey goto done;
771 b3f1f953 2020-02-09 tracey }
772 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
773 b3f1f953 2020-02-09 tracey href_commits, KATTR__MAX);
774 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
775 b3f1f953 2020-02-09 tracey goto done;
776 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "commits");
777 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
778 b3f1f953 2020-02-09 tracey goto done;
779 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
780 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
781 b3f1f953 2020-02-09 tracey goto done;
782 b3f1f953 2020-02-09 tracey
783 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
784 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
785 b3f1f953 2020-02-09 tracey goto done;
786 b3f1f953 2020-02-09 tracey
787 b3f1f953 2020-02-09 tracey if (asprintf(&href_tree, "?path=%s&action=tree",
788 b3f1f953 2020-02-09 tracey gw_dir->name) == -1) {
789 b3f1f953 2020-02-09 tracey error = got_error_from_errno("asprintf");
790 b3f1f953 2020-02-09 tracey goto done;
791 b3f1f953 2020-02-09 tracey }
792 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
793 b3f1f953 2020-02-09 tracey href_tree, KATTR__MAX);
794 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
795 b3f1f953 2020-02-09 tracey goto done;
796 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "tree");
797 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
798 b3f1f953 2020-02-09 tracey goto done;
799 b3f1f953 2020-02-09 tracey
800 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
801 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
802 b3f1f953 2020-02-09 tracey goto done;
803 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
804 b3f1f953 2020-02-09 tracey "dotted_line", KATTR__MAX);
805 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
806 b3f1f953 2020-02-09 tracey goto done;
807 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
808 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
809 b3f1f953 2020-02-09 tracey goto done;
810 b3f1f953 2020-02-09 tracey
811 b3f1f953 2020-02-09 tracey free(href_summary);
812 b3f1f953 2020-02-09 tracey href_summary = NULL;
813 b3f1f953 2020-02-09 tracey free(href_briefs);
814 b3f1f953 2020-02-09 tracey href_briefs = NULL;
815 b3f1f953 2020-02-09 tracey free(href_commits);
816 b3f1f953 2020-02-09 tracey href_commits = NULL;
817 b3f1f953 2020-02-09 tracey free(href_tree);
818 b3f1f953 2020-02-09 tracey href_tree = NULL;
819 b3f1f953 2020-02-09 tracey
820 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display == 0)
821 2c251c14 2020-01-15 tracey continue;
822 2c251c14 2020-01-15 tracey
823 c25c2314 2020-01-28 stsp if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
824 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
825 63ee0dca 2020-02-08 tracey KATTR_ID, "np_wrapper", KATTR__MAX);
826 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
827 b3f1f953 2020-02-09 tracey goto done;
828 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
829 63ee0dca 2020-02-08 tracey KATTR_ID, "nav_prev", KATTR__MAX);
830 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
831 b3f1f953 2020-02-09 tracey goto done;
832 c25c2314 2020-01-28 stsp } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
833 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
834 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
835 c25c2314 2020-01-28 stsp prev_disp == gw_trans->repos_total)) {
836 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
837 63ee0dca 2020-02-08 tracey KATTR_ID, "np_wrapper", KATTR__MAX);
838 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
839 b3f1f953 2020-02-09 tracey goto done;
840 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
841 63ee0dca 2020-02-08 tracey KATTR_ID, "nav_prev", KATTR__MAX);
842 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
843 b3f1f953 2020-02-09 tracey goto done;
844 c25c2314 2020-01-28 stsp }
845 2c251c14 2020-01-15 tracey
846 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
847 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
848 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
849 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total)) {
850 63ee0dca 2020-02-08 tracey if (asprintf(&href_prev, "?page=%d",
851 b3f1f953 2020-02-09 tracey gw_trans->page - 1) == -1) {
852 b3f1f953 2020-02-09 tracey error = got_error_from_errno("asprintf");
853 b3f1f953 2020-02-09 tracey goto done;
854 b3f1f953 2020-02-09 tracey }
855 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
856 63ee0dca 2020-02-08 tracey KATTR_HREF, href_prev, KATTR__MAX);
857 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
858 565d9dcb 2020-02-09 tracey goto done;
859 63ee0dca 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
860 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
861 b3f1f953 2020-02-09 tracey goto done;
862 63ee0dca 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
863 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
864 b3f1f953 2020-02-09 tracey goto done;
865 2c251c14 2020-01-15 tracey }
866 2c251c14 2020-01-15 tracey
867 63ee0dca 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
868 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
869 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
870 2c251c14 2020-01-15 tracey
871 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display > 0 &&
872 2c251c14 2020-01-15 tracey next_disp == gw_trans->gw_conf->got_max_repos_display &&
873 2c251c14 2020-01-15 tracey dir_c != (gw_trans->page + 1) *
874 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) {
875 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
876 63ee0dca 2020-02-08 tracey KATTR_ID, "nav_next", KATTR__MAX);
877 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
878 b3f1f953 2020-02-09 tracey goto done;
879 63ee0dca 2020-02-08 tracey if (asprintf(&href_next, "?page=%d",
880 b3f1f953 2020-02-09 tracey gw_trans->page + 1) == -1) {
881 b3f1f953 2020-02-09 tracey error = got_error_from_errno("calloc");
882 b3f1f953 2020-02-09 tracey goto done;
883 b3f1f953 2020-02-09 tracey }
884 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
885 63ee0dca 2020-02-08 tracey KATTR_HREF, href_next, KATTR__MAX);
886 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
887 b3f1f953 2020-02-09 tracey goto done;
888 63ee0dca 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Next");
889 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
890 b3f1f953 2020-02-09 tracey goto done;
891 63ee0dca 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
892 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
893 b3f1f953 2020-02-09 tracey goto done;
894 2c251c14 2020-01-15 tracey next_disp = 0;
895 2c251c14 2020-01-15 tracey break;
896 2c251c14 2020-01-15 tracey }
897 2c251c14 2020-01-15 tracey
898 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
899 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
900 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
901 c25c2314 2020-01-28 stsp prev_disp == gw_trans->repos_total)) {
902 63ee0dca 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
903 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
904 b3f1f953 2020-02-09 tracey goto done;
905 c25c2314 2020-01-28 stsp }
906 2c251c14 2020-01-15 tracey next_disp++;
907 2c251c14 2020-01-15 tracey }
908 b3f1f953 2020-02-09 tracey done:
909 00f13350 2020-02-10 tracey free(href_prev);
910 00f13350 2020-02-10 tracey free(href_next);
911 b3f1f953 2020-02-09 tracey free(href_summary);
912 b3f1f953 2020-02-09 tracey free(href_briefs);
913 b3f1f953 2020-02-09 tracey free(href_commits);
914 b3f1f953 2020-02-09 tracey free(href_tree);
915 565d9dcb 2020-02-09 tracey if (error == NULL && kerr != KCGI_OK)
916 565d9dcb 2020-02-09 tracey error = gw_kcgi_error(kerr);
917 2c251c14 2020-01-15 tracey return error;
918 2c251c14 2020-01-15 tracey }
919 2c251c14 2020-01-15 tracey
920 2c251c14 2020-01-15 tracey static const struct got_error *
921 f2f46662 2020-01-23 tracey gw_commits(struct gw_trans *gw_trans)
922 2c251c14 2020-01-15 tracey {
923 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
924 f2f46662 2020-01-23 tracey struct gw_header *header = NULL, *n_header = NULL;
925 78a9dab5 2020-02-07 tracey char *age = NULL;
926 626b25b6 2020-02-06 tracey char *href_diff = NULL, *href_blob = NULL;
927 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
928 6bee13de 2020-01-16 tracey
929 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
930 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
931 f2f46662 2020-01-23 tracey
932 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
933 6bee13de 2020-01-16 tracey NULL) == -1) {
934 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
935 5ddf0079 2020-01-29 stsp goto done;
936 6bee13de 2020-01-16 tracey }
937 8087c3c5 2020-01-15 tracey
938 d4159696 2020-02-13 stsp error = gw_apply_unveil(gw_trans->gw_dir->path);
939 8087c3c5 2020-01-15 tracey if (error)
940 5ddf0079 2020-01-29 stsp goto done;
941 2c251c14 2020-01-15 tracey
942 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header,
943 f2f46662 2020-01-23 tracey gw_trans->gw_conf->got_max_commits_display);
944 18da9978 2020-01-29 stsp if (error)
945 18da9978 2020-01-29 stsp goto done;
946 4ceb8155 2020-01-15 tracey
947 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
948 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
949 21a55cc7 2020-02-04 tracey "commits_line_wrapper", KATTR__MAX);
950 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
951 18da9978 2020-01-29 stsp goto done;
952 21a55cc7 2020-02-04 tracey error = gw_gen_commit_header(gw_trans, n_header->commit_id,
953 21a55cc7 2020-02-04 tracey n_header->refs_str);
954 f7cc9480 2020-02-05 tracey if (error)
955 f7cc9480 2020-02-05 tracey goto done;
956 21a55cc7 2020-02-04 tracey error = gw_gen_author_header(gw_trans, n_header->author);
957 21a55cc7 2020-02-04 tracey if (error)
958 21a55cc7 2020-02-04 tracey goto done;
959 21a55cc7 2020-02-04 tracey error = gw_gen_committer_header(gw_trans, n_header->author);
960 21a55cc7 2020-02-04 tracey if (error)
961 21a55cc7 2020-02-04 tracey goto done;
962 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, n_header->committer_time,
963 55ccf528 2020-02-03 stsp TM_LONG);
964 55ccf528 2020-02-03 stsp if (error)
965 55ccf528 2020-02-03 stsp goto done;
966 21a55cc7 2020-02-04 tracey error = gw_gen_age_header(gw_trans, age ?age : "");
967 21a55cc7 2020-02-04 tracey if (error)
968 55ccf528 2020-02-03 stsp goto done;
969 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
970 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
971 21a55cc7 2020-02-04 tracey goto done;
972 21a55cc7 2020-02-04 tracey
973 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
974 21a55cc7 2020-02-04 tracey "dotted_line", KATTR__MAX);
975 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
976 21a55cc7 2020-02-04 tracey goto done;
977 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
978 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
979 21a55cc7 2020-02-04 tracey goto done;
980 21a55cc7 2020-02-04 tracey
981 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
982 21a55cc7 2020-02-04 tracey "commit", KATTR__MAX);
983 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
984 21a55cc7 2020-02-04 tracey goto done;
985 78a9dab5 2020-02-07 tracey kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
986 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
987 21a55cc7 2020-02-04 tracey goto done;
988 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
989 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
990 21a55cc7 2020-02-04 tracey goto done;
991 21a55cc7 2020-02-04 tracey
992 21a55cc7 2020-02-04 tracey if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
993 21a55cc7 2020-02-04 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
994 18da9978 2020-01-29 stsp error = got_error_from_errno("asprintf");
995 18da9978 2020-01-29 stsp goto done;
996 18da9978 2020-01-29 stsp }
997 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
998 21a55cc7 2020-02-04 tracey KATTR_ID, "navs_wrapper", KATTR__MAX);
999 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1000 21a55cc7 2020-02-04 tracey goto done;
1001 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1002 21a55cc7 2020-02-04 tracey KATTR_ID, "navs", KATTR__MAX);
1003 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1004 21a55cc7 2020-02-04 tracey goto done;
1005 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1006 21a55cc7 2020-02-04 tracey KATTR_HREF, href_diff, KATTR__MAX);
1007 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1008 21a55cc7 2020-02-04 tracey goto done;
1009 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1010 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1011 21a55cc7 2020-02-04 tracey goto done;
1012 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1013 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1014 21a55cc7 2020-02-04 tracey goto done;
1015 21a55cc7 2020-02-04 tracey
1016 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1017 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1018 21a55cc7 2020-02-04 tracey goto done;
1019 21a55cc7 2020-02-04 tracey
1020 626b25b6 2020-02-06 tracey if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1021 21a55cc7 2020-02-04 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
1022 21a55cc7 2020-02-04 tracey error = got_error_from_errno("asprintf");
1023 21a55cc7 2020-02-04 tracey goto done;
1024 21a55cc7 2020-02-04 tracey }
1025 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1026 626b25b6 2020-02-06 tracey KATTR_HREF, href_blob, KATTR__MAX);
1027 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1028 21a55cc7 2020-02-04 tracey goto done;
1029 21a55cc7 2020-02-04 tracey khtml_puts(gw_trans->gw_html_req, "tree");
1030 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1031 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1032 21a55cc7 2020-02-04 tracey goto done;
1033 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1034 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1035 21a55cc7 2020-02-04 tracey goto done;
1036 21a55cc7 2020-02-04 tracey
1037 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1038 21a55cc7 2020-02-04 tracey "solid_line", KATTR__MAX);
1039 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1040 21a55cc7 2020-02-04 tracey goto done;
1041 b4fba448 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1042 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1043 21a55cc7 2020-02-04 tracey goto done;
1044 21a55cc7 2020-02-04 tracey
1045 55ccf528 2020-02-03 stsp free(age);
1046 55ccf528 2020-02-03 stsp age = NULL;
1047 f2f46662 2020-01-23 tracey }
1048 18da9978 2020-01-29 stsp done:
1049 b0a1bc86 2020-02-14 stsp gw_free_header(header);
1050 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1051 b0a1bc86 2020-02-14 stsp gw_free_header(n_header);
1052 55ccf528 2020-02-03 stsp free(age);
1053 21a55cc7 2020-02-04 tracey free(href_diff);
1054 626b25b6 2020-02-06 tracey free(href_blob);
1055 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
1056 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1057 2c251c14 2020-01-15 tracey return error;
1058 2c251c14 2020-01-15 tracey }
1059 2c251c14 2020-01-15 tracey
1060 2c251c14 2020-01-15 tracey static const struct got_error *
1061 f2f46662 2020-01-23 tracey gw_briefs(struct gw_trans *gw_trans)
1062 2c251c14 2020-01-15 tracey {
1063 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1064 bd275801 2020-02-03 tracey struct gw_header *header = NULL, *n_header = NULL;
1065 53e81e48 2020-02-13 tracey char *age = NULL;
1066 626b25b6 2020-02-06 tracey char *href_diff = NULL, *href_blob = NULL;
1067 bd275801 2020-02-03 tracey char *newline, *smallerthan;
1068 ef72fe2c 2020-02-04 stsp enum kcgi_err kerr = KCGI_OK;
1069 17a96b9f 2020-01-15 tracey
1070 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
1071 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
1072 f2f46662 2020-01-23 tracey
1073 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
1074 6bee13de 2020-01-16 tracey NULL) == -1) {
1075 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
1076 5ddf0079 2020-01-29 stsp goto done;
1077 6bee13de 2020-01-16 tracey }
1078 6bee13de 2020-01-16 tracey
1079 85993e64 2020-02-17 tracey if (gw_trans->action != GW_SUMMARY) {
1080 85993e64 2020-02-17 tracey error = gw_apply_unveil(gw_trans->gw_dir->path);
1081 85993e64 2020-02-17 tracey if (error)
1082 85993e64 2020-02-17 tracey goto done;
1083 85993e64 2020-02-17 tracey }
1084 9d84e7dd 2020-01-15 tracey
1085 f2f46662 2020-01-23 tracey if (gw_trans->action == GW_SUMMARY)
1086 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1087 f2f46662 2020-01-23 tracey else
1088 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header,
1089 f2f46662 2020-01-23 tracey gw_trans->gw_conf->got_max_commits_display);
1090 c25c2314 2020-01-28 stsp if (error)
1091 0e00e8f4 2020-01-29 stsp goto done;
1092 c25c2314 2020-01-28 stsp
1093 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1094 bd275801 2020-02-03 tracey error = gw_get_time_str(&age, n_header->committer_time,
1095 bd275801 2020-02-03 tracey TM_DIFF);
1096 bd275801 2020-02-03 tracey if (error)
1097 bd275801 2020-02-03 tracey goto done;
1098 bd275801 2020-02-03 tracey
1099 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1100 bd275801 2020-02-03 tracey KATTR_ID, "briefs_wrapper", KATTR__MAX);
1101 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1102 bd275801 2020-02-03 tracey goto done;
1103 bd275801 2020-02-03 tracey
1104 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1105 bd275801 2020-02-03 tracey KATTR_ID, "briefs_age", KATTR__MAX);
1106 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1107 bd275801 2020-02-03 tracey goto done;
1108 53e81e48 2020-02-13 tracey kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1109 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1110 bd275801 2020-02-03 tracey goto done;
1111 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1112 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1113 bd275801 2020-02-03 tracey goto done;
1114 bd275801 2020-02-03 tracey
1115 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1116 bd275801 2020-02-03 tracey KATTR_ID, "briefs_author", KATTR__MAX);
1117 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1118 bd275801 2020-02-03 tracey goto done;
1119 bd275801 2020-02-03 tracey smallerthan = strchr(n_header->author, '<');
1120 bd275801 2020-02-03 tracey if (smallerthan)
1121 bd275801 2020-02-03 tracey *smallerthan = '\0';
1122 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1123 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1124 bd275801 2020-02-03 tracey goto done;
1125 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1126 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1127 bd275801 2020-02-03 tracey goto done;
1128 bd275801 2020-02-03 tracey
1129 52f8346c 2020-02-03 tracey if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1130 52f8346c 2020-02-03 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
1131 52f8346c 2020-02-03 tracey error = got_error_from_errno("asprintf");
1132 52f8346c 2020-02-03 tracey goto done;
1133 52f8346c 2020-02-03 tracey }
1134 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1135 bd275801 2020-02-03 tracey KATTR_ID, "briefs_log", KATTR__MAX);
1136 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1137 bd275801 2020-02-03 tracey goto done;
1138 f2f46662 2020-01-23 tracey newline = strchr(n_header->commit_msg, '\n');
1139 f2f46662 2020-01-23 tracey if (newline)
1140 f2f46662 2020-01-23 tracey *newline = '\0';
1141 52f8346c 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1142 52f8346c 2020-02-03 tracey KATTR_HREF, href_diff, KATTR__MAX);
1143 52f8346c 2020-02-03 tracey if (kerr != KCGI_OK)
1144 52f8346c 2020-02-03 tracey goto done;
1145 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1146 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1147 55ccf528 2020-02-03 stsp goto done;
1148 bf93a5c0 2020-02-15 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1149 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
1150 bf93a5c0 2020-02-15 tracey goto done;
1151 bf93a5c0 2020-02-15 tracey
1152 bf93a5c0 2020-02-15 tracey if (n_header->refs_str) {
1153 bf93a5c0 2020-02-15 tracey kerr = khtml_puts(gw_trans->gw_html_req, " ");
1154 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
1155 bf93a5c0 2020-02-15 tracey goto done;
1156 bf93a5c0 2020-02-15 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1157 bf93a5c0 2020-02-15 tracey KATTR_ID, "refs_str", KATTR__MAX);
1158 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
1159 bf93a5c0 2020-02-15 tracey goto done;
1160 bf93a5c0 2020-02-15 tracey kerr = khtml_puts(gw_trans->gw_html_req, "(");
1161 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
1162 bf93a5c0 2020-02-15 tracey goto done;
1163 bf93a5c0 2020-02-15 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1164 bf93a5c0 2020-02-15 tracey n_header->refs_str);
1165 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
1166 bf93a5c0 2020-02-15 tracey goto done;
1167 bf93a5c0 2020-02-15 tracey kerr = khtml_puts(gw_trans->gw_html_req, ")");
1168 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
1169 bf93a5c0 2020-02-15 tracey goto done;
1170 bf93a5c0 2020-02-15 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1171 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
1172 bf93a5c0 2020-02-15 tracey goto done;
1173 bf93a5c0 2020-02-15 tracey }
1174 bf93a5c0 2020-02-15 tracey
1175 bf93a5c0 2020-02-15 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1176 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1177 bd275801 2020-02-03 tracey goto done;
1178 bd275801 2020-02-03 tracey
1179 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1180 bd275801 2020-02-03 tracey KATTR_ID, "navs_wrapper", KATTR__MAX);
1181 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1182 bd275801 2020-02-03 tracey goto done;
1183 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1184 bd275801 2020-02-03 tracey KATTR_ID, "navs", KATTR__MAX);
1185 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1186 bd275801 2020-02-03 tracey goto done;
1187 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1188 bd275801 2020-02-03 tracey KATTR_HREF, href_diff, KATTR__MAX);
1189 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1190 070fee27 2020-02-03 stsp goto done;
1191 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1192 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1193 bd275801 2020-02-03 tracey goto done;
1194 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1195 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1196 bd275801 2020-02-03 tracey goto done;
1197 bd275801 2020-02-03 tracey
1198 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1199 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1200 bd275801 2020-02-03 tracey goto done;
1201 bd275801 2020-02-03 tracey
1202 626b25b6 2020-02-06 tracey if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1203 bd275801 2020-02-03 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
1204 0e00e8f4 2020-01-29 stsp error = got_error_from_errno("asprintf");
1205 0e00e8f4 2020-01-29 stsp goto done;
1206 0e00e8f4 2020-01-29 stsp }
1207 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1208 626b25b6 2020-02-06 tracey KATTR_HREF, href_blob, KATTR__MAX);
1209 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1210 bd275801 2020-02-03 tracey goto done;
1211 bd275801 2020-02-03 tracey khtml_puts(gw_trans->gw_html_req, "tree");
1212 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1213 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1214 bd275801 2020-02-03 tracey goto done;
1215 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1216 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1217 bd275801 2020-02-03 tracey goto done;
1218 bd275801 2020-02-03 tracey
1219 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1220 bd275801 2020-02-03 tracey KATTR_ID, "dotted_line", KATTR__MAX);
1221 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1222 bd275801 2020-02-03 tracey goto done;
1223 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1224 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1225 bd275801 2020-02-03 tracey goto done;
1226 bd275801 2020-02-03 tracey
1227 55ccf528 2020-02-03 stsp free(age);
1228 55ccf528 2020-02-03 stsp age = NULL;
1229 bd275801 2020-02-03 tracey free(href_diff);
1230 bd275801 2020-02-03 tracey href_diff = NULL;
1231 626b25b6 2020-02-06 tracey free(href_blob);
1232 626b25b6 2020-02-06 tracey href_blob = NULL;
1233 9d84e7dd 2020-01-15 tracey }
1234 0e00e8f4 2020-01-29 stsp done:
1235 b0a1bc86 2020-02-14 stsp gw_free_header(header);
1236 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1237 b0a1bc86 2020-02-14 stsp gw_free_header(n_header);
1238 55ccf528 2020-02-03 stsp free(age);
1239 bd275801 2020-02-03 tracey free(href_diff);
1240 626b25b6 2020-02-06 tracey free(href_blob);
1241 ef72fe2c 2020-02-04 stsp if (error == NULL && kerr != KCGI_OK)
1242 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1243 2c251c14 2020-01-15 tracey return error;
1244 2c251c14 2020-01-15 tracey }
1245 2c251c14 2020-01-15 tracey
1246 2c251c14 2020-01-15 tracey static const struct got_error *
1247 54415d85 2020-01-15 tracey gw_summary(struct gw_trans *gw_trans)
1248 387a29ba 2020-01-15 tracey {
1249 387a29ba 2020-01-15 tracey const struct got_error *error = NULL;
1250 9eed6f10 2020-02-08 tracey char *age = NULL;
1251 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
1252 387a29ba 2020-01-15 tracey
1253 f4df82f9 2020-01-29 stsp if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1254 f4df82f9 2020-01-29 stsp return got_error_from_errno("pledge");
1255 6bee13de 2020-01-16 tracey
1256 85993e64 2020-02-17 tracey error = gw_apply_unveil(gw_trans->gw_dir->path);
1257 85993e64 2020-02-17 tracey if (error)
1258 85993e64 2020-02-17 tracey goto done;
1259 46b9c89b 2020-01-15 tracey
1260 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1261 783ec107 2020-02-03 tracey "summary_wrapper", KATTR__MAX);
1262 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1263 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1264 c25c2314 2020-01-28 stsp
1265 783ec107 2020-02-03 tracey if (gw_trans->gw_conf->got_show_repo_description &&
1266 783ec107 2020-02-03 tracey gw_trans->gw_dir->description != NULL &&
1267 783ec107 2020-02-03 tracey (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1268 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1269 783ec107 2020-02-03 tracey KATTR_ID, "description_title", KATTR__MAX);
1270 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1271 783ec107 2020-02-03 tracey goto done;
1272 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1273 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1274 783ec107 2020-02-03 tracey goto done;
1275 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1276 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1277 783ec107 2020-02-03 tracey goto done;
1278 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1279 783ec107 2020-02-03 tracey KATTR_ID, "description", KATTR__MAX);
1280 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1281 783ec107 2020-02-03 tracey goto done;
1282 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1283 783ec107 2020-02-03 tracey gw_trans->gw_dir->description);
1284 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1285 783ec107 2020-02-03 tracey goto done;
1286 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1287 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1288 783ec107 2020-02-03 tracey goto done;
1289 46b9c89b 2020-01-15 tracey }
1290 46b9c89b 2020-01-15 tracey
1291 9a1cc63f 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_owner &&
1292 a942aa37 2020-02-10 tracey gw_trans->gw_dir->owner != NULL &&
1293 a942aa37 2020-02-10 tracey (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1294 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1295 783ec107 2020-02-03 tracey KATTR_ID, "repo_owner_title", KATTR__MAX);
1296 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1297 9a1cc63f 2020-02-03 stsp goto done;
1298 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1299 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1300 9a1cc63f 2020-02-03 stsp goto done;
1301 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1302 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1303 783ec107 2020-02-03 tracey goto done;
1304 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1305 783ec107 2020-02-03 tracey KATTR_ID, "repo_owner", KATTR__MAX);
1306 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1307 783ec107 2020-02-03 tracey goto done;
1308 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1309 783ec107 2020-02-03 tracey gw_trans->gw_dir->owner);
1310 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1311 783ec107 2020-02-03 tracey goto done;
1312 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1313 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1314 783ec107 2020-02-03 tracey goto done;
1315 46b9c89b 2020-01-15 tracey }
1316 46b9c89b 2020-01-15 tracey
1317 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age) {
1318 d126c1b7 2020-01-29 stsp error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1319 2011c142 2020-02-14 stsp NULL, TM_LONG);
1320 d126c1b7 2020-01-29 stsp if (error)
1321 20f34652 2020-01-29 stsp goto done;
1322 55ccf528 2020-02-03 stsp if (age != NULL) {
1323 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1324 783ec107 2020-02-03 tracey KATTR_ID, "last_change_title", KATTR__MAX);
1325 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1326 20f34652 2020-01-29 stsp goto done;
1327 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1328 783ec107 2020-02-03 tracey "Last Change: ");
1329 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1330 20f34652 2020-01-29 stsp goto done;
1331 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1332 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1333 20f34652 2020-01-29 stsp goto done;
1334 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1335 783ec107 2020-02-03 tracey KATTR_ID, "last_change", KATTR__MAX);
1336 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1337 20f34652 2020-01-29 stsp goto done;
1338 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, age);
1339 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1340 783ec107 2020-02-03 tracey goto done;
1341 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1342 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1343 783ec107 2020-02-03 tracey goto done;
1344 46b9c89b 2020-01-15 tracey }
1345 46b9c89b 2020-01-15 tracey }
1346 783ec107 2020-02-03 tracey
1347 783ec107 2020-02-03 tracey if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1348 783ec107 2020-02-03 tracey gw_trans->gw_dir->url != NULL &&
1349 783ec107 2020-02-03 tracey (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1350 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1351 783ec107 2020-02-03 tracey KATTR_ID, "cloneurl_title", KATTR__MAX);
1352 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1353 783ec107 2020-02-03 tracey goto done;
1354 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1355 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1356 783ec107 2020-02-03 tracey goto done;
1357 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1358 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1359 783ec107 2020-02-03 tracey goto done;
1360 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1361 783ec107 2020-02-03 tracey KATTR_ID, "cloneurl", KATTR__MAX);
1362 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1363 783ec107 2020-02-03 tracey goto done;
1364 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1365 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1366 21a55cc7 2020-02-04 tracey goto done;
1367 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1368 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1369 783ec107 2020-02-03 tracey goto done;
1370 783ec107 2020-02-03 tracey }
1371 783ec107 2020-02-03 tracey
1372 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1373 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1374 bd275801 2020-02-03 tracey goto done;
1375 bd275801 2020-02-03 tracey
1376 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1377 bd275801 2020-02-03 tracey "briefs_title_wrapper", KATTR__MAX);
1378 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1379 bd275801 2020-02-03 tracey goto done;
1380 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1381 bd275801 2020-02-03 tracey "briefs_title", KATTR__MAX);
1382 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1383 20f34652 2020-01-29 stsp goto done;
1384 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1385 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1386 bd275801 2020-02-03 tracey goto done;
1387 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1388 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1389 bd275801 2020-02-03 tracey goto done;
1390 f2f46662 2020-01-23 tracey error = gw_briefs(gw_trans);
1391 f2f46662 2020-01-23 tracey if (error)
1392 20f34652 2020-01-29 stsp goto done;
1393 f2f46662 2020-01-23 tracey
1394 38b240fb 2020-02-06 tracey error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1395 6eccd105 2020-02-03 stsp TAGBRIEF);
1396 6eccd105 2020-02-03 stsp if (error)
1397 6eccd105 2020-02-03 stsp goto done;
1398 8d4d2453 2020-01-15 tracey
1399 9eed6f10 2020-02-08 tracey error = gw_output_repo_heads(gw_trans);
1400 20f34652 2020-01-29 stsp done:
1401 20f34652 2020-01-29 stsp free(age);
1402 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
1403 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1404 2204c934 2020-01-15 tracey return error;
1405 2204c934 2020-01-15 tracey }
1406 2204c934 2020-01-15 tracey
1407 2204c934 2020-01-15 tracey static const struct got_error *
1408 f2f46662 2020-01-23 tracey gw_tree(struct gw_trans *gw_trans)
1409 b772de24 2020-01-15 tracey {
1410 b772de24 2020-01-15 tracey const struct got_error *error = NULL;
1411 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
1412 f2f46662 2020-01-23 tracey char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1413 53e81e48 2020-02-13 tracey char *age = NULL;
1414 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1415 6bee13de 2020-01-16 tracey
1416 f2f46662 2020-01-23 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1417 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
1418 b772de24 2020-01-15 tracey
1419 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
1420 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
1421 f2f46662 2020-01-23 tracey
1422 d4159696 2020-02-13 stsp error = gw_apply_unveil(gw_trans->gw_dir->path);
1423 b772de24 2020-01-15 tracey if (error)
1424 5ddf0079 2020-01-29 stsp goto done;
1425 b772de24 2020-01-15 tracey
1426 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
1427 f2f46662 2020-01-23 tracey if (error)
1428 42281175 2020-01-29 stsp goto done;
1429 b772de24 2020-01-15 tracey
1430 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1431 626b25b6 2020-02-06 tracey "tree_header_wrapper", KATTR__MAX);
1432 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
1433 55ccf528 2020-02-03 stsp goto done;
1434 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1435 626b25b6 2020-02-06 tracey "tree_header", KATTR__MAX);
1436 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
1437 55ccf528 2020-02-03 stsp goto done;
1438 626b25b6 2020-02-06 tracey error = gw_gen_tree_header(gw_trans, header->tree_id);
1439 626b25b6 2020-02-06 tracey if (error)
1440 626b25b6 2020-02-06 tracey goto done;
1441 626b25b6 2020-02-06 tracey error = gw_get_time_str(&age, header->committer_time,
1442 626b25b6 2020-02-06 tracey TM_LONG);
1443 626b25b6 2020-02-06 tracey if (error)
1444 626b25b6 2020-02-06 tracey goto done;
1445 626b25b6 2020-02-06 tracey error = gw_gen_age_header(gw_trans, age ?age : "");
1446 070fee27 2020-02-03 stsp if (error)
1447 070fee27 2020-02-03 stsp goto done;
1448 626b25b6 2020-02-06 tracey error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1449 626b25b6 2020-02-06 tracey if (error)
1450 42281175 2020-01-29 stsp goto done;
1451 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1452 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
1453 42281175 2020-01-29 stsp goto done;
1454 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1455 626b25b6 2020-02-06 tracey "dotted_line", KATTR__MAX);
1456 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1457 626b25b6 2020-02-06 tracey goto done;
1458 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1459 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
1460 626b25b6 2020-02-06 tracey goto done;
1461 626b25b6 2020-02-06 tracey
1462 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1463 626b25b6 2020-02-06 tracey "tree", KATTR__MAX);
1464 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
1465 626b25b6 2020-02-06 tracey goto done;
1466 626b25b6 2020-02-06 tracey error = gw_output_repo_tree(gw_trans);
1467 626b25b6 2020-02-06 tracey if (error)
1468 626b25b6 2020-02-06 tracey goto done;
1469 626b25b6 2020-02-06 tracey
1470 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1471 4ff7391f 2020-01-28 tracey done:
1472 b0a1bc86 2020-02-14 stsp gw_free_header(header);
1473 f2f46662 2020-01-23 tracey free(tree_html_disp);
1474 f2f46662 2020-01-23 tracey free(tree_html);
1475 f2f46662 2020-01-23 tracey free(tree);
1476 55ccf528 2020-02-03 stsp free(age);
1477 626b25b6 2020-02-06 tracey if (error == NULL && kerr != KCGI_OK)
1478 626b25b6 2020-02-06 tracey error = gw_kcgi_error(kerr);
1479 4ff7391f 2020-01-28 tracey return error;
1480 4ff7391f 2020-01-28 tracey }
1481 4ff7391f 2020-01-28 tracey
1482 4ff7391f 2020-01-28 tracey static const struct got_error *
1483 4ff7391f 2020-01-28 tracey gw_tag(struct gw_trans *gw_trans)
1484 4ff7391f 2020-01-28 tracey {
1485 4ff7391f 2020-01-28 tracey const struct got_error *error = NULL;
1486 4ff7391f 2020-01-28 tracey struct gw_header *header = NULL;
1487 4ff7391f 2020-01-28 tracey enum kcgi_err kerr;
1488 4ff7391f 2020-01-28 tracey
1489 4ff7391f 2020-01-28 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1490 4ff7391f 2020-01-28 tracey return got_error_from_errno("pledge");
1491 4ff7391f 2020-01-28 tracey
1492 4ff7391f 2020-01-28 tracey if ((header = gw_init_header()) == NULL)
1493 4ff7391f 2020-01-28 tracey return got_error_from_errno("malloc");
1494 4ff7391f 2020-01-28 tracey
1495 d4159696 2020-02-13 stsp error = gw_apply_unveil(gw_trans->gw_dir->path);
1496 4ff7391f 2020-01-28 tracey if (error)
1497 5ddf0079 2020-01-29 stsp goto done;
1498 4ff7391f 2020-01-28 tracey
1499 9f33591a 2020-02-14 tracey if (gw_trans->commit_id == NULL) {
1500 9f33591a 2020-02-14 tracey error = got_error_msg(GOT_ERR_QUERYSTRING,
1501 9f33591a 2020-02-14 tracey "commit required in querystring");
1502 9f33591a 2020-02-14 tracey goto done;
1503 9f33591a 2020-02-14 tracey }
1504 9f33591a 2020-02-14 tracey
1505 4ff7391f 2020-01-28 tracey error = gw_get_header(gw_trans, header, 1);
1506 4ff7391f 2020-01-28 tracey if (error)
1507 470cd826 2020-01-29 stsp goto done;
1508 4ff7391f 2020-01-28 tracey
1509 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1510 38b240fb 2020-02-06 tracey "tag_header_wrapper", KATTR__MAX);
1511 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
1512 38b240fb 2020-02-06 tracey goto done;
1513 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1514 38b240fb 2020-02-06 tracey "tag_header", KATTR__MAX);
1515 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
1516 38b240fb 2020-02-06 tracey goto done;
1517 38b240fb 2020-02-06 tracey error = gw_gen_commit_header(gw_trans, header->commit_id,
1518 38b240fb 2020-02-06 tracey header->refs_str);
1519 6eccd105 2020-02-03 stsp if (error)
1520 6eccd105 2020-02-03 stsp goto done;
1521 38b240fb 2020-02-06 tracey error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1522 38b240fb 2020-02-06 tracey if (error)
1523 470cd826 2020-01-29 stsp goto done;
1524 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1525 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
1526 470cd826 2020-01-29 stsp goto done;
1527 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1528 38b240fb 2020-02-06 tracey "dotted_line", KATTR__MAX);
1529 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
1530 38b240fb 2020-02-06 tracey goto done;
1531 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1532 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
1533 38b240fb 2020-02-06 tracey goto done;
1534 4ff7391f 2020-01-28 tracey
1535 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1536 38b240fb 2020-02-06 tracey "tree", KATTR__MAX);
1537 4ff7391f 2020-01-28 tracey if (kerr != KCGI_OK)
1538 38b240fb 2020-02-06 tracey goto done;
1539 38b240fb 2020-02-06 tracey
1540 38b240fb 2020-02-06 tracey error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1541 38b240fb 2020-02-06 tracey if (error)
1542 38b240fb 2020-02-06 tracey goto done;
1543 38b240fb 2020-02-06 tracey
1544 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1545 4ff7391f 2020-01-28 tracey done:
1546 b0a1bc86 2020-02-14 stsp gw_free_header(header);
1547 38b240fb 2020-02-06 tracey if (error == NULL && kerr != KCGI_OK)
1548 38b240fb 2020-02-06 tracey error = gw_kcgi_error(kerr);
1549 2c251c14 2020-01-15 tracey return error;
1550 2c251c14 2020-01-15 tracey }
1551 2c251c14 2020-01-15 tracey
1552 2c251c14 2020-01-15 tracey static const struct got_error *
1553 54415d85 2020-01-15 tracey gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1554 2c251c14 2020-01-15 tracey {
1555 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1556 2c251c14 2020-01-15 tracey DIR *dt;
1557 2c251c14 2020-01-15 tracey char *dir_test;
1558 4ceb8155 2020-01-15 tracey int opened = 0;
1559 2c251c14 2020-01-15 tracey
1560 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s/%s",
1561 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
1562 88d59c36 2020-01-29 stsp GOTWEB_GIT_DIR) == -1)
1563 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1564 2c251c14 2020-01-15 tracey
1565 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
1566 2c251c14 2020-01-15 tracey if (dt == NULL) {
1567 2c251c14 2020-01-15 tracey free(dir_test);
1568 2c251c14 2020-01-15 tracey } else {
1569 d1635ae4 2020-02-13 tracey gw_dir->path = strdup(dir_test);
1570 d1635ae4 2020-02-13 tracey if (gw_dir->path == NULL) {
1571 d1635ae4 2020-02-13 tracey opened = 1;
1572 d1635ae4 2020-02-13 tracey error = got_error_from_errno("strdup");
1573 a942aa37 2020-02-10 tracey goto errored;
1574 d1635ae4 2020-02-13 tracey }
1575 d1635ae4 2020-02-13 tracey opened = 1;
1576 2c251c14 2020-01-15 tracey goto done;
1577 2c251c14 2020-01-15 tracey }
1578 2c251c14 2020-01-15 tracey
1579 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s/%s",
1580 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
1581 d81f9039 2020-02-13 stsp GOTWEB_GOT_DIR) == -1) {
1582 d81f9039 2020-02-13 stsp dir_test = NULL;
1583 d81f9039 2020-02-13 stsp error = got_error_from_errno("asprintf");
1584 d81f9039 2020-02-13 stsp goto errored;
1585 d81f9039 2020-02-13 stsp }
1586 2c251c14 2020-01-15 tracey
1587 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
1588 2c251c14 2020-01-15 tracey if (dt == NULL)
1589 2c251c14 2020-01-15 tracey free(dir_test);
1590 2c251c14 2020-01-15 tracey else {
1591 4ceb8155 2020-01-15 tracey opened = 1;
1592 2c251c14 2020-01-15 tracey error = got_error(GOT_ERR_NOT_GIT_REPO);
1593 2c251c14 2020-01-15 tracey goto errored;
1594 2c251c14 2020-01-15 tracey }
1595 2c251c14 2020-01-15 tracey
1596 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s",
1597 d81f9039 2020-02-13 stsp gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1598 d81f9039 2020-02-13 stsp error = got_error_from_errno("asprintf");
1599 d81f9039 2020-02-13 stsp dir_test = NULL;
1600 d81f9039 2020-02-13 stsp goto errored;
1601 d81f9039 2020-02-13 stsp }
1602 2c251c14 2020-01-15 tracey
1603 d1635ae4 2020-02-13 tracey gw_dir->path = strdup(dir_test);
1604 d1635ae4 2020-02-13 tracey if (gw_dir->path == NULL) {
1605 a942aa37 2020-02-10 tracey opened = 1;
1606 d1635ae4 2020-02-13 tracey error = got_error_from_errno("strdup");
1607 a942aa37 2020-02-10 tracey goto errored;
1608 a942aa37 2020-02-10 tracey }
1609 cc18a904 2020-02-13 tracey
1610 cc18a904 2020-02-13 tracey dt = opendir(dir_test);
1611 cc18a904 2020-02-13 tracey if (dt == NULL) {
1612 d90bcdd6 2020-02-17 stsp error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1613 cc18a904 2020-02-13 tracey goto errored;
1614 7ecc73af 2020-02-13 tracey } else
1615 7ecc73af 2020-02-13 tracey opened = 1;
1616 2c251c14 2020-01-15 tracey done:
1617 32cd4d18 2020-02-03 stsp error = gw_get_repo_description(&gw_dir->description, gw_trans,
1618 2c251c14 2020-01-15 tracey gw_dir->path);
1619 32cd4d18 2020-02-03 stsp if (error)
1620 32cd4d18 2020-02-03 stsp goto errored;
1621 9a1cc63f 2020-02-03 stsp error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1622 9a1cc63f 2020-02-03 stsp if (error)
1623 9a1cc63f 2020-02-03 stsp goto errored;
1624 d126c1b7 2020-01-29 stsp error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1625 2011c142 2020-02-14 stsp NULL, TM_DIFF);
1626 d126c1b7 2020-01-29 stsp if (error)
1627 d126c1b7 2020-01-29 stsp goto errored;
1628 59d3c40e 2020-02-03 stsp error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1629 2c251c14 2020-01-15 tracey errored:
1630 2c251c14 2020-01-15 tracey free(dir_test);
1631 2c251c14 2020-01-15 tracey if (opened)
1632 795c5bd7 2020-02-17 tracey if (dt && closedir(dt) == -1 && error == NULL)
1633 e227880d 2020-02-17 tracey error = got_error_from_errno("closedir");
1634 2c251c14 2020-01-15 tracey return error;
1635 2c251c14 2020-01-15 tracey }
1636 2c251c14 2020-01-15 tracey
1637 2c251c14 2020-01-15 tracey static const struct got_error *
1638 54415d85 2020-01-15 tracey gw_load_got_paths(struct gw_trans *gw_trans)
1639 2c251c14 2020-01-15 tracey {
1640 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1641 2c251c14 2020-01-15 tracey DIR *d;
1642 2c251c14 2020-01-15 tracey struct dirent **sd_dent;
1643 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
1644 2c251c14 2020-01-15 tracey struct stat st;
1645 2c251c14 2020-01-15 tracey unsigned int d_cnt, d_i;
1646 2c251c14 2020-01-15 tracey
1647 2c251c14 2020-01-15 tracey d = opendir(gw_trans->gw_conf->got_repos_path);
1648 2c251c14 2020-01-15 tracey if (d == NULL) {
1649 2c251c14 2020-01-15 tracey error = got_error_from_errno2("opendir",
1650 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
1651 2c251c14 2020-01-15 tracey return error;
1652 2c251c14 2020-01-15 tracey }
1653 2c251c14 2020-01-15 tracey
1654 2c251c14 2020-01-15 tracey d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1655 2c251c14 2020-01-15 tracey alphasort);
1656 2c251c14 2020-01-15 tracey if (d_cnt == -1) {
1657 2c251c14 2020-01-15 tracey error = got_error_from_errno2("scandir",
1658 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
1659 8182bd34 2020-02-17 tracey goto done;
1660 2c251c14 2020-01-15 tracey }
1661 2c251c14 2020-01-15 tracey
1662 2c251c14 2020-01-15 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
1663 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos > 0 &&
1664 2c251c14 2020-01-15 tracey (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1665 2c251c14 2020-01-15 tracey break; /* account for parent and self */
1666 2c251c14 2020-01-15 tracey
1667 2c251c14 2020-01-15 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1668 2c251c14 2020-01-15 tracey strcmp(sd_dent[d_i]->d_name, "..") == 0)
1669 2c251c14 2020-01-15 tracey continue;
1670 2c251c14 2020-01-15 tracey
1671 4619e1f2 2020-02-13 stsp error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1672 4619e1f2 2020-02-13 stsp if (error)
1673 8182bd34 2020-02-17 tracey goto done;
1674 2c251c14 2020-01-15 tracey
1675 2c251c14 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_dir);
1676 7bf16821 2020-02-07 stsp if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1677 7bf16821 2020-02-07 stsp error = NULL;
1678 2c251c14 2020-01-15 tracey continue;
1679 7bf16821 2020-02-07 stsp }
1680 2c251c14 2020-01-15 tracey else if (error)
1681 8182bd34 2020-02-17 tracey goto done;
1682 2c251c14 2020-01-15 tracey
1683 2c251c14 2020-01-15 tracey if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1684 2c251c14 2020-01-15 tracey !got_path_dir_is_empty(gw_dir->path)) {
1685 2c251c14 2020-01-15 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1686 2c251c14 2020-01-15 tracey entry);
1687 2c251c14 2020-01-15 tracey gw_trans->repos_total++;
1688 2c251c14 2020-01-15 tracey }
1689 2c251c14 2020-01-15 tracey }
1690 8182bd34 2020-02-17 tracey done:
1691 e227880d 2020-02-17 tracey if (d && closedir(d) == -1 && error == NULL)
1692 e227880d 2020-02-17 tracey error = got_error_from_errno("closedir");
1693 2c251c14 2020-01-15 tracey return error;
1694 2c251c14 2020-01-15 tracey }
1695 2c251c14 2020-01-15 tracey
1696 2c251c14 2020-01-15 tracey static const struct got_error *
1697 54415d85 2020-01-15 tracey gw_parse_querystring(struct gw_trans *gw_trans)
1698 2c251c14 2020-01-15 tracey {
1699 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1700 2c251c14 2020-01-15 tracey struct kpair *p;
1701 2c251c14 2020-01-15 tracey struct gw_query_action *action = NULL;
1702 2c251c14 2020-01-15 tracey unsigned int i;
1703 2c251c14 2020-01-15 tracey
1704 2c251c14 2020-01-15 tracey if (gw_trans->gw_req->fieldnmap[0]) {
1705 8f214b62 2020-02-17 stsp return got_error(GOT_ERR_QUERYSTRING);
1706 2c251c14 2020-01-15 tracey } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1707 2c251c14 2020-01-15 tracey /* define gw_trans->repo_path */
1708 f652ec0c 2020-02-13 stsp gw_trans->repo_name = p->parsed.s;
1709 2c251c14 2020-01-15 tracey
1710 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_path, "%s/%s",
1711 88d59c36 2020-01-29 stsp gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1712 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1713 2c251c14 2020-01-15 tracey
1714 2c251c14 2020-01-15 tracey /* get action and set function */
1715 f1200fe3 2020-02-13 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1716 2c251c14 2020-01-15 tracey for (i = 0; i < nitems(gw_query_funcs); i++) {
1717 2c251c14 2020-01-15 tracey action = &gw_query_funcs[i];
1718 f1200fe3 2020-02-13 tracey if (action->func_name == NULL)
1719 2c251c14 2020-01-15 tracey continue;
1720 f1200fe3 2020-02-13 tracey if (strcmp(action->func_name,
1721 f1200fe3 2020-02-13 tracey p->parsed.s) == 0) {
1722 f1200fe3 2020-02-13 tracey gw_trans->action = i;
1723 f1200fe3 2020-02-13 tracey break;
1724 f1200fe3 2020-02-13 tracey }
1725 f1200fe3 2020-02-13 tracey }
1726 6f6f771f 2020-02-13 tracey }
1727 6f6f771f 2020-02-13 tracey if (gw_trans->action == -1) {
1728 6f6f771f 2020-02-13 tracey gw_trans->action = GW_ERR;
1729 8f214b62 2020-02-17 stsp gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
1730 8f214b62 2020-02-17 stsp p != NULL ? "bad action in querystring" :
1731 8f214b62 2020-02-17 stsp "no action in querystring");
1732 cc18a904 2020-02-13 tracey return error;
1733 f1200fe3 2020-02-13 tracey }
1734 ec46ccd7 2020-01-15 tracey
1735 26dc3004 2020-02-13 stsp if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1736 56997cd3 2020-02-14 tracey if (asprintf(&gw_trans->commit_id, "%s",
1737 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1738 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
1739 26dc3004 2020-02-13 stsp }
1740 2c251c14 2020-01-15 tracey
1741 4d6abe2b 2020-02-13 stsp if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1742 4d6abe2b 2020-02-13 stsp gw_trans->repo_file = p->parsed.s;
1743 8087c3c5 2020-01-15 tracey
1744 26dc3004 2020-02-13 stsp if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1745 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_folder, "%s",
1746 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1747 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
1748 26dc3004 2020-02-13 stsp }
1749 ec46ccd7 2020-01-15 tracey
1750 742ba378 2020-02-14 stsp if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1751 742ba378 2020-02-14 stsp gw_trans->headref = p->parsed.s;
1752 2c251c14 2020-01-15 tracey
1753 4619e1f2 2020-02-13 stsp error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1754 4619e1f2 2020-02-13 stsp if (error)
1755 4619e1f2 2020-02-13 stsp return error;
1756 46b9c89b 2020-01-15 tracey
1757 cc18a904 2020-02-13 tracey gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1758 2c251c14 2020-01-15 tracey } else
1759 2c251c14 2020-01-15 tracey gw_trans->action = GW_INDEX;
1760 2c251c14 2020-01-15 tracey
1761 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1762 2c251c14 2020-01-15 tracey gw_trans->page = p->parsed.i;
1763 2c251c14 2020-01-15 tracey
1764 2c251c14 2020-01-15 tracey return error;
1765 2c251c14 2020-01-15 tracey }
1766 2c251c14 2020-01-15 tracey
1767 4619e1f2 2020-02-13 stsp static const struct got_error *
1768 f652ec0c 2020-02-13 stsp gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1769 4619e1f2 2020-02-13 stsp {
1770 4619e1f2 2020-02-13 stsp const struct got_error *error;
1771 2c251c14 2020-01-15 tracey
1772 4619e1f2 2020-02-13 stsp *gw_dir = malloc(sizeof(**gw_dir));
1773 4619e1f2 2020-02-13 stsp if (*gw_dir == NULL)
1774 4619e1f2 2020-02-13 stsp return got_error_from_errno("malloc");
1775 2c251c14 2020-01-15 tracey
1776 4619e1f2 2020-02-13 stsp if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1777 4619e1f2 2020-02-13 stsp error = got_error_from_errno("asprintf");
1778 4619e1f2 2020-02-13 stsp free(*gw_dir);
1779 4619e1f2 2020-02-13 stsp *gw_dir = NULL;
1780 2c251c14 2020-01-15 tracey return NULL;
1781 4619e1f2 2020-02-13 stsp }
1782 2c251c14 2020-01-15 tracey
1783 4619e1f2 2020-02-13 stsp return NULL;
1784 474370cb 2020-01-15 tracey }
1785 474370cb 2020-01-15 tracey
1786 c25c2314 2020-01-28 stsp static const struct got_error *
1787 54415d85 2020-01-15 tracey gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1788 2c251c14 2020-01-15 tracey {
1789 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1790 c25c2314 2020-01-28 stsp
1791 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1792 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1793 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1794 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1795 2c251c14 2020-01-15 tracey khttps[code]);
1796 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1797 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1798 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1799 2c251c14 2020-01-15 tracey kmimetypes[mime]);
1800 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1801 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1802 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1803 017d6da3 2020-01-29 tracey "nosniff");
1804 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1805 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1806 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1807 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1808 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1809 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1810 017d6da3 2020-01-29 tracey "1; mode=block");
1811 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1812 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1813 c25c2314 2020-01-28 stsp
1814 017d6da3 2020-01-29 tracey if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1815 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req,
1816 017d6da3 2020-01-29 tracey kresps[KRESP_CONTENT_DISPOSITION],
1817 017d6da3 2020-01-29 tracey "attachment; filename=%s", gw_trans->repo_file);
1818 017d6da3 2020-01-29 tracey if (kerr != KCGI_OK)
1819 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1820 017d6da3 2020-01-29 tracey }
1821 017d6da3 2020-01-29 tracey
1822 c25c2314 2020-01-28 stsp kerr = khttp_body(gw_trans->gw_req);
1823 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1824 2c251c14 2020-01-15 tracey }
1825 2c251c14 2020-01-15 tracey
1826 c25c2314 2020-01-28 stsp static const struct got_error *
1827 6d8d8a26 2020-01-29 stsp gw_display_index(struct gw_trans *gw_trans)
1828 2c251c14 2020-01-15 tracey {
1829 4bec7539 2020-01-29 stsp const struct got_error *error;
1830 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1831 c25c2314 2020-01-28 stsp
1832 cc18a904 2020-02-13 tracey /* catch early querystring errors */
1833 cc18a904 2020-02-13 tracey if (gw_trans->error)
1834 cc18a904 2020-02-13 tracey gw_trans->action = GW_ERR;
1835 cc18a904 2020-02-13 tracey
1836 4bec7539 2020-01-29 stsp error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1837 4bec7539 2020-01-29 stsp if (error)
1838 4bec7539 2020-01-29 stsp return error;
1839 4bec7539 2020-01-29 stsp
1840 f7cc9480 2020-02-05 tracey kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1841 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1842 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1843 2c251c14 2020-01-15 tracey
1844 017d6da3 2020-01-29 tracey if (gw_trans->action != GW_BLOB) {
1845 017d6da3 2020-01-29 tracey kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1846 017d6da3 2020-01-29 tracey gw_query_funcs[gw_trans->action].template);
1847 017d6da3 2020-01-29 tracey if (kerr != KCGI_OK) {
1848 017d6da3 2020-01-29 tracey khtml_close(gw_trans->gw_html_req);
1849 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1850 017d6da3 2020-01-29 tracey }
1851 7a9bfbff 2020-01-29 stsp }
1852 2c251c14 2020-01-15 tracey
1853 7a9bfbff 2020-01-29 stsp return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1854 2c251c14 2020-01-15 tracey }
1855 2c251c14 2020-01-15 tracey
1856 f1200fe3 2020-02-13 tracey static const struct got_error *
1857 f1200fe3 2020-02-13 tracey gw_error(struct gw_trans *gw_trans)
1858 6d8d8a26 2020-01-29 stsp {
1859 f1200fe3 2020-02-13 tracey enum kcgi_err kerr;
1860 6d8d8a26 2020-01-29 stsp
1861 f1200fe3 2020-02-13 tracey kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
1862 f1200fe3 2020-02-13 tracey
1863 f1200fe3 2020-02-13 tracey return gw_kcgi_error(kerr);
1864 6d8d8a26 2020-01-29 stsp }
1865 6d8d8a26 2020-01-29 stsp
1866 2c251c14 2020-01-15 tracey static int
1867 2c251c14 2020-01-15 tracey gw_template(size_t key, void *arg)
1868 2c251c14 2020-01-15 tracey {
1869 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1870 bd275801 2020-02-03 tracey enum kcgi_err kerr;
1871 54415d85 2020-01-15 tracey struct gw_trans *gw_trans = arg;
1872 4ae179a2 2020-02-08 tracey char *img_src = NULL;
1873 2c251c14 2020-01-15 tracey
1874 2c251c14 2020-01-15 tracey switch (key) {
1875 2c251c14 2020-01-15 tracey case (TEMPL_HEAD):
1876 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1877 1dcb8908 2020-02-12 tracey KATTR_NAME, "viewport",
1878 1dcb8908 2020-02-12 tracey KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1879 aaed5792 2020-02-08 tracey KATTR__MAX);
1880 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1881 bd275801 2020-02-03 tracey return 0;
1882 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1883 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1884 aaed5792 2020-02-08 tracey return 0;
1885 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1886 aaed5792 2020-02-08 tracey KATTR_CHARSET, "utf-8",
1887 aaed5792 2020-02-08 tracey KATTR__MAX);
1888 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1889 aaed5792 2020-02-08 tracey return 0;
1890 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1891 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1892 aaed5792 2020-02-08 tracey return 0;
1893 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1894 aaed5792 2020-02-08 tracey KATTR_NAME, "msapplication-TileColor",
1895 aaed5792 2020-02-08 tracey KATTR_CONTENT, "#da532c", KATTR__MAX);
1896 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1897 aaed5792 2020-02-08 tracey return 0;
1898 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1899 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1900 aaed5792 2020-02-08 tracey return 0;
1901 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1902 aaed5792 2020-02-08 tracey KATTR_NAME, "theme-color",
1903 aaed5792 2020-02-08 tracey KATTR_CONTENT, "#ffffff", KATTR__MAX);
1904 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1905 aaed5792 2020-02-08 tracey return 0;
1906 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1907 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1908 aaed5792 2020-02-08 tracey return 0;
1909 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1910 aaed5792 2020-02-08 tracey KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1911 aaed5792 2020-02-08 tracey KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1912 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1913 aaed5792 2020-02-08 tracey return 0;
1914 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1915 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1916 aaed5792 2020-02-08 tracey return 0;
1917 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1918 aaed5792 2020-02-08 tracey KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1919 aaed5792 2020-02-08 tracey "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1920 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1921 aaed5792 2020-02-08 tracey return 0;
1922 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1923 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1924 aaed5792 2020-02-08 tracey return 0;
1925 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1926 aaed5792 2020-02-08 tracey KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1927 aaed5792 2020-02-08 tracey "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1928 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1929 aaed5792 2020-02-08 tracey return 0;
1930 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1931 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1932 aaed5792 2020-02-08 tracey return 0;
1933 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1934 aaed5792 2020-02-08 tracey KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1935 aaed5792 2020-02-08 tracey KATTR__MAX);
1936 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1937 aaed5792 2020-02-08 tracey return 0;
1938 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1939 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1940 aaed5792 2020-02-08 tracey return 0;
1941 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1942 aaed5792 2020-02-08 tracey KATTR_REL, "mask-icon", KATTR_HREF,
1943 aaed5792 2020-02-08 tracey "/safari-pinned-tab.svg", KATTR__MAX);
1944 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1945 aaed5792 2020-02-08 tracey return 0;
1946 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1947 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1948 aaed5792 2020-02-08 tracey return 0;
1949 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1950 aaed5792 2020-02-08 tracey KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1951 aaed5792 2020-02-08 tracey KATTR_HREF, "/gotweb.css", KATTR__MAX);
1952 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1953 aaed5792 2020-02-08 tracey return 0;
1954 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1955 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
1956 aaed5792 2020-02-08 tracey return 0;
1957 2c251c14 2020-01-15 tracey break;
1958 2c251c14 2020-01-15 tracey case(TEMPL_HEADER):
1959 185ba3ba 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1960 185ba3ba 2020-02-04 tracey KATTR_ID, "got_link", KATTR__MAX);
1961 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK)
1962 185ba3ba 2020-02-04 tracey return 0;
1963 185ba3ba 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1964 185ba3ba 2020-02-04 tracey KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1965 185ba3ba 2020-02-04 tracey KATTR_TARGET, "_sotd", KATTR__MAX);
1966 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK)
1967 185ba3ba 2020-02-04 tracey return 0;
1968 185ba3ba 2020-02-04 tracey if (asprintf(&img_src, "/%s",
1969 185ba3ba 2020-02-04 tracey gw_trans->gw_conf->got_logo) == -1)
1970 185ba3ba 2020-02-04 tracey return 0;
1971 185ba3ba 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1972 185ba3ba 2020-02-04 tracey KATTR_SRC, img_src, KATTR__MAX);
1973 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK) {
1974 185ba3ba 2020-02-04 tracey free(img_src);
1975 185ba3ba 2020-02-04 tracey return 0;
1976 bd275801 2020-02-03 tracey }
1977 185ba3ba 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1978 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK) {
1979 185ba3ba 2020-02-04 tracey free(img_src);
1980 185ba3ba 2020-02-04 tracey return 0;
1981 185ba3ba 2020-02-04 tracey }
1982 2c251c14 2020-01-15 tracey break;
1983 2c251c14 2020-01-15 tracey case (TEMPL_SITEPATH):
1984 4ae179a2 2020-02-08 tracey error = gw_output_site_link(gw_trans);
1985 4ae179a2 2020-02-08 tracey if (error)
1986 4ae179a2 2020-02-08 tracey return 0;
1987 2c251c14 2020-01-15 tracey break;
1988 2c251c14 2020-01-15 tracey case(TEMPL_TITLE):
1989 bd275801 2020-02-03 tracey if (gw_trans->gw_conf->got_site_name != NULL) {
1990 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1991 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_name);
1992 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1993 bd275801 2020-02-03 tracey return 0;
1994 bd275801 2020-02-03 tracey }
1995 2c251c14 2020-01-15 tracey break;
1996 2c251c14 2020-01-15 tracey case (TEMPL_SEARCH):
1997 63ee0dca 2020-02-08 tracey break;
1998 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1999 63ee0dca 2020-02-08 tracey "search", KATTR__MAX);
2000 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
2001 bd275801 2020-02-03 tracey return 0;
2002 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2003 63ee0dca 2020-02-08 tracey KATTR_METHOD, "POST", KATTR__MAX);
2004 63ee0dca 2020-02-08 tracey if (kerr != KCGI_OK)
2005 63ee0dca 2020-02-08 tracey return 0;
2006 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2007 63ee0dca 2020-02-08 tracey "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2008 63ee0dca 2020-02-08 tracey KATTR_MAXLENGTH, "50", KATTR__MAX);
2009 63ee0dca 2020-02-08 tracey if (kerr != KCGI_OK)
2010 63ee0dca 2020-02-08 tracey return 0;
2011 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2012 63ee0dca 2020-02-08 tracey KATTR__MAX);
2013 63ee0dca 2020-02-08 tracey if (kerr != KCGI_OK)
2014 63ee0dca 2020-02-08 tracey return 0;
2015 63ee0dca 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2016 63ee0dca 2020-02-08 tracey if (kerr != KCGI_OK)
2017 63ee0dca 2020-02-08 tracey return 0;
2018 63ee0dca 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2019 63ee0dca 2020-02-08 tracey if (kerr != KCGI_OK)
2020 63ee0dca 2020-02-08 tracey return 0;
2021 2c251c14 2020-01-15 tracey break;
2022 2c251c14 2020-01-15 tracey case(TEMPL_SITEOWNER):
2023 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_owner != NULL &&
2024 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_show_site_owner) {
2025 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2026 bd275801 2020-02-03 tracey KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2027 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
2028 070fee27 2020-02-03 stsp return 0;
2029 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2030 bd275801 2020-02-03 tracey KATTR_ID, "site_owner", KATTR__MAX);
2031 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
2032 2c251c14 2020-01-15 tracey return 0;
2033 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
2034 bd275801 2020-02-03 tracey gw_trans->gw_conf->got_site_owner);
2035 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2036 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
2037 bd275801 2020-02-03 tracey return 0;
2038 2c251c14 2020-01-15 tracey }
2039 2c251c14 2020-01-15 tracey break;
2040 2c251c14 2020-01-15 tracey case(TEMPL_CONTENT):
2041 2c251c14 2020-01-15 tracey error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2042 bd275801 2020-02-03 tracey if (error) {
2043 0d100d0b 2020-02-07 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2044 0d100d0b 2020-02-07 tracey KATTR_ID, "tmpl_err", KATTR__MAX);
2045 0d100d0b 2020-02-07 tracey if (kerr != KCGI_OK)
2046 0d100d0b 2020-02-07 tracey return 0;
2047 0d100d0b 2020-02-07 tracey kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2048 0d100d0b 2020-02-07 tracey if (kerr != KCGI_OK)
2049 0d100d0b 2020-02-07 tracey return 0;
2050 bd275801 2020-02-03 tracey kerr = khttp_puts(gw_trans->gw_req, error->msg);
2051 0d100d0b 2020-02-07 tracey if (kerr != KCGI_OK)
2052 0d100d0b 2020-02-07 tracey return 0;
2053 0d100d0b 2020-02-07 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2054 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
2055 bd275801 2020-02-03 tracey return 0;
2056 bd275801 2020-02-03 tracey }
2057 2c251c14 2020-01-15 tracey break;
2058 2c251c14 2020-01-15 tracey default:
2059 2c251c14 2020-01-15 tracey return 0;
2060 2c251c14 2020-01-15 tracey }
2061 2c251c14 2020-01-15 tracey return 1;
2062 f2f46662 2020-01-23 tracey }
2063 21a55cc7 2020-02-04 tracey
2064 21a55cc7 2020-02-04 tracey static const struct got_error *
2065 21a55cc7 2020-02-04 tracey gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2066 21a55cc7 2020-02-04 tracey {
2067 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
2068 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
2069 21a55cc7 2020-02-04 tracey
2070 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2071 21a55cc7 2020-02-04 tracey KATTR_ID, "header_commit_title", KATTR__MAX);
2072 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2073 21a55cc7 2020-02-04 tracey goto done;
2074 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2075 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2076 21a55cc7 2020-02-04 tracey goto done;
2077 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2078 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2079 21a55cc7 2020-02-04 tracey goto done;
2080 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2081 21a55cc7 2020-02-04 tracey KATTR_ID, "header_commit", KATTR__MAX);
2082 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2083 21a55cc7 2020-02-04 tracey goto done;
2084 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, str1);
2085 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2086 21a55cc7 2020-02-04 tracey goto done;
2087 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, " ");
2088 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2089 21a55cc7 2020-02-04 tracey goto done;
2090 5c65becf 2020-02-13 tracey if (str2 != NULL) {
2091 bf93a5c0 2020-02-15 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2092 bf93a5c0 2020-02-15 tracey KATTR_ID, "refs_str", KATTR__MAX);
2093 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
2094 bf93a5c0 2020-02-15 tracey goto done;
2095 5c65becf 2020-02-13 tracey kerr = khtml_puts(gw_trans->gw_html_req, "(");
2096 5c65becf 2020-02-13 tracey if (kerr != KCGI_OK)
2097 5c65becf 2020-02-13 tracey goto done;
2098 5c65becf 2020-02-13 tracey kerr = khtml_puts(gw_trans->gw_html_req, str2);
2099 5c65becf 2020-02-13 tracey if (kerr != KCGI_OK)
2100 5c65becf 2020-02-13 tracey goto done;
2101 5c65becf 2020-02-13 tracey kerr = khtml_puts(gw_trans->gw_html_req, ")");
2102 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
2103 bf93a5c0 2020-02-15 tracey goto done;
2104 bf93a5c0 2020-02-15 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2105 5c65becf 2020-02-13 tracey if (kerr != KCGI_OK)
2106 5c65becf 2020-02-13 tracey goto done;
2107 5c65becf 2020-02-13 tracey }
2108 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2109 21a55cc7 2020-02-04 tracey done:
2110 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
2111 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2112 21a55cc7 2020-02-04 tracey return error;
2113 21a55cc7 2020-02-04 tracey }
2114 21a55cc7 2020-02-04 tracey
2115 f7cc9480 2020-02-05 tracey static const struct got_error *
2116 f7cc9480 2020-02-05 tracey gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2117 f2f46662 2020-01-23 tracey {
2118 27192be7 2020-02-04 tracey const struct got_error *error = NULL;
2119 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
2120 f2f46662 2020-01-23 tracey
2121 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2122 f7cc9480 2020-02-05 tracey KATTR_ID, "header_diff_title", KATTR__MAX);
2123 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2124 f7cc9480 2020-02-05 tracey goto done;
2125 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2126 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2127 f7cc9480 2020-02-05 tracey goto done;
2128 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2129 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2130 f7cc9480 2020-02-05 tracey goto done;
2131 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2132 f7cc9480 2020-02-05 tracey KATTR_ID, "header_diff", KATTR__MAX);
2133 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2134 f7cc9480 2020-02-05 tracey goto done;
2135 5c65becf 2020-02-13 tracey if (str1 != NULL) {
2136 5c65becf 2020-02-13 tracey kerr = khtml_puts(gw_trans->gw_html_req, str1);
2137 5c65becf 2020-02-13 tracey if (kerr != KCGI_OK)
2138 5c65becf 2020-02-13 tracey goto done;
2139 5c65becf 2020-02-13 tracey }
2140 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2141 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2142 f7cc9480 2020-02-05 tracey goto done;
2143 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, str2);
2144 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2145 f7cc9480 2020-02-05 tracey goto done;
2146 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2147 f7cc9480 2020-02-05 tracey done:
2148 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
2149 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2150 f7cc9480 2020-02-05 tracey return error;
2151 21a55cc7 2020-02-04 tracey }
2152 21a55cc7 2020-02-04 tracey
2153 21a55cc7 2020-02-04 tracey static const struct got_error *
2154 21a55cc7 2020-02-04 tracey gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2155 21a55cc7 2020-02-04 tracey {
2156 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
2157 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
2158 21a55cc7 2020-02-04 tracey
2159 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2160 21a55cc7 2020-02-04 tracey KATTR_ID, "header_age_title", KATTR__MAX);
2161 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2162 21a55cc7 2020-02-04 tracey goto done;
2163 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2164 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2165 21a55cc7 2020-02-04 tracey goto done;
2166 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2167 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2168 21a55cc7 2020-02-04 tracey goto done;
2169 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2170 21a55cc7 2020-02-04 tracey KATTR_ID, "header_age", KATTR__MAX);
2171 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2172 21a55cc7 2020-02-04 tracey goto done;
2173 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, str);
2174 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2175 21a55cc7 2020-02-04 tracey goto done;
2176 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2177 21a55cc7 2020-02-04 tracey done:
2178 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
2179 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2180 21a55cc7 2020-02-04 tracey return error;
2181 21a55cc7 2020-02-04 tracey }
2182 21a55cc7 2020-02-04 tracey
2183 21a55cc7 2020-02-04 tracey static const struct got_error *
2184 21a55cc7 2020-02-04 tracey gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2185 21a55cc7 2020-02-04 tracey {
2186 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
2187 21a55cc7 2020-02-04 tracey enum kcgi_err kerr;
2188 21a55cc7 2020-02-04 tracey
2189 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2190 21a55cc7 2020-02-04 tracey KATTR_ID, "header_author_title", KATTR__MAX);
2191 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2192 21a55cc7 2020-02-04 tracey goto done;
2193 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2194 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2195 21a55cc7 2020-02-04 tracey goto done;
2196 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2197 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2198 21a55cc7 2020-02-04 tracey goto done;
2199 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2200 21a55cc7 2020-02-04 tracey KATTR_ID, "header_author", KATTR__MAX);
2201 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2202 21a55cc7 2020-02-04 tracey goto done;
2203 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, str);
2204 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2205 21a55cc7 2020-02-04 tracey goto done;
2206 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2207 21a55cc7 2020-02-04 tracey done:
2208 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
2209 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2210 21a55cc7 2020-02-04 tracey return error;
2211 f2f46662 2020-01-23 tracey }
2212 f2f46662 2020-01-23 tracey
2213 21a55cc7 2020-02-04 tracey static const struct got_error *
2214 21a55cc7 2020-02-04 tracey gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2215 21a55cc7 2020-02-04 tracey {
2216 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
2217 21a55cc7 2020-02-04 tracey enum kcgi_err kerr;
2218 21a55cc7 2020-02-04 tracey
2219 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2220 21a55cc7 2020-02-04 tracey KATTR_ID, "header_committer_title", KATTR__MAX);
2221 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2222 21a55cc7 2020-02-04 tracey goto done;
2223 87ca24ac 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2224 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2225 21a55cc7 2020-02-04 tracey goto done;
2226 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2227 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2228 21a55cc7 2020-02-04 tracey goto done;
2229 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2230 21a55cc7 2020-02-04 tracey KATTR_ID, "header_committer", KATTR__MAX);
2231 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2232 21a55cc7 2020-02-04 tracey goto done;
2233 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, str);
2234 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2235 21a55cc7 2020-02-04 tracey goto done;
2236 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2237 21a55cc7 2020-02-04 tracey done:
2238 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
2239 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2240 21a55cc7 2020-02-04 tracey return error;
2241 21a55cc7 2020-02-04 tracey }
2242 21a55cc7 2020-02-04 tracey
2243 f7cc9480 2020-02-05 tracey static const struct got_error *
2244 f7cc9480 2020-02-05 tracey gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2245 f2f46662 2020-01-23 tracey {
2246 27192be7 2020-02-04 tracey const struct got_error *error = NULL;
2247 f7cc9480 2020-02-05 tracey enum kcgi_err kerr;
2248 f2f46662 2020-01-23 tracey
2249 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2250 f7cc9480 2020-02-05 tracey KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2251 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2252 f7cc9480 2020-02-05 tracey goto done;
2253 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2254 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2255 f7cc9480 2020-02-05 tracey goto done;
2256 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2257 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2258 f7cc9480 2020-02-05 tracey goto done;
2259 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2260 f7cc9480 2020-02-05 tracey KATTR_ID, "header_commit_msg", KATTR__MAX);
2261 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2262 f7cc9480 2020-02-05 tracey goto done;
2263 f7cc9480 2020-02-05 tracey kerr = khttp_puts(gw_trans->gw_req, str);
2264 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2265 f7cc9480 2020-02-05 tracey goto done;
2266 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2267 f7cc9480 2020-02-05 tracey done:
2268 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
2269 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2270 f7cc9480 2020-02-05 tracey return error;
2271 f2f46662 2020-01-23 tracey }
2272 f2f46662 2020-01-23 tracey
2273 f7cc9480 2020-02-05 tracey static const struct got_error *
2274 f7cc9480 2020-02-05 tracey gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2275 f2f46662 2020-01-23 tracey {
2276 f7cc9480 2020-02-05 tracey const struct got_error *error = NULL;
2277 f7cc9480 2020-02-05 tracey enum kcgi_err kerr;
2278 f2f46662 2020-01-23 tracey
2279 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2280 f7cc9480 2020-02-05 tracey KATTR_ID, "header_tree_title", KATTR__MAX);
2281 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2282 f7cc9480 2020-02-05 tracey goto done;
2283 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2284 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2285 f7cc9480 2020-02-05 tracey goto done;
2286 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2287 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2288 f7cc9480 2020-02-05 tracey goto done;
2289 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2290 f7cc9480 2020-02-05 tracey KATTR_ID, "header_tree", KATTR__MAX);
2291 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2292 f7cc9480 2020-02-05 tracey goto done;
2293 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, str);
2294 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2295 f7cc9480 2020-02-05 tracey goto done;
2296 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2297 f7cc9480 2020-02-05 tracey done:
2298 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
2299 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2300 f7cc9480 2020-02-05 tracey return error;
2301 f2f46662 2020-01-23 tracey }
2302 f2f46662 2020-01-23 tracey
2303 32cd4d18 2020-02-03 stsp static const struct got_error *
2304 32cd4d18 2020-02-03 stsp gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2305 32cd4d18 2020-02-03 stsp char *dir)
2306 2c251c14 2020-01-15 tracey {
2307 32cd4d18 2020-02-03 stsp const struct got_error *error = NULL;
2308 b8b04565 2020-02-02 tracey FILE *f = NULL;
2309 32cd4d18 2020-02-03 stsp char *d_file = NULL;
2310 2c251c14 2020-01-15 tracey unsigned int len;
2311 1ab830c1 2020-02-03 stsp size_t n;
2312 2c251c14 2020-01-15 tracey
2313 32cd4d18 2020-02-03 stsp *description = NULL;
2314 0b20762b 2020-01-29 stsp if (gw_trans->gw_conf->got_show_repo_description == 0)
2315 a942aa37 2020-02-10 tracey return NULL;
2316 2c251c14 2020-01-15 tracey
2317 88d59c36 2020-01-29 stsp if (asprintf(&d_file, "%s/description", dir) == -1)
2318 32cd4d18 2020-02-03 stsp return got_error_from_errno("asprintf");
2319 2c251c14 2020-01-15 tracey
2320 32cd4d18 2020-02-03 stsp f = fopen(d_file, "r");
2321 32cd4d18 2020-02-03 stsp if (f == NULL) {
2322 32cd4d18 2020-02-03 stsp if (errno == ENOENT || errno == EACCES)
2323 a942aa37 2020-02-10 tracey return NULL;
2324 32cd4d18 2020-02-03 stsp error = got_error_from_errno2("fopen", d_file);
2325 32cd4d18 2020-02-03 stsp goto done;
2326 32cd4d18 2020-02-03 stsp }
2327 2c251c14 2020-01-15 tracey
2328 32cd4d18 2020-02-03 stsp if (fseek(f, 0, SEEK_END) == -1) {
2329 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2330 32cd4d18 2020-02-03 stsp goto done;
2331 32cd4d18 2020-02-03 stsp }
2332 32cd4d18 2020-02-03 stsp len = ftell(f);
2333 32cd4d18 2020-02-03 stsp if (len == -1) {
2334 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2335 32cd4d18 2020-02-03 stsp goto done;
2336 32cd4d18 2020-02-03 stsp }
2337 32cd4d18 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
2338 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2339 32cd4d18 2020-02-03 stsp goto done;
2340 32cd4d18 2020-02-03 stsp }
2341 32cd4d18 2020-02-03 stsp *description = calloc(len + 1, sizeof(**description));
2342 32cd4d18 2020-02-03 stsp if (*description == NULL) {
2343 32cd4d18 2020-02-03 stsp error = got_error_from_errno("calloc");
2344 32cd4d18 2020-02-03 stsp goto done;
2345 32cd4d18 2020-02-03 stsp }
2346 32cd4d18 2020-02-03 stsp
2347 32cd4d18 2020-02-03 stsp n = fread(*description, 1, len, f);
2348 1ab830c1 2020-02-03 stsp if (n == 0 && ferror(f))
2349 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2350 32cd4d18 2020-02-03 stsp done:
2351 32cd4d18 2020-02-03 stsp if (f != NULL && fclose(f) == -1 && error == NULL)
2352 32cd4d18 2020-02-03 stsp error = got_error_from_errno("fclose");
2353 2c251c14 2020-01-15 tracey free(d_file);
2354 32cd4d18 2020-02-03 stsp return error;
2355 2c251c14 2020-01-15 tracey }
2356 2c251c14 2020-01-15 tracey
2357 55ccf528 2020-02-03 stsp static const struct got_error *
2358 55ccf528 2020-02-03 stsp gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2359 474370cb 2020-01-15 tracey {
2360 474370cb 2020-01-15 tracey struct tm tm;
2361 474370cb 2020-01-15 tracey time_t diff_time;
2362 474370cb 2020-01-15 tracey char *years = "years ago", *months = "months ago";
2363 474370cb 2020-01-15 tracey char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2364 474370cb 2020-01-15 tracey char *minutes = "minutes ago", *seconds = "seconds ago";
2365 474370cb 2020-01-15 tracey char *now = "right now";
2366 55ccf528 2020-02-03 stsp char *s;
2367 6c6c85af 2020-01-15 tracey char datebuf[29];
2368 474370cb 2020-01-15 tracey
2369 55ccf528 2020-02-03 stsp *repo_age = NULL;
2370 55ccf528 2020-02-03 stsp
2371 474370cb 2020-01-15 tracey switch (ref_tm) {
2372 474370cb 2020-01-15 tracey case TM_DIFF:
2373 474370cb 2020-01-15 tracey diff_time = time(NULL) - committer_time;
2374 474370cb 2020-01-15 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
2375 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2376 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24 / 365), years) == -1)
2377 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2378 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2379 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2380 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
2381 88d59c36 2020-01-29 stsp months) == -1)
2382 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2383 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2384 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2385 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2386 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2387 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
2388 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2389 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24), days) == -1)
2390 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2391 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 2) {
2392 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2393 88d59c36 2020-01-29 stsp (diff_time / 60 / 60), hours) == -1)
2394 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2395 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 2) {
2396 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2397 88d59c36 2020-01-29 stsp minutes) == -1)
2398 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2399 474370cb 2020-01-15 tracey } else if (diff_time > 2) {
2400 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s", diff_time,
2401 88d59c36 2020-01-29 stsp seconds) == -1)
2402 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2403 474370cb 2020-01-15 tracey } else {
2404 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%s", now) == -1)
2405 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2406 474370cb 2020-01-15 tracey }
2407 474370cb 2020-01-15 tracey break;
2408 474370cb 2020-01-15 tracey case TM_LONG:
2409 474370cb 2020-01-15 tracey if (gmtime_r(&committer_time, &tm) == NULL)
2410 55ccf528 2020-02-03 stsp return got_error_from_errno("gmtime_r");
2411 474370cb 2020-01-15 tracey
2412 474370cb 2020-01-15 tracey s = asctime_r(&tm, datebuf);
2413 474370cb 2020-01-15 tracey if (s == NULL)
2414 55ccf528 2020-02-03 stsp return got_error_from_errno("asctime_r");
2415 474370cb 2020-01-15 tracey
2416 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2417 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2418 474370cb 2020-01-15 tracey break;
2419 474370cb 2020-01-15 tracey }
2420 55ccf528 2020-02-03 stsp return NULL;
2421 474370cb 2020-01-15 tracey }
2422 474370cb 2020-01-15 tracey
2423 d126c1b7 2020-01-29 stsp static const struct got_error *
2424 d126c1b7 2020-01-29 stsp gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2425 2011c142 2020-02-14 stsp const char *refname, int ref_tm)
2426 2c251c14 2020-01-15 tracey {
2427 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
2428 2c251c14 2020-01-15 tracey struct got_repository *repo = NULL;
2429 2c251c14 2020-01-15 tracey struct got_commit_object *commit = NULL;
2430 2c251c14 2020-01-15 tracey struct got_reflist_head refs;
2431 2c251c14 2020-01-15 tracey struct got_reflist_entry *re;
2432 474370cb 2020-01-15 tracey time_t committer_time = 0, cmp_time = 0;
2433 a0f36e03 2020-01-28 stsp
2434 d126c1b7 2020-01-29 stsp *repo_age = NULL;
2435 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
2436 387a29ba 2020-01-15 tracey
2437 55ccf528 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_age == 0)
2438 d126c1b7 2020-01-29 stsp return NULL;
2439 87f9ebf5 2020-01-15 tracey
2440 deac617c 2020-02-14 tracey if (gw_trans->repo)
2441 deac617c 2020-02-14 tracey repo = gw_trans->repo;
2442 deac617c 2020-02-14 tracey else {
2443 deac617c 2020-02-14 tracey error = got_repo_open(&repo, dir, NULL);
2444 deac617c 2020-02-14 tracey if (error)
2445 deac617c 2020-02-14 tracey return error;
2446 deac617c 2020-02-14 tracey }
2447 2c251c14 2020-01-15 tracey
2448 2011c142 2020-02-14 stsp error = got_ref_list(&refs, repo, "refs/heads",
2449 2011c142 2020-02-14 stsp got_ref_cmp_by_name, NULL);
2450 ec46ccd7 2020-01-15 tracey if (error)
2451 d126c1b7 2020-01-29 stsp goto done;
2452 2c251c14 2020-01-15 tracey
2453 2011c142 2020-02-14 stsp /*
2454 2011c142 2020-02-14 stsp * Find the youngest branch tip in the repository, or the age of
2455 2011c142 2020-02-14 stsp * the a specific branch tip if a name was provided by the caller.
2456 2011c142 2020-02-14 stsp */
2457 2c251c14 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
2458 2011c142 2020-02-14 stsp struct got_object_id *id = NULL;
2459 17d4bf8d 2020-02-14 stsp
2460 2011c142 2020-02-14 stsp if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2461 2011c142 2020-02-14 stsp continue;
2462 4e8d6e3e 2020-02-14 tracey
2463 2011c142 2020-02-14 stsp error = got_ref_resolve(&id, repo, re->ref);
2464 ec46ccd7 2020-01-15 tracey if (error)
2465 d126c1b7 2020-01-29 stsp goto done;
2466 2c251c14 2020-01-15 tracey
2467 2c251c14 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
2468 2011c142 2020-02-14 stsp free(id);
2469 ec46ccd7 2020-01-15 tracey if (error)
2470 d126c1b7 2020-01-29 stsp goto done;
2471 2c251c14 2020-01-15 tracey
2472 2c251c14 2020-01-15 tracey committer_time =
2473 2c251c14 2020-01-15 tracey got_object_commit_get_committer_time(commit);
2474 2011c142 2020-02-14 stsp got_object_commit_close(commit);
2475 387a29ba 2020-01-15 tracey if (cmp_time < committer_time)
2476 2c251c14 2020-01-15 tracey cmp_time = committer_time;
2477 2011c142 2020-02-14 stsp
2478 2011c142 2020-02-14 stsp if (refname)
2479 2011c142 2020-02-14 stsp break;
2480 2c251c14 2020-01-15 tracey }
2481 2c251c14 2020-01-15 tracey
2482 474370cb 2020-01-15 tracey if (cmp_time != 0) {
2483 2c251c14 2020-01-15 tracey committer_time = cmp_time;
2484 55ccf528 2020-02-03 stsp error = gw_get_time_str(repo_age, committer_time, ref_tm);
2485 d126c1b7 2020-01-29 stsp }
2486 d126c1b7 2020-01-29 stsp done:
2487 2c251c14 2020-01-15 tracey got_ref_list_free(&refs);
2488 deac617c 2020-02-14 tracey if (gw_trans->repo == NULL)
2489 deac617c 2020-02-14 tracey got_repo_close(repo);
2490 d126c1b7 2020-01-29 stsp return error;
2491 ec46ccd7 2020-01-15 tracey }
2492 ec46ccd7 2020-01-15 tracey
2493 83eb9a68 2020-02-03 stsp static const struct got_error *
2494 38b240fb 2020-02-06 tracey gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2495 ec46ccd7 2020-01-15 tracey {
2496 ec46ccd7 2020-01-15 tracey const struct got_error *error;
2497 ec46ccd7 2020-01-15 tracey FILE *f = NULL;
2498 ec46ccd7 2020-01-15 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
2499 f7cc9480 2020-02-05 tracey char *label1 = NULL, *label2 = NULL, *line = NULL;
2500 05ce9a79 2020-01-24 tracey int obj_type;
2501 f7cc9480 2020-02-05 tracey size_t linesize = 0;
2502 83eb9a68 2020-02-03 stsp ssize_t linelen;
2503 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
2504 ec46ccd7 2020-01-15 tracey
2505 ec46ccd7 2020-01-15 tracey f = got_opentemp();
2506 ec46ccd7 2020-01-15 tracey if (f == NULL)
2507 ec46ccd7 2020-01-15 tracey return NULL;
2508 ec46ccd7 2020-01-15 tracey
2509 5c65becf 2020-02-13 tracey if (header->parent_id != NULL &&
2510 5c65becf 2020-02-13 tracey strncmp(header->parent_id, "/dev/null", 9) != 0) {
2511 05ce9a79 2020-01-24 tracey error = got_repo_match_object_id(&id1, &label1,
2512 deac617c 2020-02-14 tracey header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2513 05ce9a79 2020-01-24 tracey if (error)
2514 05ce9a79 2020-01-24 tracey goto done;
2515 05ce9a79 2020-01-24 tracey }
2516 ec46ccd7 2020-01-15 tracey
2517 90f16cb8 2020-01-24 tracey error = got_repo_match_object_id(&id2, &label2,
2518 deac617c 2020-02-14 tracey header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2519 90f16cb8 2020-01-24 tracey if (error)
2520 90f16cb8 2020-01-24 tracey goto done;
2521 ec46ccd7 2020-01-15 tracey
2522 deac617c 2020-02-14 tracey error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2523 90f16cb8 2020-01-24 tracey if (error)
2524 90f16cb8 2020-01-24 tracey goto done;
2525 05ce9a79 2020-01-24 tracey switch (obj_type) {
2526 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_BLOB:
2527 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2528 deac617c 2020-02-14 tracey gw_trans->repo, f);
2529 ec46ccd7 2020-01-15 tracey break;
2530 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_TREE:
2531 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2532 deac617c 2020-02-14 tracey gw_trans->repo, f);
2533 ec46ccd7 2020-01-15 tracey break;
2534 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_COMMIT:
2535 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_commits(id1, id2, 3, 0,
2536 deac617c 2020-02-14 tracey gw_trans->repo, f);
2537 ec46ccd7 2020-01-15 tracey break;
2538 ec46ccd7 2020-01-15 tracey default:
2539 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2540 ec46ccd7 2020-01-15 tracey }
2541 b8b04565 2020-02-02 tracey if (error)
2542 b8b04565 2020-02-02 tracey goto done;
2543 b8b04565 2020-02-02 tracey
2544 d4729381 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
2545 d4729381 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2546 ec46ccd7 2020-01-15 tracey goto done;
2547 d4729381 2020-02-03 stsp }
2548 ec46ccd7 2020-01-15 tracey
2549 83eb9a68 2020-02-03 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
2550 f7cc9480 2020-02-05 tracey error = gw_colordiff_line(gw_trans, line);
2551 ec46ccd7 2020-01-15 tracey if (error)
2552 b8b04565 2020-02-02 tracey goto done;
2553 f7cc9480 2020-02-05 tracey /* XXX: KHTML_PRETTY breaks this */
2554 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, line);
2555 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2556 83eb9a68 2020-02-03 stsp goto done;
2557 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2558 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2559 b8b04565 2020-02-02 tracey goto done;
2560 ec46ccd7 2020-01-15 tracey }
2561 f7cc9480 2020-02-05 tracey if (linelen == -1 && ferror(f))
2562 83eb9a68 2020-02-03 stsp error = got_error_from_errno("getline");
2563 ec46ccd7 2020-01-15 tracey done:
2564 d4729381 2020-02-03 stsp if (f && fclose(f) == -1 && error == NULL)
2565 d4729381 2020-02-03 stsp error = got_error_from_errno("fclose");
2566 d4729381 2020-02-03 stsp free(line);
2567 ec46ccd7 2020-01-15 tracey free(label1);
2568 ec46ccd7 2020-01-15 tracey free(label2);
2569 ec46ccd7 2020-01-15 tracey free(id1);
2570 ec46ccd7 2020-01-15 tracey free(id2);
2571 ec46ccd7 2020-01-15 tracey
2572 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
2573 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2574 83eb9a68 2020-02-03 stsp return error;
2575 2c251c14 2020-01-15 tracey }
2576 2c251c14 2020-01-15 tracey
2577 9a1cc63f 2020-02-03 stsp static const struct got_error *
2578 9a1cc63f 2020-02-03 stsp gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2579 9a1cc63f 2020-02-03 stsp {
2580 9a1cc63f 2020-02-03 stsp const struct got_error *error = NULL;
2581 9a1cc63f 2020-02-03 stsp struct got_repository *repo;
2582 9a1cc63f 2020-02-03 stsp const char *gitconfig_owner;
2583 2c251c14 2020-01-15 tracey
2584 9a1cc63f 2020-02-03 stsp *owner = NULL;
2585 2c251c14 2020-01-15 tracey
2586 9a1cc63f 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_owner == 0)
2587 9a1cc63f 2020-02-03 stsp return NULL;
2588 2c251c14 2020-01-15 tracey
2589 9a1cc63f 2020-02-03 stsp error = got_repo_open(&repo, dir, NULL);
2590 9a1cc63f 2020-02-03 stsp if (error)
2591 9a1cc63f 2020-02-03 stsp return error;
2592 9a1cc63f 2020-02-03 stsp gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2593 d1635ae4 2020-02-13 tracey if (gitconfig_owner) {
2594 d1635ae4 2020-02-13 tracey *owner = strdup(gitconfig_owner);
2595 d1635ae4 2020-02-13 tracey if (*owner == NULL)
2596 d1635ae4 2020-02-13 tracey error = got_error_from_errno("strdup");
2597 d1635ae4 2020-02-13 tracey }
2598 9a1cc63f 2020-02-03 stsp got_repo_close(repo);
2599 9a1cc63f 2020-02-03 stsp return error;
2600 2c251c14 2020-01-15 tracey }
2601 2c251c14 2020-01-15 tracey
2602 59d3c40e 2020-02-03 stsp static const struct got_error *
2603 59d3c40e 2020-02-03 stsp gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2604 2c251c14 2020-01-15 tracey {
2605 59d3c40e 2020-02-03 stsp const struct got_error *error = NULL;
2606 2c251c14 2020-01-15 tracey FILE *f;
2607 59d3c40e 2020-02-03 stsp char *d_file = NULL;
2608 2c251c14 2020-01-15 tracey unsigned int len;
2609 59d3c40e 2020-02-03 stsp size_t n;
2610 2c251c14 2020-01-15 tracey
2611 59d3c40e 2020-02-03 stsp *url = NULL;
2612 2c251c14 2020-01-15 tracey
2613 59d3c40e 2020-02-03 stsp if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2614 59d3c40e 2020-02-03 stsp return got_error_from_errno("asprintf");
2615 2c251c14 2020-01-15 tracey
2616 59d3c40e 2020-02-03 stsp f = fopen(d_file, "r");
2617 59d3c40e 2020-02-03 stsp if (f == NULL) {
2618 59d3c40e 2020-02-03 stsp if (errno != ENOENT && errno != EACCES)
2619 59d3c40e 2020-02-03 stsp error = got_error_from_errno2("fopen", d_file);
2620 59d3c40e 2020-02-03 stsp goto done;
2621 59d3c40e 2020-02-03 stsp }
2622 59d3c40e 2020-02-03 stsp
2623 59d3c40e 2020-02-03 stsp if (fseek(f, 0, SEEK_END) == -1) {
2624 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2625 59d3c40e 2020-02-03 stsp goto done;
2626 59d3c40e 2020-02-03 stsp }
2627 59d3c40e 2020-02-03 stsp len = ftell(f);
2628 59d3c40e 2020-02-03 stsp if (len == -1) {
2629 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2630 59d3c40e 2020-02-03 stsp goto done;
2631 59d3c40e 2020-02-03 stsp }
2632 59d3c40e 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
2633 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2634 59d3c40e 2020-02-03 stsp goto done;
2635 59d3c40e 2020-02-03 stsp }
2636 2c251c14 2020-01-15 tracey
2637 59d3c40e 2020-02-03 stsp *url = calloc(len + 1, sizeof(**url));
2638 59d3c40e 2020-02-03 stsp if (*url == NULL) {
2639 59d3c40e 2020-02-03 stsp error = got_error_from_errno("calloc");
2640 59d3c40e 2020-02-03 stsp goto done;
2641 59d3c40e 2020-02-03 stsp }
2642 2c251c14 2020-01-15 tracey
2643 59d3c40e 2020-02-03 stsp n = fread(*url, 1, len, f);
2644 59d3c40e 2020-02-03 stsp if (n == 0 && ferror(f))
2645 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2646 59d3c40e 2020-02-03 stsp done:
2647 59d3c40e 2020-02-03 stsp if (f && fclose(f) == -1 && error == NULL)
2648 59d3c40e 2020-02-03 stsp error = got_error_from_errno("fclose");
2649 2c251c14 2020-01-15 tracey free(d_file);
2650 59d3c40e 2020-02-03 stsp return NULL;
2651 8d4d2453 2020-01-15 tracey }
2652 8d4d2453 2020-01-15 tracey
2653 6eccd105 2020-02-03 stsp static const struct got_error *
2654 38b240fb 2020-02-06 tracey gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2655 38b240fb 2020-02-06 tracey int limit, int tag_type)
2656 8d4d2453 2020-01-15 tracey {
2657 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
2658 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
2659 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
2660 38b240fb 2020-02-06 tracey char *age = NULL;
2661 ef2c200c 2020-02-07 tracey char *id_str = NULL, *newline, *href_commits = NULL;
2662 38b240fb 2020-02-06 tracey char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2663 6eccd105 2020-02-03 stsp struct got_tag_object *tag = NULL;
2664 38b240fb 2020-02-06 tracey enum kcgi_err kerr = KCGI_OK;
2665 38b240fb 2020-02-06 tracey int summary_header_displayed = 0;
2666 8d4d2453 2020-01-15 tracey
2667 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
2668 a0f36e03 2020-01-28 stsp
2669 deac617c 2020-02-14 tracey error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2670 deac617c 2020-02-14 tracey got_ref_cmp_tags, gw_trans->repo);
2671 ec46ccd7 2020-01-15 tracey if (error)
2672 87f9ebf5 2020-01-15 tracey goto done;
2673 87f9ebf5 2020-01-15 tracey
2674 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
2675 87f9ebf5 2020-01-15 tracey const char *refname;
2676 4ff7391f 2020-01-28 tracey const char *tagger;
2677 6eccd105 2020-02-03 stsp const char *tag_commit;
2678 87f9ebf5 2020-01-15 tracey time_t tagger_time;
2679 87f9ebf5 2020-01-15 tracey struct got_object_id *id;
2680 ef2c200c 2020-02-07 tracey struct got_commit_object *commit = NULL;
2681 87f9ebf5 2020-01-15 tracey
2682 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
2683 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/tags/", 10) != 0)
2684 87f9ebf5 2020-01-15 tracey continue;
2685 87f9ebf5 2020-01-15 tracey refname += 10;
2686 87f9ebf5 2020-01-15 tracey
2687 deac617c 2020-02-14 tracey error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2688 87f9ebf5 2020-01-15 tracey if (error)
2689 87f9ebf5 2020-01-15 tracey goto done;
2690 38b240fb 2020-02-06 tracey
2691 deac617c 2020-02-14 tracey error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2692 38b240fb 2020-02-06 tracey if (error) {
2693 ef2c200c 2020-02-07 tracey if (error->code != GOT_ERR_OBJ_TYPE) {
2694 ef2c200c 2020-02-07 tracey free(id);
2695 ef2c200c 2020-02-07 tracey goto done;
2696 ef2c200c 2020-02-07 tracey }
2697 ef2c200c 2020-02-07 tracey /* "lightweight" tag */
2698 deac617c 2020-02-14 tracey error = got_object_open_as_commit(&commit,
2699 deac617c 2020-02-14 tracey gw_trans->repo, id);
2700 ef2c200c 2020-02-07 tracey if (error) {
2701 ef2c200c 2020-02-07 tracey free(id);
2702 ef2c200c 2020-02-07 tracey goto done;
2703 ef2c200c 2020-02-07 tracey }
2704 ef2c200c 2020-02-07 tracey tagger = got_object_commit_get_committer(commit);
2705 ef2c200c 2020-02-07 tracey tagger_time =
2706 ef2c200c 2020-02-07 tracey got_object_commit_get_committer_time(commit);
2707 ef2c200c 2020-02-07 tracey error = got_object_id_str(&id_str, id);
2708 ef2c200c 2020-02-07 tracey free(id);
2709 ef2c200c 2020-02-07 tracey } else {
2710 ef2c200c 2020-02-07 tracey free(id);
2711 ef2c200c 2020-02-07 tracey tagger = got_object_tag_get_tagger(tag);
2712 ef2c200c 2020-02-07 tracey tagger_time = got_object_tag_get_tagger_time(tag);
2713 ef2c200c 2020-02-07 tracey error = got_object_id_str(&id_str,
2714 ef2c200c 2020-02-07 tracey got_object_tag_get_object_id(tag));
2715 38b240fb 2020-02-06 tracey }
2716 87f9ebf5 2020-01-15 tracey if (error)
2717 87f9ebf5 2020-01-15 tracey goto done;
2718 87f9ebf5 2020-01-15 tracey
2719 38b240fb 2020-02-06 tracey if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2720 38b240fb 2020-02-06 tracey strlen(id_str)) != 0)
2721 38b240fb 2020-02-06 tracey continue;
2722 38b240fb 2020-02-06 tracey
2723 ef2c200c 2020-02-07 tracey if (commit) {
2724 ef2c200c 2020-02-07 tracey error = got_object_commit_get_logmsg(&tag_commit0,
2725 ef2c200c 2020-02-07 tracey commit);
2726 ef2c200c 2020-02-07 tracey if (error)
2727 ef2c200c 2020-02-07 tracey goto done;
2728 ef2c200c 2020-02-07 tracey got_object_commit_close(commit);
2729 ef2c200c 2020-02-07 tracey } else {
2730 ef2c200c 2020-02-07 tracey tag_commit0 = strdup(got_object_tag_get_message(tag));
2731 ef2c200c 2020-02-07 tracey if (tag_commit0 == NULL) {
2732 ef2c200c 2020-02-07 tracey error = got_error_from_errno("strdup");
2733 ef2c200c 2020-02-07 tracey goto done;
2734 ef2c200c 2020-02-07 tracey }
2735 87f9ebf5 2020-01-15 tracey }
2736 87f9ebf5 2020-01-15 tracey
2737 f2f46662 2020-01-23 tracey tag_commit = tag_commit0;
2738 f2f46662 2020-01-23 tracey while (*tag_commit == '\n')
2739 f2f46662 2020-01-23 tracey tag_commit++;
2740 87f9ebf5 2020-01-15 tracey
2741 87f9ebf5 2020-01-15 tracey switch (tag_type) {
2742 87f9ebf5 2020-01-15 tracey case TAGBRIEF:
2743 f2f46662 2020-01-23 tracey newline = strchr(tag_commit, '\n');
2744 87f9ebf5 2020-01-15 tracey if (newline)
2745 87f9ebf5 2020-01-15 tracey *newline = '\0';
2746 87f9ebf5 2020-01-15 tracey
2747 38b240fb 2020-02-06 tracey if (summary_header_displayed == 0) {
2748 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req,
2749 38b240fb 2020-02-06 tracey KELEM_DIV, KATTR_ID,
2750 38b240fb 2020-02-06 tracey "summary_tags_title_wrapper", KATTR__MAX);
2751 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2752 38b240fb 2020-02-06 tracey goto done;
2753 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req,
2754 38b240fb 2020-02-06 tracey KELEM_DIV, KATTR_ID,
2755 38b240fb 2020-02-06 tracey "summary_tags_title", KATTR__MAX);
2756 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2757 38b240fb 2020-02-06 tracey goto done;
2758 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req,
2759 38b240fb 2020-02-06 tracey "Tags");
2760 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2761 38b240fb 2020-02-06 tracey goto done;
2762 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req,
2763 38b240fb 2020-02-06 tracey 2);
2764 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2765 38b240fb 2020-02-06 tracey goto done;
2766 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req,
2767 38b240fb 2020-02-06 tracey KELEM_DIV, KATTR_ID,
2768 38b240fb 2020-02-06 tracey "summary_tags_content", KATTR__MAX);
2769 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2770 38b240fb 2020-02-06 tracey goto done;
2771 38b240fb 2020-02-06 tracey summary_header_displayed = 1;
2772 38b240fb 2020-02-06 tracey }
2773 38b240fb 2020-02-06 tracey
2774 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2775 38b240fb 2020-02-06 tracey KATTR_ID, "tags_wrapper", KATTR__MAX);
2776 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2777 38b240fb 2020-02-06 tracey goto done;
2778 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2779 38b240fb 2020-02-06 tracey KATTR_ID, "tags_age", KATTR__MAX);
2780 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2781 38b240fb 2020-02-06 tracey goto done;
2782 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2783 55ccf528 2020-02-03 stsp if (error)
2784 87f9ebf5 2020-01-15 tracey goto done;
2785 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req,
2786 38b240fb 2020-02-06 tracey age ? age : "");
2787 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2788 38b240fb 2020-02-06 tracey goto done;
2789 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2790 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2791 38b240fb 2020-02-06 tracey goto done;
2792 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2793 38b240fb 2020-02-06 tracey KATTR_ID, "tags", KATTR__MAX);
2794 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2795 38b240fb 2020-02-06 tracey goto done;
2796 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, refname);
2797 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2798 38b240fb 2020-02-06 tracey goto done;
2799 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2800 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2801 38b240fb 2020-02-06 tracey goto done;
2802 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2803 38b240fb 2020-02-06 tracey KATTR_ID, "tags_name", KATTR__MAX);
2804 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2805 dad0fde4 2020-02-06 tracey goto done;
2806 dad0fde4 2020-02-06 tracey if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2807 dad0fde4 2020-02-06 tracey gw_trans->repo_name, id_str) == -1) {
2808 dad0fde4 2020-02-06 tracey error = got_error_from_errno("asprintf");
2809 38b240fb 2020-02-06 tracey goto done;
2810 dad0fde4 2020-02-06 tracey }
2811 056e30c8 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2812 056e30c8 2020-02-06 tracey KATTR_HREF, href_tag, KATTR__MAX);
2813 056e30c8 2020-02-06 tracey if (kerr != KCGI_OK)
2814 056e30c8 2020-02-06 tracey goto done;
2815 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2816 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2817 38b240fb 2020-02-06 tracey goto done;
2818 dad0fde4 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2819 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2820 38b240fb 2020-02-06 tracey goto done;
2821 87f9ebf5 2020-01-15 tracey
2822 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2823 38b240fb 2020-02-06 tracey KATTR_ID, "navs_wrapper", KATTR__MAX);
2824 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2825 38b240fb 2020-02-06 tracey goto done;
2826 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2827 38b240fb 2020-02-06 tracey KATTR_ID, "navs", KATTR__MAX);
2828 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2829 38b240fb 2020-02-06 tracey goto done;
2830 38b240fb 2020-02-06 tracey
2831 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2832 38b240fb 2020-02-06 tracey KATTR_HREF, href_tag, KATTR__MAX);
2833 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2834 38b240fb 2020-02-06 tracey goto done;
2835 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2836 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2837 38b240fb 2020-02-06 tracey goto done;
2838 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2839 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2840 38b240fb 2020-02-06 tracey goto done;
2841 87f9ebf5 2020-01-15 tracey
2842 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2843 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2844 38b240fb 2020-02-06 tracey goto done;
2845 38b240fb 2020-02-06 tracey if (asprintf(&href_briefs,
2846 38b240fb 2020-02-06 tracey "?path=%s&action=briefs&commit=%s",
2847 38b240fb 2020-02-06 tracey gw_trans->repo_name, id_str) == -1) {
2848 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2849 87f9ebf5 2020-01-15 tracey goto done;
2850 87f9ebf5 2020-01-15 tracey }
2851 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2852 38b240fb 2020-02-06 tracey KATTR_HREF, href_briefs, KATTR__MAX);
2853 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2854 38b240fb 2020-02-06 tracey goto done;
2855 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req,
2856 38b240fb 2020-02-06 tracey "commit briefs");
2857 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2858 38b240fb 2020-02-06 tracey goto done;
2859 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2860 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2861 38b240fb 2020-02-06 tracey goto done;
2862 87f9ebf5 2020-01-15 tracey
2863 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2864 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2865 38b240fb 2020-02-06 tracey goto done;
2866 38b240fb 2020-02-06 tracey
2867 38b240fb 2020-02-06 tracey if (asprintf(&href_commits,
2868 38b240fb 2020-02-06 tracey "?path=%s&action=commits&commit=%s",
2869 38b240fb 2020-02-06 tracey gw_trans->repo_name, id_str) == -1) {
2870 38b240fb 2020-02-06 tracey error = got_error_from_errno("asprintf");
2871 38b240fb 2020-02-06 tracey goto done;
2872 38b240fb 2020-02-06 tracey }
2873 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2874 38b240fb 2020-02-06 tracey KATTR_HREF, href_commits, KATTR__MAX);
2875 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2876 38b240fb 2020-02-06 tracey goto done;
2877 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req,
2878 38b240fb 2020-02-06 tracey "commits");
2879 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2880 38b240fb 2020-02-06 tracey goto done;
2881 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2882 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2883 38b240fb 2020-02-06 tracey goto done;
2884 38b240fb 2020-02-06 tracey
2885 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2886 38b240fb 2020-02-06 tracey KATTR_ID, "dotted_line", KATTR__MAX);
2887 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2888 38b240fb 2020-02-06 tracey goto done;
2889 ef2c200c 2020-02-07 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2890 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2891 38b240fb 2020-02-06 tracey goto done;
2892 87f9ebf5 2020-01-15 tracey break;
2893 87f9ebf5 2020-01-15 tracey case TAGFULL:
2894 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2895 38b240fb 2020-02-06 tracey KATTR_ID, "tag_info_date_title", KATTR__MAX);
2896 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2897 38b240fb 2020-02-06 tracey goto done;
2898 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2899 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2900 38b240fb 2020-02-06 tracey goto done;
2901 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2902 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2903 38b240fb 2020-02-06 tracey goto done;
2904 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2905 38b240fb 2020-02-06 tracey KATTR_ID, "tag_info_date", KATTR__MAX);
2906 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2907 38b240fb 2020-02-06 tracey goto done;
2908 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, tagger_time, TM_LONG);
2909 55ccf528 2020-02-03 stsp if (error)
2910 4ff7391f 2020-01-28 tracey goto done;
2911 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req,
2912 38b240fb 2020-02-06 tracey age ? age : "");
2913 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2914 070fee27 2020-02-03 stsp goto done;
2915 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2916 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2917 38b240fb 2020-02-06 tracey goto done;
2918 38b240fb 2020-02-06 tracey
2919 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2920 38b240fb 2020-02-06 tracey KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2921 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2922 38b240fb 2020-02-06 tracey goto done;
2923 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2924 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2925 38b240fb 2020-02-06 tracey goto done;
2926 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2927 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2928 38b240fb 2020-02-06 tracey goto done;
2929 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2930 38b240fb 2020-02-06 tracey KATTR_ID, "tag_info_date", KATTR__MAX);
2931 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2932 38b240fb 2020-02-06 tracey goto done;
2933 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2934 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2935 38b240fb 2020-02-06 tracey goto done;
2936 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2937 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2938 38b240fb 2020-02-06 tracey goto done;
2939 38b240fb 2020-02-06 tracey
2940 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2941 38b240fb 2020-02-06 tracey KATTR_ID, "tag_info", KATTR__MAX);
2942 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2943 38b240fb 2020-02-06 tracey goto done;
2944 78a9dab5 2020-02-07 tracey kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2945 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2946 4ff7391f 2020-01-28 tracey goto done;
2947 87f9ebf5 2020-01-15 tracey break;
2948 87f9ebf5 2020-01-15 tracey default:
2949 87f9ebf5 2020-01-15 tracey break;
2950 87f9ebf5 2020-01-15 tracey }
2951 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2952 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2953 6eccd105 2020-02-03 stsp goto done;
2954 87ca24ac 2020-02-04 tracey
2955 6eccd105 2020-02-03 stsp if (limit && --limit == 0)
2956 6eccd105 2020-02-03 stsp break;
2957 87f9ebf5 2020-01-15 tracey
2958 ef2c200c 2020-02-07 tracey if (tag)
2959 ef2c200c 2020-02-07 tracey got_object_tag_close(tag);
2960 6eccd105 2020-02-03 stsp tag = NULL;
2961 6c6c85af 2020-01-15 tracey free(id_str);
2962 6eccd105 2020-02-03 stsp id_str = NULL;
2963 6c6c85af 2020-01-15 tracey free(age);
2964 55ccf528 2020-02-03 stsp age = NULL;
2965 f2f46662 2020-01-23 tracey free(tag_commit0);
2966 6eccd105 2020-02-03 stsp tag_commit0 = NULL;
2967 38b240fb 2020-02-06 tracey free(href_tag);
2968 38b240fb 2020-02-06 tracey href_tag = NULL;
2969 38b240fb 2020-02-06 tracey free(href_briefs);
2970 38b240fb 2020-02-06 tracey href_briefs = NULL;
2971 38b240fb 2020-02-06 tracey free(href_commits);
2972 38b240fb 2020-02-06 tracey href_commits = NULL;
2973 6c6c85af 2020-01-15 tracey }
2974 87f9ebf5 2020-01-15 tracey done:
2975 6eccd105 2020-02-03 stsp if (tag)
2976 6eccd105 2020-02-03 stsp got_object_tag_close(tag);
2977 6eccd105 2020-02-03 stsp free(id_str);
2978 55ccf528 2020-02-03 stsp free(age);
2979 6eccd105 2020-02-03 stsp free(tag_commit0);
2980 38b240fb 2020-02-06 tracey free(href_tag);
2981 38b240fb 2020-02-06 tracey free(href_briefs);
2982 38b240fb 2020-02-06 tracey free(href_commits);
2983 6eccd105 2020-02-03 stsp got_ref_list_free(&refs);
2984 38b240fb 2020-02-06 tracey if (error == NULL && kerr != KCGI_OK)
2985 38b240fb 2020-02-06 tracey error = gw_kcgi_error(kerr);
2986 6eccd105 2020-02-03 stsp return error;
2987 f2f46662 2020-01-23 tracey }
2988 f2f46662 2020-01-23 tracey
2989 f2f46662 2020-01-23 tracey static void
2990 b0a1bc86 2020-02-14 stsp gw_free_header(struct gw_header *header)
2991 f2f46662 2020-01-23 tracey {
2992 f2f46662 2020-01-23 tracey free(header->path);
2993 4e8d6e3e 2020-02-14 tracey free(header->author);
2994 4e8d6e3e 2020-02-14 tracey free(header->committer);
2995 f2f46662 2020-01-23 tracey free(header->refs_str);
2996 f2f46662 2020-01-23 tracey free(header->commit_id);
2997 f2f46662 2020-01-23 tracey free(header->parent_id);
2998 f2f46662 2020-01-23 tracey free(header->tree_id);
2999 f2f46662 2020-01-23 tracey free(header->commit_msg);
3000 f2f46662 2020-01-23 tracey }
3001 f2f46662 2020-01-23 tracey
3002 f2f46662 2020-01-23 tracey static struct gw_header *
3003 f2f46662 2020-01-23 tracey gw_init_header()
3004 f2f46662 2020-01-23 tracey {
3005 f2f46662 2020-01-23 tracey struct gw_header *header;
3006 f2f46662 2020-01-23 tracey
3007 ae36ed87 2020-01-28 stsp header = malloc(sizeof(*header));
3008 ae36ed87 2020-01-28 stsp if (header == NULL)
3009 f2f46662 2020-01-23 tracey return NULL;
3010 f2f46662 2020-01-23 tracey
3011 f2f46662 2020-01-23 tracey header->path = NULL;
3012 ae36ed87 2020-01-28 stsp SIMPLEQ_INIT(&header->refs);
3013 5c65becf 2020-02-13 tracey
3014 5c65becf 2020-02-13 tracey header->refs_str = NULL;
3015 5c65becf 2020-02-13 tracey header->commit_id = NULL;
3016 f9315db0 2020-02-14 tracey header->committer = NULL;
3017 f9315db0 2020-02-14 tracey header->author = NULL;
3018 5c65becf 2020-02-13 tracey header->parent_id = NULL;
3019 5c65becf 2020-02-13 tracey header->tree_id = NULL;
3020 5c65becf 2020-02-13 tracey header->commit_msg = NULL;
3021 f2f46662 2020-01-23 tracey
3022 f2f46662 2020-01-23 tracey return header;
3023 f2f46662 2020-01-23 tracey }
3024 f2f46662 2020-01-23 tracey
3025 f2f46662 2020-01-23 tracey static const struct got_error *
3026 f2f46662 2020-01-23 tracey gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3027 3c245a0e 2020-02-14 tracey int limit, struct got_object_id *id)
3028 f2f46662 2020-01-23 tracey {
3029 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
3030 f2f46662 2020-01-23 tracey struct got_commit_graph *graph = NULL;
3031 4e8d6e3e 2020-02-14 tracey struct got_commit_object *commit = NULL;
3032 f2f46662 2020-01-23 tracey
3033 f2f46662 2020-01-23 tracey error = got_commit_graph_open(&graph, header->path, 0);
3034 f2f46662 2020-01-23 tracey if (error)
3035 00f13350 2020-02-10 tracey return error;
3036 f2f46662 2020-01-23 tracey
3037 deac617c 2020-02-14 tracey error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3038 3c245a0e 2020-02-14 tracey NULL);
3039 f2f46662 2020-01-23 tracey if (error)
3040 f2f46662 2020-01-23 tracey goto done;
3041 f2f46662 2020-01-23 tracey
3042 f2f46662 2020-01-23 tracey for (;;) {
3043 deac617c 2020-02-14 tracey error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3044 3c245a0e 2020-02-14 tracey NULL, NULL);
3045 f2f46662 2020-01-23 tracey if (error) {
3046 f2f46662 2020-01-23 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
3047 f2f46662 2020-01-23 tracey error = NULL;
3048 f2f46662 2020-01-23 tracey goto done;
3049 f2f46662 2020-01-23 tracey }
3050 3c245a0e 2020-02-14 tracey if (id == NULL)
3051 f2f46662 2020-01-23 tracey goto done;
3052 f2f46662 2020-01-23 tracey
3053 deac617c 2020-02-14 tracey error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3054 4e8d6e3e 2020-02-14 tracey if (error)
3055 4e8d6e3e 2020-02-14 tracey goto done;
3056 4e8d6e3e 2020-02-14 tracey if (limit == 1) {
3057 3c245a0e 2020-02-14 tracey error = gw_get_commit(gw_trans, header, commit, id);
3058 4e8d6e3e 2020-02-14 tracey if (error)
3059 4e8d6e3e 2020-02-14 tracey goto done;
3060 4e8d6e3e 2020-02-14 tracey } else {
3061 f2f46662 2020-01-23 tracey struct gw_header *n_header = NULL;
3062 5147ab56 2020-01-29 stsp if ((n_header = gw_init_header()) == NULL) {
3063 f2f46662 2020-01-23 tracey error = got_error_from_errno("malloc");
3064 a942aa37 2020-02-10 tracey goto done;
3065 d1635ae4 2020-02-13 tracey }
3066 bf93a5c0 2020-02-15 tracey error = got_ref_list(&n_header->refs, gw_trans->repo,
3067 bf93a5c0 2020-02-15 tracey NULL, got_ref_cmp_by_name, NULL);
3068 bf93a5c0 2020-02-15 tracey if (error)
3069 bf93a5c0 2020-02-15 tracey goto done;
3070 bf93a5c0 2020-02-15 tracey
3071 3c245a0e 2020-02-14 tracey error = gw_get_commit(gw_trans, n_header, commit, id);
3072 4e8d6e3e 2020-02-14 tracey if (error)
3073 d1635ae4 2020-02-13 tracey goto done;
3074 bc8c6114 2020-02-17 tracey got_ref_list_free(&n_header->refs);
3075 f2f46662 2020-01-23 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3076 f2f46662 2020-01-23 tracey entry);
3077 f2f46662 2020-01-23 tracey }
3078 f2f46662 2020-01-23 tracey if (error || (limit && --limit == 0))
3079 f2f46662 2020-01-23 tracey break;
3080 f2f46662 2020-01-23 tracey }
3081 f2f46662 2020-01-23 tracey done:
3082 4e8d6e3e 2020-02-14 tracey if (commit != NULL)
3083 4e8d6e3e 2020-02-14 tracey got_object_commit_close(commit);
3084 f2f46662 2020-01-23 tracey if (graph)
3085 f2f46662 2020-01-23 tracey got_commit_graph_close(graph);
3086 f2f46662 2020-01-23 tracey return error;
3087 f2f46662 2020-01-23 tracey }
3088 f2f46662 2020-01-23 tracey
3089 f2f46662 2020-01-23 tracey static const struct got_error *
3090 4e8d6e3e 2020-02-14 tracey gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3091 4e8d6e3e 2020-02-14 tracey struct got_commit_object *commit, struct got_object_id *id)
3092 f2f46662 2020-01-23 tracey {
3093 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
3094 f2f46662 2020-01-23 tracey struct got_reflist_entry *re;
3095 f2f46662 2020-01-23 tracey struct got_object_id *id2 = NULL;
3096 f2f46662 2020-01-23 tracey struct got_object_qid *parent_id;
3097 a81394a7 2020-02-13 stsp char *commit_msg = NULL, *commit_msg0;
3098 f2f46662 2020-01-23 tracey
3099 f2f46662 2020-01-23 tracey /*print commit*/
3100 f2f46662 2020-01-23 tracey SIMPLEQ_FOREACH(re, &header->refs, entry) {
3101 f2f46662 2020-01-23 tracey char *s;
3102 f2f46662 2020-01-23 tracey const char *name;
3103 f2f46662 2020-01-23 tracey struct got_tag_object *tag = NULL;
3104 f2f46662 2020-01-23 tracey int cmp;
3105 f2f46662 2020-01-23 tracey
3106 eb4f3ad3 2020-02-14 stsp if (got_ref_is_symbolic(re->ref))
3107 f2f46662 2020-01-23 tracey continue;
3108 eb4f3ad3 2020-02-14 stsp
3109 eb4f3ad3 2020-02-14 stsp name = got_ref_get_name(re->ref);
3110 f2f46662 2020-01-23 tracey if (strncmp(name, "refs/", 5) == 0)
3111 f2f46662 2020-01-23 tracey name += 5;
3112 f2f46662 2020-01-23 tracey if (strncmp(name, "got/", 4) == 0)
3113 f2f46662 2020-01-23 tracey continue;
3114 f2f46662 2020-01-23 tracey if (strncmp(name, "heads/", 6) == 0)
3115 f2f46662 2020-01-23 tracey name += 6;
3116 f2f46662 2020-01-23 tracey if (strncmp(name, "remotes/", 8) == 0)
3117 f2f46662 2020-01-23 tracey name += 8;
3118 f2f46662 2020-01-23 tracey if (strncmp(name, "tags/", 5) == 0) {
3119 deac617c 2020-02-14 tracey error = got_object_open_as_tag(&tag, gw_trans->repo,
3120 f2f46662 2020-01-23 tracey re->id);
3121 f2f46662 2020-01-23 tracey if (error) {
3122 f2f46662 2020-01-23 tracey if (error->code != GOT_ERR_OBJ_TYPE)
3123 f2f46662 2020-01-23 tracey continue;
3124 f2f46662 2020-01-23 tracey /*
3125 f2f46662 2020-01-23 tracey * Ref points at something other
3126 f2f46662 2020-01-23 tracey * than a tag.
3127 f2f46662 2020-01-23 tracey */
3128 f2f46662 2020-01-23 tracey error = NULL;
3129 f2f46662 2020-01-23 tracey tag = NULL;
3130 f2f46662 2020-01-23 tracey }
3131 f2f46662 2020-01-23 tracey }
3132 f2f46662 2020-01-23 tracey cmp = got_object_id_cmp(tag ?
3133 4e8d6e3e 2020-02-14 tracey got_object_tag_get_object_id(tag) : re->id, id);
3134 f2f46662 2020-01-23 tracey if (tag)
3135 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
3136 f2f46662 2020-01-23 tracey if (cmp != 0)
3137 f2f46662 2020-01-23 tracey continue;
3138 a81394a7 2020-02-13 stsp s = header->refs_str;
3139 a81394a7 2020-02-13 stsp if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3140 88d59c36 2020-01-29 stsp s ? ", " : "", name) == -1) {
3141 f2f46662 2020-01-23 tracey error = got_error_from_errno("asprintf");
3142 f2f46662 2020-01-23 tracey free(s);
3143 a81394a7 2020-02-13 stsp header->refs_str = NULL;
3144 f2f46662 2020-01-23 tracey return error;
3145 f2f46662 2020-01-23 tracey }
3146 f2f46662 2020-01-23 tracey free(s);
3147 f2f46662 2020-01-23 tracey }
3148 f1200fe3 2020-02-13 tracey
3149 4e8d6e3e 2020-02-14 tracey error = got_object_id_str(&header->commit_id, id);
3150 f2f46662 2020-01-23 tracey if (error)
3151 f2f46662 2020-01-23 tracey return error;
3152 f2f46662 2020-01-23 tracey
3153 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->tree_id,
3154 4e8d6e3e 2020-02-14 tracey got_object_commit_get_tree_id(commit));
3155 f2f46662 2020-01-23 tracey if (error)
3156 f2f46662 2020-01-23 tracey return error;
3157 f2f46662 2020-01-23 tracey
3158 f2f46662 2020-01-23 tracey if (gw_trans->action == GW_DIFF) {
3159 f2f46662 2020-01-23 tracey parent_id = SIMPLEQ_FIRST(
3160 4e8d6e3e 2020-02-14 tracey got_object_commit_get_parent_ids(commit));
3161 f2f46662 2020-01-23 tracey if (parent_id != NULL) {
3162 f2f46662 2020-01-23 tracey id2 = got_object_id_dup(parent_id->id);
3163 f2f46662 2020-01-23 tracey free (parent_id);
3164 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->parent_id, id2);
3165 f2f46662 2020-01-23 tracey if (error)
3166 f2f46662 2020-01-23 tracey return error;
3167 f2f46662 2020-01-23 tracey free(id2);
3168 a942aa37 2020-02-10 tracey } else {
3169 d1635ae4 2020-02-13 tracey header->parent_id = strdup("/dev/null");
3170 d1635ae4 2020-02-13 tracey if (header->parent_id == NULL) {
3171 d1635ae4 2020-02-13 tracey error = got_error_from_errno("strdup");
3172 a942aa37 2020-02-10 tracey return error;
3173 d1635ae4 2020-02-13 tracey }
3174 d1635ae4 2020-02-13 tracey }
3175 a942aa37 2020-02-10 tracey }
3176 f2f46662 2020-01-23 tracey
3177 f2f46662 2020-01-23 tracey header->committer_time =
3178 4e8d6e3e 2020-02-14 tracey got_object_commit_get_committer_time(commit);
3179 1177268c 2020-01-28 tracey
3180 d1635ae4 2020-02-13 tracey header->author =
3181 4e8d6e3e 2020-02-14 tracey strdup(got_object_commit_get_author(commit));
3182 4e8d6e3e 2020-02-14 tracey if (header->author == NULL) {
3183 4e8d6e3e 2020-02-14 tracey error = got_error_from_errno("strdup");
3184 4e8d6e3e 2020-02-14 tracey return error;
3185 4e8d6e3e 2020-02-14 tracey }
3186 d1635ae4 2020-02-13 tracey header->committer =
3187 4e8d6e3e 2020-02-14 tracey strdup(got_object_commit_get_committer(commit));
3188 4e8d6e3e 2020-02-14 tracey if (header->committer == NULL) {
3189 4e8d6e3e 2020-02-14 tracey error = got_error_from_errno("strdup");
3190 a942aa37 2020-02-10 tracey return error;
3191 4e8d6e3e 2020-02-14 tracey }
3192 4e8d6e3e 2020-02-14 tracey error = got_object_commit_get_logmsg(&commit_msg0, commit);
3193 f2f46662 2020-01-23 tracey if (error)
3194 f2f46662 2020-01-23 tracey return error;
3195 f2f46662 2020-01-23 tracey
3196 f2f46662 2020-01-23 tracey commit_msg = commit_msg0;
3197 f2f46662 2020-01-23 tracey while (*commit_msg == '\n')
3198 f2f46662 2020-01-23 tracey commit_msg++;
3199 f2f46662 2020-01-23 tracey
3200 d1635ae4 2020-02-13 tracey header->commit_msg = strdup(commit_msg);
3201 d1635ae4 2020-02-13 tracey if (header->commit_msg == NULL)
3202 d1635ae4 2020-02-13 tracey error = got_error_from_errno("strdup");
3203 f2f46662 2020-01-23 tracey free(commit_msg0);
3204 f2f46662 2020-01-23 tracey return error;
3205 f2f46662 2020-01-23 tracey }
3206 f2f46662 2020-01-23 tracey
3207 f2f46662 2020-01-23 tracey static const struct got_error *
3208 f2f46662 2020-01-23 tracey gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3209 f2f46662 2020-01-23 tracey {
3210 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
3211 f2f46662 2020-01-23 tracey char *in_repo_path = NULL;
3212 3c245a0e 2020-02-14 tracey struct got_object_id *id = NULL;
3213 f2f46662 2020-01-23 tracey
3214 deac617c 2020-02-14 tracey error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3215 f2f46662 2020-01-23 tracey if (error)
3216 f2f46662 2020-01-23 tracey return error;
3217 f2f46662 2020-01-23 tracey
3218 56997cd3 2020-02-14 tracey if (gw_trans->commit_id == NULL) {
3219 f2f46662 2020-01-23 tracey struct got_reference *head_ref;
3220 deac617c 2020-02-14 tracey error = got_ref_open(&head_ref, gw_trans->repo,
3221 f2f46662 2020-01-23 tracey gw_trans->headref, 0);
3222 f2f46662 2020-01-23 tracey if (error)
3223 f2f46662 2020-01-23 tracey return error;
3224 f2f46662 2020-01-23 tracey
3225 deac617c 2020-02-14 tracey error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3226 f2f46662 2020-01-23 tracey got_ref_close(head_ref);
3227 f2f46662 2020-01-23 tracey if (error)
3228 f2f46662 2020-01-23 tracey return error;
3229 f2f46662 2020-01-23 tracey } else {
3230 f2f46662 2020-01-23 tracey struct got_reference *ref;
3231 56997cd3 2020-02-14 tracey error = got_ref_open(&ref, gw_trans->repo,
3232 56997cd3 2020-02-14 tracey gw_trans->commit_id, 0);
3233 f2f46662 2020-01-23 tracey if (error == NULL) {
3234 f2f46662 2020-01-23 tracey int obj_type;
3235 deac617c 2020-02-14 tracey error = got_ref_resolve(&id, gw_trans->repo, ref);
3236 f2f46662 2020-01-23 tracey got_ref_close(ref);
3237 f2f46662 2020-01-23 tracey if (error)
3238 f2f46662 2020-01-23 tracey return error;
3239 deac617c 2020-02-14 tracey error = got_object_get_type(&obj_type, gw_trans->repo,
3240 3c245a0e 2020-02-14 tracey id);
3241 f2f46662 2020-01-23 tracey if (error)
3242 4e8d6e3e 2020-02-14 tracey goto done;
3243 f2f46662 2020-01-23 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
3244 f2f46662 2020-01-23 tracey struct got_tag_object *tag;
3245 f2f46662 2020-01-23 tracey error = got_object_open_as_tag(&tag,
3246 deac617c 2020-02-14 tracey gw_trans->repo, id);
3247 f2f46662 2020-01-23 tracey if (error)
3248 4e8d6e3e 2020-02-14 tracey goto done;
3249 f2f46662 2020-01-23 tracey if (got_object_tag_get_object_type(tag) !=
3250 f2f46662 2020-01-23 tracey GOT_OBJ_TYPE_COMMIT) {
3251 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
3252 f2f46662 2020-01-23 tracey error = got_error(GOT_ERR_OBJ_TYPE);
3253 4e8d6e3e 2020-02-14 tracey goto done;
3254 f2f46662 2020-01-23 tracey }
3255 3c245a0e 2020-02-14 tracey free(id);
3256 3c245a0e 2020-02-14 tracey id = got_object_id_dup(
3257 f2f46662 2020-01-23 tracey got_object_tag_get_object_id(tag));
3258 3c245a0e 2020-02-14 tracey if (id == NULL)
3259 f2f46662 2020-01-23 tracey error = got_error_from_errno(
3260 f2f46662 2020-01-23 tracey "got_object_id_dup");
3261 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
3262 f2f46662 2020-01-23 tracey if (error)
3263 4e8d6e3e 2020-02-14 tracey goto done;
3264 f2f46662 2020-01-23 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3265 f2f46662 2020-01-23 tracey error = got_error(GOT_ERR_OBJ_TYPE);
3266 4e8d6e3e 2020-02-14 tracey goto done;
3267 f2f46662 2020-01-23 tracey }
3268 f2f46662 2020-01-23 tracey }
3269 3c245a0e 2020-02-14 tracey error = got_repo_match_object_id_prefix(&id,
3270 56997cd3 2020-02-14 tracey gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3271 deac617c 2020-02-14 tracey gw_trans->repo);
3272 122f3af5 2020-02-14 stsp if (error)
3273 4e8d6e3e 2020-02-14 tracey goto done;
3274 f2f46662 2020-01-23 tracey }
3275 f2f46662 2020-01-23 tracey
3276 deac617c 2020-02-14 tracey error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3277 f2f46662 2020-01-23 tracey gw_trans->repo_path, 1);
3278 f2f46662 2020-01-23 tracey if (error)
3279 4e8d6e3e 2020-02-14 tracey goto done;
3280 f2f46662 2020-01-23 tracey
3281 f2f46662 2020-01-23 tracey if (in_repo_path) {
3282 d1635ae4 2020-02-13 tracey header->path = strdup(in_repo_path);
3283 d1635ae4 2020-02-13 tracey if (header->path == NULL) {
3284 d1635ae4 2020-02-13 tracey error = got_error_from_errno("strdup");
3285 4e8d6e3e 2020-02-14 tracey goto done;
3286 a942aa37 2020-02-10 tracey }
3287 f2f46662 2020-01-23 tracey }
3288 f2f46662 2020-01-23 tracey
3289 deac617c 2020-02-14 tracey error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3290 f2f46662 2020-01-23 tracey got_ref_cmp_by_name, NULL);
3291 f2f46662 2020-01-23 tracey if (error)
3292 4e8d6e3e 2020-02-14 tracey goto done;
3293 f2f46662 2020-01-23 tracey
3294 3c245a0e 2020-02-14 tracey error = gw_get_commits(gw_trans, header, limit, id);
3295 4e8d6e3e 2020-02-14 tracey done:
3296 4e8d6e3e 2020-02-14 tracey got_ref_list_free(&header->refs);
3297 3c245a0e 2020-02-14 tracey free(id);
3298 4e8d6e3e 2020-02-14 tracey free(in_repo_path);
3299 f2f46662 2020-01-23 tracey return error;
3300 ec46ccd7 2020-01-15 tracey }
3301 ec46ccd7 2020-01-15 tracey
3302 ec46ccd7 2020-01-15 tracey struct blame_line {
3303 ec46ccd7 2020-01-15 tracey int annotated;
3304 ec46ccd7 2020-01-15 tracey char *id_str;
3305 ec46ccd7 2020-01-15 tracey char *committer;
3306 ec46ccd7 2020-01-15 tracey char datebuf[11]; /* YYYY-MM-DD + NUL */
3307 ec46ccd7 2020-01-15 tracey };
3308 ec46ccd7 2020-01-15 tracey
3309 147269d5 2020-01-15 tracey struct gw_blame_cb_args {
3310 ec46ccd7 2020-01-15 tracey struct blame_line *lines;
3311 ec46ccd7 2020-01-15 tracey int nlines;
3312 ec46ccd7 2020-01-15 tracey int nlines_prec;
3313 ec46ccd7 2020-01-15 tracey int lineno_cur;
3314 ec46ccd7 2020-01-15 tracey off_t *line_offsets;
3315 ec46ccd7 2020-01-15 tracey FILE *f;
3316 ec46ccd7 2020-01-15 tracey struct got_repository *repo;
3317 54415d85 2020-01-15 tracey struct gw_trans *gw_trans;
3318 ec46ccd7 2020-01-15 tracey };
3319 ec46ccd7 2020-01-15 tracey
3320 ec46ccd7 2020-01-15 tracey static const struct got_error *
3321 147269d5 2020-01-15 tracey gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3322 ec46ccd7 2020-01-15 tracey {
3323 ec46ccd7 2020-01-15 tracey const struct got_error *err = NULL;
3324 147269d5 2020-01-15 tracey struct gw_blame_cb_args *a = arg;
3325 ec46ccd7 2020-01-15 tracey struct blame_line *bline;
3326 ec46ccd7 2020-01-15 tracey char *line = NULL;
3327 9a9d12ca 2020-02-06 tracey size_t linesize = 0;
3328 ec46ccd7 2020-01-15 tracey struct got_commit_object *commit = NULL;
3329 ec46ccd7 2020-01-15 tracey off_t offset;
3330 ec46ccd7 2020-01-15 tracey struct tm tm;
3331 ec46ccd7 2020-01-15 tracey time_t committer_time;
3332 9a9d12ca 2020-02-06 tracey enum kcgi_err kerr = KCGI_OK;
3333 ec46ccd7 2020-01-15 tracey
3334 ec46ccd7 2020-01-15 tracey if (nlines != a->nlines ||
3335 ec46ccd7 2020-01-15 tracey (lineno != -1 && lineno < 1) || lineno > a->nlines)
3336 ec46ccd7 2020-01-15 tracey return got_error(GOT_ERR_RANGE);
3337 ec46ccd7 2020-01-15 tracey
3338 ec46ccd7 2020-01-15 tracey if (lineno == -1)
3339 ec46ccd7 2020-01-15 tracey return NULL; /* no change in this commit */
3340 ec46ccd7 2020-01-15 tracey
3341 ec46ccd7 2020-01-15 tracey /* Annotate this line. */
3342 ec46ccd7 2020-01-15 tracey bline = &a->lines[lineno - 1];
3343 ec46ccd7 2020-01-15 tracey if (bline->annotated)
3344 ec46ccd7 2020-01-15 tracey return NULL;
3345 ec46ccd7 2020-01-15 tracey err = got_object_id_str(&bline->id_str, id);
3346 ec46ccd7 2020-01-15 tracey if (err)
3347 ec46ccd7 2020-01-15 tracey return err;
3348 ec46ccd7 2020-01-15 tracey
3349 ec46ccd7 2020-01-15 tracey err = got_object_open_as_commit(&commit, a->repo, id);
3350 ec46ccd7 2020-01-15 tracey if (err)
3351 ec46ccd7 2020-01-15 tracey goto done;
3352 ec46ccd7 2020-01-15 tracey
3353 d1635ae4 2020-02-13 tracey bline->committer = strdup(got_object_commit_get_committer(commit));
3354 d1635ae4 2020-02-13 tracey if (bline->committer == NULL) {
3355 d1635ae4 2020-02-13 tracey err = got_error_from_errno("strdup");
3356 ec46ccd7 2020-01-15 tracey goto done;
3357 d1635ae4 2020-02-13 tracey }
3358 ec46ccd7 2020-01-15 tracey
3359 ec46ccd7 2020-01-15 tracey committer_time = got_object_commit_get_committer_time(commit);
3360 ec46ccd7 2020-01-15 tracey if (localtime_r(&committer_time, &tm) == NULL)
3361 ec46ccd7 2020-01-15 tracey return got_error_from_errno("localtime_r");
3362 ec46ccd7 2020-01-15 tracey if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3363 ec46ccd7 2020-01-15 tracey &tm) >= sizeof(bline->datebuf)) {
3364 ec46ccd7 2020-01-15 tracey err = got_error(GOT_ERR_NO_SPACE);
3365 ec46ccd7 2020-01-15 tracey goto done;
3366 ec46ccd7 2020-01-15 tracey }
3367 ec46ccd7 2020-01-15 tracey bline->annotated = 1;
3368 ec46ccd7 2020-01-15 tracey
3369 ec46ccd7 2020-01-15 tracey /* Print lines annotated so far. */
3370 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
3371 ec46ccd7 2020-01-15 tracey if (!bline->annotated)
3372 ec46ccd7 2020-01-15 tracey goto done;
3373 ec46ccd7 2020-01-15 tracey
3374 ec46ccd7 2020-01-15 tracey offset = a->line_offsets[a->lineno_cur - 1];
3375 ec46ccd7 2020-01-15 tracey if (fseeko(a->f, offset, SEEK_SET) == -1) {
3376 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("fseeko");
3377 ec46ccd7 2020-01-15 tracey goto done;
3378 ec46ccd7 2020-01-15 tracey }
3379 ec46ccd7 2020-01-15 tracey
3380 ec46ccd7 2020-01-15 tracey while (bline->annotated) {
3381 9a9d12ca 2020-02-06 tracey char *smallerthan, *at, *nl, *committer;
3382 9a9d12ca 2020-02-06 tracey char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3383 ec46ccd7 2020-01-15 tracey size_t len;
3384 ec46ccd7 2020-01-15 tracey
3385 ec46ccd7 2020-01-15 tracey if (getline(&line, &linesize, a->f) == -1) {
3386 ec46ccd7 2020-01-15 tracey if (ferror(a->f))
3387 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("getline");
3388 ec46ccd7 2020-01-15 tracey break;
3389 ec46ccd7 2020-01-15 tracey }
3390 ec46ccd7 2020-01-15 tracey
3391 ec46ccd7 2020-01-15 tracey committer = bline->committer;
3392 ec46ccd7 2020-01-15 tracey smallerthan = strchr(committer, '<');
3393 ec46ccd7 2020-01-15 tracey if (smallerthan && smallerthan[1] != '\0')
3394 ec46ccd7 2020-01-15 tracey committer = smallerthan + 1;
3395 ec46ccd7 2020-01-15 tracey at = strchr(committer, '@');
3396 ec46ccd7 2020-01-15 tracey if (at)
3397 ec46ccd7 2020-01-15 tracey *at = '\0';
3398 ec46ccd7 2020-01-15 tracey len = strlen(committer);
3399 ec46ccd7 2020-01-15 tracey if (len >= 9)
3400 ec46ccd7 2020-01-15 tracey committer[8] = '\0';
3401 ec46ccd7 2020-01-15 tracey
3402 ec46ccd7 2020-01-15 tracey nl = strchr(line, '\n');
3403 ec46ccd7 2020-01-15 tracey if (nl)
3404 ec46ccd7 2020-01-15 tracey *nl = '\0';
3405 0311ce2d 2020-01-15 tracey
3406 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3407 9a9d12ca 2020-02-06 tracey "blame_wrapper", KATTR__MAX);
3408 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3409 9a9d12ca 2020-02-06 tracey goto err;
3410 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3411 9a9d12ca 2020-02-06 tracey "blame_number", KATTR__MAX);
3412 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3413 9a9d12ca 2020-02-06 tracey goto err;
3414 9a9d12ca 2020-02-06 tracey if (asprintf(&lineno, "%.*d", a->nlines_prec,
3415 9a9d12ca 2020-02-06 tracey a->lineno_cur) == -1)
3416 9a9d12ca 2020-02-06 tracey goto err;
3417 9a9d12ca 2020-02-06 tracey kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3418 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3419 9a9d12ca 2020-02-06 tracey goto err;
3420 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3421 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3422 9a9d12ca 2020-02-06 tracey goto err;
3423 9a9d12ca 2020-02-06 tracey
3424 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3425 9a9d12ca 2020-02-06 tracey "blame_hash", KATTR__MAX);
3426 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3427 9a9d12ca 2020-02-06 tracey goto err;
3428 1b33afc0 2020-02-14 tracey
3429 9a9d12ca 2020-02-06 tracey if (asprintf(&href_diff,
3430 fa44fa48 2020-02-17 stsp "?path=%s&action=diff&commit=%s",
3431 fa44fa48 2020-02-17 stsp a->gw_trans->repo_name, bline->id_str) == -1) {
3432 9a9d12ca 2020-02-06 tracey err = got_error_from_errno("asprintf");
3433 9a9d12ca 2020-02-06 tracey goto err;
3434 9a9d12ca 2020-02-06 tracey }
3435 9a9d12ca 2020-02-06 tracey if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3436 9a9d12ca 2020-02-06 tracey err = got_error_from_errno("asprintf");
3437 9a9d12ca 2020-02-06 tracey goto err;
3438 9a9d12ca 2020-02-06 tracey }
3439 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3440 9a9d12ca 2020-02-06 tracey KATTR_HREF, href_diff, KATTR__MAX);
3441 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3442 9a9d12ca 2020-02-06 tracey goto done;
3443 9a9d12ca 2020-02-06 tracey kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3444 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3445 9a9d12ca 2020-02-06 tracey goto err;
3446 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3447 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3448 9a9d12ca 2020-02-06 tracey goto err;
3449 2e676fc5 2020-01-15 tracey
3450 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3451 9a9d12ca 2020-02-06 tracey "blame_date", KATTR__MAX);
3452 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3453 9a9d12ca 2020-02-06 tracey goto err;
3454 9a9d12ca 2020-02-06 tracey kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3455 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3456 9a9d12ca 2020-02-06 tracey goto err;
3457 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3458 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3459 9a9d12ca 2020-02-06 tracey goto err;
3460 9a9d12ca 2020-02-06 tracey
3461 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3462 9a9d12ca 2020-02-06 tracey "blame_author", KATTR__MAX);
3463 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3464 9a9d12ca 2020-02-06 tracey goto err;
3465 9a9d12ca 2020-02-06 tracey kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3466 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3467 9a9d12ca 2020-02-06 tracey goto err;
3468 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3469 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3470 9a9d12ca 2020-02-06 tracey goto err;
3471 9a9d12ca 2020-02-06 tracey
3472 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3473 9a9d12ca 2020-02-06 tracey "blame_code", KATTR__MAX);
3474 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3475 9a9d12ca 2020-02-06 tracey goto err;
3476 9a9d12ca 2020-02-06 tracey kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3477 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3478 9a9d12ca 2020-02-06 tracey goto err;
3479 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3480 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3481 9a9d12ca 2020-02-06 tracey goto err;
3482 9a9d12ca 2020-02-06 tracey
3483 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3484 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3485 9a9d12ca 2020-02-06 tracey goto err;
3486 9a9d12ca 2020-02-06 tracey
3487 9a9d12ca 2020-02-06 tracey a->lineno_cur++;
3488 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
3489 de6bdba4 2020-01-31 tracey err:
3490 9a9d12ca 2020-02-06 tracey free(lineno);
3491 9a9d12ca 2020-02-06 tracey free(href_diff);
3492 9a9d12ca 2020-02-06 tracey free(href_link);
3493 ec46ccd7 2020-01-15 tracey }
3494 ec46ccd7 2020-01-15 tracey done:
3495 ec46ccd7 2020-01-15 tracey if (commit)
3496 ec46ccd7 2020-01-15 tracey got_object_commit_close(commit);
3497 ec46ccd7 2020-01-15 tracey free(line);
3498 9a9d12ca 2020-02-06 tracey if (err == NULL && kerr != KCGI_OK)
3499 9a9d12ca 2020-02-06 tracey err = gw_kcgi_error(kerr);
3500 ec46ccd7 2020-01-15 tracey return err;
3501 872742ce 2020-02-01 stsp }
3502 872742ce 2020-02-01 stsp
3503 a81f3554 2020-02-03 stsp static const struct got_error *
3504 9a9d12ca 2020-02-06 tracey gw_output_file_blame(struct gw_trans *gw_trans)
3505 bcbc97d8 2020-01-15 tracey {
3506 bcbc97d8 2020-01-15 tracey const struct got_error *error = NULL;
3507 ec46ccd7 2020-01-15 tracey struct got_object_id *obj_id = NULL;
3508 ec46ccd7 2020-01-15 tracey struct got_object_id *commit_id = NULL;
3509 ec46ccd7 2020-01-15 tracey struct got_blob_object *blob = NULL;
3510 a81f3554 2020-02-03 stsp char *path = NULL, *in_repo_path = NULL;
3511 147269d5 2020-01-15 tracey struct gw_blame_cb_args bca;
3512 119bf4ed 2020-01-15 tracey int i, obj_type;
3513 ec46ccd7 2020-01-15 tracey size_t filesize;
3514 ec46ccd7 2020-01-15 tracey
3515 a81f3554 2020-02-03 stsp if (asprintf(&path, "%s%s%s",
3516 a81f3554 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3517 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? "/" : "",
3518 a81f3554 2020-02-03 stsp gw_trans->repo_file) == -1) {
3519 2e676fc5 2020-01-15 tracey error = got_error_from_errno("asprintf");
3520 2e676fc5 2020-01-15 tracey goto done;
3521 2e676fc5 2020-01-15 tracey }
3522 2e676fc5 2020-01-15 tracey
3523 deac617c 2020-02-14 tracey error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3524 ec46ccd7 2020-01-15 tracey if (error)
3525 ec46ccd7 2020-01-15 tracey goto done;
3526 ec46ccd7 2020-01-15 tracey
3527 56997cd3 2020-02-14 tracey error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3528 deac617c 2020-02-14 tracey GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3529 ec46ccd7 2020-01-15 tracey if (error)
3530 ec46ccd7 2020-01-15 tracey goto done;
3531 ec46ccd7 2020-01-15 tracey
3532 deac617c 2020-02-14 tracey error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3533 deac617c 2020-02-14 tracey in_repo_path);
3534 ec46ccd7 2020-01-15 tracey if (error)
3535 ec46ccd7 2020-01-15 tracey goto done;
3536 2e676fc5 2020-01-15 tracey
3537 ec46ccd7 2020-01-15 tracey if (obj_id == NULL) {
3538 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_NO_OBJ);
3539 ec46ccd7 2020-01-15 tracey goto done;
3540 ec46ccd7 2020-01-15 tracey }
3541 ec46ccd7 2020-01-15 tracey
3542 deac617c 2020-02-14 tracey error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3543 ec46ccd7 2020-01-15 tracey if (error)
3544 ec46ccd7 2020-01-15 tracey goto done;
3545 ec46ccd7 2020-01-15 tracey
3546 ec46ccd7 2020-01-15 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
3547 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
3548 ec46ccd7 2020-01-15 tracey goto done;
3549 ec46ccd7 2020-01-15 tracey }
3550 ec46ccd7 2020-01-15 tracey
3551 deac617c 2020-02-14 tracey error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3552 ec46ccd7 2020-01-15 tracey if (error)
3553 ec46ccd7 2020-01-15 tracey goto done;
3554 ec46ccd7 2020-01-15 tracey
3555 ec46ccd7 2020-01-15 tracey bca.f = got_opentemp();
3556 ec46ccd7 2020-01-15 tracey if (bca.f == NULL) {
3557 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("got_opentemp");
3558 ec46ccd7 2020-01-15 tracey goto done;
3559 ec46ccd7 2020-01-15 tracey }
3560 ec46ccd7 2020-01-15 tracey error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3561 ec46ccd7 2020-01-15 tracey &bca.line_offsets, bca.f, blob);
3562 ec46ccd7 2020-01-15 tracey if (error || bca.nlines == 0)
3563 ec46ccd7 2020-01-15 tracey goto done;
3564 a81f3554 2020-02-03 stsp
3565 ec46ccd7 2020-01-15 tracey /* Don't include \n at EOF in the blame line count. */
3566 ec46ccd7 2020-01-15 tracey if (bca.line_offsets[bca.nlines - 1] == filesize)
3567 ec46ccd7 2020-01-15 tracey bca.nlines--;
3568 ec46ccd7 2020-01-15 tracey
3569 ec46ccd7 2020-01-15 tracey bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3570 ec46ccd7 2020-01-15 tracey if (bca.lines == NULL) {
3571 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("calloc");
3572 ec46ccd7 2020-01-15 tracey goto done;
3573 ec46ccd7 2020-01-15 tracey }
3574 ec46ccd7 2020-01-15 tracey bca.lineno_cur = 1;
3575 ec46ccd7 2020-01-15 tracey bca.nlines_prec = 0;
3576 ec46ccd7 2020-01-15 tracey i = bca.nlines;
3577 ec46ccd7 2020-01-15 tracey while (i > 0) {
3578 ec46ccd7 2020-01-15 tracey i /= 10;
3579 ec46ccd7 2020-01-15 tracey bca.nlines_prec++;
3580 ec46ccd7 2020-01-15 tracey }
3581 deac617c 2020-02-14 tracey bca.repo = gw_trans->repo;
3582 2e676fc5 2020-01-15 tracey bca.gw_trans = gw_trans;
3583 ec46ccd7 2020-01-15 tracey
3584 deac617c 2020-02-14 tracey error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3585 deac617c 2020-02-14 tracey &bca, NULL, NULL);
3586 ec46ccd7 2020-01-15 tracey done:
3587 2e676fc5 2020-01-15 tracey free(in_repo_path);
3588 2e676fc5 2020-01-15 tracey free(commit_id);
3589 2e676fc5 2020-01-15 tracey free(obj_id);
3590 2e676fc5 2020-01-15 tracey free(path);
3591 2e676fc5 2020-01-15 tracey
3592 1b33afc0 2020-02-14 tracey if (blob) {
3593 1b33afc0 2020-02-14 tracey free(bca.line_offsets);
3594 1b33afc0 2020-02-14 tracey for (i = 0; i < bca.nlines; i++) {
3595 1b33afc0 2020-02-14 tracey struct blame_line *bline = &bca.lines[i];
3596 1b33afc0 2020-02-14 tracey free(bline->id_str);
3597 1b33afc0 2020-02-14 tracey free(bline->committer);
3598 1b33afc0 2020-02-14 tracey }
3599 1b33afc0 2020-02-14 tracey free(bca.lines);
3600 1b33afc0 2020-02-14 tracey if (bca.f && fclose(bca.f) == EOF && error == NULL)
3601 1b33afc0 2020-02-14 tracey error = got_error_from_errno("fclose");
3602 2e676fc5 2020-01-15 tracey }
3603 4fd4726a 2020-02-03 stsp if (blob)
3604 4fd4726a 2020-02-03 stsp got_object_blob_close(blob);
3605 4fd4726a 2020-02-03 stsp return error;
3606 4fd4726a 2020-02-03 stsp }
3607 4fd4726a 2020-02-03 stsp
3608 4fd4726a 2020-02-03 stsp static const struct got_error *
3609 e0857cfe 2020-02-06 tracey gw_output_blob_buf(struct gw_trans *gw_trans)
3610 4fd4726a 2020-02-03 stsp {
3611 4fd4726a 2020-02-03 stsp const struct got_error *error = NULL;
3612 4fd4726a 2020-02-03 stsp struct got_object_id *obj_id = NULL;
3613 4fd4726a 2020-02-03 stsp struct got_object_id *commit_id = NULL;
3614 4fd4726a 2020-02-03 stsp struct got_blob_object *blob = NULL;
3615 4fd4726a 2020-02-03 stsp char *path = NULL, *in_repo_path = NULL;
3616 e0857cfe 2020-02-06 tracey int obj_type, set_mime = 0;
3617 e0857cfe 2020-02-06 tracey size_t len, hdrlen;
3618 e0857cfe 2020-02-06 tracey const uint8_t *buf;
3619 e0857cfe 2020-02-06 tracey enum kcgi_err kerr = KCGI_OK;
3620 4fd4726a 2020-02-03 stsp
3621 4fd4726a 2020-02-03 stsp if (asprintf(&path, "%s%s%s",
3622 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3623 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? "/" : "",
3624 4fd4726a 2020-02-03 stsp gw_trans->repo_file) == -1) {
3625 4fd4726a 2020-02-03 stsp error = got_error_from_errno("asprintf");
3626 4fd4726a 2020-02-03 stsp goto done;
3627 4fd4726a 2020-02-03 stsp }
3628 4fd4726a 2020-02-03 stsp
3629 deac617c 2020-02-14 tracey error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3630 4fd4726a 2020-02-03 stsp if (error)
3631 4fd4726a 2020-02-03 stsp goto done;
3632 4fd4726a 2020-02-03 stsp
3633 56997cd3 2020-02-14 tracey error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3634 deac617c 2020-02-14 tracey GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3635 4fd4726a 2020-02-03 stsp if (error)
3636 4fd4726a 2020-02-03 stsp goto done;
3637 4fd4726a 2020-02-03 stsp
3638 deac617c 2020-02-14 tracey error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3639 deac617c 2020-02-14 tracey in_repo_path);
3640 4fd4726a 2020-02-03 stsp if (error)
3641 4fd4726a 2020-02-03 stsp goto done;
3642 4fd4726a 2020-02-03 stsp
3643 4fd4726a 2020-02-03 stsp if (obj_id == NULL) {
3644 4fd4726a 2020-02-03 stsp error = got_error(GOT_ERR_NO_OBJ);
3645 4fd4726a 2020-02-03 stsp goto done;
3646 4fd4726a 2020-02-03 stsp }
3647 4fd4726a 2020-02-03 stsp
3648 deac617c 2020-02-14 tracey error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3649 4fd4726a 2020-02-03 stsp if (error)
3650 4fd4726a 2020-02-03 stsp goto done;
3651 4fd4726a 2020-02-03 stsp
3652 4fd4726a 2020-02-03 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3653 4fd4726a 2020-02-03 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3654 4fd4726a 2020-02-03 stsp goto done;
3655 4fd4726a 2020-02-03 stsp }
3656 4fd4726a 2020-02-03 stsp
3657 deac617c 2020-02-14 tracey error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3658 4fd4726a 2020-02-03 stsp if (error)
3659 4fd4726a 2020-02-03 stsp goto done;
3660 4fd4726a 2020-02-03 stsp
3661 e0857cfe 2020-02-06 tracey hdrlen = got_object_blob_get_hdrlen(blob);
3662 e0857cfe 2020-02-06 tracey do {
3663 e0857cfe 2020-02-06 tracey error = got_object_blob_read_block(&len, blob);
3664 e0857cfe 2020-02-06 tracey if (error)
3665 e0857cfe 2020-02-06 tracey goto done;
3666 e0857cfe 2020-02-06 tracey buf = got_object_blob_get_read_buf(blob);
3667 4fd4726a 2020-02-03 stsp
3668 e0857cfe 2020-02-06 tracey /*
3669 e0857cfe 2020-02-06 tracey * Skip blob object header first time around,
3670 e0857cfe 2020-02-06 tracey * which also contains a zero byte.
3671 e0857cfe 2020-02-06 tracey */
3672 e0857cfe 2020-02-06 tracey buf += hdrlen;
3673 e0857cfe 2020-02-06 tracey if (set_mime == 0) {
3674 e0857cfe 2020-02-06 tracey if (isbinary(buf, len - hdrlen))
3675 e0857cfe 2020-02-06 tracey gw_trans->mime = KMIME_APP_OCTET_STREAM;
3676 e0857cfe 2020-02-06 tracey else
3677 e0857cfe 2020-02-06 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
3678 e0857cfe 2020-02-06 tracey set_mime = 1;
3679 e0857cfe 2020-02-06 tracey error = gw_display_index(gw_trans);
3680 e0857cfe 2020-02-06 tracey if (error)
3681 e0857cfe 2020-02-06 tracey goto done;
3682 e0857cfe 2020-02-06 tracey }
3683 3b7fdcf4 2020-02-14 tracey kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3684 c56af3fd 2020-02-17 stsp if (kerr != KCGI_OK)
3685 c56af3fd 2020-02-17 stsp goto done;
3686 e0857cfe 2020-02-06 tracey hdrlen = 0;
3687 e0857cfe 2020-02-06 tracey } while (len != 0);
3688 4fd4726a 2020-02-03 stsp done:
3689 4fd4726a 2020-02-03 stsp free(in_repo_path);
3690 4fd4726a 2020-02-03 stsp free(commit_id);
3691 4fd4726a 2020-02-03 stsp free(obj_id);
3692 4fd4726a 2020-02-03 stsp free(path);
3693 e46f587c 2020-01-29 tracey if (blob)
3694 a81f3554 2020-02-03 stsp got_object_blob_close(blob);
3695 e0857cfe 2020-02-06 tracey if (error == NULL && kerr != KCGI_OK)
3696 e0857cfe 2020-02-06 tracey error = gw_kcgi_error(kerr);
3697 a81f3554 2020-02-03 stsp return error;
3698 ec46ccd7 2020-01-15 tracey }
3699 ec46ccd7 2020-01-15 tracey
3700 84bf4df2 2020-02-03 stsp static const struct got_error *
3701 626b25b6 2020-02-06 tracey gw_output_repo_tree(struct gw_trans *gw_trans)
3702 ec46ccd7 2020-01-15 tracey {
3703 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
3704 bcbc97d8 2020-01-15 tracey struct got_object_id *tree_id = NULL, *commit_id = NULL;
3705 bcbc97d8 2020-01-15 tracey struct got_tree_object *tree = NULL;
3706 626b25b6 2020-02-06 tracey char *path = NULL, *in_repo_path = NULL;
3707 84bf4df2 2020-02-03 stsp char *id_str = NULL;
3708 84bf4df2 2020-02-03 stsp char *build_folder = NULL;
3709 626b25b6 2020-02-06 tracey char *href_blob = NULL, *href_blame = NULL;
3710 84bf4df2 2020-02-03 stsp const char *class = NULL;
3711 c3bcdfd5 2020-01-29 tracey int nentries, i, class_flip = 0;
3712 626b25b6 2020-02-06 tracey enum kcgi_err kerr = KCGI_OK;
3713 8d4d2453 2020-01-15 tracey
3714 a942aa37 2020-02-10 tracey if (gw_trans->repo_folder != NULL) {
3715 d1635ae4 2020-02-13 tracey path = strdup(gw_trans->repo_folder);
3716 d1635ae4 2020-02-13 tracey if (path == NULL) {
3717 d1635ae4 2020-02-13 tracey error = got_error_from_errno("strdup");
3718 a942aa37 2020-02-10 tracey goto done;
3719 d1635ae4 2020-02-13 tracey }
3720 a942aa37 2020-02-10 tracey } else {
3721 deac617c 2020-02-14 tracey error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3722 84bf4df2 2020-02-03 stsp gw_trans->repo_path, 1);
3723 84bf4df2 2020-02-03 stsp if (error)
3724 84bf4df2 2020-02-03 stsp goto done;
3725 bcbc97d8 2020-01-15 tracey free(path);
3726 bcbc97d8 2020-01-15 tracey path = in_repo_path;
3727 bcbc97d8 2020-01-15 tracey }
3728 bcbc97d8 2020-01-15 tracey
3729 56997cd3 2020-02-14 tracey if (gw_trans->commit_id == NULL) {
3730 ec46ccd7 2020-01-15 tracey struct got_reference *head_ref;
3731 deac617c 2020-02-14 tracey error = got_ref_open(&head_ref, gw_trans->repo,
3732 deac617c 2020-02-14 tracey gw_trans->headref, 0);
3733 ec46ccd7 2020-01-15 tracey if (error)
3734 ec46ccd7 2020-01-15 tracey goto done;
3735 deac617c 2020-02-14 tracey error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
3736 84bf4df2 2020-02-03 stsp if (error)
3737 84bf4df2 2020-02-03 stsp goto done;
3738 ec46ccd7 2020-01-15 tracey got_ref_close(head_ref);
3739 56997cd3 2020-02-14 tracey /*
3740 56997cd3 2020-02-14 tracey * gw_trans->commit_id was not parsed from the querystring
3741 56997cd3 2020-02-14 tracey * we hit this code path from gw_index, where we don't know the
3742 56997cd3 2020-02-14 tracey * commit values for the tree link yet, so set
3743 56997cd3 2020-02-14 tracey * gw_trans->commit_id here to continue further into the tree
3744 56997cd3 2020-02-14 tracey */
3745 56997cd3 2020-02-14 tracey error = got_object_id_str(&gw_trans->commit_id, commit_id);
3746 56997cd3 2020-02-14 tracey if (error)
3747 56997cd3 2020-02-14 tracey goto done;
3748 ec46ccd7 2020-01-15 tracey
3749 84bf4df2 2020-02-03 stsp } else {
3750 f2f46662 2020-01-23 tracey error = got_repo_match_object_id(&commit_id, NULL,
3751 56997cd3 2020-02-14 tracey gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
3752 56997cd3 2020-02-14 tracey gw_trans->repo);
3753 84bf4df2 2020-02-03 stsp if (error)
3754 84bf4df2 2020-02-03 stsp goto done;
3755 84bf4df2 2020-02-03 stsp }
3756 f2f46662 2020-01-23 tracey
3757 56997cd3 2020-02-14 tracey error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
3758 56997cd3 2020-02-14 tracey path);
3759 bcbc97d8 2020-01-15 tracey if (error)
3760 bcbc97d8 2020-01-15 tracey goto done;
3761 bcbc97d8 2020-01-15 tracey
3762 deac617c 2020-02-14 tracey error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
3763 bcbc97d8 2020-01-15 tracey if (error)
3764 bcbc97d8 2020-01-15 tracey goto done;
3765 bcbc97d8 2020-01-15 tracey
3766 bcbc97d8 2020-01-15 tracey nentries = got_object_tree_get_nentries(tree);
3767 bcbc97d8 2020-01-15 tracey for (i = 0; i < nentries; i++) {
3768 bcbc97d8 2020-01-15 tracey struct got_tree_entry *te;
3769 ec46ccd7 2020-01-15 tracey const char *modestr = "";
3770 84bf4df2 2020-02-03 stsp mode_t mode;
3771 bcbc97d8 2020-01-15 tracey
3772 bcbc97d8 2020-01-15 tracey te = got_object_tree_get_entry(tree, i);
3773 bcbc97d8 2020-01-15 tracey
3774 bcbc97d8 2020-01-15 tracey error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3775 bcbc97d8 2020-01-15 tracey if (error)
3776 bcbc97d8 2020-01-15 tracey goto done;
3777 bcbc97d8 2020-01-15 tracey
3778 84bf4df2 2020-02-03 stsp mode = got_tree_entry_get_mode(te);
3779 bcbc97d8 2020-01-15 tracey if (got_object_tree_entry_is_submodule(te))
3780 bcbc97d8 2020-01-15 tracey modestr = "$";
3781 bcbc97d8 2020-01-15 tracey else if (S_ISLNK(mode))
3782 bcbc97d8 2020-01-15 tracey modestr = "@";
3783 bcbc97d8 2020-01-15 tracey else if (S_ISDIR(mode))
3784 bcbc97d8 2020-01-15 tracey modestr = "/";
3785 bcbc97d8 2020-01-15 tracey else if (mode & S_IXUSR)
3786 bcbc97d8 2020-01-15 tracey modestr = "*";
3787 c3bcdfd5 2020-01-29 tracey
3788 c3bcdfd5 2020-01-29 tracey if (class_flip == 0) {
3789 84bf4df2 2020-02-03 stsp class = "back_lightgray";
3790 c3bcdfd5 2020-01-29 tracey class_flip = 1;
3791 c3bcdfd5 2020-01-29 tracey } else {
3792 84bf4df2 2020-02-03 stsp class = "back_white";
3793 c3bcdfd5 2020-01-29 tracey class_flip = 0;
3794 c3bcdfd5 2020-01-29 tracey }
3795 bcbc97d8 2020-01-15 tracey
3796 84bf4df2 2020-02-03 stsp if (S_ISDIR(mode)) {
3797 a5594e37 2020-02-05 tracey if (asprintf(&build_folder, "%s/%s",
3798 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3799 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te)) == -1) {
3800 53e81e48 2020-02-13 tracey error = got_error_from_errno("asprintf");
3801 84bf4df2 2020-02-03 stsp goto done;
3802 ec46ccd7 2020-01-15 tracey }
3803 626b25b6 2020-02-06 tracey if (asprintf(&href_blob,
3804 626b25b6 2020-02-06 tracey "?path=%s&action=%s&commit=%s&folder=%s",
3805 5d1296de 2020-02-13 stsp gw_trans->repo_name, gw_get_action_name(gw_trans),
3806 56997cd3 2020-02-14 tracey gw_trans->commit_id, build_folder) == -1) {
3807 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
3808 ec46ccd7 2020-01-15 tracey goto done;
3809 ec46ccd7 2020-01-15 tracey }
3810 626b25b6 2020-02-06 tracey
3811 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3812 626b25b6 2020-02-06 tracey KATTR_ID, "tree_wrapper", KATTR__MAX);
3813 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3814 626b25b6 2020-02-06 tracey goto done;
3815 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3816 626b25b6 2020-02-06 tracey KATTR_ID, "tree_line", KATTR_CLASS, class,
3817 626b25b6 2020-02-06 tracey KATTR__MAX);
3818 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3819 626b25b6 2020-02-06 tracey goto done;
3820 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3821 626b25b6 2020-02-06 tracey KATTR_HREF, href_blob, KATTR_CLASS,
3822 626b25b6 2020-02-06 tracey "diff_directory", KATTR__MAX);
3823 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3824 626b25b6 2020-02-06 tracey goto done;
3825 626b25b6 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req,
3826 626b25b6 2020-02-06 tracey got_tree_entry_get_name(te));
3827 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3828 626b25b6 2020-02-06 tracey goto done;
3829 626b25b6 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3830 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3831 626b25b6 2020-02-06 tracey goto done;
3832 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3833 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3834 626b25b6 2020-02-06 tracey goto done;
3835 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3836 626b25b6 2020-02-06 tracey KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3837 626b25b6 2020-02-06 tracey KATTR__MAX);
3838 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3839 84bf4df2 2020-02-03 stsp goto done;
3840 626b25b6 2020-02-06 tracey kerr = khtml_entity(gw_trans->gw_html_req,
3841 626b25b6 2020-02-06 tracey KENTITY_nbsp);
3842 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3843 626b25b6 2020-02-06 tracey goto done;
3844 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3845 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3846 626b25b6 2020-02-06 tracey goto done;
3847 ec46ccd7 2020-01-15 tracey } else {
3848 626b25b6 2020-02-06 tracey if (asprintf(&href_blob,
3849 626b25b6 2020-02-06 tracey "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3850 56997cd3 2020-02-14 tracey gw_trans->repo_name, "blob", gw_trans->commit_id,
3851 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te),
3852 626b25b6 2020-02-06 tracey gw_trans->repo_folder ?
3853 626b25b6 2020-02-06 tracey gw_trans->repo_folder : "") == -1) {
3854 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
3855 ec46ccd7 2020-01-15 tracey goto done;
3856 ec46ccd7 2020-01-15 tracey }
3857 626b25b6 2020-02-06 tracey if (asprintf(&href_blame,
3858 626b25b6 2020-02-06 tracey "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3859 56997cd3 2020-02-14 tracey gw_trans->repo_name, "blame", gw_trans->commit_id,
3860 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te),
3861 626b25b6 2020-02-06 tracey gw_trans->repo_folder ?
3862 626b25b6 2020-02-06 tracey gw_trans->repo_folder : "") == -1) {
3863 e46f587c 2020-01-29 tracey error = got_error_from_errno("asprintf");
3864 e46f587c 2020-01-29 tracey goto done;
3865 e46f587c 2020-01-29 tracey }
3866 ec46ccd7 2020-01-15 tracey
3867 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3868 626b25b6 2020-02-06 tracey KATTR_ID, "tree_wrapper", KATTR__MAX);
3869 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3870 626b25b6 2020-02-06 tracey goto done;
3871 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3872 626b25b6 2020-02-06 tracey KATTR_ID, "tree_line", KATTR_CLASS, class,
3873 626b25b6 2020-02-06 tracey KATTR__MAX);
3874 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3875 626b25b6 2020-02-06 tracey goto done;
3876 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3877 626b25b6 2020-02-06 tracey KATTR_HREF, href_blob, KATTR__MAX);
3878 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3879 626b25b6 2020-02-06 tracey goto done;
3880 626b25b6 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req,
3881 626b25b6 2020-02-06 tracey got_tree_entry_get_name(te));
3882 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3883 626b25b6 2020-02-06 tracey goto done;
3884 626b25b6 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3885 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3886 626b25b6 2020-02-06 tracey goto done;
3887 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3888 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3889 626b25b6 2020-02-06 tracey goto done;
3890 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3891 626b25b6 2020-02-06 tracey KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3892 626b25b6 2020-02-06 tracey KATTR__MAX);
3893 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3894 626b25b6 2020-02-06 tracey goto done;
3895 ec46ccd7 2020-01-15 tracey
3896 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3897 626b25b6 2020-02-06 tracey KATTR_HREF, href_blob, KATTR__MAX);
3898 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3899 626b25b6 2020-02-06 tracey goto done;
3900 626b25b6 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3901 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3902 626b25b6 2020-02-06 tracey goto done;
3903 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3904 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3905 626b25b6 2020-02-06 tracey goto done;
3906 626b25b6 2020-02-06 tracey
3907 626b25b6 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3908 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3909 626b25b6 2020-02-06 tracey goto done;
3910 626b25b6 2020-02-06 tracey
3911 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3912 626b25b6 2020-02-06 tracey KATTR_HREF, href_blame, KATTR__MAX);
3913 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3914 626b25b6 2020-02-06 tracey goto done;
3915 626b25b6 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3916 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3917 626b25b6 2020-02-06 tracey goto done;
3918 626b25b6 2020-02-06 tracey
3919 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3920 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
3921 626b25b6 2020-02-06 tracey goto done;
3922 626b25b6 2020-02-06 tracey }
3923 bcbc97d8 2020-01-15 tracey free(id_str);
3924 84bf4df2 2020-02-03 stsp id_str = NULL;
3925 626b25b6 2020-02-06 tracey free(href_blob);
3926 626b25b6 2020-02-06 tracey href_blob = NULL;
3927 84bf4df2 2020-02-03 stsp free(build_folder);
3928 84bf4df2 2020-02-03 stsp build_folder = NULL;
3929 bcbc97d8 2020-01-15 tracey }
3930 bcbc97d8 2020-01-15 tracey done:
3931 bcbc97d8 2020-01-15 tracey if (tree)
3932 bcbc97d8 2020-01-15 tracey got_object_tree_close(tree);
3933 84bf4df2 2020-02-03 stsp free(id_str);
3934 626b25b6 2020-02-06 tracey free(href_blob);
3935 626b25b6 2020-02-06 tracey free(href_blame);
3936 2e676fc5 2020-01-15 tracey free(in_repo_path);
3937 bcbc97d8 2020-01-15 tracey free(tree_id);
3938 84bf4df2 2020-02-03 stsp free(build_folder);
3939 626b25b6 2020-02-06 tracey if (error == NULL && kerr != KCGI_OK)
3940 626b25b6 2020-02-06 tracey error = gw_kcgi_error(kerr);
3941 84bf4df2 2020-02-03 stsp return error;
3942 bcbc97d8 2020-01-15 tracey }
3943 bcbc97d8 2020-01-15 tracey
3944 51d4a92d 2020-02-03 tracey static const struct got_error *
3945 9eed6f10 2020-02-08 tracey gw_output_repo_heads(struct gw_trans *gw_trans)
3946 8d4d2453 2020-01-15 tracey {
3947 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
3948 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
3949 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
3950 9eed6f10 2020-02-08 tracey char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3951 9eed6f10 2020-02-08 tracey char *href_commits = NULL;
3952 9eed6f10 2020-02-08 tracey enum kcgi_err kerr = KCGI_OK;
3953 51d4a92d 2020-02-03 tracey
3954 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
3955 87f9ebf5 2020-01-15 tracey
3956 deac617c 2020-02-14 tracey error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
3957 deac617c 2020-02-14 tracey got_ref_cmp_by_name, NULL);
3958 87f9ebf5 2020-01-15 tracey if (error)
3959 87f9ebf5 2020-01-15 tracey goto done;
3960 87f9ebf5 2020-01-15 tracey
3961 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3962 9eed6f10 2020-02-08 tracey KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3963 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
3964 9eed6f10 2020-02-08 tracey goto done;
3965 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3966 9eed6f10 2020-02-08 tracey KATTR_ID, "summary_heads_title", KATTR__MAX);
3967 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
3968 9eed6f10 2020-02-08 tracey goto done;
3969 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3970 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
3971 9eed6f10 2020-02-08 tracey goto done;
3972 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3973 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
3974 9eed6f10 2020-02-08 tracey goto done;
3975 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3976 9eed6f10 2020-02-08 tracey KATTR_ID, "summary_heads_content", KATTR__MAX);
3977 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
3978 9eed6f10 2020-02-08 tracey goto done;
3979 9eed6f10 2020-02-08 tracey
3980 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
3981 7eb2c8df 2020-02-14 stsp const char *refname;
3982 87f9ebf5 2020-01-15 tracey
3983 eb4f3ad3 2020-02-14 stsp if (got_ref_is_symbolic(re->ref))
3984 eb4f3ad3 2020-02-14 stsp continue;
3985 eb4f3ad3 2020-02-14 stsp
3986 7eb2c8df 2020-02-14 stsp refname = got_ref_get_name(re->ref);
3987 7eb2c8df 2020-02-14 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
3988 87f9ebf5 2020-01-15 tracey continue;
3989 87f9ebf5 2020-01-15 tracey
3990 017d6da3 2020-01-29 tracey error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3991 017d6da3 2020-01-29 tracey refname, TM_DIFF);
3992 d126c1b7 2020-01-29 stsp if (error)
3993 d126c1b7 2020-01-29 stsp goto done;
3994 87f9ebf5 2020-01-15 tracey
3995 9eed6f10 2020-02-08 tracey if (strncmp(refname, "refs/heads/", 11) == 0)
3996 9eed6f10 2020-02-08 tracey refname += 11;
3997 9eed6f10 2020-02-08 tracey
3998 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3999 9eed6f10 2020-02-08 tracey KATTR_ID, "heads_wrapper", KATTR__MAX);
4000 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4001 9eed6f10 2020-02-08 tracey goto done;
4002 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4003 9eed6f10 2020-02-08 tracey KATTR_ID, "heads_age", KATTR__MAX);
4004 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4005 9eed6f10 2020-02-08 tracey goto done;
4006 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4007 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4008 9eed6f10 2020-02-08 tracey goto done;
4009 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4010 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4011 9eed6f10 2020-02-08 tracey goto done;
4012 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4013 9eed6f10 2020-02-08 tracey KATTR_ID, "heads_space", KATTR__MAX);
4014 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4015 9eed6f10 2020-02-08 tracey goto done;
4016 9eed6f10 2020-02-08 tracey kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4017 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4018 9eed6f10 2020-02-08 tracey goto done;
4019 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4020 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4021 9eed6f10 2020-02-08 tracey goto done;
4022 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4023 9eed6f10 2020-02-08 tracey KATTR_ID, "head", KATTR__MAX);
4024 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4025 9eed6f10 2020-02-08 tracey goto done;
4026 9eed6f10 2020-02-08 tracey if (asprintf(&href_summary,
4027 9eed6f10 2020-02-08 tracey "?path=%s&action=summary&headref=%s",
4028 9eed6f10 2020-02-08 tracey gw_trans->repo_name, refname) == -1) {
4029 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
4030 87f9ebf5 2020-01-15 tracey goto done;
4031 87f9ebf5 2020-01-15 tracey }
4032 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4033 9eed6f10 2020-02-08 tracey href_summary, KATTR__MAX);
4034 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, refname);
4035 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4036 9eed6f10 2020-02-08 tracey goto done;
4037 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4038 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4039 9eed6f10 2020-02-08 tracey goto done;
4040 87f9ebf5 2020-01-15 tracey
4041 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4042 9eed6f10 2020-02-08 tracey "navs_wrapper", KATTR__MAX);
4043 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4044 9eed6f10 2020-02-08 tracey goto done;
4045 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4046 9eed6f10 2020-02-08 tracey "navs", KATTR__MAX);
4047 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4048 9eed6f10 2020-02-08 tracey goto done;
4049 87f9ebf5 2020-01-15 tracey
4050 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4051 9eed6f10 2020-02-08 tracey href_summary, KATTR__MAX);
4052 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4053 9eed6f10 2020-02-08 tracey goto done;
4054 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4055 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4056 9eed6f10 2020-02-08 tracey goto done;
4057 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4058 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4059 9eed6f10 2020-02-08 tracey goto done;
4060 9eed6f10 2020-02-08 tracey
4061 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4062 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4063 9eed6f10 2020-02-08 tracey goto done;
4064 9eed6f10 2020-02-08 tracey if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4065 9eed6f10 2020-02-08 tracey gw_trans->repo_name, refname) == -1) {
4066 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
4067 87f9ebf5 2020-01-15 tracey goto done;
4068 87f9ebf5 2020-01-15 tracey }
4069 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4070 9eed6f10 2020-02-08 tracey href_briefs, KATTR__MAX);
4071 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4072 9eed6f10 2020-02-08 tracey goto done;
4073 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4074 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4075 9eed6f10 2020-02-08 tracey goto done;
4076 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4077 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4078 9eed6f10 2020-02-08 tracey goto done;
4079 87f9ebf5 2020-01-15 tracey
4080 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4081 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4082 9eed6f10 2020-02-08 tracey goto done;
4083 87f9ebf5 2020-01-15 tracey
4084 9eed6f10 2020-02-08 tracey if (asprintf(&href_commits,
4085 9eed6f10 2020-02-08 tracey "?path=%s&action=commits&headref=%s",
4086 9eed6f10 2020-02-08 tracey gw_trans->repo_name, refname) == -1) {
4087 9eed6f10 2020-02-08 tracey error = got_error_from_errno("asprintf");
4088 9eed6f10 2020-02-08 tracey goto done;
4089 9eed6f10 2020-02-08 tracey }
4090 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4091 9eed6f10 2020-02-08 tracey href_commits, KATTR__MAX);
4092 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4093 9eed6f10 2020-02-08 tracey goto done;
4094 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4095 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4096 9eed6f10 2020-02-08 tracey goto done;
4097 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4098 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4099 9eed6f10 2020-02-08 tracey goto done;
4100 87f9ebf5 2020-01-15 tracey
4101 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4102 9eed6f10 2020-02-08 tracey "dotted_line", KATTR__MAX);
4103 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4104 9eed6f10 2020-02-08 tracey goto done;
4105 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4106 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4107 9eed6f10 2020-02-08 tracey goto done;
4108 9eed6f10 2020-02-08 tracey free(href_summary);
4109 9eed6f10 2020-02-08 tracey href_summary = NULL;
4110 9eed6f10 2020-02-08 tracey free(href_briefs);
4111 9eed6f10 2020-02-08 tracey href_briefs = NULL;
4112 9eed6f10 2020-02-08 tracey free(href_commits);
4113 9eed6f10 2020-02-08 tracey href_commits = NULL;
4114 6c6c85af 2020-01-15 tracey }
4115 87f9ebf5 2020-01-15 tracey done:
4116 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
4117 9eed6f10 2020-02-08 tracey free(href_summary);
4118 9eed6f10 2020-02-08 tracey free(href_briefs);
4119 9eed6f10 2020-02-08 tracey free(href_commits);
4120 51d4a92d 2020-02-03 tracey return error;
4121 8d4d2453 2020-01-15 tracey }
4122 8d4d2453 2020-01-15 tracey
4123 4ae179a2 2020-02-08 tracey static const struct got_error *
4124 4ae179a2 2020-02-08 tracey gw_output_site_link(struct gw_trans *gw_trans)
4125 2c251c14 2020-01-15 tracey {
4126 4ae179a2 2020-02-08 tracey const struct got_error *error = NULL;
4127 4ae179a2 2020-02-08 tracey char *href_summary = NULL;
4128 4ae179a2 2020-02-08 tracey enum kcgi_err kerr = KCGI_OK;
4129 2c251c14 2020-01-15 tracey
4130 4ae179a2 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4131 4ae179a2 2020-02-08 tracey "site_link", KATTR__MAX);
4132 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4133 4ae179a2 2020-02-08 tracey goto done;
4134 4ae179a2 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4135 4ae179a2 2020-02-08 tracey KATTR__MAX);
4136 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4137 4ae179a2 2020-02-08 tracey goto done;
4138 4ae179a2 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req,
4139 4ae179a2 2020-02-08 tracey gw_trans->gw_conf->got_site_link);
4140 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4141 4ae179a2 2020-02-08 tracey goto done;
4142 4ae179a2 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4143 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4144 4ae179a2 2020-02-08 tracey goto done;
4145 2c251c14 2020-01-15 tracey
4146 4ae179a2 2020-02-08 tracey if (gw_trans->repo_name != NULL) {
4147 4ae179a2 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4148 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4149 4ae179a2 2020-02-08 tracey goto done;
4150 4ae179a2 2020-02-08 tracey if (asprintf(&href_summary, "?path=%s&action=summary",
4151 4ae179a2 2020-02-08 tracey gw_trans->repo_name) == -1)
4152 4ae179a2 2020-02-08 tracey goto done;
4153 4ae179a2 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4154 4ae179a2 2020-02-08 tracey href_summary, KATTR__MAX);
4155 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4156 4ae179a2 2020-02-08 tracey goto done;
4157 4ae179a2 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4158 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4159 4ae179a2 2020-02-08 tracey goto done;
4160 4ae179a2 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4161 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4162 4ae179a2 2020-02-08 tracey goto done;
4163 4ae179a2 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4164 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4165 4ae179a2 2020-02-08 tracey goto done;
4166 5d1296de 2020-02-13 stsp kerr = khtml_puts(gw_trans->gw_html_req,
4167 5d1296de 2020-02-13 stsp gw_get_action_name(gw_trans));
4168 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4169 4ae179a2 2020-02-08 tracey goto done;
4170 eb89db64 2020-02-03 stsp }
4171 2c251c14 2020-01-15 tracey
4172 4ae179a2 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4173 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4174 4ae179a2 2020-02-08 tracey goto done;
4175 4ae179a2 2020-02-08 tracey done:
4176 4ae179a2 2020-02-08 tracey free(href_summary);
4177 4ae179a2 2020-02-08 tracey return error;
4178 2c251c14 2020-01-15 tracey }
4179 2c251c14 2020-01-15 tracey
4180 83eb9a68 2020-02-03 stsp static const struct got_error *
4181 f7cc9480 2020-02-05 tracey gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4182 ec46ccd7 2020-01-15 tracey {
4183 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
4184 f7cc9480 2020-02-05 tracey char *color = NULL;
4185 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
4186 83eb9a68 2020-02-03 stsp
4187 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "-", 1) == 0)
4188 ec46ccd7 2020-01-15 tracey color = "diff_minus";
4189 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "+", 1) == 0)
4190 ec46ccd7 2020-01-15 tracey color = "diff_plus";
4191 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "@@", 2) == 0)
4192 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
4193 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "@@", 2) == 0)
4194 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
4195 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "commit +", 8) == 0)
4196 ec46ccd7 2020-01-15 tracey color = "diff_meta";
4197 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "commit -", 8) == 0)
4198 ec46ccd7 2020-01-15 tracey color = "diff_meta";
4199 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "blob +", 6) == 0)
4200 ec46ccd7 2020-01-15 tracey color = "diff_meta";
4201 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "blob -", 6) == 0)
4202 ec46ccd7 2020-01-15 tracey color = "diff_meta";
4203 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "file +", 6) == 0)
4204 ec46ccd7 2020-01-15 tracey color = "diff_meta";
4205 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "file -", 6) == 0)
4206 ec46ccd7 2020-01-15 tracey color = "diff_meta";
4207 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "from:", 5) == 0)
4208 ec46ccd7 2020-01-15 tracey color = "diff_author";
4209 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "via:", 4) == 0)
4210 ec46ccd7 2020-01-15 tracey color = "diff_author";
4211 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "date:", 5) == 0)
4212 ec46ccd7 2020-01-15 tracey color = "diff_date";
4213 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4214 f7cc9480 2020-02-05 tracey "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4215 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
4216 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
4217 070fee27 2020-02-03 stsp return error;
4218 2c251c14 2020-01-15 tracey }
4219 2c251c14 2020-01-15 tracey
4220 2c251c14 2020-01-15 tracey int
4221 c08369d7 2020-01-15 tracey main(int argc, char *argv[])
4222 2c251c14 2020-01-15 tracey {
4223 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
4224 54415d85 2020-01-15 tracey struct gw_trans *gw_trans;
4225 2c251c14 2020-01-15 tracey struct gw_dir *dir = NULL, *tdir;
4226 2c251c14 2020-01-15 tracey const char *page = "index";
4227 4ceb8155 2020-01-15 tracey int gw_malloc = 1;
4228 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
4229 2c251c14 2020-01-15 tracey
4230 54415d85 2020-01-15 tracey if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4231 2c251c14 2020-01-15 tracey errx(1, "malloc");
4232 2c251c14 2020-01-15 tracey
4233 2c251c14 2020-01-15 tracey if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4234 2c251c14 2020-01-15 tracey errx(1, "malloc");
4235 2c251c14 2020-01-15 tracey
4236 2c251c14 2020-01-15 tracey if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4237 2c251c14 2020-01-15 tracey errx(1, "malloc");
4238 2c251c14 2020-01-15 tracey
4239 2c251c14 2020-01-15 tracey if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4240 2c251c14 2020-01-15 tracey errx(1, "malloc");
4241 2c251c14 2020-01-15 tracey
4242 c25c2314 2020-01-28 stsp kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4243 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK) {
4244 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
4245 c119608e 2020-01-28 stsp goto done;
4246 c25c2314 2020-01-28 stsp }
4247 2c251c14 2020-01-15 tracey
4248 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf =
4249 2c251c14 2020-01-15 tracey malloc(sizeof(struct gotweb_conf))) == NULL) {
4250 4ceb8155 2020-01-15 tracey gw_malloc = 0;
4251 387a29ba 2020-01-15 tracey error = got_error_from_errno("malloc");
4252 c119608e 2020-01-28 stsp goto done;
4253 46b9c89b 2020-01-15 tracey }
4254 46b9c89b 2020-01-15 tracey
4255 2c251c14 2020-01-15 tracey TAILQ_INIT(&gw_trans->gw_dirs);
4256 f2f46662 2020-01-23 tracey TAILQ_INIT(&gw_trans->gw_headers);
4257 2c251c14 2020-01-15 tracey
4258 f1200fe3 2020-02-13 tracey gw_trans->action = -1;
4259 2c251c14 2020-01-15 tracey gw_trans->page = 0;
4260 2c251c14 2020-01-15 tracey gw_trans->repos_total = 0;
4261 2c251c14 2020-01-15 tracey gw_trans->repo_path = NULL;
4262 56997cd3 2020-02-14 tracey gw_trans->commit_id = NULL;
4263 742ba378 2020-02-14 stsp gw_trans->headref = GOT_REF_HEAD;
4264 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_HTML;
4265 54415d85 2020-01-15 tracey gw_trans->gw_tmpl->key = gw_templs;
4266 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4267 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->arg = gw_trans;
4268 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->cb = gw_template;
4269 2c251c14 2020-01-15 tracey error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4270 c119608e 2020-01-28 stsp if (error)
4271 2c251c14 2020-01-15 tracey goto done;
4272 2c251c14 2020-01-15 tracey
4273 2c251c14 2020-01-15 tracey error = gw_parse_querystring(gw_trans);
4274 2c251c14 2020-01-15 tracey if (error)
4275 c119608e 2020-01-28 stsp goto done;
4276 2c251c14 2020-01-15 tracey
4277 017d6da3 2020-01-29 tracey if (gw_trans->action == GW_BLOB)
4278 017d6da3 2020-01-29 tracey error = gw_blob(gw_trans);
4279 017d6da3 2020-01-29 tracey else
4280 017d6da3 2020-01-29 tracey error = gw_display_index(gw_trans);
4281 2c251c14 2020-01-15 tracey done:
4282 2c251c14 2020-01-15 tracey if (gw_malloc) {
4283 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_repos_path);
4284 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_name);
4285 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_owner);
4286 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_link);
4287 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo);
4288 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo_url);
4289 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf);
4290 56997cd3 2020-02-14 tracey free(gw_trans->commit_id);
4291 2c251c14 2020-01-15 tracey free(gw_trans->repo_path);
4292 deac617c 2020-02-14 tracey if (gw_trans->repo)
4293 deac617c 2020-02-14 tracey got_repo_close(gw_trans->repo);
4294 2c251c14 2020-01-15 tracey
4295 2c251c14 2020-01-15 tracey TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4296 2c251c14 2020-01-15 tracey free(dir->name);
4297 2c251c14 2020-01-15 tracey free(dir->description);
4298 2c251c14 2020-01-15 tracey free(dir->age);
4299 2c251c14 2020-01-15 tracey free(dir->url);
4300 2c251c14 2020-01-15 tracey free(dir->path);
4301 2c251c14 2020-01-15 tracey free(dir);
4302 2c251c14 2020-01-15 tracey }
4303 2c251c14 2020-01-15 tracey
4304 2c251c14 2020-01-15 tracey }
4305 2c251c14 2020-01-15 tracey
4306 2c251c14 2020-01-15 tracey khttp_free(gw_trans->gw_req);
4307 d56b34ca 2020-01-29 stsp return 0;
4308 2c251c14 2020-01-15 tracey }