Blame


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