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