X-Git-Url: https://gitweb.ps.run/ps-cgit/blobdiff_plain/318d106300102c19d114a4ea89265b0a4060d9cb..d14c5f6d3ac827e7b46831c4151638ab4b638ae1:/cgit.c diff --git a/cgit.c b/cgit.c index dc91125..30a9a2a 100644 --- a/cgit.c +++ b/cgit.c @@ -10,16 +10,6 @@ const char cgit_version[] = CGIT_VERSION; -const char cgit_doctype[] = -"\n"; - -const char cgit_error[] = -"
%s
"; - -const char cgit_lib_error[] = -"
%s: %s
"; - int htmlfd = 0; char *cgit_root = "/usr/src/git"; @@ -53,32 +43,6 @@ char *cgit_query_sha1 = NULL; struct cacheitem cacheitem; -int cgit_parse_query(char *txt, configfn fn) -{ - char *t, *value = NULL, c; - - if (!txt) - return 0; - - t = txt = xstrdup(txt); - - while((c=*t) != '\0') { - if (c=='=') { - *t = '\0'; - value = t+1; - } else if (c=='&') { - *t = '\0'; - (*fn)(txt, value); - txt = t+1; - value = NULL; - } - t++; - } - if (t!=txt) - (*fn)(txt, value); - return 0; -} - void cgit_global_config_cb(const char *name, const char *value) { if (!strcmp(name, "root")) @@ -120,282 +84,6 @@ void cgit_querystring_cb(const char *name, const char *value) } } -char *cgit_repourl(const char *reponame) -{ - if (cgit_virtual_root) { - return fmt("%s/%s/", cgit_virtual_root, reponame); - } else { - return fmt("?r=%s", reponame); - } -} - -char *cgit_pageurl(const char *reponame, const char *pagename, - const char *query) -{ - if (cgit_virtual_root) { - return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame, - pagename, query); - } else { - return fmt("?r=%s&p=%s&%s", reponame, pagename, query); - } -} - -static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1, - int flags, void *cb_data) -{ - struct commit *commit; - char buf[256], *url; - - commit = lookup_commit(sha1); - if (commit && !parse_commit(commit)){ - html(""); - url = cgit_pageurl(cgit_query_repo, "log", - fmt("h=%s", refname)); - html_link_open(url, NULL, NULL); - strncpy(buf, refname, sizeof(buf)); - html_txt(buf); - html_link_close(); - html(""); - pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, buf, - sizeof(buf), 0, NULL, NULL, 0); - html_txt(buf); - html("\n"); - } else { - html(""); - html_txt(buf); - html(""); - htmlf("*** bad ref %s", sha1_to_hex(sha1)); - html("\n"); - } - return 0; -} - -/* Sun, 06 Nov 1994 08:49:37 GMT */ -static char *http_date(time_t t) -{ - static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; - static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; - struct tm *tm = gmtime(&t); - return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], - tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, - tm->tm_hour, tm->tm_min, tm->tm_sec); -} - -static int ttl_seconds(int ttl) -{ - if (ttl<0) - return 60 * 60 * 24 * 365; - else - return ttl * 60; -} - -static void cgit_print_docstart(char *title) -{ - html("Content-Type: text/html; charset=utf-8\n"); - htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime)); - htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime + - ttl_seconds(cacheitem.ttl))); - html("\n"); - html(cgit_doctype); - html("\n"); - html("\n"); - html(""); - html_txt(title); - html("\n"); - htmlf("\n", cgit_version); - html("\n"); - html("\n"); - html("\n"); -} - -static void cgit_print_docend() -{ - html("\n\n"); -} - -static void cgit_print_pageheader(char *title) -{ - html(""); -} - -static void cgit_print_repolist() -{ - DIR *d; - struct dirent *de; - struct stat st; - char *name; - - chdir(cgit_root); - cgit_print_docstart(cgit_root_title); - cgit_print_pageheader(cgit_root_title); - - if (!(d = opendir("."))) { - htmlf(cgit_lib_error, "Unable to scan repository directory", - strerror(errno)); - cgit_print_docend(); - return; - } - - html("

Repositories

