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 f2f46662 2020-01-23 tracey char *author;
91 f2f46662 2020-01-23 tracey char *committer;
92 f2f46662 2020-01-23 tracey char *commit_msg;
93 f2f46662 2020-01-23 tracey time_t committer_time;
94 2c251c14 2020-01-15 tracey };
95 2c251c14 2020-01-15 tracey
96 2c251c14 2020-01-15 tracey struct gw_dir {
97 2c251c14 2020-01-15 tracey TAILQ_ENTRY(gw_dir) entry;
98 2c251c14 2020-01-15 tracey char *name;
99 2c251c14 2020-01-15 tracey char *owner;
100 2c251c14 2020-01-15 tracey char *description;
101 2c251c14 2020-01-15 tracey char *url;
102 2c251c14 2020-01-15 tracey char *age;
103 2c251c14 2020-01-15 tracey char *path;
104 2c251c14 2020-01-15 tracey };
105 2c251c14 2020-01-15 tracey
106 f2f46662 2020-01-23 tracey enum gw_key {
107 f2f46662 2020-01-23 tracey KEY_ACTION,
108 f2f46662 2020-01-23 tracey KEY_COMMIT_ID,
109 f2f46662 2020-01-23 tracey KEY_FILE,
110 f2f46662 2020-01-23 tracey KEY_FOLDER,
111 f2f46662 2020-01-23 tracey KEY_HEADREF,
112 f2f46662 2020-01-23 tracey KEY_PAGE,
113 f2f46662 2020-01-23 tracey KEY_PATH,
114 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 54415d85 2020-01-15 tracey static char *gw_get_repo_description(struct gw_trans *,
162 2c251c14 2020-01-15 tracey char *);
163 54415d85 2020-01-15 tracey static char *gw_get_repo_owner(struct gw_trans *,
164 2c251c14 2020-01-15 tracey char *);
165 474370cb 2020-01-15 tracey static char *gw_get_time_str(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 e46f587c 2020-01-29 tracey static char *gw_get_file_blame_blob(struct gw_trans *);
169 f2f46662 2020-01-23 tracey static char *gw_get_repo_tree(struct gw_trans *);
170 f2f46662 2020-01-23 tracey static char *gw_get_diff(struct gw_trans *,
171 f2f46662 2020-01-23 tracey struct gw_header *);
172 4ff7391f 2020-01-28 tracey static char *gw_get_repo_tags(struct gw_trans *,
173 4ff7391f 2020-01-28 tracey struct gw_header *, int, int);
174 54415d85 2020-01-15 tracey static char *gw_get_repo_heads(struct gw_trans *);
175 54415d85 2020-01-15 tracey static char *gw_get_clone_url(struct gw_trans *, char *);
176 54415d85 2020-01-15 tracey static char *gw_get_got_link(struct gw_trans *);
177 54415d85 2020-01-15 tracey static char *gw_get_site_link(struct gw_trans *);
178 2c251c14 2020-01-15 tracey static char *gw_html_escape(const char *);
179 d0ea9c5b 2020-01-15 tracey static char *gw_colordiff_line(char *);
180 2c251c14 2020-01-15 tracey
181 f2f46662 2020-01-23 tracey static char *gw_gen_commit_header(char *, char*);
182 f2f46662 2020-01-23 tracey static char *gw_gen_diff_header(char *, char*);
183 f2f46662 2020-01-23 tracey static char *gw_gen_author_header(char *);
184 f2f46662 2020-01-23 tracey static char *gw_gen_committer_header(char *);
185 f2f46662 2020-01-23 tracey static char *gw_gen_age_header(char *);
186 f2f46662 2020-01-23 tracey static char *gw_gen_commit_msg_header(char *);
187 f2f46662 2020-01-23 tracey static char *gw_gen_tree_header(char *);
188 f2f46662 2020-01-23 tracey
189 f2f46662 2020-01-23 tracey static void gw_free_headers(struct gw_header *);
190 c25c2314 2020-01-28 stsp static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
191 2c251c14 2020-01-15 tracey enum kmime);
192 6d8d8a26 2020-01-29 stsp static const struct got_error* gw_display_index(struct gw_trans *);
193 6d8d8a26 2020-01-29 stsp static void gw_display_error(struct gw_trans *,
194 2c251c14 2020-01-15 tracey const struct got_error *);
195 2c251c14 2020-01-15 tracey
196 2c251c14 2020-01-15 tracey static int gw_template(size_t, void *);
197 2c251c14 2020-01-15 tracey
198 f2f46662 2020-01-23 tracey static const struct got_error* gw_get_header(struct gw_trans *,
199 f2f46662 2020-01-23 tracey struct gw_header *, int);
200 f2f46662 2020-01-23 tracey static const struct got_error* gw_get_commits(struct gw_trans *,
201 f2f46662 2020-01-23 tracey struct gw_header *, int);
202 f2f46662 2020-01-23 tracey static const struct got_error* gw_get_commit(struct gw_trans *,
203 f2f46662 2020-01-23 tracey struct gw_header *);
204 54415d85 2020-01-15 tracey static const struct got_error* gw_apply_unveil(const char *, const char *);
205 147269d5 2020-01-15 tracey static const struct got_error* gw_blame_cb(void *, int, int,
206 ec46ccd7 2020-01-15 tracey struct got_object_id *);
207 54415d85 2020-01-15 tracey static const struct got_error* gw_load_got_paths(struct gw_trans *);
208 54415d85 2020-01-15 tracey static const struct got_error* gw_load_got_path(struct gw_trans *,
209 2c251c14 2020-01-15 tracey struct gw_dir *);
210 54415d85 2020-01-15 tracey static const struct got_error* gw_parse_querystring(struct gw_trans *);
211 2c251c14 2020-01-15 tracey
212 54415d85 2020-01-15 tracey static const struct got_error* gw_blame(struct gw_trans *);
213 e46f587c 2020-01-29 tracey static const struct got_error* gw_blob(struct gw_trans *);
214 f2f46662 2020-01-23 tracey static const struct got_error* gw_diff(struct gw_trans *);
215 54415d85 2020-01-15 tracey static const struct got_error* gw_index(struct gw_trans *);
216 f2f46662 2020-01-23 tracey static const struct got_error* gw_commits(struct gw_trans *);
217 f2f46662 2020-01-23 tracey static const struct got_error* gw_briefs(struct gw_trans *);
218 54415d85 2020-01-15 tracey static const struct got_error* gw_summary(struct gw_trans *);
219 54415d85 2020-01-15 tracey static const struct got_error* gw_tree(struct gw_trans *);
220 4ff7391f 2020-01-28 tracey static const struct got_error* gw_tag(struct gw_trans *);
221 2c251c14 2020-01-15 tracey
222 2c251c14 2020-01-15 tracey struct gw_query_action {
223 2c251c14 2020-01-15 tracey unsigned int func_id;
224 2c251c14 2020-01-15 tracey const char *func_name;
225 54415d85 2020-01-15 tracey const struct got_error *(*func_main)(struct gw_trans *);
226 2c251c14 2020-01-15 tracey char *template;
227 2c251c14 2020-01-15 tracey };
228 2c251c14 2020-01-15 tracey
229 2c251c14 2020-01-15 tracey enum gw_query_actions {
230 2c251c14 2020-01-15 tracey GW_BLAME,
231 e46f587c 2020-01-29 tracey GW_BLOB,
232 f2f46662 2020-01-23 tracey GW_BRIEFS,
233 f2f46662 2020-01-23 tracey GW_COMMITS,
234 f2f46662 2020-01-23 tracey GW_DIFF,
235 2c251c14 2020-01-15 tracey GW_ERR,
236 2c251c14 2020-01-15 tracey GW_INDEX,
237 2c251c14 2020-01-15 tracey GW_SUMMARY,
238 4ff7391f 2020-01-28 tracey GW_TAG,
239 b772de24 2020-01-15 tracey GW_TREE,
240 2c251c14 2020-01-15 tracey };
241 2c251c14 2020-01-15 tracey
242 2c251c14 2020-01-15 tracey static struct gw_query_action gw_query_funcs[] = {
243 f2f46662 2020-01-23 tracey { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
244 017d6da3 2020-01-29 tracey { GW_BLOB, "blob", NULL, NULL },
245 f2f46662 2020-01-23 tracey { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
246 f2f46662 2020-01-23 tracey { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
247 f2f46662 2020-01-23 tracey { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
248 f2f46662 2020-01-23 tracey { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
249 f2f46662 2020-01-23 tracey { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
250 f2f46662 2020-01-23 tracey { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
251 4ff7391f 2020-01-28 tracey { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
252 f2f46662 2020-01-23 tracey { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
253 2c251c14 2020-01-15 tracey };
254 c25c2314 2020-01-28 stsp
255 c25c2314 2020-01-28 stsp static const struct got_error *
256 c25c2314 2020-01-28 stsp gw_kcgi_error(enum kcgi_err kerr)
257 c25c2314 2020-01-28 stsp {
258 c25c2314 2020-01-28 stsp if (kerr == KCGI_OK)
259 c25c2314 2020-01-28 stsp return NULL;
260 c25c2314 2020-01-28 stsp
261 c25c2314 2020-01-28 stsp if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
262 c25c2314 2020-01-28 stsp return got_error(GOT_ERR_CANCELLED);
263 c25c2314 2020-01-28 stsp
264 c25c2314 2020-01-28 stsp if (kerr == KCGI_ENOMEM)
265 c25c2314 2020-01-28 stsp return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
266 2c251c14 2020-01-15 tracey
267 c25c2314 2020-01-28 stsp if (kerr == KCGI_ENFILE)
268 c25c2314 2020-01-28 stsp return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
269 c25c2314 2020-01-28 stsp
270 c25c2314 2020-01-28 stsp if (kerr == KCGI_EAGAIN)
271 c25c2314 2020-01-28 stsp return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
272 c25c2314 2020-01-28 stsp
273 c25c2314 2020-01-28 stsp if (kerr == KCGI_FORM)
274 c25c2314 2020-01-28 stsp return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
275 c25c2314 2020-01-28 stsp
276 c25c2314 2020-01-28 stsp return got_error_from_errno(kcgi_strerror(kerr));
277 c25c2314 2020-01-28 stsp }
278 c25c2314 2020-01-28 stsp
279 2c251c14 2020-01-15 tracey static const struct got_error *
280 54415d85 2020-01-15 tracey gw_apply_unveil(const char *repo_path, const char *repo_file)
281 2c251c14 2020-01-15 tracey {
282 2c251c14 2020-01-15 tracey const struct got_error *err;
283 2c251c14 2020-01-15 tracey
284 2c251c14 2020-01-15 tracey if (repo_path && repo_file) {
285 2c251c14 2020-01-15 tracey char *full_path;
286 88d59c36 2020-01-29 stsp if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
287 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf unveil");
288 2c251c14 2020-01-15 tracey if (unveil(full_path, "r") != 0)
289 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", full_path);
290 2c251c14 2020-01-15 tracey }
291 2c251c14 2020-01-15 tracey
292 2c251c14 2020-01-15 tracey if (repo_path && unveil(repo_path, "r") != 0)
293 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", repo_path);
294 2c251c14 2020-01-15 tracey
295 2c251c14 2020-01-15 tracey if (unveil("/tmp", "rwc") != 0)
296 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", "/tmp");
297 2c251c14 2020-01-15 tracey
298 2c251c14 2020-01-15 tracey err = got_privsep_unveil_exec_helpers();
299 2c251c14 2020-01-15 tracey if (err != NULL)
300 2c251c14 2020-01-15 tracey return err;
301 2c251c14 2020-01-15 tracey
302 2c251c14 2020-01-15 tracey if (unveil(NULL, NULL) != 0)
303 2c251c14 2020-01-15 tracey return got_error_from_errno("unveil");
304 2c251c14 2020-01-15 tracey
305 2c251c14 2020-01-15 tracey return NULL;
306 87f9ebf5 2020-01-15 tracey }
307 65b95fb2 2020-01-15 tracey
308 2c251c14 2020-01-15 tracey static const struct got_error *
309 54415d85 2020-01-15 tracey gw_blame(struct gw_trans *gw_trans)
310 2c251c14 2020-01-15 tracey {
311 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
312 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
313 f2f46662 2020-01-23 tracey char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
314 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
315 2c251c14 2020-01-15 tracey
316 6bee13de 2020-01-16 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
317 f2f46662 2020-01-23 tracey NULL) == -1)
318 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
319 6bee13de 2020-01-16 tracey
320 f2f46662 2020-01-23 tracey if ((header = gw_init_header()) == NULL)
321 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
322 f2f46662 2020-01-23 tracey
323 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
324 ec46ccd7 2020-01-15 tracey if (error)
325 5ddf0079 2020-01-29 stsp goto done;
326 ec46ccd7 2020-01-15 tracey
327 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
328 f2f46662 2020-01-23 tracey if (error)
329 5ddf0079 2020-01-29 stsp goto done;
330 ec46ccd7 2020-01-15 tracey
331 e46f587c 2020-01-29 tracey blame_html = gw_get_file_blame_blob(gw_trans);
332 f2f46662 2020-01-23 tracey
333 6d9fc692 2020-01-28 stsp if (blame_html == NULL) {
334 f2f46662 2020-01-23 tracey blame_html = strdup("");
335 5ddf0079 2020-01-29 stsp if (blame_html == NULL) {
336 5ddf0079 2020-01-29 stsp error = got_error_from_errno("strdup");
337 5ddf0079 2020-01-29 stsp goto done;
338 5ddf0079 2020-01-29 stsp }
339 6d9fc692 2020-01-28 stsp }
340 f2f46662 2020-01-23 tracey
341 88d59c36 2020-01-29 stsp if (asprintf(&blame_html_disp, blame_header,
342 f2f46662 2020-01-23 tracey gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
343 f2f46662 2020-01-23 tracey gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
344 88d59c36 2020-01-29 stsp blame_html) == -1) {
345 6d9fc692 2020-01-28 stsp error = got_error_from_errno("asprintf");
346 6d9fc692 2020-01-28 stsp goto done;
347 6d9fc692 2020-01-28 stsp }
348 f2f46662 2020-01-23 tracey
349 6d9fc692 2020-01-28 stsp if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
350 6d9fc692 2020-01-28 stsp error = got_error_from_errno("asprintf");
351 6d9fc692 2020-01-28 stsp goto done;
352 6d9fc692 2020-01-28 stsp }
353 f2f46662 2020-01-23 tracey
354 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, blame);
355 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
356 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
357 6d9fc692 2020-01-28 stsp done:
358 f2f46662 2020-01-23 tracey got_ref_list_free(&header->refs);
359 f2f46662 2020-01-23 tracey gw_free_headers(header);
360 f2f46662 2020-01-23 tracey free(blame_html_disp);
361 f2f46662 2020-01-23 tracey free(blame_html);
362 f2f46662 2020-01-23 tracey free(blame);
363 2c251c14 2020-01-15 tracey return error;
364 2c251c14 2020-01-15 tracey }
365 2c251c14 2020-01-15 tracey
366 2c251c14 2020-01-15 tracey static const struct got_error *
367 e46f587c 2020-01-29 tracey gw_blob(struct gw_trans *gw_trans)
368 e46f587c 2020-01-29 tracey {
369 e46f587c 2020-01-29 tracey const struct got_error *error = NULL;
370 e46f587c 2020-01-29 tracey struct gw_header *header = NULL;
371 b27da801 2020-01-29 tracey char *blob = NULL;
372 e46f587c 2020-01-29 tracey enum kcgi_err kerr;
373 e46f587c 2020-01-29 tracey
374 e46f587c 2020-01-29 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
375 e46f587c 2020-01-29 tracey NULL) == -1)
376 e46f587c 2020-01-29 tracey return got_error_from_errno("pledge");
377 e46f587c 2020-01-29 tracey
378 e46f587c 2020-01-29 tracey if ((header = gw_init_header()) == NULL)
379 e46f587c 2020-01-29 tracey return got_error_from_errno("malloc");
380 e46f587c 2020-01-29 tracey
381 e46f587c 2020-01-29 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
382 e46f587c 2020-01-29 tracey if (error)
383 e46f587c 2020-01-29 tracey goto done;
384 e46f587c 2020-01-29 tracey
385 e46f587c 2020-01-29 tracey error = gw_get_header(gw_trans, header, 1);
386 e46f587c 2020-01-29 tracey if (error)
387 e46f587c 2020-01-29 tracey goto done;
388 e46f587c 2020-01-29 tracey
389 b27da801 2020-01-29 tracey blob = gw_get_file_blame_blob(gw_trans);
390 e46f587c 2020-01-29 tracey
391 b27da801 2020-01-29 tracey if (blob == NULL) {
392 b27da801 2020-01-29 tracey blob = strdup("");
393 b27da801 2020-01-29 tracey if (blob == NULL) {
394 e46f587c 2020-01-29 tracey error = got_error_from_errno("strdup");
395 e46f587c 2020-01-29 tracey goto done;
396 e46f587c 2020-01-29 tracey }
397 e46f587c 2020-01-29 tracey }
398 e46f587c 2020-01-29 tracey
399 017d6da3 2020-01-29 tracey if (gw_trans->mime == KMIME_APP_OCTET_STREAM)
400 017d6da3 2020-01-29 tracey goto done;
401 017d6da3 2020-01-29 tracey else {
402 017d6da3 2020-01-29 tracey kerr = khttp_puts(gw_trans->gw_req, blob);
403 017d6da3 2020-01-29 tracey if (kerr != KCGI_OK)
404 017d6da3 2020-01-29 tracey error = gw_kcgi_error(kerr);
405 017d6da3 2020-01-29 tracey }
406 e46f587c 2020-01-29 tracey done:
407 e46f587c 2020-01-29 tracey got_ref_list_free(&header->refs);
408 e46f587c 2020-01-29 tracey gw_free_headers(header);
409 e46f587c 2020-01-29 tracey free(blob);
410 e46f587c 2020-01-29 tracey return error;
411 e46f587c 2020-01-29 tracey }
412 e46f587c 2020-01-29 tracey
413 e46f587c 2020-01-29 tracey static const struct got_error *
414 f2f46662 2020-01-23 tracey gw_diff(struct gw_trans *gw_trans)
415 2c251c14 2020-01-15 tracey {
416 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
417 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
418 f2f46662 2020-01-23 tracey char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
419 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
420 2c251c14 2020-01-15 tracey
421 f2f46662 2020-01-23 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
422 f2f46662 2020-01-23 tracey NULL) == -1)
423 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
424 6bee13de 2020-01-16 tracey
425 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
426 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
427 f2f46662 2020-01-23 tracey
428 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
429 8087c3c5 2020-01-15 tracey if (error)
430 390d412c 2020-01-28 stsp goto done;
431 8087c3c5 2020-01-15 tracey
432 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
433 f2f46662 2020-01-23 tracey if (error)
434 390d412c 2020-01-28 stsp goto done;
435 2c251c14 2020-01-15 tracey
436 f2f46662 2020-01-23 tracey diff_html = gw_get_diff(gw_trans, header);
437 6bee13de 2020-01-16 tracey
438 390d412c 2020-01-28 stsp if (diff_html == NULL) {
439 f2f46662 2020-01-23 tracey diff_html = strdup("");
440 390d412c 2020-01-28 stsp if (diff_html == NULL) {
441 390d412c 2020-01-28 stsp error = got_error_from_errno("strdup");
442 390d412c 2020-01-28 stsp goto done;
443 390d412c 2020-01-28 stsp }
444 390d412c 2020-01-28 stsp }
445 87f9ebf5 2020-01-15 tracey
446 390d412c 2020-01-28 stsp if (asprintf(&diff_html_disp, diff_header,
447 2ac037ec 2020-01-24 tracey gw_gen_diff_header(header->parent_id, header->commit_id),
448 f2f46662 2020-01-23 tracey gw_gen_commit_header(header->commit_id, header->refs_str),
449 f2f46662 2020-01-23 tracey gw_gen_tree_header(header->tree_id),
450 f2f46662 2020-01-23 tracey gw_gen_author_header(header->author),
451 f2f46662 2020-01-23 tracey gw_gen_committer_header(header->committer),
452 f2f46662 2020-01-23 tracey gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
453 f2f46662 2020-01-23 tracey gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
454 390d412c 2020-01-28 stsp diff_html) == -1) {
455 390d412c 2020-01-28 stsp error = got_error_from_errno("asprintf");
456 390d412c 2020-01-28 stsp goto done;
457 390d412c 2020-01-28 stsp }
458 2204c934 2020-01-15 tracey
459 390d412c 2020-01-28 stsp if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
460 390d412c 2020-01-28 stsp error = got_error_from_errno("asprintf");
461 390d412c 2020-01-28 stsp goto done;
462 390d412c 2020-01-28 stsp }
463 87f9ebf5 2020-01-15 tracey
464 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, diff);
465 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
466 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
467 390d412c 2020-01-28 stsp done:
468 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
469 f2f46662 2020-01-23 tracey gw_free_headers(header);
470 f2f46662 2020-01-23 tracey free(diff_html_disp);
471 f2f46662 2020-01-23 tracey free(diff_html);
472 f2f46662 2020-01-23 tracey free(diff);
473 2204c934 2020-01-15 tracey return error;
474 2204c934 2020-01-15 tracey }
475 2204c934 2020-01-15 tracey
476 2204c934 2020-01-15 tracey static const struct got_error *
477 54415d85 2020-01-15 tracey gw_index(struct gw_trans *gw_trans)
478 2c251c14 2020-01-15 tracey {
479 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
480 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir = NULL;
481 2c251c14 2020-01-15 tracey char *html, *navs, *next, *prev;
482 387a29ba 2020-01-15 tracey unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
483 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
484 2c251c14 2020-01-15 tracey
485 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
486 6bee13de 2020-01-16 tracey NULL) == -1) {
487 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
488 6bee13de 2020-01-16 tracey return error;
489 6bee13de 2020-01-16 tracey }
490 6bee13de 2020-01-16 tracey
491 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
492 46b9c89b 2020-01-15 tracey if (error)
493 46b9c89b 2020-01-15 tracey return error;
494 46b9c89b 2020-01-15 tracey
495 2c251c14 2020-01-15 tracey error = gw_load_got_paths(gw_trans);
496 46b9c89b 2020-01-15 tracey if (error)
497 2c251c14 2020-01-15 tracey return error;
498 2c251c14 2020-01-15 tracey
499 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
500 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
501 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
502 2c251c14 2020-01-15 tracey
503 bce5dac1 2020-01-28 stsp if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
504 bce5dac1 2020-01-28 stsp if (asprintf(&html, index_projects_empty,
505 bce5dac1 2020-01-28 stsp gw_trans->gw_conf->got_repos_path) == -1)
506 bce5dac1 2020-01-28 stsp return got_error_from_errno("asprintf");
507 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, html);
508 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
509 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
510 bce5dac1 2020-01-28 stsp free(html);
511 c25c2314 2020-01-28 stsp return error;
512 bce5dac1 2020-01-28 stsp }
513 bce5dac1 2020-01-28 stsp
514 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
515 2c251c14 2020-01-15 tracey dir_c++;
516 2c251c14 2020-01-15 tracey
517 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
518 2c251c14 2020-01-15 tracey if (gw_trans->page > 0 && (gw_trans->page *
519 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
520 2c251c14 2020-01-15 tracey prev_disp++;
521 2c251c14 2020-01-15 tracey continue;
522 2c251c14 2020-01-15 tracey }
523 2c251c14 2020-01-15 tracey
524 2c251c14 2020-01-15 tracey prev_disp++;
525 f2f46662 2020-01-23 tracey
526 f2f46662 2020-01-23 tracey if (error)
527 f2f46662 2020-01-23 tracey return error;
528 88d59c36 2020-01-29 stsp if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
529 88d59c36 2020-01-29 stsp gw_dir->name, gw_dir->name) == -1)
530 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
531 2c251c14 2020-01-15 tracey
532 88d59c36 2020-01-29 stsp if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
533 46b9c89b 2020-01-15 tracey gw_dir->description, gw_dir->owner, gw_dir->age,
534 88d59c36 2020-01-29 stsp navs) == -1)
535 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
536 2c251c14 2020-01-15 tracey
537 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, html);
538 2c251c14 2020-01-15 tracey free(navs);
539 2c251c14 2020-01-15 tracey free(html);
540 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
541 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
542 2c251c14 2020-01-15 tracey
543 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display == 0)
544 2c251c14 2020-01-15 tracey continue;
545 2c251c14 2020-01-15 tracey
546 c25c2314 2020-01-28 stsp if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
547 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
548 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
549 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
550 c25c2314 2020-01-28 stsp } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
551 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
552 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
553 c25c2314 2020-01-28 stsp prev_disp == gw_trans->repos_total)) {
554 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
555 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
556 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
557 c25c2314 2020-01-28 stsp }
558 2c251c14 2020-01-15 tracey
559 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
560 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
561 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
562 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total)) {
563 88d59c36 2020-01-29 stsp if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
564 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
565 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, prev);
566 2c251c14 2020-01-15 tracey free(prev);
567 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
568 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
569 2c251c14 2020-01-15 tracey }
570 2c251c14 2020-01-15 tracey
571 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
572 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
573 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
574 2c251c14 2020-01-15 tracey
575 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display > 0 &&
576 2c251c14 2020-01-15 tracey next_disp == gw_trans->gw_conf->got_max_repos_display &&
577 2c251c14 2020-01-15 tracey dir_c != (gw_trans->page + 1) *
578 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) {
579 88d59c36 2020-01-29 stsp if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
580 2c251c14 2020-01-15 tracey return got_error_from_errno("calloc");
581 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, next);
582 2c251c14 2020-01-15 tracey free(next);
583 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
584 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
585 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
586 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
587 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
588 2c251c14 2020-01-15 tracey next_disp = 0;
589 2c251c14 2020-01-15 tracey break;
590 2c251c14 2020-01-15 tracey }
591 2c251c14 2020-01-15 tracey
592 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
593 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
594 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
595 c25c2314 2020-01-28 stsp prev_disp == gw_trans->repos_total)) {
596 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
597 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
598 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
599 c25c2314 2020-01-28 stsp }
600 2c251c14 2020-01-15 tracey
601 2c251c14 2020-01-15 tracey next_disp++;
602 2c251c14 2020-01-15 tracey }
603 2c251c14 2020-01-15 tracey return error;
604 2c251c14 2020-01-15 tracey }
605 2c251c14 2020-01-15 tracey
606 2c251c14 2020-01-15 tracey static const struct got_error *
607 f2f46662 2020-01-23 tracey gw_commits(struct gw_trans *gw_trans)
608 2c251c14 2020-01-15 tracey {
609 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
610 f2f46662 2020-01-23 tracey char *commits_html, *commits_navs_html;
611 f2f46662 2020-01-23 tracey struct gw_header *header = NULL, *n_header = NULL;
612 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
613 6bee13de 2020-01-16 tracey
614 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
615 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
616 f2f46662 2020-01-23 tracey
617 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
618 6bee13de 2020-01-16 tracey NULL) == -1) {
619 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
620 5ddf0079 2020-01-29 stsp goto done;
621 6bee13de 2020-01-16 tracey }
622 8087c3c5 2020-01-15 tracey
623 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
624 8087c3c5 2020-01-15 tracey if (error)
625 5ddf0079 2020-01-29 stsp goto done;
626 2c251c14 2020-01-15 tracey
627 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header,
628 f2f46662 2020-01-23 tracey gw_trans->gw_conf->got_max_commits_display);
629 18da9978 2020-01-29 stsp if (error)
630 18da9978 2020-01-29 stsp goto done;
631 4ceb8155 2020-01-15 tracey
632 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
633 18da9978 2020-01-29 stsp if (kerr != KCGI_OK) {
634 18da9978 2020-01-29 stsp error = gw_kcgi_error(kerr);
635 18da9978 2020-01-29 stsp goto done;
636 18da9978 2020-01-29 stsp }
637 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
638 88d59c36 2020-01-29 stsp if (asprintf(&commits_navs_html, commits_navs,
639 f2f46662 2020-01-23 tracey gw_trans->repo_name, n_header->commit_id,
640 f2f46662 2020-01-23 tracey gw_trans->repo_name, n_header->commit_id,
641 88d59c36 2020-01-29 stsp gw_trans->repo_name, n_header->commit_id) == -1) {
642 18da9978 2020-01-29 stsp error = got_error_from_errno("asprintf");
643 18da9978 2020-01-29 stsp goto done;
644 18da9978 2020-01-29 stsp }
645 2c251c14 2020-01-15 tracey
646 88d59c36 2020-01-29 stsp if (asprintf(&commits_html, commits_line,
647 e46f587c 2020-01-29 tracey gw_gen_commit_header(n_header->commit_id,
648 f2f46662 2020-01-23 tracey n_header->refs_str),
649 e46f587c 2020-01-29 tracey gw_gen_author_header(n_header->author),
650 e46f587c 2020-01-29 tracey gw_gen_committer_header(n_header->committer),
651 f2f46662 2020-01-23 tracey gw_gen_age_header(gw_get_time_str(n_header->committer_time,
652 f2f46662 2020-01-23 tracey TM_LONG)), gw_html_escape(n_header->commit_msg),
653 88d59c36 2020-01-29 stsp commits_navs_html) == -1) {
654 18da9978 2020-01-29 stsp error = got_error_from_errno("asprintf");
655 18da9978 2020-01-29 stsp goto done;
656 18da9978 2020-01-29 stsp }
657 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, commits_html);
658 18da9978 2020-01-29 stsp if (kerr != KCGI_OK) {
659 18da9978 2020-01-29 stsp error = gw_kcgi_error(kerr);
660 18da9978 2020-01-29 stsp goto done;
661 18da9978 2020-01-29 stsp }
662 f2f46662 2020-01-23 tracey }
663 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
664 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
665 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
666 18da9978 2020-01-29 stsp done:
667 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
668 f2f46662 2020-01-23 tracey gw_free_headers(header);
669 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
670 f2f46662 2020-01-23 tracey gw_free_headers(n_header);
671 2c251c14 2020-01-15 tracey return error;
672 2c251c14 2020-01-15 tracey }
673 2c251c14 2020-01-15 tracey
674 2c251c14 2020-01-15 tracey static const struct got_error *
675 f2f46662 2020-01-23 tracey gw_briefs(struct gw_trans *gw_trans)
676 2c251c14 2020-01-15 tracey {
677 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
678 f2f46662 2020-01-23 tracey char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
679 f2f46662 2020-01-23 tracey struct gw_header *header = NULL, *n_header = NULL;
680 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
681 17a96b9f 2020-01-15 tracey
682 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
683 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
684 f2f46662 2020-01-23 tracey
685 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
686 6bee13de 2020-01-16 tracey NULL) == -1) {
687 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
688 5ddf0079 2020-01-29 stsp goto done;
689 6bee13de 2020-01-16 tracey }
690 6bee13de 2020-01-16 tracey
691 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
692 8087c3c5 2020-01-15 tracey if (error)
693 5ddf0079 2020-01-29 stsp goto done;
694 9d84e7dd 2020-01-15 tracey
695 f2f46662 2020-01-23 tracey if (gw_trans->action == GW_SUMMARY)
696 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
697 f2f46662 2020-01-23 tracey else
698 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header,
699 f2f46662 2020-01-23 tracey gw_trans->gw_conf->got_max_commits_display);
700 c25c2314 2020-01-28 stsp if (error)
701 0e00e8f4 2020-01-29 stsp goto done;
702 c25c2314 2020-01-28 stsp
703 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
704 0e00e8f4 2020-01-29 stsp if (kerr != KCGI_OK) {
705 0e00e8f4 2020-01-29 stsp error = gw_kcgi_error(kerr);
706 0e00e8f4 2020-01-29 stsp goto done;
707 0e00e8f4 2020-01-29 stsp }
708 c25c2314 2020-01-28 stsp
709 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
710 88d59c36 2020-01-29 stsp if (asprintf(&briefs_navs_html, briefs_navs,
711 f2f46662 2020-01-23 tracey gw_trans->repo_name, n_header->commit_id,
712 f2f46662 2020-01-23 tracey gw_trans->repo_name, n_header->commit_id,
713 88d59c36 2020-01-29 stsp gw_trans->repo_name, n_header->commit_id) == -1) {
714 0e00e8f4 2020-01-29 stsp error = got_error_from_errno("asprintf");
715 0e00e8f4 2020-01-29 stsp goto done;
716 0e00e8f4 2020-01-29 stsp }
717 f2f46662 2020-01-23 tracey newline = strchr(n_header->commit_msg, '\n');
718 f2f46662 2020-01-23 tracey if (newline)
719 f2f46662 2020-01-23 tracey *newline = '\0';
720 88d59c36 2020-01-29 stsp if (asprintf(&briefs_html, briefs_line,
721 f2f46662 2020-01-23 tracey gw_get_time_str(n_header->committer_time, TM_DIFF),
722 f2f46662 2020-01-23 tracey n_header->author, gw_html_escape(n_header->commit_msg),
723 88d59c36 2020-01-29 stsp briefs_navs_html) == -1) {
724 0e00e8f4 2020-01-29 stsp error = got_error_from_errno("asprintf");
725 0e00e8f4 2020-01-29 stsp goto done;
726 0e00e8f4 2020-01-29 stsp }
727 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, briefs_html);
728 0e00e8f4 2020-01-29 stsp if (kerr != KCGI_OK) {
729 0e00e8f4 2020-01-29 stsp error = gw_kcgi_error(kerr);
730 0e00e8f4 2020-01-29 stsp goto done;
731 0e00e8f4 2020-01-29 stsp }
732 9d84e7dd 2020-01-15 tracey }
733 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
734 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
735 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
736 0e00e8f4 2020-01-29 stsp done:
737 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
738 f2f46662 2020-01-23 tracey gw_free_headers(header);
739 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
740 f2f46662 2020-01-23 tracey gw_free_headers(n_header);
741 2c251c14 2020-01-15 tracey return error;
742 2c251c14 2020-01-15 tracey }
743 2c251c14 2020-01-15 tracey
744 2c251c14 2020-01-15 tracey static const struct got_error *
745 54415d85 2020-01-15 tracey gw_summary(struct gw_trans *gw_trans)
746 387a29ba 2020-01-15 tracey {
747 387a29ba 2020-01-15 tracey const struct got_error *error = NULL;
748 20f34652 2020-01-29 stsp char *description_html = NULL, *repo_owner_html = NULL;
749 20f34652 2020-01-29 stsp char *age = NULL, *repo_age_html = NULL, *cloneurl_html = NULL;
750 20f34652 2020-01-29 stsp char *tags = NULL, *tags_html = NULL;
751 20f34652 2020-01-29 stsp char *heads = NULL, *heads_html = NULL;
752 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
753 387a29ba 2020-01-15 tracey
754 f4df82f9 2020-01-29 stsp if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
755 f4df82f9 2020-01-29 stsp return got_error_from_errno("pledge");
756 6bee13de 2020-01-16 tracey
757 f2f46662 2020-01-23 tracey /* unveil is applied with gw_briefs below */
758 46b9c89b 2020-01-15 tracey
759 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
760 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
761 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
762 c25c2314 2020-01-28 stsp
763 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_description) {
764 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->description != NULL &&
765 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->description, "") != 0)) {
766 88d59c36 2020-01-29 stsp if (asprintf(&description_html, description,
767 20f34652 2020-01-29 stsp gw_trans->gw_dir->description) == -1) {
768 20f34652 2020-01-29 stsp error = got_error_from_errno("asprintf");
769 20f34652 2020-01-29 stsp goto done;
770 20f34652 2020-01-29 stsp }
771 46b9c89b 2020-01-15 tracey
772 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, description_html);
773 20f34652 2020-01-29 stsp if (kerr != KCGI_OK) {
774 20f34652 2020-01-29 stsp error = gw_kcgi_error(kerr);
775 20f34652 2020-01-29 stsp goto done;
776 20f34652 2020-01-29 stsp }
777 46b9c89b 2020-01-15 tracey }
778 46b9c89b 2020-01-15 tracey }
779 46b9c89b 2020-01-15 tracey
780 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_owner) {
781 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->owner != NULL &&
782 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
783 88d59c36 2020-01-29 stsp if (asprintf(&repo_owner_html, repo_owner,
784 20f34652 2020-01-29 stsp gw_trans->gw_dir->owner) == -1) {
785 20f34652 2020-01-29 stsp error = got_error_from_errno("asprintf");
786 20f34652 2020-01-29 stsp goto done;
787 20f34652 2020-01-29 stsp }
788 46b9c89b 2020-01-15 tracey
789 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
790 20f34652 2020-01-29 stsp if (kerr != KCGI_OK) {
791 20f34652 2020-01-29 stsp error = gw_kcgi_error(kerr);
792 20f34652 2020-01-29 stsp goto done;
793 20f34652 2020-01-29 stsp }
794 46b9c89b 2020-01-15 tracey }
795 46b9c89b 2020-01-15 tracey }
796 46b9c89b 2020-01-15 tracey
797 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age) {
798 d126c1b7 2020-01-29 stsp error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
799 c6b62706 2020-01-15 tracey "refs/heads", TM_LONG);
800 d126c1b7 2020-01-29 stsp if (error)
801 20f34652 2020-01-29 stsp goto done;
802 d126c1b7 2020-01-29 stsp if (strcmp(age, "") != 0) {
803 20f34652 2020-01-29 stsp if (asprintf(&repo_age_html, last_change, age) == -1) {
804 20f34652 2020-01-29 stsp error = got_error_from_errno("asprintf");
805 20f34652 2020-01-29 stsp goto done;
806 20f34652 2020-01-29 stsp }
807 46b9c89b 2020-01-15 tracey
808 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
809 20f34652 2020-01-29 stsp if (kerr != KCGI_OK) {
810 20f34652 2020-01-29 stsp error = gw_kcgi_error(kerr);
811 20f34652 2020-01-29 stsp goto done;
812 20f34652 2020-01-29 stsp }
813 46b9c89b 2020-01-15 tracey }
814 46b9c89b 2020-01-15 tracey }
815 46b9c89b 2020-01-15 tracey
816 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_cloneurl) {
817 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->url != NULL &&
818 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->url, "") != 0)) {
819 88d59c36 2020-01-29 stsp if (asprintf(&cloneurl_html, cloneurl,
820 20f34652 2020-01-29 stsp gw_trans->gw_dir->url) == -1) {
821 20f34652 2020-01-29 stsp error = got_error_from_errno("asprintf");
822 20f34652 2020-01-29 stsp goto done;
823 20f34652 2020-01-29 stsp }
824 46b9c89b 2020-01-15 tracey
825 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
826 20f34652 2020-01-29 stsp if (kerr != KCGI_OK) {
827 20f34652 2020-01-29 stsp error = gw_kcgi_error(kerr);
828 20f34652 2020-01-29 stsp goto done;
829 20f34652 2020-01-29 stsp }
830 46b9c89b 2020-01-15 tracey }
831 46b9c89b 2020-01-15 tracey }
832 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
833 20f34652 2020-01-29 stsp if (kerr != KCGI_OK) {
834 20f34652 2020-01-29 stsp error = gw_kcgi_error(kerr);
835 20f34652 2020-01-29 stsp goto done;
836 20f34652 2020-01-29 stsp }
837 46b9c89b 2020-01-15 tracey
838 f2f46662 2020-01-23 tracey error = gw_briefs(gw_trans);
839 f2f46662 2020-01-23 tracey if (error)
840 20f34652 2020-01-29 stsp goto done;
841 f2f46662 2020-01-23 tracey
842 4ff7391f 2020-01-28 tracey tags = gw_get_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP, TAGBRIEF);
843 8d4d2453 2020-01-15 tracey heads = gw_get_repo_heads(gw_trans);
844 387a29ba 2020-01-15 tracey
845 8d4d2453 2020-01-15 tracey if (tags != NULL && strcmp(tags, "") != 0) {
846 20f34652 2020-01-29 stsp if (asprintf(&tags_html, summary_tags, tags) == -1) {
847 20f34652 2020-01-29 stsp error = got_error_from_errno("asprintf");
848 20f34652 2020-01-29 stsp goto done;
849 20f34652 2020-01-29 stsp }
850 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, tags_html);
851 20f34652 2020-01-29 stsp if (kerr != KCGI_OK) {
852 20f34652 2020-01-29 stsp error = gw_kcgi_error(kerr);
853 20f34652 2020-01-29 stsp goto done;
854 20f34652 2020-01-29 stsp }
855 8d4d2453 2020-01-15 tracey }
856 8d4d2453 2020-01-15 tracey
857 8d4d2453 2020-01-15 tracey if (heads != NULL && strcmp(heads, "") != 0) {
858 20f34652 2020-01-29 stsp if (asprintf(&heads_html, summary_heads, heads) == -1) {
859 20f34652 2020-01-29 stsp error = got_error_from_errno("asprintf");
860 20f34652 2020-01-29 stsp goto done;
861 20f34652 2020-01-29 stsp }
862 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, heads_html);
863 20f34652 2020-01-29 stsp if (kerr != KCGI_OK) {
864 20f34652 2020-01-29 stsp error = gw_kcgi_error(kerr);
865 20f34652 2020-01-29 stsp goto done;
866 20f34652 2020-01-29 stsp }
867 8d4d2453 2020-01-15 tracey }
868 20f34652 2020-01-29 stsp done:
869 20f34652 2020-01-29 stsp free(description_html);
870 20f34652 2020-01-29 stsp free(repo_owner_html);
871 20f34652 2020-01-29 stsp free(age);
872 20f34652 2020-01-29 stsp free(repo_age_html);
873 20f34652 2020-01-29 stsp free(cloneurl_html);
874 20f34652 2020-01-29 stsp free(tags);
875 20f34652 2020-01-29 stsp free(tags_html);
876 20f34652 2020-01-29 stsp free(heads);
877 20f34652 2020-01-29 stsp free(heads_html);
878 2204c934 2020-01-15 tracey return error;
879 2204c934 2020-01-15 tracey }
880 2204c934 2020-01-15 tracey
881 2204c934 2020-01-15 tracey static const struct got_error *
882 f2f46662 2020-01-23 tracey gw_tree(struct gw_trans *gw_trans)
883 b772de24 2020-01-15 tracey {
884 b772de24 2020-01-15 tracey const struct got_error *error = NULL;
885 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
886 f2f46662 2020-01-23 tracey char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
887 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
888 6bee13de 2020-01-16 tracey
889 f2f46662 2020-01-23 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
890 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
891 b772de24 2020-01-15 tracey
892 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
893 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
894 f2f46662 2020-01-23 tracey
895 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
896 b772de24 2020-01-15 tracey if (error)
897 5ddf0079 2020-01-29 stsp goto done;
898 b772de24 2020-01-15 tracey
899 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
900 f2f46662 2020-01-23 tracey if (error)
901 42281175 2020-01-29 stsp goto done;
902 b772de24 2020-01-15 tracey
903 f2f46662 2020-01-23 tracey tree_html = gw_get_repo_tree(gw_trans);
904 b772de24 2020-01-15 tracey
905 4ff7391f 2020-01-28 tracey if (tree_html == NULL) {
906 f2f46662 2020-01-23 tracey tree_html = strdup("");
907 4ff7391f 2020-01-28 tracey if (tree_html == NULL) {
908 4ff7391f 2020-01-28 tracey error = got_error_from_errno("strdup");
909 4ff7391f 2020-01-28 tracey goto done;
910 4ff7391f 2020-01-28 tracey }
911 4ff7391f 2020-01-28 tracey }
912 6bee13de 2020-01-16 tracey
913 88d59c36 2020-01-29 stsp if (asprintf(&tree_html_disp, tree_header,
914 f2f46662 2020-01-23 tracey gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
915 f2f46662 2020-01-23 tracey gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
916 88d59c36 2020-01-29 stsp tree_html) == -1) {
917 42281175 2020-01-29 stsp error = got_error_from_errno("asprintf");
918 42281175 2020-01-29 stsp goto done;
919 42281175 2020-01-29 stsp }
920 8087c3c5 2020-01-15 tracey
921 42281175 2020-01-29 stsp if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
922 42281175 2020-01-29 stsp error = got_error_from_errno("asprintf");
923 42281175 2020-01-29 stsp goto done;
924 42281175 2020-01-29 stsp }
925 8087c3c5 2020-01-15 tracey
926 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, tree);
927 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
928 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
929 4ff7391f 2020-01-28 tracey done:
930 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
931 f2f46662 2020-01-23 tracey gw_free_headers(header);
932 f2f46662 2020-01-23 tracey free(tree_html_disp);
933 f2f46662 2020-01-23 tracey free(tree_html);
934 f2f46662 2020-01-23 tracey free(tree);
935 4ff7391f 2020-01-28 tracey return error;
936 4ff7391f 2020-01-28 tracey }
937 4ff7391f 2020-01-28 tracey
938 4ff7391f 2020-01-28 tracey static const struct got_error *
939 4ff7391f 2020-01-28 tracey gw_tag(struct gw_trans *gw_trans)
940 4ff7391f 2020-01-28 tracey {
941 4ff7391f 2020-01-28 tracey const struct got_error *error = NULL;
942 4ff7391f 2020-01-28 tracey struct gw_header *header = NULL;
943 4ff7391f 2020-01-28 tracey char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
944 4ff7391f 2020-01-28 tracey enum kcgi_err kerr;
945 4ff7391f 2020-01-28 tracey
946 4ff7391f 2020-01-28 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
947 4ff7391f 2020-01-28 tracey return got_error_from_errno("pledge");
948 4ff7391f 2020-01-28 tracey
949 4ff7391f 2020-01-28 tracey if ((header = gw_init_header()) == NULL)
950 4ff7391f 2020-01-28 tracey return got_error_from_errno("malloc");
951 4ff7391f 2020-01-28 tracey
952 4ff7391f 2020-01-28 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
953 4ff7391f 2020-01-28 tracey if (error)
954 5ddf0079 2020-01-29 stsp goto done;
955 4ff7391f 2020-01-28 tracey
956 4ff7391f 2020-01-28 tracey error = gw_get_header(gw_trans, header, 1);
957 4ff7391f 2020-01-28 tracey if (error)
958 470cd826 2020-01-29 stsp goto done;
959 4ff7391f 2020-01-28 tracey
960 4ff7391f 2020-01-28 tracey tag_html = gw_get_repo_tags(gw_trans, header, 1, TAGFULL);
961 4ff7391f 2020-01-28 tracey if (tag_html == NULL) {
962 4ff7391f 2020-01-28 tracey tag_html = strdup("");
963 4ff7391f 2020-01-28 tracey if (tag_html == NULL) {
964 4ff7391f 2020-01-28 tracey error = got_error_from_errno("strdup");
965 4ff7391f 2020-01-28 tracey goto done;
966 4ff7391f 2020-01-28 tracey }
967 4ff7391f 2020-01-28 tracey }
968 4ff7391f 2020-01-28 tracey
969 88d59c36 2020-01-29 stsp if (asprintf(&tag_html_disp, tag_header,
970 4ff7391f 2020-01-28 tracey gw_gen_commit_header(header->commit_id, header->refs_str),
971 4ff7391f 2020-01-28 tracey gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
972 88d59c36 2020-01-29 stsp tag_html) == -1) {
973 470cd826 2020-01-29 stsp error = got_error_from_errno("asprintf");
974 470cd826 2020-01-29 stsp goto done;
975 470cd826 2020-01-29 stsp }
976 4ff7391f 2020-01-28 tracey
977 470cd826 2020-01-29 stsp if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
978 470cd826 2020-01-29 stsp error = got_error_from_errno("asprintf");
979 470cd826 2020-01-29 stsp goto done;
980 470cd826 2020-01-29 stsp }
981 4ff7391f 2020-01-28 tracey
982 4ff7391f 2020-01-28 tracey kerr = khttp_puts(gw_trans->gw_req, tag);
983 4ff7391f 2020-01-28 tracey if (kerr != KCGI_OK)
984 4ff7391f 2020-01-28 tracey error = gw_kcgi_error(kerr);
985 4ff7391f 2020-01-28 tracey done:
986 4ff7391f 2020-01-28 tracey got_ref_list_free(&header->refs);
987 4ff7391f 2020-01-28 tracey gw_free_headers(header);
988 4ff7391f 2020-01-28 tracey free(tag_html_disp);
989 4ff7391f 2020-01-28 tracey free(tag_html);
990 4ff7391f 2020-01-28 tracey free(tag);
991 2c251c14 2020-01-15 tracey return error;
992 2c251c14 2020-01-15 tracey }
993 2c251c14 2020-01-15 tracey
994 2c251c14 2020-01-15 tracey static const struct got_error *
995 54415d85 2020-01-15 tracey gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
996 2c251c14 2020-01-15 tracey {
997 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
998 2c251c14 2020-01-15 tracey DIR *dt;
999 2c251c14 2020-01-15 tracey char *dir_test;
1000 4ceb8155 2020-01-15 tracey int opened = 0;
1001 2c251c14 2020-01-15 tracey
1002 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s/%s",
1003 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
1004 88d59c36 2020-01-29 stsp GOTWEB_GIT_DIR) == -1)
1005 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1006 2c251c14 2020-01-15 tracey
1007 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
1008 2c251c14 2020-01-15 tracey if (dt == NULL) {
1009 2c251c14 2020-01-15 tracey free(dir_test);
1010 2c251c14 2020-01-15 tracey } else {
1011 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
1012 4ceb8155 2020-01-15 tracey opened = 1;
1013 2c251c14 2020-01-15 tracey goto done;
1014 2c251c14 2020-01-15 tracey }
1015 2c251c14 2020-01-15 tracey
1016 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s/%s",
1017 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
1018 88d59c36 2020-01-29 stsp GOTWEB_GOT_DIR) == -1)
1019 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1020 2c251c14 2020-01-15 tracey
1021 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
1022 2c251c14 2020-01-15 tracey if (dt == NULL)
1023 2c251c14 2020-01-15 tracey free(dir_test);
1024 2c251c14 2020-01-15 tracey else {
1025 4ceb8155 2020-01-15 tracey opened = 1;
1026 2c251c14 2020-01-15 tracey error = got_error(GOT_ERR_NOT_GIT_REPO);
1027 2c251c14 2020-01-15 tracey goto errored;
1028 2c251c14 2020-01-15 tracey }
1029 2c251c14 2020-01-15 tracey
1030 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s",
1031 88d59c36 2020-01-29 stsp gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1032 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1033 2c251c14 2020-01-15 tracey
1034 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
1035 2c251c14 2020-01-15 tracey
1036 2c251c14 2020-01-15 tracey done:
1037 2c251c14 2020-01-15 tracey gw_dir->description = gw_get_repo_description(gw_trans,
1038 2c251c14 2020-01-15 tracey gw_dir->path);
1039 2c251c14 2020-01-15 tracey gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
1040 d126c1b7 2020-01-29 stsp error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1041 d126c1b7 2020-01-29 stsp "refs/heads", TM_DIFF);
1042 d126c1b7 2020-01-29 stsp if (error)
1043 d126c1b7 2020-01-29 stsp goto errored;
1044 2c251c14 2020-01-15 tracey gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
1045 2c251c14 2020-01-15 tracey
1046 2c251c14 2020-01-15 tracey errored:
1047 2c251c14 2020-01-15 tracey free(dir_test);
1048 2c251c14 2020-01-15 tracey if (opened)
1049 2c251c14 2020-01-15 tracey closedir(dt);
1050 2c251c14 2020-01-15 tracey return error;
1051 2c251c14 2020-01-15 tracey }
1052 2c251c14 2020-01-15 tracey
1053 2c251c14 2020-01-15 tracey static const struct got_error *
1054 54415d85 2020-01-15 tracey gw_load_got_paths(struct gw_trans *gw_trans)
1055 2c251c14 2020-01-15 tracey {
1056 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1057 2c251c14 2020-01-15 tracey DIR *d;
1058 2c251c14 2020-01-15 tracey struct dirent **sd_dent;
1059 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
1060 2c251c14 2020-01-15 tracey struct stat st;
1061 2c251c14 2020-01-15 tracey unsigned int d_cnt, d_i;
1062 2c251c14 2020-01-15 tracey
1063 2c251c14 2020-01-15 tracey d = opendir(gw_trans->gw_conf->got_repos_path);
1064 2c251c14 2020-01-15 tracey if (d == NULL) {
1065 2c251c14 2020-01-15 tracey error = got_error_from_errno2("opendir",
1066 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
1067 2c251c14 2020-01-15 tracey return error;
1068 2c251c14 2020-01-15 tracey }
1069 2c251c14 2020-01-15 tracey
1070 2c251c14 2020-01-15 tracey d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1071 2c251c14 2020-01-15 tracey alphasort);
1072 2c251c14 2020-01-15 tracey if (d_cnt == -1) {
1073 2c251c14 2020-01-15 tracey error = got_error_from_errno2("scandir",
1074 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
1075 2c251c14 2020-01-15 tracey return error;
1076 2c251c14 2020-01-15 tracey }
1077 2c251c14 2020-01-15 tracey
1078 2c251c14 2020-01-15 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
1079 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos > 0 &&
1080 2c251c14 2020-01-15 tracey (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1081 2c251c14 2020-01-15 tracey break; /* account for parent and self */
1082 2c251c14 2020-01-15 tracey
1083 2c251c14 2020-01-15 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1084 2c251c14 2020-01-15 tracey strcmp(sd_dent[d_i]->d_name, "..") == 0)
1085 2c251c14 2020-01-15 tracey continue;
1086 2c251c14 2020-01-15 tracey
1087 2c251c14 2020-01-15 tracey if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1088 2c251c14 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
1089 2c251c14 2020-01-15 tracey
1090 2c251c14 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_dir);
1091 2c251c14 2020-01-15 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1092 2c251c14 2020-01-15 tracey continue;
1093 2c251c14 2020-01-15 tracey else if (error)
1094 2c251c14 2020-01-15 tracey return error;
1095 2c251c14 2020-01-15 tracey
1096 2c251c14 2020-01-15 tracey if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1097 2c251c14 2020-01-15 tracey !got_path_dir_is_empty(gw_dir->path)) {
1098 2c251c14 2020-01-15 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1099 2c251c14 2020-01-15 tracey entry);
1100 2c251c14 2020-01-15 tracey gw_trans->repos_total++;
1101 2c251c14 2020-01-15 tracey }
1102 2c251c14 2020-01-15 tracey }
1103 2c251c14 2020-01-15 tracey
1104 2c251c14 2020-01-15 tracey closedir(d);
1105 2c251c14 2020-01-15 tracey return error;
1106 2c251c14 2020-01-15 tracey }
1107 2c251c14 2020-01-15 tracey
1108 2c251c14 2020-01-15 tracey static const struct got_error *
1109 54415d85 2020-01-15 tracey gw_parse_querystring(struct gw_trans *gw_trans)
1110 2c251c14 2020-01-15 tracey {
1111 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1112 2c251c14 2020-01-15 tracey struct kpair *p;
1113 2c251c14 2020-01-15 tracey struct gw_query_action *action = NULL;
1114 2c251c14 2020-01-15 tracey unsigned int i;
1115 2c251c14 2020-01-15 tracey
1116 2c251c14 2020-01-15 tracey if (gw_trans->gw_req->fieldnmap[0]) {
1117 2c251c14 2020-01-15 tracey error = got_error_from_errno("bad parse");
1118 2c251c14 2020-01-15 tracey return error;
1119 2c251c14 2020-01-15 tracey } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1120 2c251c14 2020-01-15 tracey /* define gw_trans->repo_path */
1121 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1122 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1123 2c251c14 2020-01-15 tracey
1124 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_path, "%s/%s",
1125 88d59c36 2020-01-29 stsp gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1126 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1127 2c251c14 2020-01-15 tracey
1128 2c251c14 2020-01-15 tracey /* get action and set function */
1129 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1130 2c251c14 2020-01-15 tracey for (i = 0; i < nitems(gw_query_funcs); i++) {
1131 2c251c14 2020-01-15 tracey action = &gw_query_funcs[i];
1132 2c251c14 2020-01-15 tracey if (action->func_name == NULL)
1133 2c251c14 2020-01-15 tracey continue;
1134 077f6c5a 2020-01-15 tracey
1135 2c251c14 2020-01-15 tracey if (strcmp(action->func_name,
1136 2c251c14 2020-01-15 tracey p->parsed.s) == 0) {
1137 2c251c14 2020-01-15 tracey gw_trans->action = i;
1138 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->action_name,
1139 88d59c36 2020-01-29 stsp "%s", action->func_name) == -1)
1140 2c251c14 2020-01-15 tracey return
1141 b772de24 2020-01-15 tracey got_error_from_errno(
1142 2c251c14 2020-01-15 tracey "asprintf");
1143 2c251c14 2020-01-15 tracey
1144 2c251c14 2020-01-15 tracey break;
1145 2c251c14 2020-01-15 tracey }
1146 2c251c14 2020-01-15 tracey
1147 2c251c14 2020-01-15 tracey action = NULL;
1148 2c251c14 2020-01-15 tracey }
1149 ec46ccd7 2020-01-15 tracey
1150 ec46ccd7 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1151 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->commit, "%s",
1152 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1153 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
1154 2c251c14 2020-01-15 tracey
1155 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1156 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_file, "%s",
1157 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1158 8087c3c5 2020-01-15 tracey return got_error_from_errno("asprintf");
1159 8087c3c5 2020-01-15 tracey
1160 ec46ccd7 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1161 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_folder, "%s",
1162 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1163 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
1164 ec46ccd7 2020-01-15 tracey
1165 8087c3c5 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1166 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->headref, "%s",
1167 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1168 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1169 2c251c14 2020-01-15 tracey
1170 2c251c14 2020-01-15 tracey if (action == NULL) {
1171 2c251c14 2020-01-15 tracey error = got_error_from_errno("invalid action");
1172 2c251c14 2020-01-15 tracey return error;
1173 2c251c14 2020-01-15 tracey }
1174 46b9c89b 2020-01-15 tracey if ((gw_trans->gw_dir =
1175 46b9c89b 2020-01-15 tracey gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1176 46b9c89b 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
1177 46b9c89b 2020-01-15 tracey
1178 46b9c89b 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1179 46b9c89b 2020-01-15 tracey if (error)
1180 46b9c89b 2020-01-15 tracey return error;
1181 2c251c14 2020-01-15 tracey } else
1182 2c251c14 2020-01-15 tracey gw_trans->action = GW_INDEX;
1183 2c251c14 2020-01-15 tracey
1184 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1185 2c251c14 2020-01-15 tracey gw_trans->page = p->parsed.i;
1186 2c251c14 2020-01-15 tracey
1187 2c251c14 2020-01-15 tracey return error;
1188 2c251c14 2020-01-15 tracey }
1189 2c251c14 2020-01-15 tracey
1190 2c251c14 2020-01-15 tracey static struct gw_dir *
1191 2c251c14 2020-01-15 tracey gw_init_gw_dir(char *dir)
1192 2c251c14 2020-01-15 tracey {
1193 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
1194 2c251c14 2020-01-15 tracey
1195 2c251c14 2020-01-15 tracey if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1196 2c251c14 2020-01-15 tracey return NULL;
1197 2c251c14 2020-01-15 tracey
1198 88d59c36 2020-01-29 stsp if (asprintf(&gw_dir->name, "%s", dir) == -1)
1199 2c251c14 2020-01-15 tracey return NULL;
1200 2c251c14 2020-01-15 tracey
1201 2c251c14 2020-01-15 tracey return gw_dir;
1202 474370cb 2020-01-15 tracey }
1203 474370cb 2020-01-15 tracey
1204 c25c2314 2020-01-28 stsp static const struct got_error *
1205 54415d85 2020-01-15 tracey gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1206 2c251c14 2020-01-15 tracey {
1207 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1208 c25c2314 2020-01-28 stsp
1209 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1210 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1211 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1212 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1213 2c251c14 2020-01-15 tracey khttps[code]);
1214 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1215 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1216 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1217 2c251c14 2020-01-15 tracey kmimetypes[mime]);
1218 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1219 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1220 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1221 017d6da3 2020-01-29 tracey "nosniff");
1222 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1223 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1224 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1225 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1226 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1227 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1228 017d6da3 2020-01-29 tracey "1; mode=block");
1229 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1230 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1231 c25c2314 2020-01-28 stsp
1232 017d6da3 2020-01-29 tracey if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1233 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req,
1234 017d6da3 2020-01-29 tracey kresps[KRESP_CONTENT_DISPOSITION],
1235 017d6da3 2020-01-29 tracey "attachment; filename=%s", gw_trans->repo_file);
1236 017d6da3 2020-01-29 tracey if (kerr != KCGI_OK)
1237 017d6da3 2020-01-29 tracey return gw_kcgi_error(kerr);
1238 017d6da3 2020-01-29 tracey }
1239 017d6da3 2020-01-29 tracey
1240 c25c2314 2020-01-28 stsp kerr = khttp_body(gw_trans->gw_req);
1241 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1242 2c251c14 2020-01-15 tracey }
1243 2c251c14 2020-01-15 tracey
1244 c25c2314 2020-01-28 stsp static const struct got_error *
1245 6d8d8a26 2020-01-29 stsp gw_display_index(struct gw_trans *gw_trans)
1246 2c251c14 2020-01-15 tracey {
1247 4bec7539 2020-01-29 stsp const struct got_error *error;
1248 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1249 c25c2314 2020-01-28 stsp
1250 4bec7539 2020-01-29 stsp error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1251 4bec7539 2020-01-29 stsp if (error)
1252 4bec7539 2020-01-29 stsp return error;
1253 4bec7539 2020-01-29 stsp
1254 c25c2314 2020-01-28 stsp kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1255 6d8d8a26 2020-01-29 stsp if (kerr)
1256 6d8d8a26 2020-01-29 stsp return gw_kcgi_error(kerr);
1257 2c251c14 2020-01-15 tracey
1258 017d6da3 2020-01-29 tracey if (gw_trans->action != GW_BLOB) {
1259 017d6da3 2020-01-29 tracey kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1260 017d6da3 2020-01-29 tracey gw_query_funcs[gw_trans->action].template);
1261 017d6da3 2020-01-29 tracey if (kerr != KCGI_OK) {
1262 017d6da3 2020-01-29 tracey khtml_close(gw_trans->gw_html_req);
1263 017d6da3 2020-01-29 tracey return gw_kcgi_error(kerr);
1264 017d6da3 2020-01-29 tracey }
1265 7a9bfbff 2020-01-29 stsp }
1266 2c251c14 2020-01-15 tracey
1267 7a9bfbff 2020-01-29 stsp return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1268 2c251c14 2020-01-15 tracey }
1269 2c251c14 2020-01-15 tracey
1270 6d8d8a26 2020-01-29 stsp static void
1271 6d8d8a26 2020-01-29 stsp gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1272 6d8d8a26 2020-01-29 stsp {
1273 6d8d8a26 2020-01-29 stsp if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1274 6d8d8a26 2020-01-29 stsp return;
1275 6d8d8a26 2020-01-29 stsp
1276 6d8d8a26 2020-01-29 stsp if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1277 6d8d8a26 2020-01-29 stsp return;
1278 6d8d8a26 2020-01-29 stsp
1279 6d8d8a26 2020-01-29 stsp khttp_puts(gw_trans->gw_req, err->msg);
1280 6d8d8a26 2020-01-29 stsp khtml_close(gw_trans->gw_html_req);
1281 6d8d8a26 2020-01-29 stsp }
1282 6d8d8a26 2020-01-29 stsp
1283 2c251c14 2020-01-15 tracey static int
1284 2c251c14 2020-01-15 tracey gw_template(size_t key, void *arg)
1285 2c251c14 2020-01-15 tracey {
1286 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1287 54415d85 2020-01-15 tracey struct gw_trans *gw_trans = arg;
1288 2c251c14 2020-01-15 tracey char *gw_got_link, *gw_site_link;
1289 2c251c14 2020-01-15 tracey char *site_owner_name, *site_owner_name_h;
1290 2c251c14 2020-01-15 tracey
1291 2c251c14 2020-01-15 tracey switch (key) {
1292 2c251c14 2020-01-15 tracey case (TEMPL_HEAD):
1293 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, head);
1294 2c251c14 2020-01-15 tracey break;
1295 2c251c14 2020-01-15 tracey case(TEMPL_HEADER):
1296 2c251c14 2020-01-15 tracey gw_got_link = gw_get_got_link(gw_trans);
1297 2c251c14 2020-01-15 tracey if (gw_got_link != NULL)
1298 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_got_link);
1299 2c251c14 2020-01-15 tracey
1300 2c251c14 2020-01-15 tracey free(gw_got_link);
1301 2c251c14 2020-01-15 tracey break;
1302 2c251c14 2020-01-15 tracey case (TEMPL_SITEPATH):
1303 2c251c14 2020-01-15 tracey gw_site_link = gw_get_site_link(gw_trans);
1304 2c251c14 2020-01-15 tracey if (gw_site_link != NULL)
1305 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_site_link);
1306 2c251c14 2020-01-15 tracey
1307 2c251c14 2020-01-15 tracey free(gw_site_link);
1308 2c251c14 2020-01-15 tracey break;
1309 2c251c14 2020-01-15 tracey case(TEMPL_TITLE):
1310 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_name != NULL)
1311 2c251c14 2020-01-15 tracey khtml_puts(gw_trans->gw_html_req,
1312 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_name);
1313 2c251c14 2020-01-15 tracey
1314 2c251c14 2020-01-15 tracey break;
1315 2c251c14 2020-01-15 tracey case (TEMPL_SEARCH):
1316 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, search);
1317 2c251c14 2020-01-15 tracey break;
1318 2c251c14 2020-01-15 tracey case(TEMPL_SITEOWNER):
1319 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_owner != NULL &&
1320 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_show_site_owner) {
1321 2c251c14 2020-01-15 tracey site_owner_name =
1322 2c251c14 2020-01-15 tracey gw_html_escape(gw_trans->gw_conf->got_site_owner);
1323 88d59c36 2020-01-29 stsp if (asprintf(&site_owner_name_h, site_owner,
1324 88d59c36 2020-01-29 stsp site_owner_name) == -1)
1325 2c251c14 2020-01-15 tracey return 0;
1326 2c251c14 2020-01-15 tracey
1327 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, site_owner_name_h);
1328 2c251c14 2020-01-15 tracey free(site_owner_name);
1329 2c251c14 2020-01-15 tracey free(site_owner_name_h);
1330 2c251c14 2020-01-15 tracey }
1331 2c251c14 2020-01-15 tracey break;
1332 2c251c14 2020-01-15 tracey case(TEMPL_CONTENT):
1333 2c251c14 2020-01-15 tracey error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1334 2c251c14 2020-01-15 tracey if (error)
1335 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, error->msg);
1336 2c251c14 2020-01-15 tracey break;
1337 2c251c14 2020-01-15 tracey default:
1338 2c251c14 2020-01-15 tracey return 0;
1339 2c251c14 2020-01-15 tracey }
1340 2c251c14 2020-01-15 tracey return 1;
1341 2c251c14 2020-01-15 tracey }
1342 2c251c14 2020-01-15 tracey
1343 2c251c14 2020-01-15 tracey static char *
1344 f2f46662 2020-01-23 tracey gw_gen_commit_header(char *str1, char *str2)
1345 f2f46662 2020-01-23 tracey {
1346 f2f46662 2020-01-23 tracey char *return_html = NULL, *ref_str = NULL;
1347 f2f46662 2020-01-23 tracey
1348 f2f46662 2020-01-23 tracey if (strcmp(str2, "") != 0) {
1349 88d59c36 2020-01-29 stsp if (asprintf(&ref_str, "(%s)", str2) == -1) {
1350 f2f46662 2020-01-23 tracey return_html = strdup("");
1351 f2f46662 2020-01-23 tracey return return_html;
1352 f2f46662 2020-01-23 tracey }
1353 f2f46662 2020-01-23 tracey } else
1354 f2f46662 2020-01-23 tracey ref_str = strdup("");
1355 f2f46662 2020-01-23 tracey
1356 f2f46662 2020-01-23 tracey
1357 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1358 f2f46662 2020-01-23 tracey return_html = strdup("");
1359 f2f46662 2020-01-23 tracey
1360 f2f46662 2020-01-23 tracey free(ref_str);
1361 f2f46662 2020-01-23 tracey return return_html;
1362 f2f46662 2020-01-23 tracey }
1363 f2f46662 2020-01-23 tracey
1364 f2f46662 2020-01-23 tracey static char *
1365 f2f46662 2020-01-23 tracey gw_gen_diff_header(char *str1, char *str2)
1366 f2f46662 2020-01-23 tracey {
1367 f2f46662 2020-01-23 tracey char *return_html = NULL;
1368 f2f46662 2020-01-23 tracey
1369 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_diff_html, str1, str2) == -1)
1370 f2f46662 2020-01-23 tracey return_html = strdup("");
1371 f2f46662 2020-01-23 tracey
1372 f2f46662 2020-01-23 tracey return return_html;
1373 f2f46662 2020-01-23 tracey }
1374 f2f46662 2020-01-23 tracey
1375 f2f46662 2020-01-23 tracey static char *
1376 f2f46662 2020-01-23 tracey gw_gen_author_header(char *str)
1377 f2f46662 2020-01-23 tracey {
1378 f2f46662 2020-01-23 tracey char *return_html = NULL;
1379 f2f46662 2020-01-23 tracey
1380 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_author_html, str) == -1)
1381 f2f46662 2020-01-23 tracey return_html = strdup("");
1382 f2f46662 2020-01-23 tracey
1383 f2f46662 2020-01-23 tracey return return_html;
1384 f2f46662 2020-01-23 tracey }
1385 f2f46662 2020-01-23 tracey
1386 f2f46662 2020-01-23 tracey static char *
1387 f2f46662 2020-01-23 tracey gw_gen_committer_header(char *str)
1388 f2f46662 2020-01-23 tracey {
1389 f2f46662 2020-01-23 tracey char *return_html = NULL;
1390 f2f46662 2020-01-23 tracey
1391 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_committer_html, str) == -1)
1392 f2f46662 2020-01-23 tracey return_html = strdup("");
1393 f2f46662 2020-01-23 tracey
1394 f2f46662 2020-01-23 tracey return return_html;
1395 f2f46662 2020-01-23 tracey }
1396 f2f46662 2020-01-23 tracey
1397 f2f46662 2020-01-23 tracey static char *
1398 f2f46662 2020-01-23 tracey gw_gen_age_header(char *str)
1399 f2f46662 2020-01-23 tracey {
1400 f2f46662 2020-01-23 tracey char *return_html = NULL;
1401 f2f46662 2020-01-23 tracey
1402 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_age_html, str) == -1)
1403 f2f46662 2020-01-23 tracey return_html = strdup("");
1404 f2f46662 2020-01-23 tracey
1405 f2f46662 2020-01-23 tracey return return_html;
1406 f2f46662 2020-01-23 tracey }
1407 f2f46662 2020-01-23 tracey
1408 f2f46662 2020-01-23 tracey static char *
1409 f2f46662 2020-01-23 tracey gw_gen_commit_msg_header(char *str)
1410 f2f46662 2020-01-23 tracey {
1411 f2f46662 2020-01-23 tracey char *return_html = NULL;
1412 f2f46662 2020-01-23 tracey
1413 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1414 f2f46662 2020-01-23 tracey return_html = strdup("");
1415 f2f46662 2020-01-23 tracey
1416 f2f46662 2020-01-23 tracey return return_html;
1417 f2f46662 2020-01-23 tracey }
1418 f2f46662 2020-01-23 tracey
1419 f2f46662 2020-01-23 tracey static char *
1420 f2f46662 2020-01-23 tracey gw_gen_tree_header(char *str)
1421 f2f46662 2020-01-23 tracey {
1422 f2f46662 2020-01-23 tracey char *return_html = NULL;
1423 f2f46662 2020-01-23 tracey
1424 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_tree_html, str) == -1)
1425 f2f46662 2020-01-23 tracey return_html = strdup("");
1426 f2f46662 2020-01-23 tracey
1427 f2f46662 2020-01-23 tracey return return_html;
1428 f2f46662 2020-01-23 tracey }
1429 f2f46662 2020-01-23 tracey
1430 f2f46662 2020-01-23 tracey static char *
1431 54415d85 2020-01-15 tracey gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1432 2c251c14 2020-01-15 tracey {
1433 2c251c14 2020-01-15 tracey FILE *f;
1434 2c251c14 2020-01-15 tracey char *description = NULL, *d_file = NULL;
1435 2c251c14 2020-01-15 tracey unsigned int len;
1436 2c251c14 2020-01-15 tracey
1437 0b20762b 2020-01-29 stsp if (gw_trans->gw_conf->got_show_repo_description == 0)
1438 2c251c14 2020-01-15 tracey goto err;
1439 2c251c14 2020-01-15 tracey
1440 88d59c36 2020-01-29 stsp if (asprintf(&d_file, "%s/description", dir) == -1)
1441 2c251c14 2020-01-15 tracey goto err;
1442 2c251c14 2020-01-15 tracey
1443 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1444 2c251c14 2020-01-15 tracey goto err;
1445 2c251c14 2020-01-15 tracey
1446 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_END);
1447 2c251c14 2020-01-15 tracey len = ftell(f) + 1;
1448 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1449 2c251c14 2020-01-15 tracey if ((description = calloc(len, sizeof(char *))) == NULL)
1450 2c251c14 2020-01-15 tracey goto err;
1451 2c251c14 2020-01-15 tracey
1452 2c251c14 2020-01-15 tracey fread(description, 1, len, f);
1453 2c251c14 2020-01-15 tracey fclose(f);
1454 2c251c14 2020-01-15 tracey free(d_file);
1455 2c251c14 2020-01-15 tracey return description;
1456 2c251c14 2020-01-15 tracey err:
1457 4b60fece 2020-01-29 stsp return strdup("");
1458 2c251c14 2020-01-15 tracey }
1459 2c251c14 2020-01-15 tracey
1460 2c251c14 2020-01-15 tracey static char *
1461 474370cb 2020-01-15 tracey gw_get_time_str(time_t committer_time, int ref_tm)
1462 474370cb 2020-01-15 tracey {
1463 474370cb 2020-01-15 tracey struct tm tm;
1464 474370cb 2020-01-15 tracey time_t diff_time;
1465 474370cb 2020-01-15 tracey char *years = "years ago", *months = "months ago";
1466 474370cb 2020-01-15 tracey char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1467 474370cb 2020-01-15 tracey char *minutes = "minutes ago", *seconds = "seconds ago";
1468 474370cb 2020-01-15 tracey char *now = "right now";
1469 474370cb 2020-01-15 tracey char *repo_age, *s;
1470 6c6c85af 2020-01-15 tracey char datebuf[29];
1471 474370cb 2020-01-15 tracey
1472 474370cb 2020-01-15 tracey switch (ref_tm) {
1473 474370cb 2020-01-15 tracey case TM_DIFF:
1474 474370cb 2020-01-15 tracey diff_time = time(NULL) - committer_time;
1475 474370cb 2020-01-15 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
1476 88d59c36 2020-01-29 stsp if (asprintf(&repo_age, "%lld %s",
1477 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24 / 365), years) == -1)
1478 474370cb 2020-01-15 tracey return NULL;
1479 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1480 88d59c36 2020-01-29 stsp if (asprintf(&repo_age, "%lld %s",
1481 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
1482 88d59c36 2020-01-29 stsp months) == -1)
1483 474370cb 2020-01-15 tracey return NULL;
1484 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1485 88d59c36 2020-01-29 stsp if (asprintf(&repo_age, "%lld %s",
1486 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1487 474370cb 2020-01-15 tracey return NULL;
1488 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
1489 88d59c36 2020-01-29 stsp if (asprintf(&repo_age, "%lld %s",
1490 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24), days) == -1)
1491 474370cb 2020-01-15 tracey return NULL;
1492 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 2) {
1493 88d59c36 2020-01-29 stsp if (asprintf(&repo_age, "%lld %s",
1494 88d59c36 2020-01-29 stsp (diff_time / 60 / 60), hours) == -1)
1495 474370cb 2020-01-15 tracey return NULL;
1496 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 2) {
1497 88d59c36 2020-01-29 stsp if (asprintf(&repo_age, "%lld %s", (diff_time / 60),
1498 88d59c36 2020-01-29 stsp minutes) == -1)
1499 474370cb 2020-01-15 tracey return NULL;
1500 474370cb 2020-01-15 tracey } else if (diff_time > 2) {
1501 88d59c36 2020-01-29 stsp if (asprintf(&repo_age, "%lld %s", diff_time,
1502 88d59c36 2020-01-29 stsp seconds) == -1)
1503 474370cb 2020-01-15 tracey return NULL;
1504 474370cb 2020-01-15 tracey } else {
1505 88d59c36 2020-01-29 stsp if (asprintf(&repo_age, "%s", now) == -1)
1506 474370cb 2020-01-15 tracey return NULL;
1507 474370cb 2020-01-15 tracey }
1508 474370cb 2020-01-15 tracey break;
1509 474370cb 2020-01-15 tracey case TM_LONG:
1510 474370cb 2020-01-15 tracey if (gmtime_r(&committer_time, &tm) == NULL)
1511 474370cb 2020-01-15 tracey return NULL;
1512 474370cb 2020-01-15 tracey
1513 474370cb 2020-01-15 tracey s = asctime_r(&tm, datebuf);
1514 474370cb 2020-01-15 tracey if (s == NULL)
1515 474370cb 2020-01-15 tracey return NULL;
1516 474370cb 2020-01-15 tracey
1517 88d59c36 2020-01-29 stsp if (asprintf(&repo_age, "%s UTC", datebuf) == -1)
1518 474370cb 2020-01-15 tracey return NULL;
1519 474370cb 2020-01-15 tracey break;
1520 474370cb 2020-01-15 tracey }
1521 474370cb 2020-01-15 tracey return repo_age;
1522 474370cb 2020-01-15 tracey }
1523 474370cb 2020-01-15 tracey
1524 d126c1b7 2020-01-29 stsp static const struct got_error *
1525 d126c1b7 2020-01-29 stsp gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
1526 d126c1b7 2020-01-29 stsp char *repo_ref, int ref_tm)
1527 2c251c14 2020-01-15 tracey {
1528 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1529 2c251c14 2020-01-15 tracey struct got_object_id *id = NULL;
1530 2c251c14 2020-01-15 tracey struct got_repository *repo = NULL;
1531 2c251c14 2020-01-15 tracey struct got_commit_object *commit = NULL;
1532 2c251c14 2020-01-15 tracey struct got_reflist_head refs;
1533 2c251c14 2020-01-15 tracey struct got_reflist_entry *re;
1534 2c251c14 2020-01-15 tracey struct got_reference *head_ref;
1535 87f9ebf5 2020-01-15 tracey int is_head = 0;
1536 474370cb 2020-01-15 tracey time_t committer_time = 0, cmp_time = 0;
1537 87f9ebf5 2020-01-15 tracey const char *refname;
1538 a0f36e03 2020-01-28 stsp
1539 d126c1b7 2020-01-29 stsp *repo_age = NULL;
1540 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
1541 387a29ba 2020-01-15 tracey
1542 387a29ba 2020-01-15 tracey if (repo_ref == NULL)
1543 387a29ba 2020-01-15 tracey return NULL;
1544 87f9ebf5 2020-01-15 tracey
1545 87f9ebf5 2020-01-15 tracey if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1546 87f9ebf5 2020-01-15 tracey is_head = 1;
1547 2c251c14 2020-01-15 tracey
1548 d126c1b7 2020-01-29 stsp if (gw_trans->gw_conf->got_show_repo_age == 0) {
1549 d126c1b7 2020-01-29 stsp *repo_age = strdup("");
1550 d126c1b7 2020-01-29 stsp if (*repo_age == NULL)
1551 d126c1b7 2020-01-29 stsp return got_error_from_errno("strdup");
1552 d126c1b7 2020-01-29 stsp return NULL;
1553 d126c1b7 2020-01-29 stsp }
1554 87f9ebf5 2020-01-15 tracey
1555 2c251c14 2020-01-15 tracey error = got_repo_open(&repo, dir, NULL);
1556 ec46ccd7 2020-01-15 tracey if (error)
1557 d126c1b7 2020-01-29 stsp goto done;
1558 2c251c14 2020-01-15 tracey
1559 87f9ebf5 2020-01-15 tracey if (is_head)
1560 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads",
1561 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
1562 87f9ebf5 2020-01-15 tracey else
1563 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, repo_ref,
1564 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
1565 ec46ccd7 2020-01-15 tracey if (error)
1566 d126c1b7 2020-01-29 stsp goto done;
1567 2c251c14 2020-01-15 tracey
1568 2c251c14 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1569 87f9ebf5 2020-01-15 tracey if (is_head)
1570 87f9ebf5 2020-01-15 tracey refname = strdup(repo_ref);
1571 87f9ebf5 2020-01-15 tracey else
1572 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
1573 2c251c14 2020-01-15 tracey error = got_ref_open(&head_ref, repo, refname, 0);
1574 ec46ccd7 2020-01-15 tracey if (error)
1575 d126c1b7 2020-01-29 stsp goto done;
1576 2c251c14 2020-01-15 tracey
1577 2c251c14 2020-01-15 tracey error = got_ref_resolve(&id, repo, head_ref);
1578 2c251c14 2020-01-15 tracey got_ref_close(head_ref);
1579 ec46ccd7 2020-01-15 tracey if (error)
1580 d126c1b7 2020-01-29 stsp goto done;
1581 2c251c14 2020-01-15 tracey
1582 2c251c14 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
1583 ec46ccd7 2020-01-15 tracey if (error)
1584 d126c1b7 2020-01-29 stsp goto done;
1585 2c251c14 2020-01-15 tracey
1586 2c251c14 2020-01-15 tracey committer_time =
1587 2c251c14 2020-01-15 tracey got_object_commit_get_committer_time(commit);
1588 2c251c14 2020-01-15 tracey
1589 387a29ba 2020-01-15 tracey if (cmp_time < committer_time)
1590 2c251c14 2020-01-15 tracey cmp_time = committer_time;
1591 2c251c14 2020-01-15 tracey }
1592 2c251c14 2020-01-15 tracey
1593 474370cb 2020-01-15 tracey if (cmp_time != 0) {
1594 2c251c14 2020-01-15 tracey committer_time = cmp_time;
1595 d126c1b7 2020-01-29 stsp *repo_age = gw_get_time_str(committer_time, ref_tm);
1596 d126c1b7 2020-01-29 stsp } else {
1597 d126c1b7 2020-01-29 stsp *repo_age = strdup("");
1598 d126c1b7 2020-01-29 stsp if (*repo_age == NULL)
1599 d126c1b7 2020-01-29 stsp error = got_error_from_errno("strdup");
1600 d126c1b7 2020-01-29 stsp }
1601 d126c1b7 2020-01-29 stsp done:
1602 2c251c14 2020-01-15 tracey got_ref_list_free(&refs);
1603 2c251c14 2020-01-15 tracey free(id);
1604 d126c1b7 2020-01-29 stsp return error;
1605 ec46ccd7 2020-01-15 tracey }
1606 ec46ccd7 2020-01-15 tracey
1607 ec46ccd7 2020-01-15 tracey static char *
1608 f2f46662 2020-01-23 tracey gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1609 ec46ccd7 2020-01-15 tracey {
1610 ec46ccd7 2020-01-15 tracey const struct got_error *error;
1611 ec46ccd7 2020-01-15 tracey FILE *f = NULL;
1612 ec46ccd7 2020-01-15 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
1613 ec46ccd7 2020-01-15 tracey struct buf *diffbuf = NULL;
1614 e46f587c 2020-01-29 tracey char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL;
1615 e46f587c 2020-01-29 tracey char *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1616 05ce9a79 2020-01-24 tracey int obj_type;
1617 ec46ccd7 2020-01-15 tracey size_t newsize;
1618 ec46ccd7 2020-01-15 tracey
1619 ec46ccd7 2020-01-15 tracey f = got_opentemp();
1620 ec46ccd7 2020-01-15 tracey if (f == NULL)
1621 ec46ccd7 2020-01-15 tracey return NULL;
1622 ec46ccd7 2020-01-15 tracey
1623 ec46ccd7 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
1624 ec46ccd7 2020-01-15 tracey if (error)
1625 ec46ccd7 2020-01-15 tracey return NULL;
1626 ec46ccd7 2020-01-15 tracey
1627 90f16cb8 2020-01-24 tracey error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1628 ec46ccd7 2020-01-15 tracey if (error)
1629 ec46ccd7 2020-01-15 tracey goto done;
1630 ec46ccd7 2020-01-15 tracey
1631 05ce9a79 2020-01-24 tracey if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1632 05ce9a79 2020-01-24 tracey error = got_repo_match_object_id(&id1, &label1,
1633 05ce9a79 2020-01-24 tracey header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1634 05ce9a79 2020-01-24 tracey if (error)
1635 05ce9a79 2020-01-24 tracey goto done;
1636 05ce9a79 2020-01-24 tracey }
1637 ec46ccd7 2020-01-15 tracey
1638 90f16cb8 2020-01-24 tracey error = got_repo_match_object_id(&id2, &label2,
1639 2ac037ec 2020-01-24 tracey header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1640 90f16cb8 2020-01-24 tracey if (error)
1641 90f16cb8 2020-01-24 tracey goto done;
1642 ec46ccd7 2020-01-15 tracey
1643 05ce9a79 2020-01-24 tracey error = got_object_get_type(&obj_type, header->repo, id2);
1644 90f16cb8 2020-01-24 tracey if (error)
1645 90f16cb8 2020-01-24 tracey goto done;
1646 05ce9a79 2020-01-24 tracey switch (obj_type) {
1647 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_BLOB:
1648 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1649 90f16cb8 2020-01-24 tracey header->repo, f);
1650 ec46ccd7 2020-01-15 tracey break;
1651 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_TREE:
1652 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1653 90f16cb8 2020-01-24 tracey header->repo, f);
1654 ec46ccd7 2020-01-15 tracey break;
1655 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_COMMIT:
1656 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_commits(id1, id2, 3, 0,
1657 90f16cb8 2020-01-24 tracey header->repo, f);
1658 ec46ccd7 2020-01-15 tracey break;
1659 ec46ccd7 2020-01-15 tracey default:
1660 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1661 ec46ccd7 2020-01-15 tracey }
1662 ec46ccd7 2020-01-15 tracey
1663 ec46ccd7 2020-01-15 tracey if ((buf = calloc(128, sizeof(char *))) == NULL)
1664 ec46ccd7 2020-01-15 tracey goto done;
1665 ec46ccd7 2020-01-15 tracey
1666 ec46ccd7 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1667 ec46ccd7 2020-01-15 tracey
1668 bfd30182 2020-01-24 tracey while ((fgets(buf, 2048, f)) != NULL) {
1669 65559f29 2020-01-24 tracey n_buf = buf;
1670 65559f29 2020-01-24 tracey while (*n_buf == '\n')
1671 65559f29 2020-01-24 tracey n_buf++;
1672 65559f29 2020-01-24 tracey newline = strchr(n_buf, '\n');
1673 65559f29 2020-01-24 tracey if (newline)
1674 65559f29 2020-01-24 tracey *newline = ' ';
1675 65559f29 2020-01-24 tracey
1676 65559f29 2020-01-24 tracey buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1677 bfd30182 2020-01-24 tracey if (buf_color == NULL)
1678 bfd30182 2020-01-24 tracey continue;
1679 bfd30182 2020-01-24 tracey
1680 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, buf_color);
1681 ec46ccd7 2020-01-15 tracey if (error)
1682 ec46ccd7 2020-01-15 tracey return NULL;
1683 ec46ccd7 2020-01-15 tracey
1684 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, div_end);
1685 ec46ccd7 2020-01-15 tracey if (error)
1686 ec46ccd7 2020-01-15 tracey return NULL;
1687 ec46ccd7 2020-01-15 tracey }
1688 ec46ccd7 2020-01-15 tracey
1689 ec46ccd7 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
1690 ec46ccd7 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
1691 ec46ccd7 2020-01-15 tracey diff_html = strdup(buf_get(diffbuf));
1692 ec46ccd7 2020-01-15 tracey }
1693 ec46ccd7 2020-01-15 tracey done:
1694 ec46ccd7 2020-01-15 tracey fclose(f);
1695 ec46ccd7 2020-01-15 tracey free(buf_color);
1696 ec46ccd7 2020-01-15 tracey free(buf);
1697 ec46ccd7 2020-01-15 tracey free(diffbuf);
1698 ec46ccd7 2020-01-15 tracey free(label1);
1699 ec46ccd7 2020-01-15 tracey free(label2);
1700 ec46ccd7 2020-01-15 tracey free(id1);
1701 ec46ccd7 2020-01-15 tracey free(id2);
1702 ec46ccd7 2020-01-15 tracey
1703 ec46ccd7 2020-01-15 tracey if (error)
1704 ec46ccd7 2020-01-15 tracey return NULL;
1705 ec46ccd7 2020-01-15 tracey else
1706 ec46ccd7 2020-01-15 tracey return diff_html;
1707 2c251c14 2020-01-15 tracey }
1708 2c251c14 2020-01-15 tracey
1709 2c251c14 2020-01-15 tracey static char *
1710 54415d85 2020-01-15 tracey gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1711 2c251c14 2020-01-15 tracey {
1712 2c251c14 2020-01-15 tracey FILE *f;
1713 2c251c14 2020-01-15 tracey char *owner = NULL, *d_file = NULL;
1714 2c251c14 2020-01-15 tracey char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1715 2c251c14 2020-01-15 tracey char *comp, *pos, *buf;
1716 2c251c14 2020-01-15 tracey unsigned int i;
1717 2c251c14 2020-01-15 tracey
1718 0b20762b 2020-01-29 stsp if (gw_trans->gw_conf->got_show_repo_owner == 0)
1719 2c251c14 2020-01-15 tracey goto err;
1720 2c251c14 2020-01-15 tracey
1721 88d59c36 2020-01-29 stsp if (asprintf(&d_file, "%s/config", dir) == -1)
1722 2c251c14 2020-01-15 tracey goto err;
1723 2c251c14 2020-01-15 tracey
1724 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1725 2c251c14 2020-01-15 tracey goto err;
1726 2c251c14 2020-01-15 tracey
1727 6c6c85af 2020-01-15 tracey if ((buf = calloc(128, sizeof(char *))) == NULL)
1728 2c251c14 2020-01-15 tracey goto err;
1729 2c251c14 2020-01-15 tracey
1730 6c6c85af 2020-01-15 tracey while ((fgets(buf, 128, f)) != NULL) {
1731 2c251c14 2020-01-15 tracey if ((pos = strstr(buf, gotweb)) != NULL)
1732 2c251c14 2020-01-15 tracey break;
1733 2c251c14 2020-01-15 tracey
1734 2c251c14 2020-01-15 tracey if ((pos = strstr(buf, gitweb)) != NULL)
1735 2c251c14 2020-01-15 tracey break;
1736 2c251c14 2020-01-15 tracey }
1737 2c251c14 2020-01-15 tracey
1738 2c251c14 2020-01-15 tracey if (pos == NULL)
1739 2c251c14 2020-01-15 tracey goto err;
1740 2c251c14 2020-01-15 tracey
1741 2c251c14 2020-01-15 tracey do {
1742 6c6c85af 2020-01-15 tracey fgets(buf, 128, f);
1743 2c251c14 2020-01-15 tracey } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1744 2c251c14 2020-01-15 tracey
1745 2c251c14 2020-01-15 tracey if (comp == NULL)
1746 2c251c14 2020-01-15 tracey goto err;
1747 2c251c14 2020-01-15 tracey
1748 2c251c14 2020-01-15 tracey if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1749 2c251c14 2020-01-15 tracey goto err;
1750 2c251c14 2020-01-15 tracey
1751 2c251c14 2020-01-15 tracey for (i = 0; i < 2; i++) {
1752 2c251c14 2020-01-15 tracey owner = strsep(&buf, "\"");
1753 2c251c14 2020-01-15 tracey }
1754 2c251c14 2020-01-15 tracey
1755 2c251c14 2020-01-15 tracey if (owner == NULL)
1756 2c251c14 2020-01-15 tracey goto err;
1757 2c251c14 2020-01-15 tracey
1758 2c251c14 2020-01-15 tracey fclose(f);
1759 2c251c14 2020-01-15 tracey free(d_file);
1760 2c251c14 2020-01-15 tracey return owner;
1761 2c251c14 2020-01-15 tracey err:
1762 4b60fece 2020-01-29 stsp return strdup("");
1763 2c251c14 2020-01-15 tracey }
1764 2c251c14 2020-01-15 tracey
1765 2c251c14 2020-01-15 tracey static char *
1766 54415d85 2020-01-15 tracey gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1767 2c251c14 2020-01-15 tracey {
1768 2c251c14 2020-01-15 tracey FILE *f;
1769 2c251c14 2020-01-15 tracey char *url = NULL, *d_file = NULL;
1770 2c251c14 2020-01-15 tracey unsigned int len;
1771 2c251c14 2020-01-15 tracey
1772 88d59c36 2020-01-29 stsp if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
1773 2c251c14 2020-01-15 tracey return NULL;
1774 2c251c14 2020-01-15 tracey
1775 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1776 2c251c14 2020-01-15 tracey return NULL;
1777 2c251c14 2020-01-15 tracey
1778 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_END);
1779 2c251c14 2020-01-15 tracey len = ftell(f) + 1;
1780 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1781 2c251c14 2020-01-15 tracey
1782 2c251c14 2020-01-15 tracey if ((url = calloc(len, sizeof(char *))) == NULL)
1783 2c251c14 2020-01-15 tracey return NULL;
1784 2c251c14 2020-01-15 tracey
1785 2c251c14 2020-01-15 tracey fread(url, 1, len, f);
1786 2c251c14 2020-01-15 tracey fclose(f);
1787 2c251c14 2020-01-15 tracey free(d_file);
1788 2c251c14 2020-01-15 tracey return url;
1789 8d4d2453 2020-01-15 tracey }
1790 8d4d2453 2020-01-15 tracey
1791 8d4d2453 2020-01-15 tracey static char *
1792 4ff7391f 2020-01-28 tracey gw_get_repo_tags(struct gw_trans *gw_trans, struct gw_header *header, int limit,
1793 4ff7391f 2020-01-28 tracey int tag_type)
1794 8d4d2453 2020-01-15 tracey {
1795 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
1796 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
1797 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
1798 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
1799 e46f587c 2020-01-29 tracey char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL;
1800 e46f587c 2020-01-29 tracey char *age = NULL, *newline;
1801 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
1802 87f9ebf5 2020-01-15 tracey size_t newsize;
1803 8d4d2453 2020-01-15 tracey
1804 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
1805 a0f36e03 2020-01-28 stsp
1806 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
1807 ec46ccd7 2020-01-15 tracey if (error)
1808 6c6c85af 2020-01-15 tracey return NULL;
1809 87f9ebf5 2020-01-15 tracey
1810 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1811 ec46ccd7 2020-01-15 tracey if (error)
1812 87f9ebf5 2020-01-15 tracey goto done;
1813 87f9ebf5 2020-01-15 tracey
1814 add40c4f 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1815 87f9ebf5 2020-01-15 tracey if (error)
1816 87f9ebf5 2020-01-15 tracey goto done;
1817 87f9ebf5 2020-01-15 tracey
1818 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1819 87f9ebf5 2020-01-15 tracey const char *refname;
1820 f2f46662 2020-01-23 tracey char *refstr, *tag_commit0, *tag_commit, *id_str;
1821 4ff7391f 2020-01-28 tracey const char *tagger;
1822 87f9ebf5 2020-01-15 tracey time_t tagger_time;
1823 87f9ebf5 2020-01-15 tracey struct got_object_id *id;
1824 87f9ebf5 2020-01-15 tracey struct got_tag_object *tag;
1825 87f9ebf5 2020-01-15 tracey
1826 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
1827 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/tags/", 10) != 0)
1828 87f9ebf5 2020-01-15 tracey continue;
1829 87f9ebf5 2020-01-15 tracey refname += 10;
1830 87f9ebf5 2020-01-15 tracey refstr = got_ref_to_str(re->ref);
1831 87f9ebf5 2020-01-15 tracey if (refstr == NULL) {
1832 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
1833 87f9ebf5 2020-01-15 tracey goto done;
1834 87f9ebf5 2020-01-15 tracey }
1835 87f9ebf5 2020-01-15 tracey
1836 87f9ebf5 2020-01-15 tracey error = got_ref_resolve(&id, repo, re->ref);
1837 87f9ebf5 2020-01-15 tracey if (error)
1838 87f9ebf5 2020-01-15 tracey goto done;
1839 87f9ebf5 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id);
1840 87f9ebf5 2020-01-15 tracey free(id);
1841 87f9ebf5 2020-01-15 tracey if (error)
1842 87f9ebf5 2020-01-15 tracey goto done;
1843 87f9ebf5 2020-01-15 tracey
1844 4ff7391f 2020-01-28 tracey tagger = got_object_tag_get_tagger(tag);
1845 87f9ebf5 2020-01-15 tracey tagger_time = got_object_tag_get_tagger_time(tag);
1846 87f9ebf5 2020-01-15 tracey
1847 87f9ebf5 2020-01-15 tracey error = got_object_id_str(&id_str,
1848 87f9ebf5 2020-01-15 tracey got_object_tag_get_object_id(tag));
1849 87f9ebf5 2020-01-15 tracey if (error)
1850 87f9ebf5 2020-01-15 tracey goto done;
1851 87f9ebf5 2020-01-15 tracey
1852 f2f46662 2020-01-23 tracey tag_commit0 = strdup(got_object_tag_get_message(tag));
1853 87f9ebf5 2020-01-15 tracey
1854 f2f46662 2020-01-23 tracey if (tag_commit0 == NULL) {
1855 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("strdup");
1856 87f9ebf5 2020-01-15 tracey goto done;
1857 87f9ebf5 2020-01-15 tracey }
1858 87f9ebf5 2020-01-15 tracey
1859 f2f46662 2020-01-23 tracey tag_commit = tag_commit0;
1860 f2f46662 2020-01-23 tracey while (*tag_commit == '\n')
1861 f2f46662 2020-01-23 tracey tag_commit++;
1862 87f9ebf5 2020-01-15 tracey
1863 87f9ebf5 2020-01-15 tracey switch (tag_type) {
1864 87f9ebf5 2020-01-15 tracey case TAGBRIEF:
1865 f2f46662 2020-01-23 tracey newline = strchr(tag_commit, '\n');
1866 87f9ebf5 2020-01-15 tracey if (newline)
1867 87f9ebf5 2020-01-15 tracey *newline = '\0';
1868 87f9ebf5 2020-01-15 tracey
1869 88d59c36 2020-01-29 stsp if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1870 88d59c36 2020-01-29 stsp TM_DIFF)) == -1) {
1871 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1872 87f9ebf5 2020-01-15 tracey goto done;
1873 87f9ebf5 2020-01-15 tracey }
1874 87f9ebf5 2020-01-15 tracey
1875 88d59c36 2020-01-29 stsp if (asprintf(&tags_navs_disp, tags_navs,
1876 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, id_str, gw_trans->repo_name,
1877 87f9ebf5 2020-01-15 tracey id_str, gw_trans->repo_name, id_str,
1878 88d59c36 2020-01-29 stsp gw_trans->repo_name, id_str) == -1) {
1879 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1880 87f9ebf5 2020-01-15 tracey goto done;
1881 87f9ebf5 2020-01-15 tracey }
1882 87f9ebf5 2020-01-15 tracey
1883 88d59c36 2020-01-29 stsp if (asprintf(&tag_row, tags_row, age, refname,
1884 88d59c36 2020-01-29 stsp tag_commit, tags_navs_disp) == -1) {
1885 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1886 87f9ebf5 2020-01-15 tracey goto done;
1887 87f9ebf5 2020-01-15 tracey }
1888 87f9ebf5 2020-01-15 tracey
1889 87f9ebf5 2020-01-15 tracey free(tags_navs_disp);
1890 87f9ebf5 2020-01-15 tracey break;
1891 87f9ebf5 2020-01-15 tracey case TAGFULL:
1892 88d59c36 2020-01-29 stsp if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1893 88d59c36 2020-01-29 stsp TM_LONG)) == -1) {
1894 4ff7391f 2020-01-28 tracey error = got_error_from_errno("asprintf");
1895 4ff7391f 2020-01-28 tracey goto done;
1896 4ff7391f 2020-01-28 tracey }
1897 88d59c36 2020-01-29 stsp if (asprintf(&tag_row, tag_info, age,
1898 4ff7391f 2020-01-28 tracey gw_html_escape(tagger),
1899 88d59c36 2020-01-29 stsp gw_html_escape(tag_commit)) == -1) {
1900 4ff7391f 2020-01-28 tracey error = got_error_from_errno("asprintf");
1901 4ff7391f 2020-01-28 tracey goto done;
1902 4ff7391f 2020-01-28 tracey }
1903 87f9ebf5 2020-01-15 tracey break;
1904 87f9ebf5 2020-01-15 tracey default:
1905 87f9ebf5 2020-01-15 tracey break;
1906 87f9ebf5 2020-01-15 tracey }
1907 87f9ebf5 2020-01-15 tracey
1908 6c6c85af 2020-01-15 tracey got_object_tag_close(tag);
1909 87f9ebf5 2020-01-15 tracey
1910 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, tag_row);
1911 87f9ebf5 2020-01-15 tracey
1912 6c6c85af 2020-01-15 tracey free(id_str);
1913 6c6c85af 2020-01-15 tracey free(refstr);
1914 6c6c85af 2020-01-15 tracey free(age);
1915 f2f46662 2020-01-23 tracey free(tag_commit0);
1916 87f9ebf5 2020-01-15 tracey free(tag_row);
1917 87f9ebf5 2020-01-15 tracey
1918 87f9ebf5 2020-01-15 tracey if (error || (limit && --limit == 0))
1919 87f9ebf5 2020-01-15 tracey break;
1920 87f9ebf5 2020-01-15 tracey }
1921 6c6c85af 2020-01-15 tracey
1922 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
1923 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
1924 6c6c85af 2020-01-15 tracey tags = strdup(buf_get(diffbuf));
1925 6c6c85af 2020-01-15 tracey }
1926 87f9ebf5 2020-01-15 tracey done:
1927 87f9ebf5 2020-01-15 tracey buf_free(diffbuf);
1928 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
1929 87f9ebf5 2020-01-15 tracey if (repo)
1930 87f9ebf5 2020-01-15 tracey got_repo_close(repo);
1931 87f9ebf5 2020-01-15 tracey if (error)
1932 87f9ebf5 2020-01-15 tracey return NULL;
1933 87f9ebf5 2020-01-15 tracey else
1934 87f9ebf5 2020-01-15 tracey return tags;
1935 f2f46662 2020-01-23 tracey }
1936 f2f46662 2020-01-23 tracey
1937 f2f46662 2020-01-23 tracey static void
1938 f2f46662 2020-01-23 tracey gw_free_headers(struct gw_header *header)
1939 f2f46662 2020-01-23 tracey {
1940 f2f46662 2020-01-23 tracey free(header->id);
1941 f2f46662 2020-01-23 tracey free(header->path);
1942 f2f46662 2020-01-23 tracey if (header->commit != NULL)
1943 f2f46662 2020-01-23 tracey got_object_commit_close(header->commit);
1944 f2f46662 2020-01-23 tracey if (header->repo)
1945 f2f46662 2020-01-23 tracey got_repo_close(header->repo);
1946 f2f46662 2020-01-23 tracey free(header->refs_str);
1947 f2f46662 2020-01-23 tracey free(header->commit_id);
1948 f2f46662 2020-01-23 tracey free(header->parent_id);
1949 f2f46662 2020-01-23 tracey free(header->tree_id);
1950 f2f46662 2020-01-23 tracey free(header->author);
1951 f2f46662 2020-01-23 tracey free(header->committer);
1952 f2f46662 2020-01-23 tracey free(header->commit_msg);
1953 f2f46662 2020-01-23 tracey }
1954 f2f46662 2020-01-23 tracey
1955 f2f46662 2020-01-23 tracey static struct gw_header *
1956 f2f46662 2020-01-23 tracey gw_init_header()
1957 f2f46662 2020-01-23 tracey {
1958 f2f46662 2020-01-23 tracey struct gw_header *header;
1959 f2f46662 2020-01-23 tracey
1960 ae36ed87 2020-01-28 stsp header = malloc(sizeof(*header));
1961 ae36ed87 2020-01-28 stsp if (header == NULL)
1962 f2f46662 2020-01-23 tracey return NULL;
1963 f2f46662 2020-01-23 tracey
1964 f2f46662 2020-01-23 tracey header->repo = NULL;
1965 f2f46662 2020-01-23 tracey header->commit = NULL;
1966 f2f46662 2020-01-23 tracey header->id = NULL;
1967 f2f46662 2020-01-23 tracey header->path = NULL;
1968 ae36ed87 2020-01-28 stsp SIMPLEQ_INIT(&header->refs);
1969 f2f46662 2020-01-23 tracey
1970 f2f46662 2020-01-23 tracey return header;
1971 f2f46662 2020-01-23 tracey }
1972 f2f46662 2020-01-23 tracey
1973 f2f46662 2020-01-23 tracey static const struct got_error *
1974 f2f46662 2020-01-23 tracey gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1975 f2f46662 2020-01-23 tracey int limit)
1976 f2f46662 2020-01-23 tracey {
1977 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
1978 f2f46662 2020-01-23 tracey struct got_commit_graph *graph = NULL;
1979 f2f46662 2020-01-23 tracey
1980 f2f46662 2020-01-23 tracey error = got_commit_graph_open(&graph, header->path, 0);
1981 f2f46662 2020-01-23 tracey if (error)
1982 f2f46662 2020-01-23 tracey goto done;
1983 f2f46662 2020-01-23 tracey
1984 f2f46662 2020-01-23 tracey error = got_commit_graph_iter_start(graph, header->id, header->repo,
1985 f2f46662 2020-01-23 tracey NULL, NULL);
1986 f2f46662 2020-01-23 tracey if (error)
1987 f2f46662 2020-01-23 tracey goto done;
1988 f2f46662 2020-01-23 tracey
1989 f2f46662 2020-01-23 tracey for (;;) {
1990 f2f46662 2020-01-23 tracey error = got_commit_graph_iter_next(&header->id, graph,
1991 f2f46662 2020-01-23 tracey header->repo, NULL, NULL);
1992 f2f46662 2020-01-23 tracey if (error) {
1993 f2f46662 2020-01-23 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
1994 f2f46662 2020-01-23 tracey error = NULL;
1995 f2f46662 2020-01-23 tracey goto done;
1996 f2f46662 2020-01-23 tracey }
1997 f2f46662 2020-01-23 tracey if (header->id == NULL)
1998 f2f46662 2020-01-23 tracey goto done;
1999 f2f46662 2020-01-23 tracey
2000 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit, header->repo,
2001 f2f46662 2020-01-23 tracey header->id);
2002 f2f46662 2020-01-23 tracey if (error)
2003 f2f46662 2020-01-23 tracey goto done;
2004 f2f46662 2020-01-23 tracey
2005 f2f46662 2020-01-23 tracey error = gw_get_commit(gw_trans, header);
2006 f2f46662 2020-01-23 tracey if (limit > 1) {
2007 f2f46662 2020-01-23 tracey struct gw_header *n_header = NULL;
2008 5147ab56 2020-01-29 stsp if ((n_header = gw_init_header()) == NULL) {
2009 f2f46662 2020-01-23 tracey error = got_error_from_errno("malloc");
2010 5147ab56 2020-01-29 stsp goto done;
2011 5147ab56 2020-01-29 stsp }
2012 f2f46662 2020-01-23 tracey
2013 f2f46662 2020-01-23 tracey n_header->refs_str = strdup(header->refs_str);
2014 f2f46662 2020-01-23 tracey n_header->commit_id = strdup(header->commit_id);
2015 f2f46662 2020-01-23 tracey n_header->parent_id = strdup(header->parent_id);
2016 f2f46662 2020-01-23 tracey n_header->tree_id = strdup(header->tree_id);
2017 f2f46662 2020-01-23 tracey n_header->author = strdup(header->author);
2018 f2f46662 2020-01-23 tracey n_header->committer = strdup(header->committer);
2019 f2f46662 2020-01-23 tracey n_header->commit_msg = strdup(header->commit_msg);
2020 f2f46662 2020-01-23 tracey n_header->committer_time = header->committer_time;
2021 f2f46662 2020-01-23 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2022 f2f46662 2020-01-23 tracey entry);
2023 f2f46662 2020-01-23 tracey }
2024 f2f46662 2020-01-23 tracey if (error || (limit && --limit == 0))
2025 f2f46662 2020-01-23 tracey break;
2026 f2f46662 2020-01-23 tracey }
2027 f2f46662 2020-01-23 tracey done:
2028 f2f46662 2020-01-23 tracey if (graph)
2029 f2f46662 2020-01-23 tracey got_commit_graph_close(graph);
2030 f2f46662 2020-01-23 tracey return error;
2031 f2f46662 2020-01-23 tracey }
2032 f2f46662 2020-01-23 tracey
2033 f2f46662 2020-01-23 tracey static const struct got_error *
2034 f2f46662 2020-01-23 tracey gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2035 f2f46662 2020-01-23 tracey {
2036 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
2037 f2f46662 2020-01-23 tracey struct got_reflist_entry *re;
2038 f2f46662 2020-01-23 tracey struct got_object_id *id2 = NULL;
2039 f2f46662 2020-01-23 tracey struct got_object_qid *parent_id;
2040 e46f587c 2020-01-29 tracey char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2041 f2f46662 2020-01-23 tracey
2042 f2f46662 2020-01-23 tracey /*print commit*/
2043 f2f46662 2020-01-23 tracey SIMPLEQ_FOREACH(re, &header->refs, entry) {
2044 f2f46662 2020-01-23 tracey char *s;
2045 f2f46662 2020-01-23 tracey const char *name;
2046 f2f46662 2020-01-23 tracey struct got_tag_object *tag = NULL;
2047 f2f46662 2020-01-23 tracey int cmp;
2048 f2f46662 2020-01-23 tracey
2049 f2f46662 2020-01-23 tracey name = got_ref_get_name(re->ref);
2050 f2f46662 2020-01-23 tracey if (strcmp(name, GOT_REF_HEAD) == 0)
2051 f2f46662 2020-01-23 tracey continue;
2052 f2f46662 2020-01-23 tracey if (strncmp(name, "refs/", 5) == 0)
2053 f2f46662 2020-01-23 tracey name += 5;
2054 f2f46662 2020-01-23 tracey if (strncmp(name, "got/", 4) == 0)
2055 f2f46662 2020-01-23 tracey continue;
2056 f2f46662 2020-01-23 tracey if (strncmp(name, "heads/", 6) == 0)
2057 f2f46662 2020-01-23 tracey name += 6;
2058 f2f46662 2020-01-23 tracey if (strncmp(name, "remotes/", 8) == 0)
2059 f2f46662 2020-01-23 tracey name += 8;
2060 f2f46662 2020-01-23 tracey if (strncmp(name, "tags/", 5) == 0) {
2061 f2f46662 2020-01-23 tracey error = got_object_open_as_tag(&tag, header->repo,
2062 f2f46662 2020-01-23 tracey re->id);
2063 f2f46662 2020-01-23 tracey if (error) {
2064 f2f46662 2020-01-23 tracey if (error->code != GOT_ERR_OBJ_TYPE)
2065 f2f46662 2020-01-23 tracey continue;
2066 f2f46662 2020-01-23 tracey /*
2067 f2f46662 2020-01-23 tracey * Ref points at something other
2068 f2f46662 2020-01-23 tracey * than a tag.
2069 f2f46662 2020-01-23 tracey */
2070 f2f46662 2020-01-23 tracey error = NULL;
2071 f2f46662 2020-01-23 tracey tag = NULL;
2072 f2f46662 2020-01-23 tracey }
2073 f2f46662 2020-01-23 tracey }
2074 f2f46662 2020-01-23 tracey cmp = got_object_id_cmp(tag ?
2075 f2f46662 2020-01-23 tracey got_object_tag_get_object_id(tag) : re->id, header->id);
2076 f2f46662 2020-01-23 tracey if (tag)
2077 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
2078 f2f46662 2020-01-23 tracey if (cmp != 0)
2079 f2f46662 2020-01-23 tracey continue;
2080 f2f46662 2020-01-23 tracey s = refs_str;
2081 88d59c36 2020-01-29 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2082 88d59c36 2020-01-29 stsp s ? ", " : "", name) == -1) {
2083 f2f46662 2020-01-23 tracey error = got_error_from_errno("asprintf");
2084 f2f46662 2020-01-23 tracey free(s);
2085 f2f46662 2020-01-23 tracey return error;
2086 f2f46662 2020-01-23 tracey }
2087 f2f46662 2020-01-23 tracey header->refs_str = strdup(refs_str);
2088 f2f46662 2020-01-23 tracey free(s);
2089 f2f46662 2020-01-23 tracey }
2090 f2f46662 2020-01-23 tracey
2091 f2f46662 2020-01-23 tracey if (refs_str == NULL)
2092 f2f46662 2020-01-23 tracey header->refs_str = strdup("");
2093 f2f46662 2020-01-23 tracey free(refs_str);
2094 f2f46662 2020-01-23 tracey
2095 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->commit_id, header->id);
2096 f2f46662 2020-01-23 tracey if (error)
2097 f2f46662 2020-01-23 tracey return error;
2098 f2f46662 2020-01-23 tracey
2099 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->tree_id,
2100 f2f46662 2020-01-23 tracey got_object_commit_get_tree_id(header->commit));
2101 f2f46662 2020-01-23 tracey if (error)
2102 f2f46662 2020-01-23 tracey return error;
2103 f2f46662 2020-01-23 tracey
2104 f2f46662 2020-01-23 tracey if (gw_trans->action == GW_DIFF) {
2105 f2f46662 2020-01-23 tracey parent_id = SIMPLEQ_FIRST(
2106 f2f46662 2020-01-23 tracey got_object_commit_get_parent_ids(header->commit));
2107 f2f46662 2020-01-23 tracey if (parent_id != NULL) {
2108 f2f46662 2020-01-23 tracey id2 = got_object_id_dup(parent_id->id);
2109 f2f46662 2020-01-23 tracey free (parent_id);
2110 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->parent_id, id2);
2111 f2f46662 2020-01-23 tracey if (error)
2112 f2f46662 2020-01-23 tracey return error;
2113 f2f46662 2020-01-23 tracey free(id2);
2114 f2f46662 2020-01-23 tracey } else
2115 f2f46662 2020-01-23 tracey header->parent_id = strdup("/dev/null");
2116 f2f46662 2020-01-23 tracey } else
2117 f2f46662 2020-01-23 tracey header->parent_id = strdup("");
2118 f2f46662 2020-01-23 tracey
2119 f2f46662 2020-01-23 tracey header->committer_time =
2120 f2f46662 2020-01-23 tracey got_object_commit_get_committer_time(header->commit);
2121 1177268c 2020-01-28 tracey
2122 1177268c 2020-01-28 tracey if (gw_trans->action != GW_BRIEFS && gw_trans->action != GW_SUMMARY) {
2123 1177268c 2020-01-28 tracey header->author = strdup(
2124 1177268c 2020-01-28 tracey gw_html_escape(got_object_commit_get_author(header->commit))
2125 1177268c 2020-01-28 tracey );
2126 1177268c 2020-01-28 tracey } else {
2127 1177268c 2020-01-28 tracey header->author = strdup(
2128 1177268c 2020-01-28 tracey got_object_commit_get_author(header->commit)
2129 1177268c 2020-01-28 tracey );
2130 1177268c 2020-01-28 tracey }
2131 1177268c 2020-01-28 tracey
2132 4ff7391f 2020-01-28 tracey header->committer = strdup(
2133 4ff7391f 2020-01-28 tracey gw_html_escape(got_object_commit_get_committer(header->commit))
2134 4ff7391f 2020-01-28 tracey );
2135 f2f46662 2020-01-23 tracey
2136 f2f46662 2020-01-23 tracey error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2137 f2f46662 2020-01-23 tracey if (error)
2138 f2f46662 2020-01-23 tracey return error;
2139 f2f46662 2020-01-23 tracey
2140 f2f46662 2020-01-23 tracey commit_msg = commit_msg0;
2141 f2f46662 2020-01-23 tracey while (*commit_msg == '\n')
2142 f2f46662 2020-01-23 tracey commit_msg++;
2143 f2f46662 2020-01-23 tracey
2144 f2f46662 2020-01-23 tracey header->commit_msg = strdup(commit_msg);
2145 f2f46662 2020-01-23 tracey free(commit_msg0);
2146 f2f46662 2020-01-23 tracey return error;
2147 f2f46662 2020-01-23 tracey }
2148 f2f46662 2020-01-23 tracey
2149 f2f46662 2020-01-23 tracey static const struct got_error *
2150 f2f46662 2020-01-23 tracey gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2151 f2f46662 2020-01-23 tracey {
2152 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
2153 f2f46662 2020-01-23 tracey char *in_repo_path = NULL;
2154 f2f46662 2020-01-23 tracey
2155 f2f46662 2020-01-23 tracey error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2156 f2f46662 2020-01-23 tracey if (error)
2157 f2f46662 2020-01-23 tracey return error;
2158 f2f46662 2020-01-23 tracey
2159 f2f46662 2020-01-23 tracey if (gw_trans->commit == NULL) {
2160 f2f46662 2020-01-23 tracey struct got_reference *head_ref;
2161 f2f46662 2020-01-23 tracey error = got_ref_open(&head_ref, header->repo,
2162 f2f46662 2020-01-23 tracey gw_trans->headref, 0);
2163 f2f46662 2020-01-23 tracey if (error)
2164 f2f46662 2020-01-23 tracey return error;
2165 f2f46662 2020-01-23 tracey
2166 f2f46662 2020-01-23 tracey error = got_ref_resolve(&header->id, header->repo, head_ref);
2167 f2f46662 2020-01-23 tracey got_ref_close(head_ref);
2168 f2f46662 2020-01-23 tracey if (error)
2169 f2f46662 2020-01-23 tracey return error;
2170 f2f46662 2020-01-23 tracey
2171 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit,
2172 f2f46662 2020-01-23 tracey header->repo, header->id);
2173 f2f46662 2020-01-23 tracey } else {
2174 f2f46662 2020-01-23 tracey struct got_reference *ref;
2175 f2f46662 2020-01-23 tracey error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2176 f2f46662 2020-01-23 tracey if (error == NULL) {
2177 f2f46662 2020-01-23 tracey int obj_type;
2178 f2f46662 2020-01-23 tracey error = got_ref_resolve(&header->id, header->repo, ref);
2179 f2f46662 2020-01-23 tracey got_ref_close(ref);
2180 f2f46662 2020-01-23 tracey if (error)
2181 f2f46662 2020-01-23 tracey return error;
2182 f2f46662 2020-01-23 tracey error = got_object_get_type(&obj_type, header->repo,
2183 f2f46662 2020-01-23 tracey header->id);
2184 f2f46662 2020-01-23 tracey if (error)
2185 f2f46662 2020-01-23 tracey return error;
2186 f2f46662 2020-01-23 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
2187 f2f46662 2020-01-23 tracey struct got_tag_object *tag;
2188 f2f46662 2020-01-23 tracey error = got_object_open_as_tag(&tag,
2189 f2f46662 2020-01-23 tracey header->repo, header->id);
2190 f2f46662 2020-01-23 tracey if (error)
2191 f2f46662 2020-01-23 tracey return error;
2192 f2f46662 2020-01-23 tracey if (got_object_tag_get_object_type(tag) !=
2193 f2f46662 2020-01-23 tracey GOT_OBJ_TYPE_COMMIT) {
2194 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
2195 f2f46662 2020-01-23 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2196 f2f46662 2020-01-23 tracey return error;
2197 f2f46662 2020-01-23 tracey }
2198 f2f46662 2020-01-23 tracey free(header->id);
2199 f2f46662 2020-01-23 tracey header->id = got_object_id_dup(
2200 f2f46662 2020-01-23 tracey got_object_tag_get_object_id(tag));
2201 f2f46662 2020-01-23 tracey if (header->id == NULL)
2202 f2f46662 2020-01-23 tracey error = got_error_from_errno(
2203 f2f46662 2020-01-23 tracey "got_object_id_dup");
2204 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
2205 f2f46662 2020-01-23 tracey if (error)
2206 f2f46662 2020-01-23 tracey return error;
2207 f2f46662 2020-01-23 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2208 f2f46662 2020-01-23 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2209 f2f46662 2020-01-23 tracey return error;
2210 f2f46662 2020-01-23 tracey }
2211 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit,
2212 f2f46662 2020-01-23 tracey header->repo, header->id);
2213 f2f46662 2020-01-23 tracey if (error)
2214 f2f46662 2020-01-23 tracey return error;
2215 f2f46662 2020-01-23 tracey }
2216 f2f46662 2020-01-23 tracey if (header->commit == NULL) {
2217 f2f46662 2020-01-23 tracey error = got_repo_match_object_id_prefix(&header->id,
2218 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2219 f2f46662 2020-01-23 tracey header->repo);
2220 f2f46662 2020-01-23 tracey if (error)
2221 f2f46662 2020-01-23 tracey return error;
2222 f2f46662 2020-01-23 tracey }
2223 f2f46662 2020-01-23 tracey error = got_repo_match_object_id_prefix(&header->id,
2224 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2225 f2f46662 2020-01-23 tracey header->repo);
2226 f2f46662 2020-01-23 tracey }
2227 f2f46662 2020-01-23 tracey
2228 f2f46662 2020-01-23 tracey error = got_repo_map_path(&in_repo_path, header->repo,
2229 f2f46662 2020-01-23 tracey gw_trans->repo_path, 1);
2230 f2f46662 2020-01-23 tracey if (error)
2231 f2f46662 2020-01-23 tracey return error;
2232 f2f46662 2020-01-23 tracey
2233 f2f46662 2020-01-23 tracey if (in_repo_path) {
2234 f2f46662 2020-01-23 tracey header->path = strdup(in_repo_path);
2235 f2f46662 2020-01-23 tracey }
2236 f2f46662 2020-01-23 tracey free(in_repo_path);
2237 f2f46662 2020-01-23 tracey
2238 f2f46662 2020-01-23 tracey error = got_ref_list(&header->refs, header->repo, NULL,
2239 f2f46662 2020-01-23 tracey got_ref_cmp_by_name, NULL);
2240 f2f46662 2020-01-23 tracey if (error)
2241 f2f46662 2020-01-23 tracey return error;
2242 f2f46662 2020-01-23 tracey
2243 f2f46662 2020-01-23 tracey error = gw_get_commits(gw_trans, header, limit);
2244 f2f46662 2020-01-23 tracey return error;
2245 ec46ccd7 2020-01-15 tracey }
2246 ec46ccd7 2020-01-15 tracey
2247 ec46ccd7 2020-01-15 tracey struct blame_line {
2248 ec46ccd7 2020-01-15 tracey int annotated;
2249 ec46ccd7 2020-01-15 tracey char *id_str;
2250 ec46ccd7 2020-01-15 tracey char *committer;
2251 ec46ccd7 2020-01-15 tracey char datebuf[11]; /* YYYY-MM-DD + NUL */
2252 ec46ccd7 2020-01-15 tracey };
2253 ec46ccd7 2020-01-15 tracey
2254 147269d5 2020-01-15 tracey struct gw_blame_cb_args {
2255 ec46ccd7 2020-01-15 tracey struct blame_line *lines;
2256 ec46ccd7 2020-01-15 tracey int nlines;
2257 ec46ccd7 2020-01-15 tracey int nlines_prec;
2258 ec46ccd7 2020-01-15 tracey int lineno_cur;
2259 ec46ccd7 2020-01-15 tracey off_t *line_offsets;
2260 ec46ccd7 2020-01-15 tracey FILE *f;
2261 ec46ccd7 2020-01-15 tracey struct got_repository *repo;
2262 54415d85 2020-01-15 tracey struct gw_trans *gw_trans;
2263 2e676fc5 2020-01-15 tracey struct buf *blamebuf;
2264 ec46ccd7 2020-01-15 tracey };
2265 ec46ccd7 2020-01-15 tracey
2266 ec46ccd7 2020-01-15 tracey static const struct got_error *
2267 147269d5 2020-01-15 tracey gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2268 ec46ccd7 2020-01-15 tracey {
2269 ec46ccd7 2020-01-15 tracey const struct got_error *err = NULL;
2270 147269d5 2020-01-15 tracey struct gw_blame_cb_args *a = arg;
2271 ec46ccd7 2020-01-15 tracey struct blame_line *bline;
2272 ec46ccd7 2020-01-15 tracey char *line = NULL;
2273 2e676fc5 2020-01-15 tracey size_t linesize = 0, newsize;
2274 ec46ccd7 2020-01-15 tracey struct got_commit_object *commit = NULL;
2275 ec46ccd7 2020-01-15 tracey off_t offset;
2276 ec46ccd7 2020-01-15 tracey struct tm tm;
2277 ec46ccd7 2020-01-15 tracey time_t committer_time;
2278 ec46ccd7 2020-01-15 tracey
2279 ec46ccd7 2020-01-15 tracey if (nlines != a->nlines ||
2280 ec46ccd7 2020-01-15 tracey (lineno != -1 && lineno < 1) || lineno > a->nlines)
2281 ec46ccd7 2020-01-15 tracey return got_error(GOT_ERR_RANGE);
2282 ec46ccd7 2020-01-15 tracey
2283 ec46ccd7 2020-01-15 tracey if (lineno == -1)
2284 ec46ccd7 2020-01-15 tracey return NULL; /* no change in this commit */
2285 ec46ccd7 2020-01-15 tracey
2286 ec46ccd7 2020-01-15 tracey /* Annotate this line. */
2287 ec46ccd7 2020-01-15 tracey bline = &a->lines[lineno - 1];
2288 ec46ccd7 2020-01-15 tracey if (bline->annotated)
2289 ec46ccd7 2020-01-15 tracey return NULL;
2290 ec46ccd7 2020-01-15 tracey err = got_object_id_str(&bline->id_str, id);
2291 ec46ccd7 2020-01-15 tracey if (err)
2292 ec46ccd7 2020-01-15 tracey return err;
2293 ec46ccd7 2020-01-15 tracey
2294 ec46ccd7 2020-01-15 tracey err = got_object_open_as_commit(&commit, a->repo, id);
2295 ec46ccd7 2020-01-15 tracey if (err)
2296 ec46ccd7 2020-01-15 tracey goto done;
2297 ec46ccd7 2020-01-15 tracey
2298 ec46ccd7 2020-01-15 tracey bline->committer = strdup(got_object_commit_get_committer(commit));
2299 ec46ccd7 2020-01-15 tracey if (bline->committer == NULL) {
2300 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("strdup");
2301 ec46ccd7 2020-01-15 tracey goto done;
2302 ec46ccd7 2020-01-15 tracey }
2303 ec46ccd7 2020-01-15 tracey
2304 ec46ccd7 2020-01-15 tracey committer_time = got_object_commit_get_committer_time(commit);
2305 ec46ccd7 2020-01-15 tracey if (localtime_r(&committer_time, &tm) == NULL)
2306 ec46ccd7 2020-01-15 tracey return got_error_from_errno("localtime_r");
2307 ec46ccd7 2020-01-15 tracey if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2308 ec46ccd7 2020-01-15 tracey &tm) >= sizeof(bline->datebuf)) {
2309 ec46ccd7 2020-01-15 tracey err = got_error(GOT_ERR_NO_SPACE);
2310 ec46ccd7 2020-01-15 tracey goto done;
2311 ec46ccd7 2020-01-15 tracey }
2312 ec46ccd7 2020-01-15 tracey bline->annotated = 1;
2313 ec46ccd7 2020-01-15 tracey
2314 ec46ccd7 2020-01-15 tracey /* Print lines annotated so far. */
2315 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
2316 ec46ccd7 2020-01-15 tracey if (!bline->annotated)
2317 ec46ccd7 2020-01-15 tracey goto done;
2318 ec46ccd7 2020-01-15 tracey
2319 ec46ccd7 2020-01-15 tracey offset = a->line_offsets[a->lineno_cur - 1];
2320 ec46ccd7 2020-01-15 tracey if (fseeko(a->f, offset, SEEK_SET) == -1) {
2321 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("fseeko");
2322 ec46ccd7 2020-01-15 tracey goto done;
2323 ec46ccd7 2020-01-15 tracey }
2324 ec46ccd7 2020-01-15 tracey
2325 ec46ccd7 2020-01-15 tracey while (bline->annotated) {
2326 0311ce2d 2020-01-15 tracey char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2327 0311ce2d 2020-01-15 tracey *line_escape = NULL;
2328 ec46ccd7 2020-01-15 tracey size_t len;
2329 ec46ccd7 2020-01-15 tracey
2330 ec46ccd7 2020-01-15 tracey if (getline(&line, &linesize, a->f) == -1) {
2331 ec46ccd7 2020-01-15 tracey if (ferror(a->f))
2332 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("getline");
2333 ec46ccd7 2020-01-15 tracey break;
2334 ec46ccd7 2020-01-15 tracey }
2335 ec46ccd7 2020-01-15 tracey
2336 ec46ccd7 2020-01-15 tracey committer = bline->committer;
2337 ec46ccd7 2020-01-15 tracey smallerthan = strchr(committer, '<');
2338 ec46ccd7 2020-01-15 tracey if (smallerthan && smallerthan[1] != '\0')
2339 ec46ccd7 2020-01-15 tracey committer = smallerthan + 1;
2340 ec46ccd7 2020-01-15 tracey at = strchr(committer, '@');
2341 ec46ccd7 2020-01-15 tracey if (at)
2342 ec46ccd7 2020-01-15 tracey *at = '\0';
2343 ec46ccd7 2020-01-15 tracey len = strlen(committer);
2344 ec46ccd7 2020-01-15 tracey if (len >= 9)
2345 ec46ccd7 2020-01-15 tracey committer[8] = '\0';
2346 ec46ccd7 2020-01-15 tracey
2347 ec46ccd7 2020-01-15 tracey nl = strchr(line, '\n');
2348 ec46ccd7 2020-01-15 tracey if (nl)
2349 ec46ccd7 2020-01-15 tracey *nl = '\0';
2350 0311ce2d 2020-01-15 tracey
2351 0311ce2d 2020-01-15 tracey if (strcmp(line, "") != 0)
2352 0311ce2d 2020-01-15 tracey line_escape = strdup(gw_html_escape(line));
2353 0311ce2d 2020-01-15 tracey else
2354 0311ce2d 2020-01-15 tracey line_escape = strdup("");
2355 0311ce2d 2020-01-15 tracey
2356 f2f46662 2020-01-23 tracey asprintf(&blame_row, blame_line, a->nlines_prec,
2357 2e676fc5 2020-01-15 tracey a->lineno_cur, bline->id_str, bline->datebuf, committer,
2358 0311ce2d 2020-01-15 tracey line_escape);
2359 ec46ccd7 2020-01-15 tracey a->lineno_cur++;
2360 2e676fc5 2020-01-15 tracey err = buf_puts(&newsize, a->blamebuf, blame_row);
2361 2e676fc5 2020-01-15 tracey if (err)
2362 2e676fc5 2020-01-15 tracey return err;
2363 2e676fc5 2020-01-15 tracey
2364 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
2365 0311ce2d 2020-01-15 tracey free(line_escape);
2366 2e676fc5 2020-01-15 tracey free(blame_row);
2367 ec46ccd7 2020-01-15 tracey }
2368 ec46ccd7 2020-01-15 tracey done:
2369 ec46ccd7 2020-01-15 tracey if (commit)
2370 ec46ccd7 2020-01-15 tracey got_object_commit_close(commit);
2371 ec46ccd7 2020-01-15 tracey free(line);
2372 ec46ccd7 2020-01-15 tracey return err;
2373 8d4d2453 2020-01-15 tracey }
2374 bcbc97d8 2020-01-15 tracey
2375 bcbc97d8 2020-01-15 tracey static char*
2376 e46f587c 2020-01-29 tracey gw_get_file_blame_blob(struct gw_trans *gw_trans)
2377 bcbc97d8 2020-01-15 tracey {
2378 bcbc97d8 2020-01-15 tracey const struct got_error *error = NULL;
2379 bcbc97d8 2020-01-15 tracey struct got_repository *repo = NULL;
2380 ec46ccd7 2020-01-15 tracey struct got_object_id *obj_id = NULL;
2381 ec46ccd7 2020-01-15 tracey struct got_object_id *commit_id = NULL;
2382 ec46ccd7 2020-01-15 tracey struct got_blob_object *blob = NULL;
2383 e46f587c 2020-01-29 tracey char *blame_html = NULL, *path = NULL, *in_repo_path = NULL;
2384 e46f587c 2020-01-29 tracey char *folder = NULL;
2385 147269d5 2020-01-15 tracey struct gw_blame_cb_args bca;
2386 119bf4ed 2020-01-15 tracey int i, obj_type;
2387 ec46ccd7 2020-01-15 tracey size_t filesize;
2388 017d6da3 2020-01-29 tracey enum kcgi_err kerr;
2389 ec46ccd7 2020-01-15 tracey
2390 ec46ccd7 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2391 ec46ccd7 2020-01-15 tracey if (error)
2392 ec46ccd7 2020-01-15 tracey goto done;
2393 ec46ccd7 2020-01-15 tracey
2394 2e676fc5 2020-01-15 tracey if (gw_trans->repo_folder != NULL) {
2395 88d59c36 2020-01-29 stsp if (asprintf(&folder, "%s/", gw_trans->repo_folder) == -1) {
2396 2e676fc5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2397 2e676fc5 2020-01-15 tracey goto done;
2398 2e676fc5 2020-01-15 tracey }
2399 2e676fc5 2020-01-15 tracey } else
2400 2e676fc5 2020-01-15 tracey folder = strdup("");
2401 2e676fc5 2020-01-15 tracey
2402 88d59c36 2020-01-29 stsp if (asprintf(&path, "%s%s", folder, gw_trans->repo_file) == -1) {
2403 2e676fc5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2404 2e676fc5 2020-01-15 tracey goto done;
2405 2e676fc5 2020-01-15 tracey }
2406 2e676fc5 2020-01-15 tracey free(folder);
2407 2e676fc5 2020-01-15 tracey
2408 2e676fc5 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, path, 1);
2409 ec46ccd7 2020-01-15 tracey if (error)
2410 ec46ccd7 2020-01-15 tracey goto done;
2411 ec46ccd7 2020-01-15 tracey
2412 f2f46662 2020-01-23 tracey error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2413 147269d5 2020-01-15 tracey GOT_OBJ_TYPE_COMMIT, 1, repo);
2414 ec46ccd7 2020-01-15 tracey if (error)
2415 ec46ccd7 2020-01-15 tracey goto done;
2416 ec46ccd7 2020-01-15 tracey
2417 ec46ccd7 2020-01-15 tracey error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2418 ec46ccd7 2020-01-15 tracey if (error)
2419 ec46ccd7 2020-01-15 tracey goto done;
2420 2e676fc5 2020-01-15 tracey
2421 ec46ccd7 2020-01-15 tracey if (obj_id == NULL) {
2422 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_NO_OBJ);
2423 ec46ccd7 2020-01-15 tracey goto done;
2424 ec46ccd7 2020-01-15 tracey }
2425 ec46ccd7 2020-01-15 tracey
2426 ec46ccd7 2020-01-15 tracey error = got_object_get_type(&obj_type, repo, obj_id);
2427 ec46ccd7 2020-01-15 tracey if (error)
2428 ec46ccd7 2020-01-15 tracey goto done;
2429 ec46ccd7 2020-01-15 tracey
2430 ec46ccd7 2020-01-15 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
2431 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2432 ec46ccd7 2020-01-15 tracey goto done;
2433 ec46ccd7 2020-01-15 tracey }
2434 ec46ccd7 2020-01-15 tracey
2435 ec46ccd7 2020-01-15 tracey error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2436 ec46ccd7 2020-01-15 tracey if (error)
2437 ec46ccd7 2020-01-15 tracey goto done;
2438 ec46ccd7 2020-01-15 tracey
2439 2e676fc5 2020-01-15 tracey error = buf_alloc(&bca.blamebuf, 0);
2440 2e676fc5 2020-01-15 tracey if (error)
2441 2e676fc5 2020-01-15 tracey goto done;
2442 2e676fc5 2020-01-15 tracey
2443 ec46ccd7 2020-01-15 tracey bca.f = got_opentemp();
2444 ec46ccd7 2020-01-15 tracey if (bca.f == NULL) {
2445 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("got_opentemp");
2446 ec46ccd7 2020-01-15 tracey goto done;
2447 ec46ccd7 2020-01-15 tracey }
2448 ec46ccd7 2020-01-15 tracey error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2449 ec46ccd7 2020-01-15 tracey &bca.line_offsets, bca.f, blob);
2450 ec46ccd7 2020-01-15 tracey if (error || bca.nlines == 0)
2451 ec46ccd7 2020-01-15 tracey goto done;
2452 ec46ccd7 2020-01-15 tracey
2453 e46f587c 2020-01-29 tracey if (gw_trans->action == GW_BLOB) {
2454 017d6da3 2020-01-29 tracey int len, p, p_check, t = 0, t_check = 50;
2455 017d6da3 2020-01-29 tracey
2456 e46f587c 2020-01-29 tracey fseek(bca.f, 0, SEEK_END);
2457 017d6da3 2020-01-29 tracey p_check = len = ftell(bca.f) + 1;
2458 e46f587c 2020-01-29 tracey fseek(bca.f, 0, SEEK_SET);
2459 017d6da3 2020-01-29 tracey
2460 e46f587c 2020-01-29 tracey if ((blame_html = calloc(len, sizeof(char *))) == NULL)
2461 e46f587c 2020-01-29 tracey goto done;
2462 017d6da3 2020-01-29 tracey
2463 e46f587c 2020-01-29 tracey fread(blame_html, 1, len, bca.f);
2464 017d6da3 2020-01-29 tracey
2465 017d6da3 2020-01-29 tracey for (p = 0; p < p_check; p++) {
2466 017d6da3 2020-01-29 tracey if (isprint(blame_html[p]) == 0)
2467 017d6da3 2020-01-29 tracey if (iscntrl(blame_html[p]) == 0)
2468 017d6da3 2020-01-29 tracey t++;
2469 017d6da3 2020-01-29 tracey }
2470 017d6da3 2020-01-29 tracey
2471 017d6da3 2020-01-29 tracey /*
2472 017d6da3 2020-01-29 tracey * Anything over zero is most likely not plain text,
2473 017d6da3 2020-01-29 tracey * but let's be sure. Perhaps there's a better way to
2474 017d6da3 2020-01-29 tracey * check in the future.
2475 017d6da3 2020-01-29 tracey */
2476 017d6da3 2020-01-29 tracey
2477 017d6da3 2020-01-29 tracey if (t > t_check)
2478 017d6da3 2020-01-29 tracey gw_trans->mime = KMIME_APP_OCTET_STREAM;
2479 017d6da3 2020-01-29 tracey else
2480 017d6da3 2020-01-29 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
2481 017d6da3 2020-01-29 tracey
2482 017d6da3 2020-01-29 tracey error = gw_display_index(gw_trans);
2483 017d6da3 2020-01-29 tracey
2484 017d6da3 2020-01-29 tracey if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2485 017d6da3 2020-01-29 tracey kerr = khttp_write(gw_trans->gw_req, blame_html, len);
2486 017d6da3 2020-01-29 tracey if (kerr != KCGI_OK)
2487 017d6da3 2020-01-29 tracey error = gw_kcgi_error(kerr);
2488 017d6da3 2020-01-29 tracey }
2489 e46f587c 2020-01-29 tracey goto done;
2490 e46f587c 2020-01-29 tracey }
2491 e46f587c 2020-01-29 tracey
2492 ec46ccd7 2020-01-15 tracey /* Don't include \n at EOF in the blame line count. */
2493 ec46ccd7 2020-01-15 tracey if (bca.line_offsets[bca.nlines - 1] == filesize)
2494 ec46ccd7 2020-01-15 tracey bca.nlines--;
2495 ec46ccd7 2020-01-15 tracey
2496 ec46ccd7 2020-01-15 tracey bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2497 ec46ccd7 2020-01-15 tracey if (bca.lines == NULL) {
2498 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("calloc");
2499 ec46ccd7 2020-01-15 tracey goto done;
2500 ec46ccd7 2020-01-15 tracey }
2501 ec46ccd7 2020-01-15 tracey bca.lineno_cur = 1;
2502 ec46ccd7 2020-01-15 tracey bca.nlines_prec = 0;
2503 ec46ccd7 2020-01-15 tracey i = bca.nlines;
2504 ec46ccd7 2020-01-15 tracey while (i > 0) {
2505 ec46ccd7 2020-01-15 tracey i /= 10;
2506 ec46ccd7 2020-01-15 tracey bca.nlines_prec++;
2507 ec46ccd7 2020-01-15 tracey }
2508 ec46ccd7 2020-01-15 tracey bca.repo = repo;
2509 2e676fc5 2020-01-15 tracey bca.gw_trans = gw_trans;
2510 ec46ccd7 2020-01-15 tracey
2511 147269d5 2020-01-15 tracey error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2512 147269d5 2020-01-15 tracey NULL, NULL);
2513 e46f587c 2020-01-29 tracey if (error)
2514 e46f587c 2020-01-29 tracey goto done;
2515 2e676fc5 2020-01-15 tracey if (buf_len(bca.blamebuf) > 0) {
2516 2e676fc5 2020-01-15 tracey error = buf_putc(bca.blamebuf, '\0');
2517 2e676fc5 2020-01-15 tracey blame_html = strdup(buf_get(bca.blamebuf));
2518 2e676fc5 2020-01-15 tracey }
2519 ec46ccd7 2020-01-15 tracey done:
2520 e46f587c 2020-01-29 tracey free(bca.line_offsets);
2521 2e676fc5 2020-01-15 tracey free(bca.blamebuf);
2522 2e676fc5 2020-01-15 tracey free(in_repo_path);
2523 2e676fc5 2020-01-15 tracey free(commit_id);
2524 2e676fc5 2020-01-15 tracey free(obj_id);
2525 2e676fc5 2020-01-15 tracey free(path);
2526 2e676fc5 2020-01-15 tracey
2527 e46f587c 2020-01-29 tracey if (gw_trans->action != GW_BLOB && bca.lines) {
2528 2e676fc5 2020-01-15 tracey for (i = 0; i < bca.nlines; i++) {
2529 2e676fc5 2020-01-15 tracey struct blame_line *bline = &bca.lines[i];
2530 2e676fc5 2020-01-15 tracey free(bline->id_str);
2531 2e676fc5 2020-01-15 tracey free(bline->committer);
2532 2e676fc5 2020-01-15 tracey }
2533 2e676fc5 2020-01-15 tracey free(bca.lines);
2534 2e676fc5 2020-01-15 tracey }
2535 e46f587c 2020-01-29 tracey if (error)
2536 e46f587c 2020-01-29 tracey return NULL;
2537 2e676fc5 2020-01-15 tracey if (bca.f && fclose(bca.f) == EOF && error == NULL)
2538 e46f587c 2020-01-29 tracey return NULL;
2539 e46f587c 2020-01-29 tracey if (blob)
2540 e46f587c 2020-01-29 tracey error = got_object_blob_close(blob);
2541 2e676fc5 2020-01-15 tracey if (error)
2542 2e676fc5 2020-01-15 tracey return NULL;
2543 e46f587c 2020-01-29 tracey if (repo)
2544 e46f587c 2020-01-29 tracey error = got_repo_close(repo);
2545 e46f587c 2020-01-29 tracey if (error)
2546 e46f587c 2020-01-29 tracey return NULL;
2547 ec46ccd7 2020-01-15 tracey else
2548 ec46ccd7 2020-01-15 tracey return blame_html;
2549 ec46ccd7 2020-01-15 tracey }
2550 ec46ccd7 2020-01-15 tracey
2551 ec46ccd7 2020-01-15 tracey static char*
2552 f2f46662 2020-01-23 tracey gw_get_repo_tree(struct gw_trans *gw_trans)
2553 ec46ccd7 2020-01-15 tracey {
2554 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
2555 ec46ccd7 2020-01-15 tracey struct got_repository *repo = NULL;
2556 bcbc97d8 2020-01-15 tracey struct got_object_id *tree_id = NULL, *commit_id = NULL;
2557 bcbc97d8 2020-01-15 tracey struct got_tree_object *tree = NULL;
2558 bcbc97d8 2020-01-15 tracey struct buf *diffbuf = NULL;
2559 bcbc97d8 2020-01-15 tracey size_t newsize;
2560 bcbc97d8 2020-01-15 tracey char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2561 c3bcdfd5 2020-01-29 tracey *tree_row = NULL, *id_str, *class = NULL;
2562 c3bcdfd5 2020-01-29 tracey int nentries, i, class_flip = 0;
2563 8d4d2453 2020-01-15 tracey
2564 bcbc97d8 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2565 ec46ccd7 2020-01-15 tracey if (error)
2566 bcbc97d8 2020-01-15 tracey return NULL;
2567 bcbc97d8 2020-01-15 tracey
2568 bcbc97d8 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2569 ec46ccd7 2020-01-15 tracey if (error)
2570 bcbc97d8 2020-01-15 tracey goto done;
2571 bcbc97d8 2020-01-15 tracey
2572 bcbc97d8 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2573 ec46ccd7 2020-01-15 tracey if (error)
2574 bcbc97d8 2020-01-15 tracey goto done;
2575 bcbc97d8 2020-01-15 tracey
2576 ec46ccd7 2020-01-15 tracey if (gw_trans->repo_folder != NULL)
2577 ec46ccd7 2020-01-15 tracey path = strdup(gw_trans->repo_folder);
2578 ec46ccd7 2020-01-15 tracey else if (in_repo_path) {
2579 bcbc97d8 2020-01-15 tracey free(path);
2580 bcbc97d8 2020-01-15 tracey path = in_repo_path;
2581 bcbc97d8 2020-01-15 tracey }
2582 bcbc97d8 2020-01-15 tracey
2583 f2f46662 2020-01-23 tracey if (gw_trans->commit == NULL) {
2584 ec46ccd7 2020-01-15 tracey struct got_reference *head_ref;
2585 ec46ccd7 2020-01-15 tracey error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2586 ec46ccd7 2020-01-15 tracey if (error)
2587 ec46ccd7 2020-01-15 tracey goto done;
2588 ec46ccd7 2020-01-15 tracey
2589 ec46ccd7 2020-01-15 tracey error = got_ref_resolve(&commit_id, repo, head_ref);
2590 ec46ccd7 2020-01-15 tracey got_ref_close(head_ref);
2591 ec46ccd7 2020-01-15 tracey
2592 ec46ccd7 2020-01-15 tracey } else
2593 f2f46662 2020-01-23 tracey error = got_repo_match_object_id(&commit_id, NULL,
2594 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2595 f2f46662 2020-01-23 tracey if (error)
2596 f2f46662 2020-01-23 tracey goto done;
2597 f2f46662 2020-01-23 tracey
2598 f2f46662 2020-01-23 tracey error = got_object_id_str(&gw_trans->commit, commit_id);
2599 bcbc97d8 2020-01-15 tracey if (error)
2600 bcbc97d8 2020-01-15 tracey goto done;
2601 bcbc97d8 2020-01-15 tracey
2602 ec46ccd7 2020-01-15 tracey error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2603 bcbc97d8 2020-01-15 tracey if (error)
2604 bcbc97d8 2020-01-15 tracey goto done;
2605 bcbc97d8 2020-01-15 tracey
2606 bcbc97d8 2020-01-15 tracey error = got_object_open_as_tree(&tree, repo, tree_id);
2607 bcbc97d8 2020-01-15 tracey if (error)
2608 bcbc97d8 2020-01-15 tracey goto done;
2609 bcbc97d8 2020-01-15 tracey
2610 bcbc97d8 2020-01-15 tracey nentries = got_object_tree_get_nentries(tree);
2611 bcbc97d8 2020-01-15 tracey
2612 bcbc97d8 2020-01-15 tracey for (i = 0; i < nentries; i++) {
2613 bcbc97d8 2020-01-15 tracey struct got_tree_entry *te;
2614 ec46ccd7 2020-01-15 tracey const char *modestr = "";
2615 ec46ccd7 2020-01-15 tracey char *id = NULL, *url_html = NULL;
2616 bcbc97d8 2020-01-15 tracey
2617 bcbc97d8 2020-01-15 tracey te = got_object_tree_get_entry(tree, i);
2618 bcbc97d8 2020-01-15 tracey
2619 bcbc97d8 2020-01-15 tracey error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2620 bcbc97d8 2020-01-15 tracey if (error)
2621 bcbc97d8 2020-01-15 tracey goto done;
2622 bcbc97d8 2020-01-15 tracey
2623 88d59c36 2020-01-29 stsp if (asprintf(&id, "%s", id_str) == -1) {
2624 bcbc97d8 2020-01-15 tracey error = got_error_from_errno("asprintf");
2625 bcbc97d8 2020-01-15 tracey free(id_str);
2626 bcbc97d8 2020-01-15 tracey goto done;
2627 bcbc97d8 2020-01-15 tracey }
2628 bcbc97d8 2020-01-15 tracey
2629 bcbc97d8 2020-01-15 tracey mode_t mode = got_tree_entry_get_mode(te);
2630 bcbc97d8 2020-01-15 tracey
2631 bcbc97d8 2020-01-15 tracey if (got_object_tree_entry_is_submodule(te))
2632 bcbc97d8 2020-01-15 tracey modestr = "$";
2633 bcbc97d8 2020-01-15 tracey else if (S_ISLNK(mode))
2634 bcbc97d8 2020-01-15 tracey modestr = "@";
2635 bcbc97d8 2020-01-15 tracey else if (S_ISDIR(mode))
2636 bcbc97d8 2020-01-15 tracey modestr = "/";
2637 bcbc97d8 2020-01-15 tracey else if (mode & S_IXUSR)
2638 bcbc97d8 2020-01-15 tracey modestr = "*";
2639 c3bcdfd5 2020-01-29 tracey
2640 c3bcdfd5 2020-01-29 tracey if (class_flip == 0) {
2641 c3bcdfd5 2020-01-29 tracey class = strdup("back_lightgray");
2642 c3bcdfd5 2020-01-29 tracey class_flip = 1;
2643 c3bcdfd5 2020-01-29 tracey } else {
2644 c3bcdfd5 2020-01-29 tracey class = strdup("back_white");
2645 c3bcdfd5 2020-01-29 tracey class_flip = 0;
2646 c3bcdfd5 2020-01-29 tracey }
2647 bcbc97d8 2020-01-15 tracey
2648 ec46ccd7 2020-01-15 tracey char *build_folder = NULL;
2649 ec46ccd7 2020-01-15 tracey if (S_ISDIR(got_tree_entry_get_mode(te))) {
2650 ec46ccd7 2020-01-15 tracey if (gw_trans->repo_folder != NULL) {
2651 88d59c36 2020-01-29 stsp if (asprintf(&build_folder, "%s/%s",
2652 ec46ccd7 2020-01-15 tracey gw_trans->repo_folder,
2653 88d59c36 2020-01-29 stsp got_tree_entry_get_name(te)) == -1) {
2654 ec46ccd7 2020-01-15 tracey error =
2655 ec46ccd7 2020-01-15 tracey got_error_from_errno("asprintf");
2656 ec46ccd7 2020-01-15 tracey goto done;
2657 ec46ccd7 2020-01-15 tracey }
2658 ec46ccd7 2020-01-15 tracey } else {
2659 ec46ccd7 2020-01-15 tracey if (asprintf(&build_folder, "%s",
2660 ec46ccd7 2020-01-15 tracey got_tree_entry_get_name(te)) == -1)
2661 ec46ccd7 2020-01-15 tracey goto done;
2662 ec46ccd7 2020-01-15 tracey }
2663 ec46ccd7 2020-01-15 tracey
2664 88d59c36 2020-01-29 stsp if (asprintf(&url_html, folder_html,
2665 ec46ccd7 2020-01-15 tracey gw_trans->repo_name, gw_trans->action_name,
2666 ec46ccd7 2020-01-15 tracey gw_trans->commit, build_folder,
2667 88d59c36 2020-01-29 stsp got_tree_entry_get_name(te), modestr) == -1) {
2668 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
2669 ec46ccd7 2020-01-15 tracey goto done;
2670 ec46ccd7 2020-01-15 tracey }
2671 017d6da3 2020-01-29 tracey
2672 e46f587c 2020-01-29 tracey if (asprintf(&tree_row, tree_line, class, url_html,
2673 e46f587c 2020-01-29 tracey class) == -1) {
2674 e46f587c 2020-01-29 tracey error = got_error_from_errno("asprintf");
2675 e46f587c 2020-01-29 tracey goto done;
2676 e46f587c 2020-01-29 tracey }
2677 e46f587c 2020-01-29 tracey
2678 ec46ccd7 2020-01-15 tracey } else {
2679 ec46ccd7 2020-01-15 tracey if (gw_trans->repo_folder != NULL) {
2680 88d59c36 2020-01-29 stsp if (asprintf(&build_folder, "%s",
2681 88d59c36 2020-01-29 stsp gw_trans->repo_folder) == -1) {
2682 ec46ccd7 2020-01-15 tracey error =
2683 ec46ccd7 2020-01-15 tracey got_error_from_errno("asprintf");
2684 ec46ccd7 2020-01-15 tracey goto done;
2685 ec46ccd7 2020-01-15 tracey }
2686 ec46ccd7 2020-01-15 tracey } else
2687 ec46ccd7 2020-01-15 tracey build_folder = strdup("");
2688 ec46ccd7 2020-01-15 tracey
2689 88d59c36 2020-01-29 stsp if (asprintf(&url_html, file_html, gw_trans->repo_name,
2690 e46f587c 2020-01-29 tracey "blob", gw_trans->commit,
2691 ec46ccd7 2020-01-15 tracey got_tree_entry_get_name(te), build_folder,
2692 88d59c36 2020-01-29 stsp got_tree_entry_get_name(te), modestr) == -1) {
2693 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
2694 ec46ccd7 2020-01-15 tracey goto done;
2695 ec46ccd7 2020-01-15 tracey }
2696 e46f587c 2020-01-29 tracey
2697 e46f587c 2020-01-29 tracey if (asprintf(&tree_row, tree_line_with_navs, class,
2698 e46f587c 2020-01-29 tracey url_html, class, gw_trans->repo_name, "blob",
2699 e46f587c 2020-01-29 tracey gw_trans->commit, got_tree_entry_get_name(te),
2700 e46f587c 2020-01-29 tracey build_folder, "blob", gw_trans->repo_name,
2701 e46f587c 2020-01-29 tracey "blame", gw_trans->commit,
2702 e46f587c 2020-01-29 tracey got_tree_entry_get_name(te), build_folder,
2703 e46f587c 2020-01-29 tracey "blame") == -1) {
2704 e46f587c 2020-01-29 tracey error = got_error_from_errno("asprintf");
2705 e46f587c 2020-01-29 tracey goto done;
2706 e46f587c 2020-01-29 tracey }
2707 ec46ccd7 2020-01-15 tracey }
2708 ec46ccd7 2020-01-15 tracey free(build_folder);
2709 ec46ccd7 2020-01-15 tracey
2710 ec46ccd7 2020-01-15 tracey if (error)
2711 ec46ccd7 2020-01-15 tracey goto done;
2712 ec46ccd7 2020-01-15 tracey
2713 bcbc97d8 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, tree_row);
2714 ec46ccd7 2020-01-15 tracey if (error)
2715 ec46ccd7 2020-01-15 tracey goto done;
2716 ec46ccd7 2020-01-15 tracey
2717 bcbc97d8 2020-01-15 tracey free(id);
2718 bcbc97d8 2020-01-15 tracey free(id_str);
2719 ec46ccd7 2020-01-15 tracey free(url_html);
2720 ec46ccd7 2020-01-15 tracey free(tree_row);
2721 bcbc97d8 2020-01-15 tracey }
2722 bcbc97d8 2020-01-15 tracey
2723 bcbc97d8 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2724 bcbc97d8 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2725 bcbc97d8 2020-01-15 tracey tree_html = strdup(buf_get(diffbuf));
2726 bcbc97d8 2020-01-15 tracey }
2727 bcbc97d8 2020-01-15 tracey done:
2728 bcbc97d8 2020-01-15 tracey if (tree)
2729 bcbc97d8 2020-01-15 tracey got_object_tree_close(tree);
2730 2e676fc5 2020-01-15 tracey if (repo)
2731 2e676fc5 2020-01-15 tracey got_repo_close(repo);
2732 bcbc97d8 2020-01-15 tracey
2733 2e676fc5 2020-01-15 tracey free(in_repo_path);
2734 bcbc97d8 2020-01-15 tracey free(tree_id);
2735 bcbc97d8 2020-01-15 tracey free(diffbuf);
2736 bcbc97d8 2020-01-15 tracey if (error)
2737 bcbc97d8 2020-01-15 tracey return NULL;
2738 bcbc97d8 2020-01-15 tracey else
2739 bcbc97d8 2020-01-15 tracey return tree_html;
2740 bcbc97d8 2020-01-15 tracey }
2741 bcbc97d8 2020-01-15 tracey
2742 8d4d2453 2020-01-15 tracey static char *
2743 54415d85 2020-01-15 tracey gw_get_repo_heads(struct gw_trans *gw_trans)
2744 8d4d2453 2020-01-15 tracey {
2745 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
2746 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
2747 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
2748 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
2749 87f9ebf5 2020-01-15 tracey char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2750 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
2751 87f9ebf5 2020-01-15 tracey size_t newsize;
2752 a0f36e03 2020-01-28 stsp
2753 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
2754 8d4d2453 2020-01-15 tracey
2755 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2756 ec46ccd7 2020-01-15 tracey if (error)
2757 6c6c85af 2020-01-15 tracey return NULL;
2758 87f9ebf5 2020-01-15 tracey
2759 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2760 ec46ccd7 2020-01-15 tracey if (error)
2761 87f9ebf5 2020-01-15 tracey goto done;
2762 87f9ebf5 2020-01-15 tracey
2763 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2764 87f9ebf5 2020-01-15 tracey NULL);
2765 87f9ebf5 2020-01-15 tracey if (error)
2766 87f9ebf5 2020-01-15 tracey goto done;
2767 87f9ebf5 2020-01-15 tracey
2768 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
2769 87f9ebf5 2020-01-15 tracey char *refname;
2770 87f9ebf5 2020-01-15 tracey
2771 87f9ebf5 2020-01-15 tracey refname = strdup(got_ref_get_name(re->ref));
2772 87f9ebf5 2020-01-15 tracey if (refname == NULL) {
2773 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
2774 87f9ebf5 2020-01-15 tracey goto done;
2775 87f9ebf5 2020-01-15 tracey }
2776 87f9ebf5 2020-01-15 tracey
2777 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) != 0) {
2778 87f9ebf5 2020-01-15 tracey free(refname);
2779 87f9ebf5 2020-01-15 tracey continue;
2780 87f9ebf5 2020-01-15 tracey }
2781 87f9ebf5 2020-01-15 tracey
2782 017d6da3 2020-01-29 tracey error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
2783 017d6da3 2020-01-29 tracey refname, TM_DIFF);
2784 d126c1b7 2020-01-29 stsp if (error)
2785 d126c1b7 2020-01-29 stsp goto done;
2786 87f9ebf5 2020-01-15 tracey
2787 88d59c36 2020-01-29 stsp if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2788 87f9ebf5 2020-01-15 tracey refname, gw_trans->repo_name, refname,
2789 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, refname, gw_trans->repo_name,
2790 88d59c36 2020-01-29 stsp refname) == -1) {
2791 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2792 87f9ebf5 2020-01-15 tracey goto done;
2793 87f9ebf5 2020-01-15 tracey }
2794 87f9ebf5 2020-01-15 tracey
2795 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) == 0)
2796 87f9ebf5 2020-01-15 tracey refname += 11;
2797 87f9ebf5 2020-01-15 tracey
2798 88d59c36 2020-01-29 stsp if (asprintf(&head_row, heads_row, age, refname,
2799 88d59c36 2020-01-29 stsp head_navs_disp) == -1) {
2800 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2801 87f9ebf5 2020-01-15 tracey goto done;
2802 87f9ebf5 2020-01-15 tracey }
2803 87f9ebf5 2020-01-15 tracey
2804 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, head_row);
2805 87f9ebf5 2020-01-15 tracey
2806 87f9ebf5 2020-01-15 tracey free(head_navs_disp);
2807 87f9ebf5 2020-01-15 tracey free(head_row);
2808 87f9ebf5 2020-01-15 tracey }
2809 87f9ebf5 2020-01-15 tracey
2810 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2811 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2812 6c6c85af 2020-01-15 tracey heads = strdup(buf_get(diffbuf));
2813 6c6c85af 2020-01-15 tracey }
2814 87f9ebf5 2020-01-15 tracey done:
2815 87f9ebf5 2020-01-15 tracey buf_free(diffbuf);
2816 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
2817 87f9ebf5 2020-01-15 tracey if (repo)
2818 87f9ebf5 2020-01-15 tracey got_repo_close(repo);
2819 87f9ebf5 2020-01-15 tracey if (error)
2820 87f9ebf5 2020-01-15 tracey return NULL;
2821 87f9ebf5 2020-01-15 tracey else
2822 87f9ebf5 2020-01-15 tracey return heads;
2823 8d4d2453 2020-01-15 tracey }
2824 8d4d2453 2020-01-15 tracey
2825 8d4d2453 2020-01-15 tracey static char *
2826 54415d85 2020-01-15 tracey gw_get_got_link(struct gw_trans *gw_trans)
2827 2c251c14 2020-01-15 tracey {
2828 2c251c14 2020-01-15 tracey char *link;
2829 2c251c14 2020-01-15 tracey
2830 88d59c36 2020-01-29 stsp if (asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2831 88d59c36 2020-01-29 stsp gw_trans->gw_conf->got_logo) == -1)
2832 2c251c14 2020-01-15 tracey return NULL;
2833 2c251c14 2020-01-15 tracey
2834 2c251c14 2020-01-15 tracey return link;
2835 2c251c14 2020-01-15 tracey }
2836 2c251c14 2020-01-15 tracey
2837 2c251c14 2020-01-15 tracey static char *
2838 54415d85 2020-01-15 tracey gw_get_site_link(struct gw_trans *gw_trans)
2839 2c251c14 2020-01-15 tracey {
2840 2c251c14 2020-01-15 tracey char *link, *repo = "", *action = "";
2841 2c251c14 2020-01-15 tracey
2842 2c251c14 2020-01-15 tracey if (gw_trans->repo_name != NULL)
2843 88d59c36 2020-01-29 stsp if (asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2844 88d59c36 2020-01-29 stsp "</a>", gw_trans->repo_name, gw_trans->repo_name) == -1)
2845 2c251c14 2020-01-15 tracey return NULL;
2846 2c251c14 2020-01-15 tracey
2847 2c251c14 2020-01-15 tracey if (gw_trans->action_name != NULL)
2848 88d59c36 2020-01-29 stsp if (asprintf(&action, " / %s", gw_trans->action_name) == -1)
2849 2c251c14 2020-01-15 tracey return NULL;
2850 2c251c14 2020-01-15 tracey
2851 88d59c36 2020-01-29 stsp if (asprintf(&link, site_link, GOTWEB,
2852 88d59c36 2020-01-29 stsp gw_trans->gw_conf->got_site_link, repo, action) == -1)
2853 2c251c14 2020-01-15 tracey return NULL;
2854 2c251c14 2020-01-15 tracey
2855 2c251c14 2020-01-15 tracey return link;
2856 2c251c14 2020-01-15 tracey }
2857 2c251c14 2020-01-15 tracey
2858 2c251c14 2020-01-15 tracey static char *
2859 d0ea9c5b 2020-01-15 tracey gw_colordiff_line(char *buf)
2860 ec46ccd7 2020-01-15 tracey {
2861 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
2862 ec46ccd7 2020-01-15 tracey char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2863 ec46ccd7 2020-01-15 tracey struct buf *diffbuf = NULL;
2864 ec46ccd7 2020-01-15 tracey size_t newsize;
2865 ec46ccd7 2020-01-15 tracey
2866 ec46ccd7 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2867 ec46ccd7 2020-01-15 tracey if (error)
2868 ec46ccd7 2020-01-15 tracey return NULL;
2869 ec46ccd7 2020-01-15 tracey
2870 bfd30182 2020-01-24 tracey if (buf == NULL)
2871 bfd30182 2020-01-24 tracey return NULL;
2872 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "-", 1) == 0)
2873 ec46ccd7 2020-01-15 tracey color = "diff_minus";
2874 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "+", 1) == 0)
2875 ec46ccd7 2020-01-15 tracey color = "diff_plus";
2876 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "@@", 2) == 0)
2877 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
2878 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "@@", 2) == 0)
2879 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
2880 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "commit +", 8) == 0)
2881 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2882 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "commit -", 8) == 0)
2883 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2884 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "blob +", 6) == 0)
2885 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2886 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "blob -", 6) == 0)
2887 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2888 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "file +", 6) == 0)
2889 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2890 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "file -", 6) == 0)
2891 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2892 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "from:", 5) == 0)
2893 ec46ccd7 2020-01-15 tracey color = "diff_author";
2894 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "via:", 4) == 0)
2895 ec46ccd7 2020-01-15 tracey color = "diff_author";
2896 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "date:", 5) == 0)
2897 ec46ccd7 2020-01-15 tracey color = "diff_date";
2898 ec46ccd7 2020-01-15 tracey
2899 88d59c36 2020-01-29 stsp if (asprintf(&div_diff_line_div, div_diff_line, color) == -1)
2900 ec46ccd7 2020-01-15 tracey return NULL;
2901 ec46ccd7 2020-01-15 tracey
2902 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2903 ec46ccd7 2020-01-15 tracey if (error)
2904 ec46ccd7 2020-01-15 tracey return NULL;
2905 ec46ccd7 2020-01-15 tracey
2906 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, buf);
2907 ec46ccd7 2020-01-15 tracey if (error)
2908 ec46ccd7 2020-01-15 tracey return NULL;
2909 ec46ccd7 2020-01-15 tracey
2910 ec46ccd7 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2911 ec46ccd7 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2912 ec46ccd7 2020-01-15 tracey colorized_line = strdup(buf_get(diffbuf));
2913 ec46ccd7 2020-01-15 tracey }
2914 ec46ccd7 2020-01-15 tracey
2915 ec46ccd7 2020-01-15 tracey free(diffbuf);
2916 ec46ccd7 2020-01-15 tracey free(div_diff_line_div);
2917 ec46ccd7 2020-01-15 tracey return colorized_line;
2918 ec46ccd7 2020-01-15 tracey }
2919 ec46ccd7 2020-01-15 tracey
2920 ec46ccd7 2020-01-15 tracey static char *
2921 2c251c14 2020-01-15 tracey gw_html_escape(const char *html)
2922 2c251c14 2020-01-15 tracey {
2923 2c251c14 2020-01-15 tracey char *escaped_str = NULL, *buf;
2924 2c251c14 2020-01-15 tracey char c[1];
2925 6c6c85af 2020-01-15 tracey size_t sz, i, buff_sz = 2048;
2926 2c251c14 2020-01-15 tracey
2927 6c6c85af 2020-01-15 tracey if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2928 2c251c14 2020-01-15 tracey return NULL;
2929 2c251c14 2020-01-15 tracey
2930 2c251c14 2020-01-15 tracey if (html == NULL)
2931 2c251c14 2020-01-15 tracey return NULL;
2932 2c251c14 2020-01-15 tracey else
2933 2c251c14 2020-01-15 tracey if ((sz = strlen(html)) == 0)
2934 2c251c14 2020-01-15 tracey return NULL;
2935 2c251c14 2020-01-15 tracey
2936 6c6c85af 2020-01-15 tracey /* only work with buff_sz */
2937 6c6c85af 2020-01-15 tracey if (buff_sz < sz)
2938 6c6c85af 2020-01-15 tracey sz = buff_sz;
2939 2c251c14 2020-01-15 tracey
2940 2c251c14 2020-01-15 tracey for (i = 0; i < sz; i++) {
2941 2c251c14 2020-01-15 tracey c[0] = html[i];
2942 2c251c14 2020-01-15 tracey switch (c[0]) {
2943 2c251c14 2020-01-15 tracey case ('>'):
2944 2c251c14 2020-01-15 tracey strcat(buf, "&gt;");
2945 2c251c14 2020-01-15 tracey break;
2946 2c251c14 2020-01-15 tracey case ('&'):
2947 2c251c14 2020-01-15 tracey strcat(buf, "&amp;");
2948 2c251c14 2020-01-15 tracey break;
2949 2c251c14 2020-01-15 tracey case ('<'):
2950 2c251c14 2020-01-15 tracey strcat(buf, "&lt;");
2951 2c251c14 2020-01-15 tracey break;
2952 2c251c14 2020-01-15 tracey case ('"'):
2953 2c251c14 2020-01-15 tracey strcat(buf, "&quot;");
2954 2c251c14 2020-01-15 tracey break;
2955 2c251c14 2020-01-15 tracey case ('\''):
2956 2c251c14 2020-01-15 tracey strcat(buf, "&apos;");
2957 2c251c14 2020-01-15 tracey break;
2958 2c251c14 2020-01-15 tracey case ('\n'):
2959 2c251c14 2020-01-15 tracey strcat(buf, "<br />");
2960 2c251c14 2020-01-15 tracey default:
2961 2c251c14 2020-01-15 tracey strcat(buf, &c[0]);
2962 2c251c14 2020-01-15 tracey break;
2963 2c251c14 2020-01-15 tracey }
2964 2c251c14 2020-01-15 tracey }
2965 2c251c14 2020-01-15 tracey asprintf(&escaped_str, "%s", buf);
2966 2c251c14 2020-01-15 tracey free(buf);
2967 2c251c14 2020-01-15 tracey return escaped_str;
2968 2c251c14 2020-01-15 tracey }
2969 2c251c14 2020-01-15 tracey
2970 2c251c14 2020-01-15 tracey int
2971 c08369d7 2020-01-15 tracey main(int argc, char *argv[])
2972 2c251c14 2020-01-15 tracey {
2973 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
2974 54415d85 2020-01-15 tracey struct gw_trans *gw_trans;
2975 2c251c14 2020-01-15 tracey struct gw_dir *dir = NULL, *tdir;
2976 2c251c14 2020-01-15 tracey const char *page = "index";
2977 4ceb8155 2020-01-15 tracey int gw_malloc = 1;
2978 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
2979 2c251c14 2020-01-15 tracey
2980 54415d85 2020-01-15 tracey if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2981 2c251c14 2020-01-15 tracey errx(1, "malloc");
2982 2c251c14 2020-01-15 tracey
2983 2c251c14 2020-01-15 tracey if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2984 2c251c14 2020-01-15 tracey errx(1, "malloc");
2985 2c251c14 2020-01-15 tracey
2986 2c251c14 2020-01-15 tracey if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2987 2c251c14 2020-01-15 tracey errx(1, "malloc");
2988 2c251c14 2020-01-15 tracey
2989 2c251c14 2020-01-15 tracey if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2990 2c251c14 2020-01-15 tracey errx(1, "malloc");
2991 2c251c14 2020-01-15 tracey
2992 c25c2314 2020-01-28 stsp kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2993 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK) {
2994 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
2995 c119608e 2020-01-28 stsp goto done;
2996 c25c2314 2020-01-28 stsp }
2997 2c251c14 2020-01-15 tracey
2998 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf =
2999 2c251c14 2020-01-15 tracey malloc(sizeof(struct gotweb_conf))) == NULL) {
3000 4ceb8155 2020-01-15 tracey gw_malloc = 0;
3001 387a29ba 2020-01-15 tracey error = got_error_from_errno("malloc");
3002 c119608e 2020-01-28 stsp goto done;
3003 46b9c89b 2020-01-15 tracey }
3004 46b9c89b 2020-01-15 tracey
3005 2c251c14 2020-01-15 tracey TAILQ_INIT(&gw_trans->gw_dirs);
3006 f2f46662 2020-01-23 tracey TAILQ_INIT(&gw_trans->gw_headers);
3007 2c251c14 2020-01-15 tracey
3008 2c251c14 2020-01-15 tracey gw_trans->page = 0;
3009 2c251c14 2020-01-15 tracey gw_trans->repos_total = 0;
3010 2c251c14 2020-01-15 tracey gw_trans->repo_path = NULL;
3011 2c251c14 2020-01-15 tracey gw_trans->commit = NULL;
3012 8087c3c5 2020-01-15 tracey gw_trans->headref = strdup(GOT_REF_HEAD);
3013 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_HTML;
3014 54415d85 2020-01-15 tracey gw_trans->gw_tmpl->key = gw_templs;
3015 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3016 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->arg = gw_trans;
3017 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->cb = gw_template;
3018 2c251c14 2020-01-15 tracey error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3019 c119608e 2020-01-28 stsp if (error)
3020 2c251c14 2020-01-15 tracey goto done;
3021 2c251c14 2020-01-15 tracey
3022 2c251c14 2020-01-15 tracey error = gw_parse_querystring(gw_trans);
3023 2c251c14 2020-01-15 tracey if (error)
3024 c119608e 2020-01-28 stsp goto done;
3025 2c251c14 2020-01-15 tracey
3026 017d6da3 2020-01-29 tracey if (gw_trans->action == GW_BLOB)
3027 017d6da3 2020-01-29 tracey error = gw_blob(gw_trans);
3028 017d6da3 2020-01-29 tracey else
3029 017d6da3 2020-01-29 tracey error = gw_display_index(gw_trans);
3030 2c251c14 2020-01-15 tracey done:
3031 c119608e 2020-01-28 stsp if (error) {
3032 c119608e 2020-01-28 stsp gw_trans->mime = KMIME_TEXT_PLAIN;
3033 c119608e 2020-01-28 stsp gw_trans->action = GW_ERR;
3034 6d8d8a26 2020-01-29 stsp gw_display_error(gw_trans, error);
3035 c119608e 2020-01-28 stsp }
3036 2c251c14 2020-01-15 tracey if (gw_malloc) {
3037 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_repos_path);
3038 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_name);
3039 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_owner);
3040 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_link);
3041 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo);
3042 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo_url);
3043 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf);
3044 2c251c14 2020-01-15 tracey free(gw_trans->commit);
3045 2c251c14 2020-01-15 tracey free(gw_trans->repo_path);
3046 2c251c14 2020-01-15 tracey free(gw_trans->repo_name);
3047 2c251c14 2020-01-15 tracey free(gw_trans->repo_file);
3048 2c251c14 2020-01-15 tracey free(gw_trans->action_name);
3049 8087c3c5 2020-01-15 tracey free(gw_trans->headref);
3050 2c251c14 2020-01-15 tracey
3051 2c251c14 2020-01-15 tracey TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3052 2c251c14 2020-01-15 tracey free(dir->name);
3053 2c251c14 2020-01-15 tracey free(dir->description);
3054 2c251c14 2020-01-15 tracey free(dir->age);
3055 2c251c14 2020-01-15 tracey free(dir->url);
3056 2c251c14 2020-01-15 tracey free(dir->path);
3057 2c251c14 2020-01-15 tracey free(dir);
3058 2c251c14 2020-01-15 tracey }
3059 2c251c14 2020-01-15 tracey
3060 2c251c14 2020-01-15 tracey }
3061 2c251c14 2020-01-15 tracey
3062 2c251c14 2020-01-15 tracey khttp_free(gw_trans->gw_req);
3063 d56b34ca 2020-01-29 stsp return 0;
3064 2c251c14 2020-01-15 tracey }