/* * The repository index, which is the page a cgit site opens on, and the site * readme the about page falls back to when a request names no repository. * Every repository cgitrc registered is a candidate row, cut down to those * matching the search terms and the url prefix the request carried, ordered * either by the column the reader asked for or by section, and split into * pages of max-repo-count rows. */ #include "cgit.h" #include "filter.h" #include "html.h" #include "shared.h" #include "ui-repolist.h" #include "ui-shared.h" // A section heading spans the whole table, so this has to stay in step with // the headings print_header_row emits. Name, description and idle are always // there, owner and links are added when cgitrc asks for them. #define BASE_COLUMNS 3 struct sort_column { const char *name; int (*cmp)(const void *a, const void *b); }; static time_t read_agefile(const char *path) { time_t result; size_t size; char *buf = NULL; struct strbuf date = STRBUF_INIT; if (cgit_read_first_line(path, &buf, &size)) { free(buf); return 0; } // parse_date writes the timestamp followed by its timezone offset, so // only the seconds at the front are wanted here. if (parse_date(buf, &date) == 0) result = strtoul(date.buf, NULL, 10); else result = 0; free(buf); strbuf_release(&date); return result; } /* * The returned name can point into head, which the caller has to keep alive * for as long as it uses it. */ static const char *tip_branch(const struct cgit_repo *repo, struct strbuf *head) { struct strbuf path = STRBUF_INIT; const char *branch = repo->defbranch; if (branch) return branch; strbuf_addf(&path, "%s/HEAD", repo->path); if (strbuf_read_file(head, path.buf, 0) > 0) { strbuf_rtrim(head); if (!skip_prefix(head->buf, "ref: refs/heads/", &branch)) branch = NULL; // HEAD belongs to the repository, so a crafted target such as // "ref: refs/heads/../../.." must not let the caller's stat // walk outside it. Git forbids ".." in a ref name anyway. if (branch && strstr(branch, "..")) branch = NULL; } strbuf_release(&path); return branch ? branch : "master"; } static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime) { struct strbuf path = STRBUF_INIT; struct strbuf head = STRBUF_INIT; struct stat st; const char *branch; // The comparators below are handed const repositories, but the answer // is kept in the repository itself so one page stats it only once. struct cgit_repo *writable = (struct cgit_repo *)repo; if (repo->mtime != -1) { *mtime = repo->mtime; return 1; } strbuf_addf(&path, "%s/%s", repo->path, ctx.cfg.agefile); if (stat(path.buf, &st) == 0) { *mtime = read_agefile(path.buf); if (*mtime) { writable->mtime = *mtime; goto end; } } branch = tip_branch(repo, &head); strbuf_reset(&path); strbuf_addf(&path, "%s/refs/heads/%s", repo->path, branch); if (stat(path.buf, &st) == 0) { *mtime = st.st_mtime; writable->mtime = *mtime; goto end; } strbuf_reset(&path); strbuf_addf(&path, "%s/packed-refs", repo->path); if (stat(path.buf, &st) == 0) { *mtime = st.st_mtime; writable->mtime = *mtime; goto end; } *mtime = 0; writable->mtime = *mtime; end: strbuf_release(&path); strbuf_release(&head); return (writable->mtime != 0); } static void print_modtime(struct cgit_repo *repo) { time_t mtime; if (get_repo_modtime(repo, &mtime)) cgit_print_age(mtime, 0, -1); } static int matches_search(struct cgit_repo *repo) { if (!ctx.qry.search) return 1; if (repo->url && strcasestr(repo->url, ctx.qry.search)) return 1; if (repo->name && strcasestr(repo->name, ctx.qry.search)) return 1; if (repo->desc && strcasestr(repo->desc, ctx.qry.search)) return 1; if (repo->owner && strcasestr(repo->owner, ctx.qry.search)) return 1; return 0; } static int matches_url(struct cgit_repo *repo) { if (!ctx.qry.url) return 1; if (repo->url && starts_with(repo->url, ctx.qry.url)) return 1; return 0; } static int is_visible(struct cgit_repo *repo) { if (repo->hide || repo->ignore) return 0; if (!(matches_search(repo) && matches_url(repo))) return 0; return 1; } static int any_repos_visible(void) { int i; for (i = 0; i < cgit_repolist.count; i++) { if (is_visible(&cgit_repolist.repos[i])) return 1; } return 0; } // currenturl is passed in because it is the same for every heading and every // row, and working it out here would mean an allocation and a free per cell. static void print_column_header(const char *title, const char *column, const char *currenturl) { htmlf("%s", title); } static void print_header_row(const char *currenturl) { html(""); print_column_header("Name", "name", currenturl); print_column_header("Description", "desc", currenturl); if (ctx.cfg.enable_index_owner) print_column_header("Owner", "owner", currenturl); print_column_header("Idle", "idle", currenturl); if (ctx.cfg.enable_index_links) html("Links"); html("\n"); } static int section_changed(const char *section, const char *last) { if (!section && !last) return 0; if (!section || !last) return 1; return strcmp(section, last) != 0; } static void print_section_row(const char *section, int columns) { htmlf("", columns); html_txt(section); html(""); } static void print_repo_row(const char *currenturl, int sublevel) { char *repourl; htmlf("", sublevel ? "sublevel-repo" : "toplevel-repo"); cgit_summary_link(ctx.repo->name, NULL, NULL, NULL); html(""); repourl = cgit_repourl(ctx.repo->url); html_link_open(repourl, NULL, NULL); free(repourl); if (html_ntxt(ctx.repo->desc, ctx.cfg.max_repodesc_len) < 0) html("..."); html_link_close(); html(""); if (ctx.cfg.enable_index_owner) { html(""); // A repo without an owner gets an empty cell, since a link // with no text cannot be clicked or read out. if (ctx.repo->owner && *ctx.repo->owner) { html(""); html_txt(ctx.repo->owner); html(""); } html(""); } html(""); print_modtime(ctx.repo); html(""); if (ctx.cfg.enable_index_links) { html(""); cgit_summary_link("summary", NULL, "button", NULL); cgit_log_link("log", NULL, "button", NULL, NULL, NULL, 0, NULL, NULL, ctx.qry.showmsg, 0); cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL); html(""); } html("\n"); } static void print_pager(int total, int pagelen, char *search, char *sort) { int i, ofs; char *class = NULL; html(""); } static int cmp_str(const char *s1, const char *s2) { if (s1 && s2) { if (ctx.cfg.case_sensitive_sort) return strcmp(s1, s2); else return strcasecmp(s1, s2); } if (s1 && !s2) return -1; if (s2 && !s1) return 1; return 0; } static int cmp_name(const void *a, const void *b) { const struct cgit_repo *r1 = a; const struct cgit_repo *r2 = b; return cmp_str(r1->name, r2->name); } static int cmp_desc(const void *a, const void *b) { const struct cgit_repo *r1 = a; const struct cgit_repo *r2 = b; return cmp_str(r1->desc, r2->desc); } static int cmp_owner(const void *a, const void *b) { const struct cgit_repo *r1 = a; const struct cgit_repo *r2 = b; return cmp_str(r1->owner, r2->owner); } static int cmp_idle(const void *a, const void *b) { const struct cgit_repo *r1 = a; const struct cgit_repo *r2 = b; time_t t1, t2; t1 = t2 = 0; get_repo_modtime(r1, &t1); get_repo_modtime(r2, &t2); // Only the sign is returned, because a 64-bit difference truncated // into an int could come back with the wrong sign and leave the // ordering inconsistent. if (t2 > t1) return 1; if (t2 < t1) return -1; return 0; } static int cmp_section(const void *a, const void *b) { const struct cgit_repo *r1 = a; const struct cgit_repo *r2 = b; int result; result = cmp_str(r1->section, r2->section); if (!result) { if (!strcmp(ctx.cfg.repository_sort, "age")) result = cmp_idle(r1, r2); if (!result) result = cmp_str(r1->name, r2->name); } return result; } /* * get_repo_modtime caches into the repository it is handed, but qsort moves * those structs around as it works, so a comparator left to fill the cache * loses most of what it stored and stats the same repository over and over. */ static void resolve_modtimes(void) { time_t t; int i; for (i = 0; i < cgit_repolist.count; i++) get_repo_modtime(&cgit_repolist.repos[i], &t); } static const struct sort_column sort_columns[] = { {"section", cmp_section}, {"name", cmp_name}, {"desc", cmp_desc}, {"owner", cmp_owner}, {"idle", cmp_idle}, {NULL, NULL} }; static int sort_repolist(char *field) { const struct sort_column *column; for (column = &sort_columns[0]; column->name; column++) { if (strcmp(field, column->name)) continue; if (column->cmp == cmp_idle || column->cmp == cmp_section) resolve_modtimes(); qsort(cgit_repolist.repos, cgit_repolist.count, sizeof(struct cgit_repo), column->cmp); return 1; } return 0; } void cgit_print_repolist(void) { char *last_section = NULL; char *currenturl; int columns = BASE_COLUMNS; int column_sorted = 0; int hits = 0; int shown = 0; int i; if (!any_repos_visible()) { cgit_print_error_page(404, "Not found", "No repositories found"); return; } if (ctx.cfg.enable_index_links) ++columns; if (ctx.cfg.enable_index_owner) ++columns; ctx.page.title = ctx.cfg.root_title; cgit_print_http_headers(); cgit_print_docstart(); cgit_print_pageheader(); if (ctx.qry.sort) column_sorted = sort_repolist(ctx.qry.sort); else if (ctx.cfg.section_sort) sort_repolist("section"); currenturl = cgit_currenturl(); html(""); for (i = 0; i < cgit_repolist.count; i++) { char *section; ctx.repo = &cgit_repolist.repos[i]; if (!is_visible(ctx.repo)) continue; hits++; // Rows outside the page are stepped over rather than broken // out of, because the pager below is sized from the total. if (hits <= ctx.qry.ofs) continue; // Written as a subtraction because max-repo-count of zero // means unlimited and is held as INT_MAX, which any positive // offset would overflow if it were added to instead. if (hits - ctx.qry.ofs > ctx.cfg.max_repo_count) continue; if (!shown++) print_header_row(currenturl); section = ctx.repo->section; if (section && !strcmp(section, "")) section = NULL; if (!column_sorted && section_changed(section, last_section)) { print_section_row(section, columns); last_section = section; } print_repo_row(currenturl, !column_sorted && section); } html("
"); if (hits > ctx.cfg.max_repo_count) print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort); free(currenturl); cgit_print_docend(); } void cgit_print_site_readme(void) { cgit_print_layout_start(); if (!ctx.cfg.root_readme) goto done; cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme); html_include(ctx.cfg.root_readme); cgit_close_filter(ctx.cfg.about_filter); done: cgit_print_layout_end(); }