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