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