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