Blame


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