diff options
Diffstat (limited to 'ui-shared.c')
| -rw-r--r-- | ui-shared.c | 234 |
1 files changed, 223 insertions, 11 deletions
diff --git a/ui-shared.c b/ui-shared.c index aba93e8..5c5bcf3 100644 --- a/ui-shared.c +++ b/ui-shared.c | |||
| @@ -57,13 +57,13 @@ char *cgit_repourl(const char *reponame) | |||
| 57 | } | 57 | } |
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | char *cgit_pageurl(const char *reponame, const char *pagename, | 60 | char *cgit_fileurl(const char *reponame, const char *pagename, |
| 61 | const char *query) | 61 | const char *filename, const char *query) |
| 62 | { | 62 | { |
| 63 | if (cgit_virtual_root) { | 63 | if (cgit_virtual_root) { |
| 64 | if (query) | 64 | if (query) |
| 65 | return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame, | 65 | return fmt("%s/%s/%s/%s?%s", cgit_virtual_root, reponame, |
| 66 | pagename, query); | 66 | pagename, filename?filename:"", query); |
| 67 | else | 67 | else |
| 68 | return fmt("%s/%s/%s/", cgit_virtual_root, reponame, | 68 | return fmt("%s/%s/%s/", cgit_virtual_root, reponame, |
| 69 | pagename); | 69 | pagename); |
| @@ -75,6 +75,37 @@ char *cgit_pageurl(const char *reponame, const char *pagename, | |||
| 75 | } | 75 | } |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | char *cgit_pageurl(const char *reponame, const char *pagename, | ||
| 79 | const char *query) | ||
| 80 | { | ||
| 81 | return cgit_fileurl(reponame,pagename,0,query); | ||
| 82 | } | ||
| 83 | |||
| 84 | const char *cgit_repobasename(const char *reponame) | ||
| 85 | { | ||
| 86 | /* I assume we don't need to store more than one repo basename */ | ||
| 87 | static char rvbuf[1024]; | ||
| 88 | int p; | ||
| 89 | const char *rv; | ||
| 90 | strncpy(rvbuf,reponame,sizeof(rvbuf)); | ||
| 91 | if(rvbuf[sizeof(rvbuf)-1]) | ||
| 92 | die("cgit_repobasename: truncated repository name '%s'", reponame); | ||
| 93 | p = strlen(rvbuf)-1; | ||
| 94 | /* strip trailing slashes */ | ||
| 95 | while(p && rvbuf[p]=='/') rvbuf[p--]=0; | ||
| 96 | /* strip trailing .git */ | ||
| 97 | if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { | ||
| 98 | p -= 3; rvbuf[p--] = 0; | ||
| 99 | } | ||
| 100 | /* strip more trailing slashes if any */ | ||
| 101 | while( p && rvbuf[p]=='/') rvbuf[p--]=0; | ||
| 102 | /* find last slash in the remaining string */ | ||
| 103 | rv = strrchr(rvbuf,'/'); | ||
| 104 | if(rv) | ||
| 105 | return ++rv; | ||
| 106 | return rvbuf; | ||
| 107 | } | ||
| 108 | |||
| 78 | char *cgit_currurl() | 109 | char *cgit_currurl() |
| 79 | { | 110 | { |
| 80 | if (!cgit_virtual_root) | 111 | if (!cgit_virtual_root) |
| @@ -87,6 +118,166 @@ char *cgit_currurl() | |||
| 87 | return fmt("%s/", cgit_virtual_root); | 118 | return fmt("%s/", cgit_virtual_root); |
| 88 | } | 119 | } |
| 89 | 120 | ||
| 121 | static char *repolink(char *title, char *class, char *page, char *head, | ||
| 122 | char *path) | ||
| 123 | { | ||
| 124 | char *delim = "?"; | ||
| 125 | |||
| 126 | html("<a"); | ||
| 127 | if (title) { | ||
| 128 | html(" title='"); | ||
| 129 | html_attr(title); | ||
| 130 | html("'"); | ||
| 131 | } | ||
| 132 | if (class) { | ||
| 133 | html(" class='"); | ||
| 134 | html_attr(class); | ||
| 135 | html("'"); | ||
| 136 | } | ||
| 137 | html(" href='"); | ||
| 138 | if (cgit_virtual_root) { | ||
| 139 | html_attr(cgit_virtual_root); | ||
| 140 | if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/') | ||
| 141 | html("/"); | ||
| 142 | html_attr(cgit_repo->url); | ||
| 143 | if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') | ||
| 144 | html("/"); | ||
| 145 | if (page) { | ||
| 146 | html(page); | ||
| 147 | html("/"); | ||
| 148 | if (path) | ||
| 149 | html_attr(path); | ||
| 150 | } | ||
| 151 | } else { | ||
| 152 | html(cgit_script_name); | ||
| 153 | html("?url="); | ||
| 154 | html_attr(cgit_repo->url); | ||
| 155 | if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') | ||
| 156 | html("/"); | ||
| 157 | if (page) { | ||
| 158 | html(page); | ||
| 159 | html("/"); | ||
| 160 | if (path) | ||
| 161 | html_attr(path); | ||
| 162 | } | ||
| 163 | delim = "&"; | ||
| 164 | } | ||
| 165 | if (head && strcmp(head, cgit_repo->defbranch)) { | ||
| 166 | html(delim); | ||
| 167 | html("h="); | ||
| 168 | html_attr(head); | ||
| 169 | delim = "&"; | ||
| 170 | } | ||
| 171 | return fmt("%s", delim); | ||
| 172 | } | ||
| 173 | |||
| 174 | static void reporevlink(char *page, char *name, char *title, char *class, | ||
| 175 | char *head, char *rev, char *path) | ||
| 176 | { | ||
| 177 | char *delim; | ||
| 178 | |||
| 179 | delim = repolink(title, class, page, head, path); | ||
| 180 | if (rev && strcmp(rev, cgit_query_head)) { | ||
| 181 | html(delim); | ||
| 182 | html("id="); | ||
| 183 | html_attr(rev); | ||
| 184 | } | ||
| 185 | html("'>"); | ||
| 186 | html_txt(name); | ||
| 187 | html("</a>"); | ||
| 188 | } | ||
| 189 | |||
| 190 | void cgit_tree_link(char *name, char *title, char *class, char *head, | ||
| 191 | char *rev, char *path) | ||
| 192 | { | ||
| 193 | reporevlink("tree", name, title, class, head, rev, path); | ||
| 194 | } | ||
| 195 | |||
| 196 | void cgit_log_link(char *name, char *title, char *class, char *head, | ||
| 197 | char *rev, char *path, int ofs) | ||
| 198 | { | ||
| 199 | char *delim; | ||
| 200 | |||
| 201 | delim = repolink(title, class, "log", head, path); | ||
| 202 | if (rev && strcmp(rev, cgit_query_head)) { | ||
| 203 | html(delim); | ||
| 204 | html("id="); | ||
| 205 | html_attr(rev); | ||
| 206 | delim = "&"; | ||
| 207 | } | ||
| 208 | if (ofs > 0) { | ||
| 209 | html(delim); | ||
| 210 | html("ofs="); | ||
| 211 | htmlf("%d", ofs); | ||
| 212 | } | ||
| 213 | html("'>"); | ||
| 214 | html_txt(name); | ||
| 215 | html("</a>"); | ||
| 216 | } | ||
| 217 | |||
| 218 | void cgit_commit_link(char *name, char *title, char *class, char *head, | ||
| 219 | char *rev) | ||
| 220 | { | ||
| 221 | if (strlen(name) > cgit_max_msg_len && cgit_max_msg_len >= 15) { | ||
| 222 | name[cgit_max_msg_len] = '\0'; | ||
| 223 | name[cgit_max_msg_len - 1] = '.'; | ||
| 224 | name[cgit_max_msg_len - 2] = '.'; | ||
| 225 | name[cgit_max_msg_len - 3] = '.'; | ||
| 226 | } | ||
| 227 | reporevlink("commit", name, title, class, head, rev, NULL); | ||
| 228 | } | ||
| 229 | |||
| 230 | void cgit_snapshot_link(char *name, char *title, char *class, char *head, | ||
| 231 | char *rev, char *archivename) | ||
| 232 | { | ||
| 233 | reporevlink("snapshot", name, title, class, head, rev, archivename); | ||
| 234 | } | ||
| 235 | |||
| 236 | void cgit_diff_link(char *name, char *title, char *class, char *head, | ||
| 237 | char *new_rev, char *old_rev, char *path) | ||
| 238 | { | ||
| 239 | char *delim; | ||
| 240 | |||
| 241 | delim = repolink(title, class, "diff", head, path); | ||
| 242 | if (new_rev && strcmp(new_rev, cgit_query_head)) { | ||
| 243 | html(delim); | ||
| 244 | html("id="); | ||
| 245 | html_attr(new_rev); | ||
| 246 | delim = "&"; | ||
| 247 | } | ||
| 248 | if (old_rev) { | ||
| 249 | html(delim); | ||
| 250 | html("id2="); | ||
| 251 | html_attr(old_rev); | ||
| 252 | } | ||
| 253 | html("'>"); | ||
| 254 | html_txt(name); | ||
| 255 | html("</a>"); | ||
| 256 | } | ||
| 257 | |||
| 258 | void cgit_object_link(struct object *obj) | ||
| 259 | { | ||
| 260 | char *page, *arg, *url; | ||
| 261 | |||
| 262 | if (obj->type == OBJ_COMMIT) { | ||
| 263 | cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, | ||
| 264 | cgit_query_head, sha1_to_hex(obj->sha1)); | ||
| 265 | return; | ||
| 266 | } else if (obj->type == OBJ_TREE) { | ||
| 267 | page = "tree"; | ||
| 268 | arg = "id"; | ||
| 269 | } else { | ||
| 270 | page = "blob"; | ||
| 271 | arg = "id"; | ||
| 272 | } | ||
| 273 | |||
| 274 | url = cgit_pageurl(cgit_query_repo, page, | ||
| 275 | fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); | ||
| 276 | html_link_open(url, NULL, NULL); | ||
| 277 | htmlf("%s %s", typename(obj->type), | ||
| 278 | sha1_to_hex(obj->sha1)); | ||
| 279 | html_link_close(); | ||
| 280 | } | ||
| 90 | 281 | ||
| 91 | void cgit_print_date(time_t secs, char *format) | 282 | void cgit_print_date(time_t secs, char *format) |
| 92 | { | 283 | { |
| @@ -152,7 +343,7 @@ void cgit_print_docstart(char *title, struct cacheitem *item) | |||
| 152 | html("<title>"); | 343 | html("<title>"); |
| 153 | html_txt(title); | 344 | html_txt(title); |
| 154 | html("</title>\n"); | 345 | html("</title>\n"); |
| 155 | htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version); | 346 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); |
| 156 | html("<link rel='stylesheet' type='text/css' href='"); | 347 | html("<link rel='stylesheet' type='text/css' href='"); |
| 157 | html_attr(cgit_css); | 348 | html_attr(cgit_css); |
| 158 | html("'/>\n"); | 349 | html("'/>\n"); |
| @@ -169,19 +360,38 @@ void cgit_print_docend() | |||
| 169 | void cgit_print_pageheader(char *title, int show_search) | 360 | void cgit_print_pageheader(char *title, int show_search) |
| 170 | { | 361 | { |
| 171 | html("<table id='layout'>"); | 362 | html("<table id='layout'>"); |
| 172 | html("<tr><td id='header'>"); | 363 | html("<tr><td id='header'><a href='"); |
| 173 | html(cgit_root_title); | 364 | html_attr(cgit_rooturl()); |
| 174 | html("</td><td id='logo'>"); | 365 | html("'>"); |
| 366 | html_txt(cgit_root_title); | ||
| 367 | html("</a></td><td id='logo'>"); | ||
| 175 | html("<a href='"); | 368 | html("<a href='"); |
| 176 | html_attr(cgit_logo_link); | 369 | html_attr(cgit_logo_link); |
| 177 | htmlf("'><img src='%s' alt='logo'/></a>", cgit_logo); | 370 | htmlf("'><img src='%s' alt='logo'/></a>", cgit_logo); |
| 178 | html("</td></tr>"); | 371 | html("</td></tr>"); |
| 179 | html("<tr><td id='crumb'>"); | 372 | html("<tr><td id='crumb'>"); |
| 180 | htmlf("<a href='%s'>root</a>", cgit_rooturl()); | ||
| 181 | if (cgit_query_repo) { | 373 | if (cgit_query_repo) { |
| 182 | htmlf(" : <a href='%s'>", cgit_repourl(cgit_repo->url)); | ||
| 183 | html_txt(cgit_repo->name); | 374 | html_txt(cgit_repo->name); |
| 184 | htmlf("</a> : %s", title); | 375 | html(" ("); |
| 376 | html_txt(cgit_query_head); | ||
| 377 | html(") : "); | ||
| 378 | reporevlink(NULL, "summary", NULL, NULL, cgit_query_head, | ||
| 379 | NULL, NULL); | ||
| 380 | html(" "); | ||
| 381 | cgit_log_link("log", NULL, NULL, cgit_query_head, | ||
| 382 | cgit_query_sha1, cgit_query_path, 0); | ||
| 383 | html(" "); | ||
| 384 | cgit_tree_link("tree", NULL, NULL, cgit_query_head, | ||
| 385 | cgit_query_sha1, NULL); | ||
| 386 | html(" "); | ||
| 387 | cgit_commit_link("commit", NULL, NULL, cgit_query_head, | ||
| 388 | cgit_query_sha1); | ||
| 389 | html(" "); | ||
| 390 | cgit_diff_link("diff", NULL, NULL, cgit_query_head, | ||
| 391 | cgit_query_sha1, cgit_query_sha2, | ||
| 392 | cgit_query_path); | ||
| 393 | } else { | ||
| 394 | html_txt("Index of repositories"); | ||
| 185 | } | 395 | } |
| 186 | html("</td>"); | 396 | html("</td>"); |
| 187 | html("<td id='search'>"); | 397 | html("<td id='search'>"); |
| @@ -219,3 +429,5 @@ void cgit_print_snapshot_start(const char *mimetype, const char *filename, | |||
| 219 | ttl_seconds(item->ttl))); | 429 | ttl_seconds(item->ttl))); |
| 220 | html("\n"); | 430 | html("\n"); |
| 221 | } | 431 | } |
| 432 | |||
| 433 | /* vim:set sw=8: */ | ||
