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