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