\n"); - html(""); - html("\n"); - while ((de = readdir(d)) != NULL) { - if (de->d_name[0] == '.') - continue; - if (stat(de->d_name, &st) < 0) - continue; - if (!S_ISDIR(st.st_mode)) - continue; - - cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL; - name = fmt("%s/info/cgit", de->d_name); - if (cgit_read_config(name, cgit_repo_config_cb)) - continue; - - html("\n"); - } - closedir(d); - html("
NameDescriptionOwner
"); - html_link_open(cgit_repourl(de->d_name), NULL, NULL); - html_txt(cgit_repo_name); - html_link_close(); - html(""); - html_txt(cgit_repo_desc); - html(""); - html_txt(cgit_repo_owner); - html("
"); - cgit_print_docend(); -} - -static void cgit_print_branches() -{ - html(""); - html("\n"); - for_each_branch_ref(cgit_print_branch_cb, NULL); - html("
Branch nameHead commit
"); -} - -static int get_one_line(char *txt) -{ - char *t; - - for(t=txt; *t != '\n' && t != '\0'; t++) - ; - *t = '\0'; - return t-txt-1; -} - -static void cgit_print_commit_shortlog(struct commit *commit) -{ - char *h, *t, *p; - char *tree = NULL, *author = NULL, *subject = NULL; - int len; - time_t sec; - struct tm *time; - char buf[32]; - - h = t = commit->buffer; - - if (strncmp(h, "tree ", 5)) - die("Bad commit format: %s", - sha1_to_hex(commit->object.sha1)); - - len = get_one_line(h); - tree = h+5; - h += len + 2; - - while (!strncmp(h, "parent ", 7)) - h += get_one_line(h) + 2; - - if (!strncmp(h, "author ", 7)) { - author = h+7; - h += get_one_line(h) + 2; - t = author; - while(t!=h && *t!='<') - t++; - *t='\0'; - p = t; - while(--t!=author && *t==' ') - *t='\0'; - while(++p!=h && *p!='>') - ; - while(++p!=h && !isdigit(*p)) - ; - - t = p; - while(++p && isdigit(*p)) - ; - *p = '\0'; - sec = atoi(t); - time = gmtime(&sec); - } - - while((len = get_one_line(h)) > 0) - h += len+2; - - h++; - len = get_one_line(h); - - subject = h; - - html(""); - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time); - html_txt(buf); - html(""); - char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1)); - char *url = cgit_pageurl(cgit_query_repo, "view", qry); - html_link_open(url, NULL, NULL); - html_txt(subject); - html_link_close(); - html(""); - html_txt(author); - html("\n"); -} - -static void cgit_print_log(const char *tip, int ofs, int cnt) -{ - struct rev_info rev; - struct commit *commit; - const char *argv[2] = {NULL, tip}; - int n = 0; - - init_revisions(&rev, NULL); - rev.abbrev = DEFAULT_ABBREV; - rev.commit_format = CMIT_FMT_DEFAULT; - rev.verbose_header = 1; - rev.show_root_diff = 0; - setup_revisions(2, argv, &rev, NULL); - prepare_revision_walk(&rev); - - html("

Log

"); - html(""); - html("\n"); - while ((commit = get_revision(&rev)) != NULL && n++ < 100) { - cgit_print_commit_shortlog(commit); - free(commit->buffer); - commit->buffer = NULL; - free_commit_list(commit->parents); - commit->parents = NULL; - } - html("
DateMessageAuthor
\n"); -} - -static void cgit_print_repo_summary() -{ - html("

"); - html_txt("Repo summary page"); - html("

"); - cgit_print_branches(); -} - static void cgit_print_object(char *hex) { unsigned char sha1[20]; @@ -405,18 +93,18 @@ static void cgit_print_object(char *hex) unsigned long size; if (get_sha1_hex(hex, sha1)){ - htmlf(cgit_error, "Bad hex value"); + cgit_print_error(fmt("Bad hex value: %s", hex)); return; } if (sha1_object_info(sha1, type, NULL)){ - htmlf(cgit_error, "Bad object name"); + cgit_print_error("Bad object name"); return; } buf = read_sha1_file(sha1, type, &size); if (!buf) { - htmlf(cgit_error, "Error reading object"); + cgit_print_error("Error reading object"); return; } @@ -428,24 +116,24 @@ static void cgit_print_object(char *hex) html(""); } -static void cgit_print_repo_page() +static void cgit_print_repo_page(struct cacheitem *item) { if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || cgit_read_config("info/cgit", cgit_repo_config_cb)) { char *title = fmt("%s - %s", cgit_root_title, "Bad request"); - cgit_print_docstart(title); + cgit_print_docstart(title, item); cgit_print_pageheader(title); - htmlf(cgit_lib_error, "Unable to scan repository", - strerror(errno)); + cgit_print_error(fmt("Unable to scan repository: %s", + strerror(errno))); cgit_print_docend(); return; } setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); - cgit_print_docstart(title); + cgit_print_docstart(title, item); cgit_print_pageheader(title); if (!cgit_query_page) - cgit_print_repo_summary(); + cgit_print_summary(); else if (!strcmp(cgit_query_page, "log")) { cgit_print_log(cgit_query_head, 0, 100); } else if (!strcmp(cgit_query_page, "view")) { @@ -459,9 +147,9 @@ static void cgit_fill_cache(struct cacheitem *item) htmlfd = item->fd; item->st.st_mtime = time(NULL); if (cgit_query_repo) - cgit_print_repo_page(); + cgit_print_repo_page(item); else - cgit_print_repolist(); + cgit_print_repolist(item); } static void cgit_refresh_cache(struct cacheitem *item)