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