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