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