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